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: move storage types to a new storage module #68

Merged
merged 2 commits into from
Nov 1, 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
13 changes: 5 additions & 8 deletions solana/solana-ibc/programs/solana-ibc/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use ibc_proto::ibc::lightclients::tendermint::v1::ClientState as RawTmClientStat
use ibc_proto::ibc::mock::ClientState as RawMockClientState;
use ibc_proto::protobuf::Protobuf;

use super::consensus_state::AnyConsensusState;
use crate::IbcStorage;
use crate::consensus_state::AnyConsensusState;
use crate::storage::IbcStorage;

#[derive(Clone, Debug, PartialEq)]
pub enum AnyClientState {
Expand Down Expand Up @@ -468,8 +468,7 @@ impl ibc::clients::ics07_tendermint::CommonContext for IbcStorage<'_, '_> {
) -> Result<Vec<Height>, ContextError> {
// TODO(mina86): use BTreeMap::range here so that we don’t iterate over
// the entire map.
self.0
.borrow()
self.borrow()
.private
.consensus_states
.keys()
Expand Down Expand Up @@ -518,8 +517,7 @@ impl ibc::clients::ics07_tendermint::ValidationContext for IbcStorage<'_, '_> {
use core::ops::Bound;
let height = (height.revision_number(), height.revision_height());
let min = (client_id.to_string(), height);
self.0
.borrow()
self.borrow()
.private
.consensus_states
.range((Bound::Excluded(min), Bound::Unbounded))
Expand All @@ -539,8 +537,7 @@ impl ibc::clients::ics07_tendermint::ValidationContext for IbcStorage<'_, '_> {
height: &Height,
) -> Result<Option<Self::AnyConsensusState>, ContextError> {
let height = (height.revision_number(), height.revision_height());
self.0
.borrow()
self.borrow()
.private
.consensus_states
.range(..(client_id.to_string(), height))
Expand Down
95 changes: 42 additions & 53 deletions solana/solana-ibc/programs/solana-ibc/src/execution_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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::error::ClientError;
use ibc::core::ics02_client::ClientExecutionContext;
use ibc::core::ics03_connection::connection::ConnectionEnd;
use ibc::core::ics04_channel::channel::ChannelEnd;
Expand All @@ -25,11 +26,9 @@ use ibc::Height;

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

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

Expand All @@ -40,22 +39,21 @@ impl ClientExecutionContext for IbcStorage<'_, '_> {

fn store_client_state(
&mut self,
client_state_path: ClientStatePath,
path: ClientStatePath,
client_state: Self::AnyClientState,
) -> Result {
msg!(
"store_client_state - path: {}, client_state: {:?}",
client_state_path,
client_state,
);
let client_state_key = client_state_path.0.to_string();
let serialized_client_state = borsh::to_vec(&client_state).unwrap();
let hash = lib::hash::CryptoHash::digest(&serialized_client_state);
let mut store = self.0.borrow_mut();
msg!("store_client_state({path}, {client_state:?})");
let key = path.0.to_string();
let serialized = borsh::to_vec(&client_state).map_err(|err| {
ClientError::Other { description: err.to_string() }
})?;
let hash = lib::hash::CryptoHash::digest(&serialized);
msg!("This is serialized client state {hash}");
store.provable.set(&TrieKey::from(&client_state_path), &hash).unwrap();
store.private.clients.insert(client_state_key, serialized_client_state);
store.private.client_id_set.push(client_state_path.0.to_string());

let mut store = self.borrow_mut();
store.provable.set(&TrieKey::from(&path), &hash).unwrap();
store.private.clients.insert(key.clone(), serialized);
store.private.client_id_set.push(key);
Ok(())
}

Expand All @@ -73,7 +71,7 @@ impl ClientExecutionContext for IbcStorage<'_, '_> {
consensus_state_path.client_id.to_string(),
(consensus_state_path.epoch, consensus_state_path.height),
);
let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
let serialized_consensus_state =
serde_json::to_string(&consensus_state).unwrap();

Expand Down Expand Up @@ -102,7 +100,7 @@ impl ClientExecutionContext for IbcStorage<'_, '_> {
) -> Result<(), ContextError> {
msg!("delete_consensus_state({})", path);
let key = (path.client_id.to_string(), (path.epoch, path.height));
let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
store.private.consensus_states.remove(&key);
store.provable.del(&TrieKey::from(&path)).unwrap();
Ok(())
Expand All @@ -114,8 +112,7 @@ impl ClientExecutionContext for IbcStorage<'_, '_> {
client_id: ClientId,
height: Height,
) -> Result<(), ContextError> {
self.0
.borrow_mut()
self.borrow_mut()
.private
.client_processed_heights
.get_mut(client_id.as_str())
Expand All @@ -133,8 +130,7 @@ impl ClientExecutionContext for IbcStorage<'_, '_> {
client_id: ClientId,
height: Height,
) -> Result<(), ContextError> {
self.0
.borrow_mut()
self.borrow_mut()
.private
.client_processed_times
.get_mut(client_id.as_str())
Expand All @@ -154,8 +150,7 @@ impl ClientExecutionContext for IbcStorage<'_, '_> {
timestamp: Timestamp,
) -> Result<(), ContextError> {
msg!("store_update_time({}, {}, {})", client_id, height, timestamp);
self.0
.borrow_mut()
self.borrow_mut()
.private
.client_processed_times
.entry(client_id.to_string())
Expand All @@ -174,8 +169,7 @@ impl ClientExecutionContext for IbcStorage<'_, '_> {
host_height: Height,
) -> Result<(), ContextError> {
msg!("store_update_height({}, {}, {})", client_id, height, host_height);
self.0
.borrow_mut()
self.borrow_mut()
.private
.client_processed_heights
.entry(client_id.to_string())
Expand All @@ -190,7 +184,7 @@ impl ClientExecutionContext for IbcStorage<'_, '_> {

impl ExecutionContext for IbcStorage<'_, '_> {
fn increase_client_counter(&mut self) -> Result {
let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
store.private.client_counter =
store.private.client_counter.checked_add(1).unwrap();
msg!(
Expand All @@ -211,7 +205,7 @@ impl ExecutionContext for IbcStorage<'_, '_> {
connection_end
);

let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
let serialized_connection_end =
serde_json::to_string(&connection_end).unwrap();
let connection_trie_key = TrieKey::from(connection_path);
Expand Down Expand Up @@ -241,7 +235,7 @@ impl ExecutionContext for IbcStorage<'_, '_> {
client_connection_path,
conn_id
);
let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
store
.private
.client_to_connection
Expand All @@ -250,7 +244,7 @@ impl ExecutionContext for IbcStorage<'_, '_> {
}

fn increase_connection_counter(&mut self) -> Result {
let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
store.private.connection_counter =
store.private.connection_counter.checked_add(1).unwrap();
msg!(
Expand All @@ -270,7 +264,7 @@ impl ExecutionContext for IbcStorage<'_, '_> {
commitment_path,
commitment
);
let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
let commitment_trie_key = TrieKey::from(commitment_path);
let trie = &mut store.provable;
trie.set(
Expand All @@ -293,7 +287,7 @@ impl ExecutionContext for IbcStorage<'_, '_> {
commitment_path: &CommitmentPath,
) -> Result {
msg!("delete_packet_commitment: path: {}", commitment_path);
let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
let sequences =
store.private.packet_commitment_sequence_sets.get_mut(&(
commitment_path.port_id.clone().to_string(),
Expand All @@ -319,7 +313,7 @@ impl ExecutionContext for IbcStorage<'_, '_> {
receipt_path,
receipt
);
let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
let receipt_trie_key = TrieKey::from(receipt_path);
let trie = &mut store.provable;
trie.set(&receipt_trie_key, &lib::hash::CryptoHash::DEFAULT).unwrap();
Expand All @@ -343,7 +337,7 @@ impl ExecutionContext for IbcStorage<'_, '_> {
ack_path,
ack_commitment
);
let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
let ack_commitment_trie_key = TrieKey::from(ack_path);
let trie = &mut store.provable;
trie.set(
Expand All @@ -362,7 +356,7 @@ impl ExecutionContext for IbcStorage<'_, '_> {

fn delete_packet_acknowledgement(&mut self, ack_path: &AckPath) -> Result {
msg!("delete_packet_acknowledgement: path: {}", ack_path,);
let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
let sequences =
store.private.packet_acknowledgement_sequence_sets.get_mut(&(
ack_path.port_id.clone().to_string(),
Expand All @@ -385,7 +379,7 @@ impl ExecutionContext for IbcStorage<'_, '_> {
channel_end_path,
channel_end
);
let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
store.private.port_channel_id_set.push((
channel_end_path.0.clone().to_string(),
channel_end_path.1.clone().to_string(),
Expand Down Expand Up @@ -413,10 +407,9 @@ impl ExecutionContext for IbcStorage<'_, '_> {
seq: Sequence,
) -> Result {
msg!("store_next_sequence_send: path: {path}, seq: {seq}");
let store: &mut IbcStorageInner<'_, '_> = &mut self.0.borrow_mut();
store.store_next_sequence(
self.borrow_mut().store_next_sequence(
path.into(),
super::SequenceTripleIdx::Send,
crate::storage::SequenceTripleIdx::Send,
seq,
)
}
Expand All @@ -427,10 +420,9 @@ impl ExecutionContext for IbcStorage<'_, '_> {
seq: Sequence,
) -> Result {
msg!("store_next_sequence_recv: path: {path}, seq: {seq}");
let store: &mut IbcStorageInner<'_, '_> = &mut self.0.borrow_mut();
store.store_next_sequence(
self.borrow_mut().store_next_sequence(
path.into(),
super::SequenceTripleIdx::Recv,
crate::storage::SequenceTripleIdx::Recv,
seq,
)
}
Expand All @@ -441,16 +433,15 @@ impl ExecutionContext for IbcStorage<'_, '_> {
seq: Sequence,
) -> Result {
msg!("store_next_sequence_ack: path: {path}, seq: {seq}");
let store: &mut IbcStorageInner<'_, '_> = &mut self.0.borrow_mut();
store.store_next_sequence(
self.borrow_mut().store_next_sequence(
path.into(),
super::SequenceTripleIdx::Ack,
crate::storage::SequenceTripleIdx::Ack,
seq,
)
}

fn increase_channel_counter(&mut self) -> Result {
let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
store.private.channel_counter += 1;
msg!(
"channel_counter has increased to: {}",
Expand All @@ -460,11 +451,9 @@ impl ExecutionContext for IbcStorage<'_, '_> {
}

fn emit_ibc_event(&mut self, event: IbcEvent) -> Result {
let mut store = self.0.borrow_mut();
let mut store = self.borrow_mut();
let host_height =
ibc::Height::new(store.private.height.0, store.private.height.1)
.map_err(ContextError::ClientError)
.unwrap();
ibc::Height::new(store.private.height.0, store.private.height.1)?;
let ibc_event = borsh::to_vec(&event).unwrap();
let inner_host_height =
(host_height.revision_height(), host_height.revision_number());
Expand All @@ -486,11 +475,11 @@ impl ExecutionContext for IbcStorage<'_, '_> {
fn get_client_execution_context(&mut self) -> &mut Self::E { self }
}

impl IbcStorageInner<'_, '_> {
impl crate::storage::IbcStorageInner<'_, '_> {
fn store_next_sequence(
&mut self,
path: crate::trie_key::SequencePath<'_>,
index: super::SequenceTripleIdx,
index: crate::storage::SequenceTripleIdx,
seq: Sequence,
) -> Result {
let trie = &mut self.provable;
Expand Down
Loading
Loading