diff --git a/solana/solana-ibc/programs/solana-ibc/src/lib.rs b/solana/solana-ibc/programs/solana-ibc/src/lib.rs index 227d7540..bd7418c9 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/lib.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/lib.rs @@ -37,7 +37,7 @@ pub mod solana_ibc { pub fn deliver( ctx: Context, - messages: Vec, + messages: Vec, ) -> Result<()> { msg!("Called deliver method"); let _sender = ctx.accounts.sender.to_account_info(); @@ -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::>(); // Drop refcount on store so we can unwrap the Rc object below. core::mem::drop(router); @@ -86,6 +78,7 @@ pub mod solana_ibc { Ok(()) } } + #[derive(Accounts)] pub struct Deliver<'info> { #[account(mut)] @@ -103,12 +96,6 @@ pub struct EmitIBCEvent { pub ibc_event: Vec, } -#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, PartialEq)] -pub struct AnyCheck { - pub type_url: String, - pub value: Vec, -} - pub type InnerHeight = (u64, u64); pub type HostHeight = InnerHeight; pub type SolanaTimestamp = u64; diff --git a/solana/solana-ibc/programs/solana-ibc/src/tests.rs b/solana/solana-ibc/programs/solana-ibc/src/tests.rs index d7ebf110..0c3b55c5 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/tests.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/tests.rs @@ -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 { @@ -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<()> { @@ -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() @@ -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 { @@ -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() @@ -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 {