Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solana-ibc: write local consensus state when witness feature is enabled #398

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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;
dhruvja marked this conversation as resolved.
Show resolved Hide resolved
let storage = &mut store.borrow_mut().private;
storage
.add_local_consensus_state(slot, timestamp, root)
.unwrap();
dhruvja marked this conversation as resolved.
Show resolved Hide resolved
}
}

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
Loading