Skip to content

Commit

Permalink
use solana height and timestamp for cf-guest host metdata
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvja committed Sep 7, 2024
1 parent 04d2070 commit ce25a40
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions solana/solana-ibc/programs/solana-ibc/src/client_state.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -236,14 +237,20 @@ impl cf_guest::CommonContext<sigverify::ed25519::PubKey>
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))
}
Expand Down

0 comments on commit ce25a40

Please sign in to comment.