Skip to content

Commit

Permalink
Merge remote-tracking branch 'up/master' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
mina86 committed Dec 10, 2023
2 parents 7aef1df + 223afb1 commit 41601b0
Show file tree
Hide file tree
Showing 11 changed files with 411 additions and 166 deletions.
2 changes: 2 additions & 0 deletions solana/solana-ibc/programs/solana-ibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ anchor-client.workspace = true
anyhow.workspace = true
ibc-testkit.workspace = true
insta.workspace = true

lib = { workspace = true, features = ["test_utils"] }
17 changes: 7 additions & 10 deletions solana/solana-ibc/programs/solana-ibc/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ type Result<T = (), E = anchor_lang::error::Error> = core::result::Result<T, E>;
pub type Epoch = blockchain::Epoch<PubKey>;
pub type Block = blockchain::Block<PubKey>;
pub type Manager = blockchain::ChainManager<PubKey>;
pub type Validator = blockchain::Validator<PubKey>;
pub use crate::ed25519::{PubKey, Signature, Verifier};

/// Guest blockchain data held in Solana account.
#[account]
pub struct ChainData {
inner: Option<ChainInner>,
inner: Option<Box<ChainInner>>,
}

impl ChainData {
Expand Down Expand Up @@ -42,9 +43,8 @@ impl ChainData {
if self.inner.is_some() {
return Err(Error::ChainAlreadyInitialised.into());
}
let last_check_height = manager.head().1.host_height;
let inner =
self.inner.insert(ChainInner { last_check_height, manager });
let inner = ChainInner { last_check_height: host_head.height, manager };
let inner = self.inner.insert(Box::new(inner));

let (finalised, head) = inner.manager.head();
assert!(finalised);
Expand Down Expand Up @@ -83,10 +83,7 @@ impl ChainData {
trie: &storage::AccountTrie,
host_head: Option<crate::host::Head>,
) -> Result {
match self.get_mut() {
Ok(inner) => inner.generate_block(trie, host_head, false),
Err(_) => Ok(()),
}
self.get_mut()?.generate_block(trie, host_head, false)
}

/// Submits a signature for the pending block.
Expand Down Expand Up @@ -131,8 +128,8 @@ impl ChainData {
}

/// Returns mutable the inner chain data if it has been initialised.
fn get_mut(&mut self) -> Result<&mut ChainInner> {
self.inner.as_mut().ok_or_else(|| Error::ChainNotInitialised.into())
fn get_mut(&mut self) -> Result<&mut ChainInner, Error> {
self.inner.as_deref_mut().ok_or(Error::ChainNotInitialised)
}
}

Expand Down
2 changes: 1 addition & 1 deletion solana/solana-ibc/programs/solana-ibc/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ impl IbcStorage<'_, '_> {
let client = store.private.client(client_id)?;
let mut range = client.consensus_states.range(range);
if dir == Direction::Next { range.next() } else { range.next_back() }
.map(|(_, data)| data.get())
.map(|(_, data)| data.state())
.transpose()
.map_err(|err| err.into())
}
Expand Down
73 changes: 39 additions & 34 deletions solana/solana-ibc/programs/solana-ibc/src/execution_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,20 @@ impl ibc::ClientExecutionContext for IbcStorage<'_, '_> {
msg!("store_consensus_state({}, {:?})", path, state);
let height =
ibc::Height::new(path.revision_number, path.revision_height)?;

let mut store = self.borrow_mut();
let processed_time = store.host_head.timestamp;
let processed_height = store.host_head.height;

let mut client = store.private.client_mut(&path.client_id, false)?;
let serialised = storage::Serialised::new(&state)?;
let hash = serialised.digest();
client.consensus_states.insert(height, serialised);
let state = storage::ClientConsensusState::new(
processed_time,
processed_height,
&state,
)?;
let hash = state.digest()?;
client.consensus_states.insert(height, state);

let trie_key =
trie_ids::TrieKey::for_consensus_state(client.index, height);
store.provable.set(&trie_key, &hash).map_err(error)?;
Expand All @@ -69,58 +78,54 @@ impl ibc::ClientExecutionContext for IbcStorage<'_, '_> {
Ok(())
}


/// Does nothing in the current implementation.
///
/// Instead, the update height is deleted when consensus state at given
/// height is deleted.
fn delete_update_height(
&mut self,
client_id: ibc::ClientId,
height: ibc::Height,
_client_id: ibc::ClientId,
_height: ibc::Height,
) -> Result {
self.borrow_mut()
.private
.client_mut(&client_id, false)?
.processed_heights
.remove(&height);
Ok(())
}

/// Does nothing in the current implementation.
///
/// Instead, the update time is deleted when consensus state at given
/// height is deleted.
fn delete_update_time(
&mut self,
client_id: ibc::ClientId,
height: ibc::Height,
_client_id: ibc::ClientId,
_height: ibc::Height,
) -> Result {
self.borrow_mut()
.private
.client_mut(&client_id, false)?
.processed_times
.remove(&height);
Ok(())
}

/// Does nothing in the current implementation.
///
/// Instead, the update time is set when storing consensus state to the host
/// time at the moment [`Self::store_consensus_state`] method is called.
fn store_update_time(
&mut self,
client_id: ibc::ClientId,
height: ibc::Height,
timestamp: ibc::Timestamp,
_client_id: ibc::ClientId,
_height: ibc::Height,
_host_timestamp: ibc::Timestamp,
) -> Result {
self.borrow_mut()
.private
.client_mut(&client_id, false)?
.processed_times
.insert(height, timestamp.nanoseconds());
Ok(())
}

/// Does nothing in the current implementation.
///
/// Instead, the update height is set when storing consensus state to the
/// host height at the moment [`Self::store_consensus_state`] method is
/// called.
fn store_update_height(
&mut self,
client_id: ibc::ClientId,
height: ibc::Height,
host_height: ibc::Height,
_client_id: ibc::ClientId,
_height: ibc::Height,
_host_height: ibc::Height,
) -> Result {
self.borrow_mut()
.private
.client_mut(&client_id, false)?
.processed_heights
.insert(height, host_height);
Ok(())
}
}
Expand Down
Loading

0 comments on commit 41601b0

Please sign in to comment.