Skip to content

Commit

Permalink
Merge remote-tracking branch 'up/master' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
mina86 committed Oct 18, 2024
2 parents a24b687 + 0bc98fe commit cfc90ea
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
10 changes: 6 additions & 4 deletions common/cf-guest/src/client/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,17 @@ impl<PK: PubKey> ibc::ClientStateCommon for ClientState<PK> {
/// See [`proof::verify`] for documentation of the proof format.
fn verify_membership(
&self,
prefix: &ibc::CommitmentPrefix,
_prefix: &ibc::CommitmentPrefix,
proof: &ibc::CommitmentProofBytes,
root: &ibc::CommitmentRoot,
path: ibc::path::Path,
value: Vec<u8>,
) -> Result {
let value = Some(value.as_slice());
// TODO: Once IBC is updated everywhere to version which supports empty
// prefixes, change `&[]` with `prefix.as_bytes()`.
proof::verify_for_block(
prefix.as_bytes(),
&[],
proof.as_ref(),
root.as_bytes(),
path,
Expand All @@ -221,13 +223,13 @@ impl<PK: PubKey> ibc::ClientStateCommon for ClientState<PK> {
/// See [`proof::verify`] for documentation of the proof format.
fn verify_non_membership(
&self,
prefix: &ibc::CommitmentPrefix,
_prefix: &ibc::CommitmentPrefix,
proof: &ibc::CommitmentProofBytes,
root: &ibc::CommitmentRoot,
path: ibc::path::Path,
) -> Result {
proof::verify_for_block(
prefix.as_bytes(),
&[],
proof.as_ref(),
root.as_bytes(),
path,
Expand Down
16 changes: 5 additions & 11 deletions common/cf-solana/src/client/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ impl ibc::ClientStateCommon for ClientState {
/// See [`proof::verify`] for documentation of the proof format.
fn verify_membership(
&self,
prefix: &ibc::CommitmentPrefix,
_prefix: &ibc::CommitmentPrefix,
proof: &ibc::CommitmentProofBytes,
root: &ibc::CommitmentRoot,
path: ibc::path::Path,
value: Vec<u8>,
) -> Result {
let value = Some(value.as_slice());
proof::verify_for_trie(
prefix.as_bytes(),
&[],
proof.as_ref(),
root.as_bytes(),
path,
Expand All @@ -154,19 +154,13 @@ impl ibc::ClientStateCommon for ClientState {
/// See [`proof::verify`] for documentation of the proof format.
fn verify_non_membership(
&self,
prefix: &ibc::CommitmentPrefix,
_prefix: &ibc::CommitmentPrefix,
proof: &ibc::CommitmentProofBytes,
root: &ibc::CommitmentRoot,
path: ibc::path::Path,
) -> Result {
proof::verify_for_trie(
prefix.as_bytes(),
proof.as_ref(),
root.as_bytes(),
path,
None,
)
.map_err(Into::into)
proof::verify_for_trie(&[], proof.as_ref(), root.as_bytes(), path, None)
.map_err(Into::into)
}
}

Expand Down
2 changes: 1 addition & 1 deletion solana/solana-ibc/programs/solana-ibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ name = "solana_ibc"

[features]
default = ["custom-entrypoint", "custom-heap"]
witness = []
cpi = ["no-entrypoint"]
custom-heap = ["solana-allocator"]
custom-entrypoint = ["custom-heap"]
mocks = ["ibc-testkit"]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
witness = []

[dependencies]
anchor-lang.workspace = true
Expand Down
10 changes: 10 additions & 0 deletions solana/solana-ibc/programs/solana-ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ pub mod solana_ibc {
&ctx.accounts.witness,
&ctx.accounts.sender,
)?;
#[cfg(feature = "witness")]
{
let storage = &mut ctx.accounts.storage;
let clock = Clock::get()?;
let slot = clock.slot;
let timestamp = clock.unix_timestamp as u64;
storage
.add_local_consensus_state(slot, timestamp, *provable.hash())
.unwrap();
}
ctx.accounts.chain.initialise(
&mut provable,
config,
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 cfc90ea

Please sign in to comment.