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: store proof of client id together with client state #113

Merged
merged 4 commits into from
Nov 20, 2023
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
29 changes: 25 additions & 4 deletions common/lib/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,31 @@ impl CryptoHash {

/// Returns hash of given bytes.
#[inline]
pub fn digest(bytes: &[u8]) -> Self {
let mut builder = Self::builder();
builder.update(bytes);
builder.build()
pub fn digest(bytes: &[u8]) -> Self { Self::digestv(&[bytes]) }

/// Returns hash of concatenation of given byte slices.
///
/// This is morally equivalent to feeding all the slices into the builder
/// one-by-one or concatenating them into a single buffer and hashing it in
/// a single step.
///
/// Depending on platform this call may be more efficient. Most notably,
/// Solana offers a vectorised syscall for calculating a SHA-2 256 digest
/// and this method will pass the request directly to it.
#[inline]
pub fn digestv(slices: &[&[u8]]) -> Self {
#[cfg(target_os = "solana")]
{
Self(solana_program::hash::hashv(slices).to_bytes())
}
#[cfg(not(target_os = "solana"))]
{
let mut builder = Self::builder();
for bytes in slices {
builder.update(bytes);
}
builder.build()
}
}

/// Decodes a base64 string representation of the hash.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ impl ClientExecutionContext for IbcStorage<'_, '_> {
msg!("store_client_state({}, {:?})", path, state);
let mut store = self.borrow_mut();
let mut client = store.private.client_mut(&path.0, true)?;
let hash = client.client_state.set(&state)?.digest();
let serialised = client.client_state.set(&state)?;
let hash = CryptoHash::digestv(&[
path.0.as_bytes(),
&[0],
serialised.as_bytes(),
]);
let key = TrieKey::for_client_state(client.index);
store.provable.set(&key, &hash).map_err(error)
}
Expand Down
2 changes: 2 additions & 0 deletions solana/solana-ibc/programs/solana-ibc/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ pub(crate) struct Serialised<T>(Vec<u8>, core::marker::PhantomData<T>);
impl<T> Serialised<T> {
pub fn empty() -> Self { Self(Vec::new(), core::marker::PhantomData) }

pub fn as_bytes(&self) -> &[u8] { self.0.as_slice() }

pub fn digest(&self) -> CryptoHash { CryptoHash::digest(self.0.as_slice()) }

fn make_err(err: io::Error) -> ibc::ClientError {
Expand Down
7 changes: 7 additions & 0 deletions solana/solana-ibc/programs/solana-ibc/src/storage/trie_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ macro_rules! new_key_impl {
impl TrieKey {
/// Constructs a new key for a client state path for client with given
/// counter.
///
/// The hash stored under the key is `hash(borsh(client_id.as_str()) ||
/// borsh(client_state))`.
pub fn for_client_state(client: ids::ClientIdx) -> Self {
new_key_impl!(Tag::ClientState, client)
}

/// Constructs a new key for a consensus state path for client with given
/// counter and specified height.
///
/// The hash stored under the key is `hash(borsh(consensus_state))`.
pub fn for_consensus_state(
client: ids::ClientIdx,
height: ibc::Height,
Expand All @@ -74,6 +79,8 @@ impl TrieKey {
}

/// Constructs a new key for a connection end path.
///
/// The hash stored under the key is `hash(borsh(connection_end))`.
pub fn for_connection(connection: ids::ConnectionIdx) -> Self {
new_key_impl!(Tag::Connection, connection)
}
Expand Down
Loading