Skip to content

Commit

Permalink
write local consensus state when witness feature is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvja committed Oct 15, 2024
1 parent ca9dadb commit f2ffe6a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions solana/solana-ibc/programs/solana-ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,26 @@ pub mod solana_ibc {
let height = store.borrow().chain.head()?.block_height;
// height just before the data is added to the trie.
msg!("Current Block height {}", height);
let previous_root = store.borrow().provable.hash().clone();

Check failure on line 405 in solana/solana-ibc/programs/solana-ibc/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `CryptoHash` which implements the `Copy` trait

error: using `clone` on type `CryptoHash` which implements the `Copy` trait --> solana/solana-ibc/programs/solana-ibc/src/lib.rs:405:29 | 405 | let previous_root = store.borrow().provable.hash().clone(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*store.borrow().provable.hash()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy = note: `-D clippy::clone-on-copy` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::clone_on_copy)]`

::ibc::core::entrypoint::dispatch(&mut store, &mut router, message)
.map_err(error::Error::ContextError)
.map_err(move |err| error!((&err)))?;

#[cfg(feature = "witness")]
{
let root = store.borrow().provable.hash().clone();
if previous_root != root {
msg!("Writing local consensus state");
let slot = Clock::get()?.slot;
let timestamp = Clock::get()?.unix_timestamp as u64;
let storage = &mut store.borrow_mut().private;
storage
.add_local_consensus_state(slot, timestamp, root)
.unwrap();
}
}

Ok(())
}

Expand Down
19 changes: 19 additions & 0 deletions solana/solana-ibc/programs/solana-ibc/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use alloc::rc::Rc;
use core::cell::RefCell;
use core::num::NonZeroU64;
#[cfg(feature = "witness")]
use std::collections::VecDeque;

use anchor_lang::prelude::*;
use borsh::maybestd::io;
Expand Down Expand Up @@ -313,6 +315,9 @@ pub struct PrivateStorage {

// Fee to be charged for each transfer
pub fee_in_lamports: u64,

#[cfg(feature = "witness")]
pub local_consensus_state: VecDeque<(u64, u64, CryptoHash)>,
}

#[derive(Clone, Debug, borsh::BorshSerialize, borsh::BorshDeserialize)]
Expand Down Expand Up @@ -387,6 +392,20 @@ impl PrivateStorage {
client_id: client_id.clone(),
})
}

#[cfg(feature = "witness")]
pub fn add_local_consensus_state(
&mut self,
slot: u64,
timestamp: u64,
trie_root: CryptoHash,
) -> Result<(), ibc::ClientError> {
if self.local_consensus_state.len() == MAX_CONSENSUS_STATES {
self.local_consensus_state.pop_front();
}
self.local_consensus_state.push_back((slot, timestamp, trie_root));
Ok(())
}
}

/// Provable storage, i.e. the trie, held in an account.
Expand Down

0 comments on commit f2ffe6a

Please sign in to comment.