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: use solana height and timestamp for cf-guest host metadata #393

Merged
merged 6 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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};


dhruvja marked this conversation as resolved.
Show resolved Hide resolved
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
Loading