Skip to content

Commit

Permalink
solana-ibc: store and read commitments from the trie (#74)
Browse files Browse the repository at this point in the history
Firstly, since packet and ack commitments are 32-byte long hashes,
store them directly in the provable trie rather than storing their
hash.

Secondly, implement retrieval of the commitments properly.  Rather
than trying to interpret sequences as commitment, read the hashes
from the provable storage.

Lastly, get rid of sequence_sets maps in the private store which don’t
serve any actual purpose.
  • Loading branch information
mina86 authored Nov 2, 2023
1 parent e99654b commit 98e4567
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 183 deletions.
147 changes: 34 additions & 113 deletions solana/solana-ibc/programs/solana-ibc/src/execution_context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::BTreeMap;

use anchor_lang::emit;
use anchor_lang::prelude::borsh;
use anchor_lang::solana_program::msg;
Expand All @@ -12,9 +10,7 @@ use ibc::core::ics04_channel::commitment::{
AcknowledgementCommitment, PacketCommitment,
};
use ibc::core::ics04_channel::packet::{Receipt, Sequence};
use ibc::core::ics24_host::identifier::{
ChannelId, ClientId, ConnectionId, PortId,
};
use ibc::core::ics24_host::identifier::{ClientId, ConnectionId};
use ibc::core::ics24_host::path::{
AckPath, ChannelEndPath, ClientConnectionPath, ClientConsensusStatePath,
ClientStatePath, CommitmentPath, ConnectionPath, ReceiptPath, SeqAckPath,
Expand All @@ -23,10 +19,11 @@ use ibc::core::ics24_host::path::{
use ibc::core::timestamp::Timestamp;
use ibc::core::{ContextError, ExecutionContext};
use ibc::Height;
use lib::hash::CryptoHash;

use crate::client_state::AnyClientState;
use crate::consensus_state::AnyConsensusState;
use crate::storage::{IbcStorage, InnerChannelId, InnerPortId, InnerSequence};
use crate::storage::IbcStorage;
use crate::trie_key::TrieKey;
use crate::EmitIBCEvent;

Expand Down Expand Up @@ -79,9 +76,7 @@ impl ClientExecutionContext for IbcStorage<'_, '_> {
let trie = &mut store.provable;
trie.set(
&consensus_state_trie_key,
&lib::hash::CryptoHash::digest(
serialized_consensus_state.as_bytes(),
),
&CryptoHash::digest(serialized_consensus_state.as_bytes()),
)
.unwrap();

Expand Down Expand Up @@ -212,9 +207,7 @@ impl ExecutionContext for IbcStorage<'_, '_> {
let trie = &mut store.provable;
trie.set(
&connection_trie_key,
&lib::hash::CryptoHash::digest(
serialized_connection_end.as_bytes(),
),
&CryptoHash::digest(serialized_connection_end.as_bytes()),
)
.unwrap();

Expand Down Expand Up @@ -256,116 +249,55 @@ impl ExecutionContext for IbcStorage<'_, '_> {

fn store_packet_commitment(
&mut self,
commitment_path: &CommitmentPath,
path: &CommitmentPath,
commitment: PacketCommitment,
) -> Result {
msg!(
"store_packet_commitment: path: {}, commitment: {:?}",
commitment_path,
commitment
);
let mut store = self.borrow_mut();
let commitment_trie_key = TrieKey::from(commitment_path);
let trie = &mut store.provable;
trie.set(
&commitment_trie_key,
&lib::hash::CryptoHash::digest(&commitment.into_vec()),
)
.unwrap();

record_packet_sequence(
&mut store.private.packet_commitment_sequence_sets,
&commitment_path.port_id,
&commitment_path.channel_id,
&commitment_path.sequence,
);
msg!("store_packet_commitment({path}, {commitment:?})");
let trie_key = TrieKey::from(path);
// PacketCommitment is always 32-byte long.
let commitment = <&CryptoHash>::try_from(commitment.as_ref()).unwrap();
self.borrow_mut().provable.set(&trie_key, commitment).unwrap();
Ok(())
}

fn delete_packet_commitment(
&mut self,
commitment_path: &CommitmentPath,
) -> Result {
msg!("delete_packet_commitment: path: {}", commitment_path);
let mut store = self.borrow_mut();
let sequences =
store.private.packet_commitment_sequence_sets.get_mut(&(
commitment_path.port_id.to_string(),
commitment_path.channel_id.to_string(),
));
if let Some(sequences) = sequences {
let index = sequences
.iter()
.position(|x| *x == u64::from(commitment_path.sequence))
.unwrap();
sequences.remove(index);
};
fn delete_packet_commitment(&mut self, path: &CommitmentPath) -> Result {
msg!("delete_packet_commitment({path})");
let trie_key = TrieKey::from(path);
self.borrow_mut().provable.del(&trie_key).unwrap();
Ok(())
}

fn store_packet_receipt(
&mut self,
receipt_path: &ReceiptPath,
receipt: Receipt,
path: &ReceiptPath,
Receipt::Ok: Receipt,
) -> Result {
msg!(
"store_packet_receipt: path: {}, receipt: {:?}",
receipt_path,
receipt
);
let mut store = self.borrow_mut();
let receipt_trie_key = TrieKey::from(receipt_path);
let trie = &mut store.provable;
trie.set_and_seal(&receipt_trie_key, &lib::hash::CryptoHash::DEFAULT)
msg!("store_packet_receipt({path}, Ok)");
let trie_key = TrieKey::from(path);
self.borrow_mut()
.provable
.set_and_seal(&trie_key, &CryptoHash::DEFAULT)
.unwrap();
record_packet_sequence(
&mut store.private.packet_receipt_sequence_sets,
&receipt_path.port_id,
&receipt_path.channel_id,
&receipt_path.sequence,
);
Ok(())
}

fn store_packet_acknowledgement(
&mut self,
ack_path: &AckPath,
ack_commitment: AcknowledgementCommitment,
path: &AckPath,
commitment: AcknowledgementCommitment,
) -> Result {
msg!(
"store_packet_acknowledgement: path: {}, ack_commitment: {:?}",
ack_path,
ack_commitment
);
let mut store = self.borrow_mut();
let ack_commitment_trie_key = TrieKey::from(ack_path);
let trie = &mut store.provable;
trie.set(
&ack_commitment_trie_key,
&lib::hash::CryptoHash::digest(&ack_commitment.into_vec()),
)
.unwrap();
record_packet_sequence(
&mut store.private.packet_acknowledgement_sequence_sets,
&ack_path.port_id,
&ack_path.channel_id,
&ack_path.sequence,
);
msg!("store_packet_acknowledgement({path}, {commitment:?})");
let trie_key = TrieKey::from(path);
// AcknowledgementCommitment is always 32-byte long.
let commitment = <&CryptoHash>::try_from(commitment.as_ref()).unwrap();
self.borrow_mut().provable.set(&trie_key, commitment).unwrap();
Ok(())
}

fn delete_packet_acknowledgement(&mut self, ack_path: &AckPath) -> Result {
msg!("delete_packet_acknowledgement: path: {}", ack_path,);
let mut store = self.borrow_mut();
let sequences =
store.private.packet_acknowledgement_sequence_sets.get_mut(&(
ack_path.port_id.to_string(),
ack_path.channel_id.to_string(),
));
if let Some(sequences) = sequences {
let sequence_as_u64: u64 = ack_path.sequence.into();
sequences.remove(sequence_as_u64 as usize);
}
fn delete_packet_acknowledgement(&mut self, path: &AckPath) -> Result {
msg!("delete_packet_acknowledgement({path})");
let trie_key = TrieKey::from(path);
self.borrow_mut().provable.del(&trie_key).unwrap();
Ok(())
}

Expand All @@ -390,7 +322,7 @@ impl ExecutionContext for IbcStorage<'_, '_> {
let trie = &mut &mut store.provable;
trie.set(
&channel_end_trie_key,
&lib::hash::CryptoHash::digest(&serialized_channel_end),
&CryptoHash::digest(&serialized_channel_end),
)
.unwrap();

Expand Down Expand Up @@ -494,14 +426,3 @@ impl crate::storage::IbcStorageInner<'_, '_> {
Ok(())
}
}


fn record_packet_sequence(
hash_map: &mut BTreeMap<(InnerPortId, InnerChannelId), Vec<InnerSequence>>,
port_id: &PortId,
channel_id: &ChannelId,
sequence: &Sequence,
) {
let key = (port_id.to_string(), channel_id.to_string());
hash_map.entry(key).or_default().push(u64::from(*sequence));
}
19 changes: 4 additions & 15 deletions solana/solana-ibc/programs/solana-ibc/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub(crate) type InnerClientId = String;
pub(crate) type InnerConnectionId = String;
pub(crate) type InnerPortId = String;
pub(crate) type InnerChannelId = String;
pub(crate) type InnerSequence = u64;
pub(crate) type InnerIbcEvent = Vec<u8>;
pub(crate) type InnerClient = Vec<u8>; // Serialized
pub(crate) type InnerConnectionEnd = String; // Serialized
Expand All @@ -30,7 +29,7 @@ pub(crate) type InnerConsensusState = String; // Serialized
borsh::BorshSerialize,
borsh::BorshDeserialize,
)]
pub(crate) struct InnerSequenceTriple {
pub(crate) struct SequenceTriple {
sequences: [u64; 3],
mask: u8,
}
Expand All @@ -42,7 +41,7 @@ pub(crate) enum SequenceTripleIdx {
Ack = 2,
}

impl InnerSequenceTriple {
impl SequenceTriple {
/// Returns sequence at given index or `None` if it wasn’t set yet.
pub(crate) fn get(&self, idx: SequenceTripleIdx) -> Option<Sequence> {
if self.mask & (1 << (idx as u32)) == 1 {
Expand Down Expand Up @@ -113,18 +112,8 @@ pub(crate) struct PrivateStorage {
/// We’re storing all three sequences in a single object to reduce amount of
/// different maps we need to maintain. This saves us on the amount of
/// trie nodes we need to maintain.
pub next_sequence:
BTreeMap<(InnerPortId, InnerChannelId), InnerSequenceTriple>,

/// The sequence numbers of the packet commitments.
pub packet_commitment_sequence_sets:
BTreeMap<(InnerPortId, InnerChannelId), Vec<InnerSequence>>,
/// The sequence numbers of the packet receipts.
pub packet_receipt_sequence_sets:
BTreeMap<(InnerPortId, InnerChannelId), Vec<InnerSequence>>,
/// The sequence numbers of the packet acknowledgements.
pub packet_acknowledgement_sequence_sets:
BTreeMap<(InnerPortId, InnerChannelId), Vec<InnerSequence>>,
pub next_sequence: BTreeMap<(InnerPortId, InnerChannelId), SequenceTriple>,

/// The history of IBC events.
pub ibc_events_history: BTreeMap<InnerHeight, Vec<InnerIbcEvent>>,
}
Expand Down
73 changes: 18 additions & 55 deletions solana/solana-ibc/programs/solana-ibc/src/validation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ use ibc::core::ics24_host::path::{
use ibc::core::timestamp::Timestamp;
use ibc::core::{ContextError, ValidationContext};
use ibc::Height;
use lib::hash::CryptoHash;

use crate::client_state::AnyClientState;
use crate::consensus_state::AnyConsensusState;
use crate::storage::IbcStorage;
use crate::trie_key::TrieKey;

impl ValidationContext for IbcStorage<'_, '_> {
type V = Self; // ClientValidationContext
Expand Down Expand Up @@ -224,79 +226,40 @@ impl ValidationContext for IbcStorage<'_, '_> {

fn get_packet_commitment(
&self,
commitment_path: &CommitmentPath,
path: &CommitmentPath,
) -> std::result::Result<PacketCommitment, ContextError> {
let commitment_key = (
commitment_path.port_id.to_string(),
commitment_path.channel_id.to_string(),
);
let store = self.borrow();
match store
.private
.packet_acknowledgement_sequence_sets
.get(&commitment_key)
{
Some(data) => {
let data_in_u8: Vec<u8> =
data.iter().map(|x| *x as u8).collect();
Ok(PacketCommitment::from(data_in_u8))
}
let trie_key = TrieKey::from(path);
match self.borrow().provable.get(&trie_key).ok().flatten() {
Some(hash) => Ok(hash.as_slice().to_vec().into()),
None => Err(ContextError::PacketError(
PacketError::PacketReceiptNotFound {
sequence: commitment_path.sequence,
},
PacketError::PacketReceiptNotFound { sequence: path.sequence },
)),
}
}

fn get_packet_receipt(
&self,
receipt_path: &ReceiptPath,
path: &ReceiptPath,
) -> std::result::Result<Receipt, ContextError> {
let receipt_key = (
receipt_path.port_id.to_string(),
receipt_path.channel_id.to_string(),
);
let store = self.borrow();
match store
.private
.packet_acknowledgement_sequence_sets
.get(&receipt_key)
{
Some(data) => {
match data.binary_search(&u64::from(receipt_path.sequence)) {
Ok(_) => Ok(Receipt::Ok),
Err(_) => Err(ContextError::PacketError(
PacketError::PacketReceiptNotFound {
sequence: receipt_path.sequence,
},
)),
}
}
None => Err(ContextError::PacketError(
PacketError::PacketReceiptNotFound {
sequence: receipt_path.sequence,
},
let trie_key = TrieKey::from(path);
match self.borrow().provable.get(&trie_key).ok().flatten() {
Some(hash) if hash == CryptoHash::DEFAULT => Ok(Receipt::Ok),
_ => Err(ContextError::PacketError(
PacketError::PacketReceiptNotFound { sequence: path.sequence },
)),
}
}

fn get_packet_acknowledgement(
&self,
ack_path: &AckPath,
path: &AckPath,
) -> std::result::Result<AcknowledgementCommitment, ContextError> {
let ack_key =
(ack_path.port_id.to_string(), ack_path.channel_id.to_string());
let store = self.borrow();
match store.private.packet_acknowledgement_sequence_sets.get(&ack_key) {
Some(data) => {
let data_in_u8: Vec<u8> =
data.iter().map(|x| *x as u8).collect();
Ok(AcknowledgementCommitment::from(data_in_u8))
}
let trie_key = TrieKey::from(path);
match self.borrow().provable.get(&trie_key).ok().flatten() {
Some(hash) => Ok(hash.as_slice().to_vec().into()),
None => Err(ContextError::PacketError(
PacketError::PacketAcknowledgementNotFound {
sequence: ack_path.sequence,
sequence: path.sequence,
},
)),
}
Expand Down

0 comments on commit 98e4567

Please sign in to comment.