Skip to content

Commit

Permalink
solana-ibc: don’t wrap deliver messages in protobuf message (#66)
Browse files Browse the repository at this point in the history
Instead of creating an any wrapper for possible deliver messages,
pass the MsgEnvelope directly and encode it with borsh.
  • Loading branch information
mina86 authored Oct 31, 2023
1 parent 4bc35a2 commit ed47cad
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 58 deletions.
27 changes: 7 additions & 20 deletions solana/solana-ibc/programs/solana-ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub mod solana_ibc {

pub fn deliver(
ctx: Context<Deliver>,
messages: Vec<AnyCheck>,
messages: Vec<ibc::core::MsgEnvelope>,
) -> Result<()> {
msg!("Called deliver method");
let _sender = ctx.accounts.sender.to_account_info();
Expand All @@ -56,19 +56,11 @@ pub mod solana_ibc {
let mut store = IbcStorage(Rc::new(RefCell::new(inner)));
let mut router = store.clone();

let mut errors = Vec::new();
for msg in messages {
let msg = ibc_proto::google::protobuf::Any {
type_url: msg.type_url,
value: msg.value,
};
let res = ibc::core::MsgEnvelope::try_from(msg).and_then(|msg| {
ibc::core::dispatch(&mut store, &mut router, msg)
});
if let Err(err) = res {
errors.push(err);
}
}
let errors = messages
.into_iter()
.map(|msg| ibc::core::dispatch(&mut store, &mut router, msg))
.filter_map(core::result::Result::err)
.collect::<Vec<_>>();

// Drop refcount on store so we can unwrap the Rc object below.
core::mem::drop(router);
Expand All @@ -86,6 +78,7 @@ pub mod solana_ibc {
Ok(())
}
}

#[derive(Accounts)]
pub struct Deliver<'info> {
#[account(mut)]
Expand All @@ -103,12 +96,6 @@ pub struct EmitIBCEvent {
pub ibc_event: Vec<u8>,
}

#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, PartialEq)]
pub struct AnyCheck {
pub type_url: String,
pub value: Vec<u8>,
}

pub type InnerHeight = (u64, u64);
pub type HostHeight = InnerHeight;
pub type SolanaTimestamp = u64;
Expand Down
75 changes: 37 additions & 38 deletions solana/solana-ibc/programs/solana-ibc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@ use ibc::mock::client_state::MockClientState;
use ibc::mock::consensus_state::MockConsensusState;
use ibc::mock::header::MockHeader;
use ibc_proto::google::protobuf::Any;
use ibc_proto::protobuf::Protobuf;

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

const CLIENT_CREATE_CLIENT: &str = "/ibc.core.client.v1.MsgCreateClient";
const CONNECTION_OPEN_INIT: &str =
"/ibc.core.connection.v1.MsgConnectionOpenInit";
const IBC_TRIE_PREFIX: &[u8] = b"ibc/";

fn airdrop(client: &RpcClient, account: Pubkey, lamports: u64) -> Signature {
Expand All @@ -52,6 +48,14 @@ fn create_mock_client_and_cs_state() -> (MockClientState, MockConsensusState) {
(mock_client_state, mock_cs_state)
}

macro_rules! make_message {
($msg:expr, $($variant:path),+ $(,)?) => {{
let message = $msg;
$( let message = $variant(message); )*
message
}}
}

#[test]
#[ignore = "Requires local validator to run"]
fn anchor_test_deliver() -> Result<()> {
Expand All @@ -78,17 +82,15 @@ fn anchor_test_deliver() -> Result<()> {

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();
let create_client_msg = MsgCreateClient::new(
Any::from(mock_client_state),
Any::from(mock_cs_state),
ibc::Signer::from(authority.pubkey().to_string()),
);
let messages = AnyCheck {
type_url: CLIENT_CREATE_CLIENT.to_string(),
value: create_client_msg.encode_vec(),
};

let all_messages = [messages].to_vec();
let messages = vec![make_message!(
MsgCreateClient::new(
Any::from(mock_client_state),
Any::from(mock_cs_state),
ibc::Signer::from(authority.pubkey().to_string()),
),
ibc::core::ics02_client::msgs::ClientMsg::CreateClient,
ibc::core::MsgEnvelope::Client,
)];

let sig = program
.request()
Expand All @@ -98,7 +100,7 @@ fn anchor_test_deliver() -> Result<()> {
trie,
system_program: system_program::ID,
})
.args(instruction::Deliver { messages: all_messages })
.args(instruction::Deliver { messages })
.payer(authority.clone())
.signer(&*authority)
.send_with_spinner_and_config(RpcSendTransactionConfig {
Expand All @@ -120,25 +122,22 @@ fn anchor_test_deliver() -> Result<()> {
let commitment_prefix: CommitmentPrefix =
IBC_TRIE_PREFIX.to_vec().try_into().unwrap();

let open_init_msg = MsgConnectionOpenInit {
client_id_on_a: ClientId::new(mock_client_state.client_type(), 0)
.unwrap(),
version: Some(Version::default()),
counterparty: Counterparty::new(
counter_party_client_id,
None,
commitment_prefix,
),
delay_period: Duration::from_secs(5),
signer: ibc::Signer::from(authority.pubkey().to_string()),
};

let new_message = AnyCheck {
type_url: CONNECTION_OPEN_INIT.to_string(),
value: open_init_msg.encode_vec(),
};

let all_messages = [new_message].to_vec();
let messages = vec![make_message!(
MsgConnectionOpenInit {
client_id_on_a: ClientId::new(mock_client_state.client_type(), 0)
.unwrap(),
version: Some(Version::default()),
counterparty: Counterparty::new(
counter_party_client_id,
None,
commitment_prefix,
),
delay_period: Duration::from_secs(5),
signer: ibc::Signer::from(authority.pubkey().to_string()),
},
ibc::core::ics03_connection::msgs::ConnectionMsg::OpenInit,
ibc::core::MsgEnvelope::Connection,
)];

let sig = program
.request()
Expand All @@ -148,7 +147,7 @@ fn anchor_test_deliver() -> Result<()> {
trie,
system_program: system_program::ID,
})
.args(instruction::Deliver { messages: all_messages })
.args(instruction::Deliver { messages })
.payer(authority.clone())
.signer(&*authority)
.send_with_spinner_and_config(RpcSendTransactionConfig {
Expand Down

0 comments on commit ed47cad

Please sign in to comment.