Skip to content

Commit

Permalink
solana-ibc: get host consensus state based on feature (#401)
Browse files Browse the repository at this point in the history
Host consensus state is required for establishing the connection. So the
host consensus state should be returned based on whether the `witness`
feature is present or not. If the feature is present, then we return the
rollup consensus state else we return the guest consensus state. But the
issue is if the `witness` feature is not enabled, it could either be
guest or wasm ( which is a wrapper version of guest ). Since the
connection with cosmos is already established, we would stick to
returning the `guest` consensus state for now.
  • Loading branch information
dhruvja authored Oct 18, 2024
1 parent ceed3e6 commit a21cd4b
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions solana/solana-ibc/programs/solana-ibc/src/validation_context.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "witness")]
use core::num::NonZeroU64;
use std::str::FromStr;
use std::time::Duration;

Expand All @@ -7,7 +9,7 @@ use spl_token::solana_program::clock::Clock;

use crate::client_state::AnyClientState;
use crate::consensus_state::AnyConsensusState;
use crate::ibc::{self, ConsensusState};
use crate::ibc;
use crate::storage::{self, IbcStorage};

type Result<T = (), E = ibc::ContextError> = core::result::Result<T, E>;
Expand Down Expand Up @@ -79,27 +81,42 @@ impl ibc::ValidationContext for IbcStorage<'_, '_> {
height: &ibc::Height,
) -> Result<Self::AnyConsensusState> {
let store = self.borrow();
let state = if height.revision_number() == 1 {
store.chain.consensus_state(height.revision_height().into())?
} else {
None
}
.ok_or(ibc::ClientError::MissingLocalConsensusState {
height: *height,
})?;
let guest_consensus_state = cf_guest::ConsensusState {
block_hash: state.0.as_array().to_vec().into(),
timestamp_ns: state.1,
};
let wasm_consensus_state = wasm::consensus_state::ConsensusState::new(
guest_consensus_state.encode_vec(),
state.1.into(),
);
Ok(AnyConsensusState::Wasm(wasm_consensus_state))
// Ok(Self::AnyConsensusState::from(cf_guest::ConsensusState {
// block_hash: state.0.as_array().to_vec().into(),
// timestamp_ns: state.1,
// }))
#[cfg(feature = "witness")]
let state = (height.revision_number() == 1)
.then(|| {
store
.private
.local_consensus_state
.iter()
.find(|cs| cs.0 == height.revision_height())
.map(|(_slot, timestamp, trie_root)| {
let state = cf_solana::ConsensusState {
trie_root: trie_root.to_vec().into(),
timestamp_sec: NonZeroU64::new(*timestamp).unwrap(),
};
AnyConsensusState::Rollup(state)
})
})
.flatten();
#[cfg(not(feature = "witness"))]
let state = (height.revision_number() == 1)
.then(|| {
let height = height.revision_height().into();
store.chain.consensus_state(height)
})
.transpose()?
.flatten()
.map(|state| {
let state = cf_guest::ConsensusState {
block_hash: state.0.as_array().to_vec().into(),
timestamp_ns: state.1,
};
AnyConsensusState::Guest(state)
});
state.ok_or(
ibc::ClientError::MissingLocalConsensusState { height: *height }
.into(),
)
}

fn client_counter(&self) -> Result<u64> {
Expand Down

0 comments on commit a21cd4b

Please sign in to comment.