Skip to content

Commit

Permalink
fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvja committed Oct 19, 2023
2 parents fba9056 + 9688148 commit d72d1d0
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 139 deletions.
96 changes: 38 additions & 58 deletions solana/solana-ibc/programs/solana-ibc/src/execution_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,81 +396,41 @@ impl ExecutionContext for SolanaIbcStorage<'_, '_> {

fn store_next_sequence_send(
&mut self,
seq_send_path: &SeqSendPath,
path: &SeqSendPath,
seq: Sequence,
) -> Result {
msg!(
"store_next_sequence_send: path: {}, seq: {:?}",
seq_send_path,
seq
);
let mut store = self.0.borrow_mut();
let seq_send_key =
(seq_send_path.0.to_string(), seq_send_path.1.to_string());

let next_seq_send_trie_key = TrieKey::from(seq_send_path);
let trie = store.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),
msg!("store_next_sequence_send: path: {path}, seq: {seq}");
self.store_next_sequence(
path.into(),
super::SequenceTripleIdx::Send,
seq,
)
.unwrap();

store.next_sequence_send.insert(seq_send_key, u64::from(seq));
Ok(())
}

fn store_next_sequence_recv(
&mut self,
seq_recv_path: &SeqRecvPath,
path: &SeqRecvPath,
seq: Sequence,
) -> Result {
msg!(
"store_next_sequence_recv: path: {}, seq: {:?}",
seq_recv_path,
seq
);
let mut store = self.0.borrow_mut();
let seq_recv_key =
(seq_recv_path.0.to_string(), seq_recv_path.1.to_string());
let next_seq_recv_trie_key = TrieKey::from(seq_recv_path);
let trie = store.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),
msg!("store_next_sequence_recv: path: {path}, seq: {seq}");
self.store_next_sequence(
path.into(),
super::SequenceTripleIdx::Recv,
seq,
)
.unwrap();
store.next_sequence_recv.insert(seq_recv_key, u64::from(seq));
Ok(())
}

fn store_next_sequence_ack(
&mut self,
seq_ack_path: &SeqAckPath,
path: &SeqAckPath,
seq: Sequence,
) -> Result {
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 mut store = self.0.borrow_mut();
let next_seq_ack_trie_key = TrieKey::from(seq_ack_path);
let trie = store.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),
msg!("store_next_sequence_ack: path: {path}, seq: {seq}");
self.store_next_sequence(
path.into(),
super::SequenceTripleIdx::Ack,
seq,
)
.unwrap();
store.next_sequence_ack.insert(seq_ack_key, u64::from(seq));
Ok(())
}

fn increase_channel_counter(&mut self) -> Result {
Expand Down Expand Up @@ -505,6 +465,26 @@ impl ExecutionContext for SolanaIbcStorage<'_, '_> {
fn get_client_execution_context(&mut self) -> &mut Self::E { self }
}

impl SolanaIbcStorage<'_, '_> {
fn store_next_sequence(
&mut self,
path: crate::trie_key::SequencePath<'_>,
index: super::SequenceTripleIdx,
seq: Sequence,
) -> Result {
let map_key = (path.port_id.to_string(), path.channel_id.to_string());
let triple = self.next_sequence.entry(map_key).or_default();
triple.set(index, seq);

let trie_key = TrieKey::from(path);
let trie = self.trie.as_mut().unwrap();
trie.set(&trie_key, &triple.to_hash()).unwrap();

Ok(())
}
}


fn record_packet_sequence(
hash_map: &mut BTreeMap<(InnerPortId, InnerChannelId), Vec<InnerSequence>>,
port_id: &PortId,
Expand Down
141 changes: 118 additions & 23 deletions solana/solana-ibc/programs/solana-ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::rc::Rc;

use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};
use ibc::core::ics04_channel::packet::Sequence;
use ibc::core::ics24_host::identifier::PortId;
use ibc::core::router::{Module, ModuleId, Router};

Expand Down Expand Up @@ -89,9 +90,7 @@ pub mod solana_ibc {
connection_to_client: solana_ibc_store.connection_to_client.clone(),
port_channel_id_set: solana_ibc_store.port_channel_id_set.clone(),
channel_counter: solana_ibc_store.channel_counter,
next_sequence_send: solana_ibc_store.next_sequence_send.clone(),
next_sequence_recv: solana_ibc_store.next_sequence_recv.clone(),
next_sequence_ack: solana_ibc_store.next_sequence_ack.clone(),
next_sequence: solana_ibc_store.next_sequence.clone(),
packet_commitment_sequence_sets: solana_ibc_store
.packet_commitment_sequence_sets
.clone(),
Expand All @@ -105,11 +104,52 @@ pub mod solana_ibc {
trie: Some(trie),
};

<<<<<<< HEAD
let mut store =
SolanaIbcStorage(Rc::<RefCell<SolanaIbcStorageTest>>::new(
solana_real_storage.into(),
));
let mut router = store.clone();
=======
let mut solana_real_storage_another = SolanaIbcStorage {
height: solana_ibc_store.height,
module_holder: solana_ibc_store.module_holder.clone(),
clients: solana_ibc_store.clients.clone(),
client_id_set: solana_ibc_store.client_id_set.clone(),
client_counter: solana_ibc_store.client_counter,
client_processed_times: solana_ibc_store
.client_processed_times
.clone(),
client_processed_heights: solana_ibc_store
.client_processed_heights
.clone(),
consensus_states: solana_ibc_store.consensus_states.clone(),
client_consensus_state_height_sets: solana_ibc_store
.client_consensus_state_height_sets
.clone(),
connection_id_set: solana_ibc_store.connection_id_set.clone(),
connection_counter: solana_ibc_store.connection_counter,
connections: solana_ibc_store.connections.clone(),
channel_ends: solana_ibc_store.channel_ends.clone(),
connection_to_client: solana_ibc_store.connection_to_client.clone(),
port_channel_id_set: solana_ibc_store.port_channel_id_set.clone(),
channel_counter: solana_ibc_store.channel_counter,
next_sequence: solana_ibc_store.next_sequence.clone(),
packet_commitment_sequence_sets: solana_ibc_store
.packet_commitment_sequence_sets
.clone(),
packet_receipt_sequence_sets: solana_ibc_store
.packet_receipt_sequence_sets
.clone(),
packet_acknowledgement_sequence_sets: solana_ibc_store
.packet_acknowledgement_sequence_sets
.clone(),
ibc_events_history: solana_ibc_store.ibc_events_history.clone(),
trie: None,
};

let router = &mut solana_real_storage_another;
>>>>>>> 9688148cb79db1623e63ff5f4eaec2a83516c5b0

let errors =
all_messages.into_iter().fold(vec![], |mut errors, msg| {
Expand Down Expand Up @@ -150,14 +190,10 @@ pub mod solana_ibc {
solana_ibc_store.connection_to_client =
sol_store.connection_to_client.clone();
solana_ibc_store.port_channel_id_set =
sol_store.port_channel_id_set.clone();
solana_ibc_store.channel_counter = sol_store.channel_counter;
solana_ibc_store.next_sequence_send =
sol_store.next_sequence_send.clone();
solana_ibc_store.next_sequence_recv =
sol_store.next_sequence_recv.clone();
solana_ibc_store.next_sequence_ack =
sol_store.next_sequence_ack.clone();
solana_real_storage.port_channel_id_set.clone();
solana_ibc_store.channel_counter = solana_real_storage.channel_counter;
solana_ibc_store.next_sequence =
solana_real_storage.next_sequence.clone();
solana_ibc_store.packet_commitment_sequence_sets =
sol_store.packet_commitment_sequence_sets.clone();
solana_ibc_store.packet_receipt_sequence_sets =
Expand Down Expand Up @@ -212,6 +248,59 @@ pub type InnerConnectionEnd = String; // Serialized
pub type InnerChannelEnd = String; // Serialized
pub type InnerConsensusState = String; // Serialized

/// A triple of send, receive and acknowledge sequences.
#[derive(
Clone,
Debug,
Default,
PartialEq,
Eq,
borsh::BorshSerialize,
borsh::BorshDeserialize,
)]
pub struct InnerSequenceTriple {
sequences: [u64; 3],
mask: u8,
}

#[derive(Clone, Copy)]
pub enum SequenceTripleIdx {
Send = 0,
Recv = 1,
Ack = 2,
}

impl InnerSequenceTriple {
/// Returns sequence at given index or `None` if it wasn’t set yet.
pub fn get(&self, idx: SequenceTripleIdx) -> Option<Sequence> {
if self.mask & (1 << (idx as u32)) == 1 {
Some(Sequence::from(self.sequences[idx as usize]))
} else {
None
}
}

/// Sets sequence at given index.
pub fn set(&mut self, idx: SequenceTripleIdx, seq: Sequence) {
self.sequences[idx as usize] = u64::from(seq);
self.mask |= 1 << (idx as u32)
}

/// Encodes the object as a `CryptoHash` so it can be stored in the trie
/// directly.
pub fn to_hash(&self) -> lib::hash::CryptoHash {
let mut hash = lib::hash::CryptoHash::default();
let (first, tail) = stdx::split_array_mut::<8, 24, 32>(&mut hash.0);
let (second, tail) = stdx::split_array_mut::<8, 16, 24>(tail);
let (third, tail) = stdx::split_array_mut::<8, 8, 16>(tail);
*first = self.sequences[0].to_be_bytes();
*second = self.sequences[1].to_be_bytes();
*third = self.sequences[2].to_be_bytes();
tail[0] = self.mask;
hash
}
}

#[account]
#[derive(Debug)]
/// All the structs from IBC are stored as String since they dont implement AnchorSerialize and AnchorDeserialize
Expand Down Expand Up @@ -241,12 +330,15 @@ pub struct SolanaIbcStorageTemp {
/// The port and channel id tuples of the channels.
pub port_channel_id_set: Vec<(InnerPortId, InnerChannelId)>,
pub channel_counter: u64,
pub next_sequence_send:
BTreeMap<(InnerPortId, InnerChannelId), InnerSequence>,
pub next_sequence_recv:
BTreeMap<(InnerPortId, InnerChannelId), InnerSequence>,
pub next_sequence_ack:
BTreeMap<(InnerPortId, InnerChannelId), InnerSequence>,

/// Next send, receive and ack sequence for given (port, channel).
///
/// 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>>,
Expand Down Expand Up @@ -288,12 +380,15 @@ pub struct SolanaIbcStorageTest<'a, 'b> {
/// The port and channel id tuples of the channels.
pub port_channel_id_set: Vec<(InnerPortId, InnerChannelId)>,
pub channel_counter: u64,
pub next_sequence_send:
BTreeMap<(InnerPortId, InnerChannelId), InnerSequence>,
pub next_sequence_recv:
BTreeMap<(InnerPortId, InnerChannelId), InnerSequence>,
pub next_sequence_ack:
BTreeMap<(InnerPortId, InnerChannelId), InnerSequence>,

/// Next send, receive and ack sequence for given (port, channel).
///
/// 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>>,
Expand Down
Loading

0 comments on commit d72d1d0

Please sign in to comment.