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

solana-ibc: accept just one message in deliver #67

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -37,13 +37,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 +54,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 +66,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 +86,32 @@ pub struct Deliver<'info> {
pub system_program: Program<'info, System>,
}

/// Error returned when handling a request.
#[derive(Clone, strum::AsRefStr, strum::EnumDiscriminants)]
#[strum_discriminants(repr(u32))]
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
Loading