Skip to content

Commit

Permalink
solana-ibc: store proof of client id together with client state (#113)
Browse files Browse the repository at this point in the history
As we parse client ids we strip out the client type.  This means that
‘foo-42’ and ‘bar-42’ are internally represented as 42.  To avoid
confusion, we keep the client ids in private storage and whenever we
index client data by the number we check that the full client id
matches.

However, that information isn’t currently available in provable
storage (i.e. it’s not verifiable by light clients).

Change hash we’re storing under client state path to include the
client id.  This way, light clients will be able to verify that
indexes they’re extracting from the client id match the client id they
are given.

Issue: #35
  • Loading branch information
mina86 authored Nov 20, 2023
1 parent 7f4abfb commit f8143cd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
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

0 comments on commit f8143cd

Please sign in to comment.