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

added trie as attribute to context #30

Merged
merged 13 commits into from
Oct 18, 2023
5 changes: 5 additions & 0 deletions solana/solana-ibc/programs/solana-ibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ ibc-proto.workspace = true
serde.workspace = true
serde_json.workspace = true

lib.workspace = true
memory.workspace = true
sealable-trie.workspace = true
stdx.workspace = true

[dev-dependencies]
anchor-client.workspace = true
anyhow.workspace = true
Expand Down
14 changes: 9 additions & 5 deletions solana/solana-ibc/programs/solana-ibc/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl From<AnyClientState> for Any {
}
}

impl ClientStateValidation<SolanaIbcStorage> for AnyClientState {
impl ClientStateValidation<SolanaIbcStorage<'_, '_>> for AnyClientState {
fn verify_client_message(
&self,
ctx: &SolanaIbcStorage,
Expand Down Expand Up @@ -283,7 +283,7 @@ impl From<MockClientState> for AnyClientState {
fn from(value: MockClientState) -> Self { AnyClientState::Mock(value) }
}

impl ClientStateExecution<SolanaIbcStorage> for AnyClientState {
impl ClientStateExecution<SolanaIbcStorage<'_, '_>> for AnyClientState {
fn initialise(
&self,
ctx: &mut SolanaIbcStorage,
Expand Down Expand Up @@ -371,7 +371,9 @@ impl ClientStateExecution<SolanaIbcStorage> for AnyClientState {
}
}

impl ibc::clients::ics07_tendermint::CommonContext for SolanaIbcStorage {
impl ibc::clients::ics07_tendermint::CommonContext
for SolanaIbcStorage<'_, '_>
{
type ConversionError = ClientError;

type AnyConsensusState = AnyConsensusState;
Expand All @@ -385,7 +387,7 @@ impl ibc::clients::ics07_tendermint::CommonContext for SolanaIbcStorage {
}

#[cfg(any(test, feature = "mocks"))]
impl MockClientContext for SolanaIbcStorage {
impl MockClientContext for SolanaIbcStorage<'_, '_> {
type ConversionError = ClientError;
type AnyConsensusState = AnyConsensusState;

Expand All @@ -401,7 +403,9 @@ impl MockClientContext for SolanaIbcStorage {
}
}

impl ibc::clients::ics07_tendermint::ValidationContext for SolanaIbcStorage {
impl ibc::clients::ics07_tendermint::ValidationContext
for SolanaIbcStorage<'_, '_>
{
fn host_timestamp(&self) -> Result<Timestamp, ContextError> {
ValidationContext::host_timestamp(self)
}
Expand Down
184 changes: 177 additions & 7 deletions solana/solana-ibc/programs/solana-ibc/src/execution_context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::BTreeMap;

use anchor_lang::emit;
use anchor_lang::prelude::borsh;
use anchor_lang::solana_program::msg;
use ibc::core::events::IbcEvent;
use ibc::core::ics02_client::ClientExecutionContext;
Expand All @@ -26,12 +27,15 @@ use crate::client_state::AnyClientState;
use crate::consensus_state::AnyConsensusState;
use crate::{
EmitIBCEvent, HostHeight, InnerChannelId, InnerHeight, InnerPortId,
InnerSequence, SolanaIbcStorage, SolanaTimestamp,
InnerSequence, SolanaIbcStorage, SolanaTimestamp, TrieKey,
};

type Result<T = (), E = ibc::core::ContextError> = core::result::Result<T, E>;

impl ClientExecutionContext for SolanaIbcStorage {
const CONNECTION_ID_PREFIX: &str = "connection-";
const CHANNEL_ID_PREFIX: &str = "channel-";

impl ClientExecutionContext for SolanaIbcStorage<'_, '_> {
type ClientValidationContext = Self;
type AnyClientState = AnyClientState;
type AnyConsensusState = AnyConsensusState;
Expand All @@ -49,6 +53,20 @@ impl ClientExecutionContext for SolanaIbcStorage {
let client_state_key = client_state_path.0.to_string();
let serialized_client_state =
serde_json::to_string(&client_state).unwrap();

let client_state_trie_key = &TrieKey::ClientState {
client_id: client_state_path.0.to_string(),
}
.to_vec();
let trie = self.trie.as_mut().unwrap();
let client_state_hash =
borsh::to_vec(&serialized_client_state).unwrap();
trie.set(
client_state_trie_key,
&lib::hash::CryptoHash::digest(&client_state_hash),
)
.unwrap();

self.clients.insert(client_state_key, serialized_client_state);
Ok(())
}
Expand All @@ -69,6 +87,22 @@ impl ClientExecutionContext for SolanaIbcStorage {
);
let serialized_consensus_state =
serde_json::to_string(&consensus_state).unwrap();

let consensus_state_trie_key = &TrieKey::ConsensusState {
client_id: consensus_state_path.client_id.to_string(),
height: consensus_state_path.height,
epoch: consensus_state_path.epoch,
}
.to_vec();
let trie = self.trie.as_mut().unwrap();
let consensus_state_hash =
borsh::to_vec(&serialized_consensus_state).unwrap();
trie.set(
consensus_state_trie_key,
&lib::hash::CryptoHash::digest(&consensus_state_hash),
)
.unwrap();

self.consensus_states
.insert(consensus_state_key, serialized_consensus_state);
self.height.0 = consensus_state_path.epoch;
Expand All @@ -77,7 +111,7 @@ impl ClientExecutionContext for SolanaIbcStorage {
}
}

impl ExecutionContext for SolanaIbcStorage {
impl ExecutionContext for SolanaIbcStorage<'_, '_> {
fn increase_client_counter(&mut self) -> Result {
self.client_counter.checked_add(1).unwrap();
msg!("client_counter has increased to: {}", self.client_counter);
Expand Down Expand Up @@ -168,10 +202,27 @@ impl ExecutionContext for SolanaIbcStorage {
connection_path,
connection_end
);
self.connections.insert(
connection_path.0.to_string(),
serde_json::to_string(&connection_end).unwrap(),
);

let serialized_connection_end =
serde_json::to_string(&connection_end).unwrap();
let connection_prefix_length = CONNECTION_ID_PREFIX.len();
let connection_id =
&connection_path.0.to_string()[connection_prefix_length..];
let consensus_state_trie_key = &TrieKey::Connection {
connection_id: connection_id.parse().unwrap(),
}
.to_vec();
let trie = self.trie.as_mut().unwrap();
let consensus_state_hash =
borsh::to_vec(&serialized_connection_end).unwrap();
trie.set(
consensus_state_trie_key,
&lib::hash::CryptoHash::digest(&consensus_state_hash),
)
.unwrap();

self.connections
.insert(connection_path.0.to_string(), serialized_connection_end);
Ok(())
}

Expand Down Expand Up @@ -209,6 +260,23 @@ impl ExecutionContext for SolanaIbcStorage {
commitment_path,
commitment
);
let channel_prefix_length = CHANNEL_ID_PREFIX.len();
let channel_id =
&commitment_path.channel_id.to_string()[channel_prefix_length..];

let commitment_trie_key = &TrieKey::Commitment {
port_id: commitment_path.port_id.clone().to_string(),
channel_id: channel_id.parse().unwrap(),
sequence: u64::from(commitment_path.sequence),
}
.to_vec();
let trie = self.trie.as_mut().unwrap();
trie.set(
commitment_trie_key,
&lib::hash::CryptoHash::digest(&commitment.into_vec()),
)
.unwrap();

record_packet_sequence(
&mut self.packet_commitment_sequence_sets,
&commitment_path.port_id,
Expand Down Expand Up @@ -247,6 +315,19 @@ impl ExecutionContext for SolanaIbcStorage {
receipt_path,
receipt
);
let channel_prefix_length = CHANNEL_ID_PREFIX.len();
let channel_id =
&receipt_path.channel_id.to_string()[channel_prefix_length..];
dhruvja marked this conversation as resolved.
Show resolved Hide resolved

let receipt_trie_key = &TrieKey::Receipts {
port_id: receipt_path.port_id.clone().to_string(),
dhruvja marked this conversation as resolved.
Show resolved Hide resolved
channel_id: channel_id.parse().unwrap(),
sequence: u64::from(receipt_path.sequence),
}
.to_vec();
let trie = self.trie.as_mut().unwrap();
trie.set(receipt_trie_key, &lib::hash::CryptoHash::DEFAULT).unwrap();
trie.seal(receipt_trie_key).unwrap();
record_packet_sequence(
&mut self.packet_receipt_sequence_sets,
&receipt_path.port_id,
Expand All @@ -266,6 +347,22 @@ impl ExecutionContext for SolanaIbcStorage {
ack_path,
ack_commitment
);
let channel_prefix_length = CHANNEL_ID_PREFIX.len();
let channel_id =
&ack_path.channel_id.to_string()[channel_prefix_length..];

let ack_commitment_trie_key = &TrieKey::Acks {
port_id: ack_path.port_id.clone().to_string(),
channel_id: channel_id.parse().unwrap(),
sequence: u64::from(ack_path.sequence),
}
.to_vec();
let trie = self.trie.as_mut().unwrap();
trie.set(
ack_commitment_trie_key,
&lib::hash::CryptoHash::digest(&ack_commitment.into_vec()),
)
.unwrap();
dhruvja marked this conversation as resolved.
Show resolved Hide resolved
record_packet_sequence(
&mut self.packet_acknowledgement_sequence_sets,
&ack_path.port_id,
Expand Down Expand Up @@ -302,6 +399,26 @@ impl ExecutionContext for SolanaIbcStorage {
channel_end_path.0.clone().to_string(),
channel_end_path.1.clone().to_string(),
));

let serialized_channel_end =
serde_json::to_string(&channel_end).unwrap();
dhruvja marked this conversation as resolved.
Show resolved Hide resolved
let channel_prefix_length = CHANNEL_ID_PREFIX.len();
let channel_id =
&channel_end_path.1.to_string()[channel_prefix_length..];

let channel_end_trie_key = &TrieKey::ChannelEnd {
port_id: channel_end_path.0.clone().to_string(),
channel_id: channel_id.parse().unwrap(),
}
.to_vec();
let trie = self.trie.as_mut().unwrap();
let channel_end_hash = borsh::to_vec(&serialized_channel_end).unwrap();
trie.set(
channel_end_trie_key,
&lib::hash::CryptoHash::digest(&channel_end_hash),
dhruvja marked this conversation as resolved.
Show resolved Hide resolved
)
.unwrap();

self.channel_ends.insert(
(channel_end_path.0.to_string(), channel_end_path.1.to_string()),
serde_json::to_string(&channel_end).unwrap(),
Expand All @@ -321,6 +438,25 @@ impl ExecutionContext for SolanaIbcStorage {
);
let seq_send_key =
(seq_send_path.0.to_string(), seq_send_path.1.to_string());

let channel_prefix_length = CHANNEL_ID_PREFIX.len();
let channel_id = &seq_send_path.1.to_string()[channel_prefix_length..];

let next_seq_send_trie_key = &TrieKey::NextSequenceSend {
port_id: seq_send_path.0.clone().to_string(),
channel_id: channel_id.parse().unwrap(),
}
.to_vec();
let trie = self.trie.as_mut().unwrap();
let seq_in_u64: u64 = seq.into();
let seq_in_bytes = seq_in_u64.to_be_bytes();

trie.set(
next_seq_send_trie_key,
&lib::hash::CryptoHash::digest(&seq_in_bytes),
)
.unwrap();

self.next_sequence_send.insert(seq_send_key, u64::from(seq));
Ok(())
}
Expand All @@ -337,6 +473,23 @@ impl ExecutionContext for SolanaIbcStorage {
);
let seq_recv_key =
(seq_recv_path.0.to_string(), seq_recv_path.1.to_string());
let channel_prefix_length = CHANNEL_ID_PREFIX.len();
let channel_id = &seq_recv_path.1.to_string()[channel_prefix_length..];

let next_seq_recv_trie_key = &TrieKey::NextSequenceRecv {
port_id: seq_recv_path.0.clone().to_string(),
channel_id: channel_id.parse().unwrap(),
}
.to_vec();
let trie = self.trie.as_mut().unwrap();
let seq_in_u64: u64 = seq.into();
let seq_in_bytes = seq_in_u64.to_be_bytes();

trie.set(
next_seq_recv_trie_key,
&lib::hash::CryptoHash::digest(&seq_in_bytes),
)
.unwrap();
self.next_sequence_recv.insert(seq_recv_key, u64::from(seq));
Ok(())
}
Expand All @@ -349,6 +502,23 @@ impl ExecutionContext for SolanaIbcStorage {
msg!("store_next_sequence_ack: path: {}, seq: {:?}", seq_ack_path, seq);
let seq_ack_key =
(seq_ack_path.0.to_string(), seq_ack_path.1.to_string());
let channel_prefix_length = CHANNEL_ID_PREFIX.len();
let channel_id = &seq_ack_path.1.to_string()[channel_prefix_length..];

let next_seq_ack_trie_key = &TrieKey::NextSequenceAck {
port_id: seq_ack_path.0.clone().to_string(),
channel_id: channel_id.parse().unwrap(),
}
.to_vec();
let trie = self.trie.as_mut().unwrap();
let seq_in_u64: u64 = seq.into();
let seq_in_bytes = seq_in_u64.to_be_bytes();

trie.set(
next_seq_ack_trie_key,
&lib::hash::CryptoHash::digest(&seq_in_bytes),
)
.unwrap();
self.next_sequence_ack.insert(seq_ack_key, u64::from(seq));
Ok(())
}
Expand Down
Loading
Loading