From ce25a408a07d8a88a7bd23f43b96f9620733d874 Mon Sep 17 00:00:00 2001 From: dhruvja Date: Sat, 7 Sep 2024 12:04:18 +0530 Subject: [PATCH 1/5] use solana height and timestamp for cf-guest host metdata --- .../programs/solana-ibc/src/client_state.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/solana/solana-ibc/programs/solana-ibc/src/client_state.rs b/solana/solana-ibc/programs/solana-ibc/src/client_state.rs index 9b10c32a..fe34d240 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/client_state.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/client_state.rs @@ -1,5 +1,6 @@ use anchor_lang::prelude::borsh; use anchor_lang::prelude::borsh::maybestd::io; +use spl_token::solana_program::sysvar::Sysvar; use crate::consensus_state::AnyConsensusState; use crate::ibc; @@ -236,14 +237,20 @@ impl cf_guest::CommonContext type AnyConsensusState = AnyConsensusState; fn host_metadata(&self) -> Result<(ibc::Timestamp, ibc::Height)> { - let timestamp = self.borrow().chain.head()?.timestamp_ns.get(); - let timestamp = - ibc::Timestamp::from_nanoseconds(timestamp).map_err(|err| { - ibc::ClientError::Other { description: err.to_string() } + let clock = anchor_lang::solana_program::sysvar::clock::Clock::get() + .map_err(|e| ibc::ClientError::ClientSpecific { + description: e.to_string(), })?; - let height = u64::from(self.borrow().chain.head()?.block_height); - let height = ibc::Height::new(1, height)?; + let slot = clock.slot; + let timestamp_sec = clock.unix_timestamp as u64; + + let timestamp = + ibc::Timestamp::from_nanoseconds(timestamp_sec * 10u64.pow(9)) + .map_err(|e| ibc::ClientError::ClientSpecific { + description: e.to_string(), + })?; + let height = ibc::Height::new(1, slot)?; Ok((timestamp, height)) } From 17406cf9f435d06ea51cf371a68f2c4fb3d32158 Mon Sep 17 00:00:00 2001 From: dhruvja Date: Sat, 7 Sep 2024 21:27:26 +0530 Subject: [PATCH 2/5] fetch host metadata based on feature --- .../programs/solana-ibc/src/client_state.rs | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/solana/solana-ibc/programs/solana-ibc/src/client_state.rs b/solana/solana-ibc/programs/solana-ibc/src/client_state.rs index fe34d240..3f2e2230 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/client_state.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/client_state.rs @@ -237,20 +237,33 @@ impl cf_guest::CommonContext type AnyConsensusState = AnyConsensusState; fn host_metadata(&self) -> Result<(ibc::Timestamp, ibc::Height)> { - let clock = anchor_lang::solana_program::sysvar::clock::Clock::get() - .map_err(|e| ibc::ClientError::ClientSpecific { - description: e.to_string(), + #[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 timestamp_sec = clock.unix_timestamp; + + let timestamp = + ibc::Timestamp::from_nanoseconds(timestamp_sec * 10i64.pow(9)) + .map_err(|e| ibc::ClientError::ClientSpecific { + description: e.to_string(), + })?; + let height = ibc::Height::new(1, slot)?; + return Ok((timestamp, height)); + } + let timestamp = self.borrow().chain.head()?.timestamp_ns.get(); + let timestamp = + ibc::Timestamp::from_nanoseconds(timestamp).map_err(|err| { + ibc::ClientError::Other { description: err.to_string() } })?; - let slot = clock.slot; - let timestamp_sec = clock.unix_timestamp as u64; - - let timestamp = - ibc::Timestamp::from_nanoseconds(timestamp_sec * 10u64.pow(9)) - .map_err(|e| ibc::ClientError::ClientSpecific { - description: e.to_string(), - })?; - let height = ibc::Height::new(1, slot)?; + let height = u64::from(self.borrow().chain.head()?.block_height); + let height = ibc::Height::new(1, height)?; Ok((timestamp, height)) } From c6ed0d9f4f8961b10275f5d9b1d04bc5dde33912 Mon Sep 17 00:00:00 2001 From: dhruvja Date: Mon, 9 Sep 2024 19:19:53 +0530 Subject: [PATCH 3/5] fetching latest slot and latest timestamp if witness feature is enabled --- .../solana-ibc/programs/solana-ibc/Cargo.toml | 1 + .../programs/solana-ibc/src/client_state.rs | 4 +- .../solana-ibc/src/validation_context.rs | 40 +++++++++++++++++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/solana/solana-ibc/programs/solana-ibc/Cargo.toml b/solana/solana-ibc/programs/solana-ibc/Cargo.toml index d9c228c6..63771bae 100644 --- a/solana/solana-ibc/programs/solana-ibc/Cargo.toml +++ b/solana/solana-ibc/programs/solana-ibc/Cargo.toml @@ -17,6 +17,7 @@ mocks = ["ibc-testkit"] no-entrypoint = [] no-idl = [] no-log-ix-name = [] +witness = [] [dependencies] anchor-lang.workspace = true diff --git a/solana/solana-ibc/programs/solana-ibc/src/client_state.rs b/solana/solana-ibc/programs/solana-ibc/src/client_state.rs index 3f2e2230..34a20f0e 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/client_state.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/client_state.rs @@ -246,10 +246,10 @@ impl cf_guest::CommonContext })?; 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(), })?; diff --git a/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs b/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs index 7c0624d4..dbb20f6d 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs @@ -2,6 +2,8 @@ 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; @@ -9,7 +11,6 @@ use crate::consensus_state::AnyConsensusState; use crate::ibc::{self, ConsensusState}; use crate::storage::{self, IbcStorage}; - type Result = core::result::Result; impl ibc::ValidationContext for IbcStorage<'_, '_> { @@ -22,7 +23,12 @@ impl ibc::ValidationContext for IbcStorage<'_, '_> { &self, client_id: &ibc::ClientId, ) -> Result { - 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( @@ -43,12 +49,41 @@ impl ibc::ValidationContext for IbcStorage<'_, '_> { } fn host_height(&self) -> Result { + #[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); let height = ibc::Height::new(1, height)?; Ok(height) } fn host_timestamp(&self) -> Result { + #[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(); ibc::Timestamp::from_nanoseconds(timestamp).map_err(|err| { ibc::ClientError::Other { description: err.to_string() }.into() @@ -296,7 +331,6 @@ impl IbcStorage<'_, '_> { } } - impl ibc::ClientValidationContext for IbcStorage<'_, '_> { fn update_meta( &self, From 90008c8a8191f25ab81f7f09288153e2827005de Mon Sep 17 00:00:00 2001 From: dhruvja Date: Wed, 16 Oct 2024 21:55:41 +0530 Subject: [PATCH 4/5] fix conditional compilation with cfg macro --- .../solana-ibc/src/validation_context.rs | 51 +++++++------------ 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs b/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs index dbb20f6d..58b20d91 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs @@ -5,6 +5,8 @@ use anchor_lang::prelude::Pubkey; #[cfg(feature = "witness")] use anchor_lang::prelude::SolanaSysvar; use lib::hash::CryptoHash; +use spl_token::solana_program::clock::Clock; +use spl_token::solana_program::sysvar::Sysvar; use crate::client_state::AnyClientState; use crate::consensus_state::AnyConsensusState; @@ -49,42 +51,27 @@ impl ibc::ValidationContext for IbcStorage<'_, '_> { } fn host_height(&self) -> Result { - #[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); - 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 { - #[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| { + let timestamp = if cfg!(feature = "witness") { + let clock = Clock::get().map_err(|e| { ibc::ClientError::ClientSpecific { description: e.to_string() } - .into() - }); - } - let timestamp = self.borrow().chain.head()?.timestamp_ns.get(); + })?; + 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() }) From 2fd06118f4ac95cc462feb5cad62503532234b36 Mon Sep 17 00:00:00 2001 From: dhruvja Date: Wed, 16 Oct 2024 22:06:29 +0530 Subject: [PATCH 5/5] fix clippy --- .../solana-ibc/programs/solana-ibc/src/validation_context.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs b/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs index 58b20d91..53b1f9a0 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs @@ -1,12 +1,9 @@ use std::str::FromStr; use std::time::Duration; -use anchor_lang::prelude::Pubkey; -#[cfg(feature = "witness")] -use anchor_lang::prelude::SolanaSysvar; +use anchor_lang::prelude::{Pubkey, SolanaSysvar}; use lib::hash::CryptoHash; use spl_token::solana_program::clock::Clock; -use spl_token::solana_program::sysvar::Sysvar; use crate::client_state::AnyClientState; use crate::consensus_state::AnyConsensusState;