Skip to content

Commit

Permalink
solana-ibc: write local consensus state when witness feature is enabl…
Browse files Browse the repository at this point in the history
…ed (#398)

Local consensus state is required for establishing connections. It is
required only to establish connection after which it wont be required.
So we store the consensus state every time trie changes for a maximum of
64 states.

---------

Co-authored-by: Michal Nazarewicz <[email protected]>
  • Loading branch information
dhruvja and mina86 authored Oct 16, 2024
1 parent 00c29e4 commit 80e1e57
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 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,28 @@ 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();

::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 clock = Clock::get()?;
let slot = clock.slot;
let timestamp = clock.unix_timestamp as u64;
store
.borrow_mut()
.private
.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 80e1e57

Please sign in to comment.