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

Store packets #64

Merged
merged 14 commits into from
Nov 1, 2023
35 changes: 33 additions & 2 deletions solana/solana-ibc/programs/solana-ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ use core::cell::RefCell;

use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};
use ibc::core::ics04_channel::msgs::PacketMsg;
use ibc::core::ics04_channel::packet::Sequence;
use ibc::core::ics24_host::identifier::PortId;
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-";

Expand All @@ -33,6 +35,8 @@ mod validation_context;

#[anchor_lang::program]
pub mod solana_ibc {
use ibc::core::MsgEnvelope;

use super::*;

pub fn deliver(
Expand All @@ -51,14 +55,33 @@ 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 inner = IbcStorageInner { private, provable };
let inner = IbcStorageInner { private, provable, packets };
let mut store = IbcStorage(Rc::new(RefCell::new(inner)));
let mut router = store.clone();

let errors = messages
.into_iter()
.map(|msg| ibc::core::dispatch(&mut store, &mut router, msg))
.map(|msg| {
let result =
ibc::core::dispatch(&mut store, &mut router, msg.clone());
if let MsgEnvelope::Packet(packet) = msg {
// store the packet if not exists
// TODO(dhruvja) Store in a PDA with channelId, portId and Sequence
dhruvja marked this conversation as resolved.
Show resolved Hide resolved
let mut inner_store = store.0.borrow_mut();
match inner_store
.packets
.0
.iter()
.find(|&pack| &packet == pack)
{
Some(_) => (),
None => inner_store.packets.0.push(packet),
}
}
result
})
.filter_map(core::result::Result::err)
.collect::<Vec<_>>();

Expand Down Expand Up @@ -88,6 +111,8 @@ pub struct Deliver<'info> {
#[account(init_if_needed, payer = sender, seeds = [TRIE_SEED], bump, space = 1000)]
/// CHECK:
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>,
}

Expand Down Expand Up @@ -214,9 +239,15 @@ pub struct PrivateStorage {
pub ibc_events_history: BTreeMap<InnerHeight, Vec<InnerIbcEvent>>,
}

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


/// All the structs from IBC are stored as String since they dont implement AnchorSerialize and AnchorDeserialize
#[derive(Debug)]
pub struct IbcStorageInner<'a, 'b> {
pub packets: &'a mut IBCPackets,
pub private: &'a mut PrivateStorage,
pub provable:
solana_trie::AccountTrie<core::cell::RefMut<'a, &'b mut [u8]>>,
Expand Down
8 changes: 6 additions & 2 deletions solana/solana-ibc/programs/solana-ibc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use ibc::mock::header::MockHeader;
use ibc_proto::google::protobuf::Any;

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

const IBC_TRIE_PREFIX: &[u8] = b"ibc/";
Expand Down Expand Up @@ -79,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 @@ -99,6 +101,7 @@ fn anchor_test_deliver() -> Result<()> {
storage: solana_ibc_storage,
trie,
system_program: system_program::ID,
packets,
})
.args(instruction::Deliver { messages })
.payer(authority.clone())
Expand Down Expand Up @@ -146,6 +149,7 @@ fn anchor_test_deliver() -> Result<()> {
storage: solana_ibc_storage,
trie,
system_program: system_program::ID,
packets,
})
.args(instruction::Deliver { messages })
.payer(authority.clone())
Expand Down
Loading