Skip to content

Commit

Permalink
solana-ibc: use solana height and timestamp for cf-guest host metadata (
Browse files Browse the repository at this point in the history
#393)

since the cf-guest light client would exist on rollup, the host metadata
is the solana slot and the timestamp instead of those fetched from the
guest chain since the rollup wouldnt have it.

But if the connection is established to another svm where the guest
chain is used, then the above should be reverted.
  • Loading branch information
dhruvja authored Oct 16, 2024
1 parent ab6ad71 commit 6778472
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions solana/solana-ibc/programs/solana-ibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mocks = ["ibc-testkit"]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
witness = []

[dependencies]
anchor-lang.workspace = true
Expand Down
34 changes: 26 additions & 8 deletions solana/solana-ibc/programs/solana-ibc/src/validation_context.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::str::FromStr;
use std::time::Duration;

use anchor_lang::prelude::Pubkey;
use anchor_lang::prelude::{Pubkey, SolanaSysvar};
use lib::hash::CryptoHash;
use spl_token::solana_program::clock::Clock;

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


type Result<T = (), E = ibc::ContextError> = core::result::Result<T, E>;

impl ibc::ValidationContext for IbcStorage<'_, '_> {
Expand All @@ -22,7 +22,12 @@ impl ibc::ValidationContext for IbcStorage<'_, '_> {
&self,
client_id: &ibc::ClientId,
) -> Result<Self::AnyClientState> {
Ok(self.borrow().private.client(client_id)?.client_state.get()?)
self.borrow()
.private
.client(client_id)?
.client_state
.get()
.map_err(Into::into)
}

fn decode_client_state(
Expand All @@ -43,13 +48,27 @@ impl ibc::ValidationContext for IbcStorage<'_, '_> {
}

fn host_height(&self) -> Result<ibc::Height> {
let height = u64::from(self.borrow().chain.head()?.block_height);
let height = ibc::Height::new(1, height)?;
Ok(height)
let height = if cfg!(feature = "witness") {
Clock::get()
.map_err(|e| ibc::ClientError::ClientSpecific {
description: e.to_string(),
})?
.slot
} else {
u64::from(self.borrow().chain.head()?.block_height)
};
Ok(ibc::Height::new(1, height)?)
}

fn host_timestamp(&self) -> Result<ibc::Timestamp> {
let timestamp = self.borrow().chain.head()?.timestamp_ns.get();
let timestamp = if cfg!(feature = "witness") {
let clock = Clock::get().map_err(|e| {
ibc::ClientError::ClientSpecific { description: e.to_string() }
})?;
clock.unix_timestamp as u64 * 10u64.pow(9)
} else {
self.borrow().chain.head()?.timestamp_ns.get()
};
ibc::Timestamp::from_nanoseconds(timestamp).map_err(|err| {
ibc::ClientError::Other { description: err.to_string() }.into()
})
Expand Down Expand Up @@ -296,7 +315,6 @@ impl IbcStorage<'_, '_> {
}
}


impl ibc::ClientValidationContext for IbcStorage<'_, '_> {
fn update_meta(
&self,
Expand Down

0 comments on commit 6778472

Please sign in to comment.