Skip to content

Commit

Permalink
Store packets (#64)
Browse files Browse the repository at this point in the history
Store the packets in a PDA so that it can be queried by the PDA. Right now it is getting stored in a single PDA and later on we can have channelId, portId and sequence as the seeds.

* storing packets in the private storage

* update comment

* store packets in PDA

* fmt

* using packetMsg struct instead of serialized

* fmt

* fix clippy

* added todo

* fmt
  • Loading branch information
dhruvja authored Nov 1, 2023
1 parent 59a6765 commit ff37a25
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
23 changes: 20 additions & 3 deletions solana/solana-ibc/programs/solana-ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ use ibc::core::router::{Module, ModuleId, Router};

const SOLANA_IBC_STORAGE_SEED: &[u8] = b"solana_ibc_storage";
const TRIE_SEED: &[u8] = b"trie";
const PACKET_SEED: &[u8] = b"packet";
const CONNECTION_ID_PREFIX: &str = "connection-";
const CHANNEL_ID_PREFIX: &str = "channel-";
use ibc::core::MsgEnvelope;

use crate::storage::IBCPackets;

declare_id!("EnfDJsAK7BGgetnmKzBx86CsgC5kfSPcsktFCQ4YLC81");

Expand Down Expand Up @@ -45,20 +49,31 @@ pub mod solana_ibc {
let provable =
solana_trie::AccountTrie::new(account.try_borrow_mut_data()?)
.ok_or(ProgramError::InvalidAccountData)?;
let packets: &mut IBCPackets = &mut ctx.accounts.packets;

let mut store = storage::IbcStorage::new(storage::IbcStorageInner {
private,
provable,
packets,
});

{
let mut router = store.clone();
if let Err(e) =
ibc::core::dispatch(&mut store, &mut router, message)
ibc::core::dispatch(&mut store, &mut router, message.clone())
{
return err!(Error::RouterError(&e));
}
}
if let MsgEnvelope::Packet(packet) = message {
// store the packet if not exists
// TODO(dhruvja) Store in a PDA with channelId, portId and Sequence
let mut store = store.borrow_mut();
let packets = &mut store.packets.0;
if !packets.iter().any(|pack| &packet == pack) {
packets.push(packet);
}
}

// `store` is the only reference to inner storage making refcount == 1
// which means try_into_inner will succeed.
Expand All @@ -80,8 +95,10 @@ pub struct Deliver<'info> {
storage: Account<'info, storage::PrivateStorage>,
#[account(init_if_needed, payer = sender, seeds = [TRIE_SEED], bump, space = 1000)]
/// CHECK:
trie: AccountInfo<'info>,
system_program: Program<'info, System>,
pub trie: AccountInfo<'info>,
#[account(init_if_needed, payer = sender, seeds = [PACKET_SEED], bump, space = 1000)]
pub packets: Account<'info, IBCPackets>,
pub system_program: Program<'info, System>,
}

/// Error returned when handling a request.
Expand Down
6 changes: 6 additions & 0 deletions solana/solana-ibc/programs/solana-ibc/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use alloc::rc::Rc;
use core::cell::RefCell;

use anchor_lang::prelude::*;
use ibc::core::ics04_channel::msgs::PacketMsg;
use ibc::core::ics04_channel::packet::Sequence;

pub(crate) type InnerHeight = (u64, u64);
Expand Down Expand Up @@ -72,6 +73,10 @@ impl InnerSequenceTriple {
}
}

#[account]
#[derive(Debug)]
pub struct IBCPackets(pub Vec<PacketMsg>);

#[account]
#[derive(Debug)]
/// All the structs from IBC are stored as String since they dont implement
Expand Down Expand Up @@ -131,6 +136,7 @@ pub(crate) struct IbcStorageInner<'a, 'b> {
pub private: &'a mut PrivateStorage,
pub provable:
solana_trie::AccountTrie<core::cell::RefMut<'a, &'b mut [u8]>>,
pub packets: &'a mut IBCPackets,
}

/// A reference-counted reference to the IBC storage.
Expand Down
8 changes: 7 additions & 1 deletion solana/solana-ibc/programs/solana-ibc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use ibc::mock::header::MockHeader;
use ibc_proto::google::protobuf::Any;

use crate::storage::PrivateStorage;
use crate::{accounts, instruction, ID, SOLANA_IBC_STORAGE_SEED, TRIE_SEED};
use crate::{
accounts, instruction, ID, PACKET_SEED, SOLANA_IBC_STORAGE_SEED, TRIE_SEED,
};

const IBC_TRIE_PREFIX: &[u8] = b"ibc/";

Expand Down Expand Up @@ -77,6 +79,8 @@ fn anchor_test_deliver() -> Result<()> {
let solana_ibc_storage = Pubkey::find_program_address(seeds, &crate::ID).0;
let trie_seeds = &[TRIE_SEED];
let trie = Pubkey::find_program_address(trie_seeds, &crate::ID).0;
let packet_seeds = &[PACKET_SEED];
let packets = Pubkey::find_program_address(packet_seeds, &crate::ID).0;

let (mock_client_state, mock_cs_state) = create_mock_client_and_cs_state();
let _client_id = ClientId::new(mock_client_state.client_type(), 0).unwrap();
Expand All @@ -97,6 +101,7 @@ fn anchor_test_deliver() -> Result<()> {
storage: solana_ibc_storage,
trie,
system_program: system_program::ID,
packets,
})
.args(instruction::Deliver { message })
.payer(authority.clone())
Expand Down Expand Up @@ -144,6 +149,7 @@ fn anchor_test_deliver() -> Result<()> {
storage: solana_ibc_storage,
trie,
system_program: system_program::ID,
packets,
})
.args(instruction::Deliver { message })
.payer(authority.clone())
Expand Down

0 comments on commit ff37a25

Please sign in to comment.