Skip to content

Commit

Permalink
fetching latest slot and latest timestamp if witness feature is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvja committed Sep 9, 2024
1 parent 17406cf commit c6ed0d9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 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
4 changes: 2 additions & 2 deletions solana/solana-ibc/programs/solana-ibc/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ impl cf_guest::CommonContext<sigverify::ed25519::PubKey>
})?;

let slot = clock.slot;
let timestamp_sec = clock.unix_timestamp;
let timestamp_sec = clock.unix_timestamp as u64;

let timestamp =
ibc::Timestamp::from_nanoseconds(timestamp_sec * 10i64.pow(9))
ibc::Timestamp::from_nanoseconds(timestamp_sec * 10u64.pow(9))
.map_err(|e| ibc::ClientError::ClientSpecific {
description: e.to_string(),
})?;
Expand Down
40 changes: 37 additions & 3 deletions solana/solana-ibc/programs/solana-ibc/src/validation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use std::str::FromStr;
use std::time::Duration;

use anchor_lang::prelude::Pubkey;
#[cfg(feature = "witness")]
use anchor_lang::prelude::SolanaSysvar;
use lib::hash::CryptoHash;

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 +23,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,12 +49,41 @@ impl ibc::ValidationContext for IbcStorage<'_, '_> {
}

fn host_height(&self) -> Result<ibc::Height> {
#[cfg(feature = "witness")]
{
let clock =
anchor_lang::solana_program::sysvar::clock::Clock::get()
.map_err(|e| ibc::ClientError::ClientSpecific {
description: e.to_string(),
})?;

let slot = clock.slot;
let height = ibc::Height::new(1, slot)?;
return Ok(height);
}
let height = u64::from(self.borrow().chain.head()?.block_height);

Check failure on line 64 in solana/solana-ibc/programs/solana-ibc/src/validation_context.rs

View workflow job for this annotation

GitHub Actions / clippy

unreachable statement

error: unreachable statement --> solana/solana-ibc/programs/solana-ibc/src/validation_context.rs:64:9 | 62 | return Ok(height); | ----------------- any code following this expression is unreachable 63 | } 64 | let height = u64::from(self.borrow().chain.head()?.block_height); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
let height = ibc::Height::new(1, height)?;
Ok(height)
}

fn host_timestamp(&self) -> Result<ibc::Timestamp> {
#[cfg(feature = "witness")]
{
let clock =
anchor_lang::solana_program::sysvar::clock::Clock::get()
.map_err(|e| ibc::ClientError::ClientSpecific {
description: e.to_string(),
})?;

let timestamp_sec = clock.unix_timestamp as u64;
return ibc::Timestamp::from_nanoseconds(
timestamp_sec * 10u64.pow(9),
)
.map_err(|e| {
ibc::ClientError::ClientSpecific { description: e.to_string() }
.into()
});
}
let timestamp = self.borrow().chain.head()?.timestamp_ns.get();

Check failure on line 87 in solana/solana-ibc/programs/solana-ibc/src/validation_context.rs

View workflow job for this annotation

GitHub Actions / clippy

unreachable statement

error: unreachable statement --> solana/solana-ibc/programs/solana-ibc/src/validation_context.rs:87:9 | 79 | / return ibc::Timestamp::from_nanoseconds( 80 | | timestamp_sec * 10u64.pow(9), 81 | | ) 82 | | .map_err(|e| { 83 | | ibc::ClientError::ClientSpecific { description: e.to_string() } 84 | | .into() 85 | | }); | |______________- any code following this expression is unreachable 86 | } 87 | let timestamp = self.borrow().chain.head()?.timestamp_ns.get(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
ibc::Timestamp::from_nanoseconds(timestamp).map_err(|err| {
ibc::ClientError::Other { description: err.to_string() }.into()
Expand Down Expand Up @@ -296,7 +331,6 @@ impl IbcStorage<'_, '_> {
}
}


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

0 comments on commit c6ed0d9

Please sign in to comment.