Skip to content

Commit

Permalink
solana-ibc: accept just one message in deliver
Browse files Browse the repository at this point in the history
Change deliver function to take a single message rather than e vector
of messages.  If caller needs to handle multiple messages, which is
probably uncommon, they can batch Solana transactions.

With that change, handle errors better by actually returning them
on failure.
  • Loading branch information
mina86 committed Oct 31, 2023
1 parent ed47cad commit e7f9373
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
1 change: 1 addition & 0 deletions solana/solana-ibc/programs/solana-ibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ibc.workspace = true
ibc-proto.workspace = true
serde.workspace = true
serde_json.workspace = true
strum.workspace = true

lib.workspace = true
memory.workspace = true
Expand Down
41 changes: 31 additions & 10 deletions solana/solana-ibc/programs/solana-ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ declare_id!("EnfDJsAK7BGgetnmKzBx86CsgC5kfSPcsktFCQ4YLC81");

mod client_state;
mod consensus_state;
mod errors;
mod execution_context;
#[cfg(test)]
mod tests;
Expand All @@ -37,13 +38,11 @@ pub mod solana_ibc {

pub fn deliver(
ctx: Context<Deliver>,
messages: Vec<ibc::core::MsgEnvelope>,
message: ibc::core::MsgEnvelope,
) -> Result<()> {
msg!("Called deliver method");
msg!("Called deliver method: {message}");
let _sender = ctx.accounts.sender.to_account_info();

msg!("These are messages {:?}", messages);

let private: &mut PrivateStorage = &mut ctx.accounts.storage;
msg!("This is private_store {:?}", private);

Expand All @@ -56,11 +55,9 @@ pub mod solana_ibc {
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))
.filter_map(core::result::Result::err)
.collect::<Vec<_>>();
if let Err(e) = ibc::core::dispatch(&mut store, &mut router, message) {
return err!(Error::RouterError(&e));
}

// Drop refcount on store so we can unwrap the Rc object below.
core::mem::drop(router);
Expand All @@ -70,7 +67,6 @@ pub mod solana_ibc {
// so using the inner function instead.
let inner = Rc::try_unwrap(store.0).unwrap().into_inner();

msg!("These are errors {:?}", errors);
msg!("This is final structure {:?}", inner.private);

// msg!("this is length {}", TrieKey::ClientState{ client_id: String::from("hello")}.into());
Expand All @@ -91,6 +87,31 @@ pub struct Deliver<'info> {
pub system_program: Program<'info, System>,
}

/// Error returned when handling a request.
#[derive(Clone, strum::AsRefStr, strum::EnumDiscriminants)]
pub enum Error<'a> {
RouterError(&'a ibc::core::RouterError),
}

impl Error<'_> {
pub fn name(&self) -> String { self.as_ref().into() }
}

impl core::fmt::Display for Error<'_> {
fn fmt(&self, fmtr: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::RouterError(err) => write!(fmtr, "{err}"),
}
}
}

impl From<Error<'_>> for u32 {
fn from(err: Error<'_>) -> u32 {
let code = ErrorDiscriminants::from(err) as u32;
anchor_lang::error::ERROR_CODE_OFFSET + code
}
}

#[event]
pub struct EmitIBCEvent {
pub ibc_event: Vec<u8>,
Expand Down
12 changes: 6 additions & 6 deletions solana/solana-ibc/programs/solana-ibc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +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 messages = vec![make_message!(
let message = 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 @@ -100,7 +100,7 @@ fn anchor_test_deliver() -> Result<()> {
trie,
system_program: system_program::ID,
})
.args(instruction::Deliver { messages })
.args(instruction::Deliver { message })
.payer(authority.clone())
.signer(&*authority)
.send_with_spinner_and_config(RpcSendTransactionConfig {
Expand All @@ -122,7 +122,7 @@ fn anchor_test_deliver() -> Result<()> {
let commitment_prefix: CommitmentPrefix =
IBC_TRIE_PREFIX.to_vec().try_into().unwrap();

let messages = vec![make_message!(
let message = make_message!(
MsgConnectionOpenInit {
client_id_on_a: ClientId::new(mock_client_state.client_type(), 0)
.unwrap(),
Expand All @@ -137,7 +137,7 @@ fn anchor_test_deliver() -> Result<()> {
},
ibc::core::ics03_connection::msgs::ConnectionMsg::OpenInit,
ibc::core::MsgEnvelope::Connection,
)];
);

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

0 comments on commit e7f9373

Please sign in to comment.