From 5c821b49ac85e9e110456b3c674610d429095aa1 Mon Sep 17 00:00:00 2001 From: yito88 Date: Mon, 20 May 2024 23:59:56 +0200 Subject: [PATCH 1/5] tx_ibc wasm again --- crates/ibc/src/actions.rs | 97 ++----------------- crates/ibc/src/context/storage.rs | 9 +- crates/ibc/src/lib.rs | 14 ++- .../src/ledger/native_vp/ibc/context.rs | 23 ++--- crates/namada/src/vm/host_env.rs | 69 ------------- crates/namada/src/vm/wasm/host_env.rs | 1 - crates/tests/src/vm_host_env/mod.rs | 71 +++++--------- crates/tx_prelude/src/ibc.rs | 18 ++-- crates/tx_prelude/src/lib.rs | 13 --- crates/vm_env/src/lib.rs | 4 - wasm/tx_ibc/src/lib.rs | 10 +- 11 files changed, 64 insertions(+), 265 deletions(-) diff --git a/crates/ibc/src/actions.rs b/crates/ibc/src/actions.rs index 9460e2ae6b..24a13a35bf 100644 --- a/crates/ibc/src/actions.rs +++ b/crates/ibc/src/actions.rs @@ -17,9 +17,8 @@ use namada_events::{EmitEvents, EventTypeBuilder}; use namada_governance::storage::proposal::PGFIbcTarget; use namada_parameters::read_epoch_duration_parameter; use namada_state::{ - DBIter, Epochs, OptionExt, ResultExt, State, StateRead, StorageError, - StorageHasher, StorageRead, StorageResult, StorageWrite, TxHostEnvState, - WlState, DB, + DBIter, Epochs, ResultExt, State, StorageError, StorageHasher, StorageRead, + StorageResult, StorageWrite, WlState, DB, }; use namada_token as token; use token::DenominatedAmount; @@ -115,83 +114,6 @@ where } } -impl IbcStorageContext for TxHostEnvState<'_, D, H> -where - D: 'static + DB + for<'iter> DBIter<'iter>, - H: 'static + StorageHasher, -{ - fn emit_ibc_event(&mut self, event: IbcEvent) -> Result<(), StorageError> { - let gas = self - .write_log_mut() - .emit_event(event) - .ok_or_err_msg("Gas overflow")?; - self.charge_gas(gas).into_storage_result()?; - Ok(()) - } - - fn get_ibc_events( - &self, - event_type: impl AsRef, - ) -> Result, StorageError> { - let event_type = EventTypeBuilder::new_of::() - .with_segment(event_type) - .build(); - - Ok(self - .write_log() - .lookup_events_with_prefix(&event_type) - .filter_map(|event| IbcEvent::try_from(event).ok()) - .collect()) - } - - fn transfer_token( - &mut self, - src: &Address, - dest: &Address, - token: &Address, - amount: Amount, - ) -> Result<(), StorageError> { - token::transfer(self, token, src, dest, amount) - } - - fn handle_masp_tx( - &mut self, - shielded: &masp_primitives::transaction::Transaction, - ) -> Result<(), StorageError> { - namada_token::utils::handle_masp_tx(self, shielded)?; - namada_token::utils::update_note_commitment_tree(self, shielded) - } - - fn mint_token( - &mut self, - target: &Address, - token: &Address, - amount: Amount, - ) -> Result<(), StorageError> { - ibc_storage::mint_tokens(self, target, token, amount) - } - - fn burn_token( - &mut self, - target: &Address, - token: &Address, - amount: Amount, - ) -> Result<(), StorageError> { - ibc_storage::burn_tokens(self, target, token, amount) - } - - fn log_string(&self, message: String) { - tracing::trace!(message); - } -} - -impl IbcCommonContext for TxHostEnvState<'_, D, H> -where - D: 'static + DB + for<'iter> DBIter<'iter>, - H: 'static + StorageHasher, -{ -} - impl IbcStorageContext for IbcProtocolContext<'_, S> where S: State + EmitEvents, @@ -230,14 +152,6 @@ where token::transfer(self.state, token, src, dest, amount) } - /// Handle masp tx - fn handle_masp_tx( - &mut self, - _shielded: &masp_primitives::transaction::Transaction, - ) -> Result<(), StorageError> { - unimplemented!("No MASP transfer in an IBC protocol transaction") - } - /// Mint token fn mint_token( &mut self, @@ -258,6 +172,13 @@ where ibc_storage::burn_tokens(self.state, target, token, amount) } + fn insert_verifier( + &mut self, + _verifier: &Address, + ) -> Result<(), StorageError> { + Ok(()) + } + fn log_string(&self, message: String) { tracing::trace!(message); } diff --git a/crates/ibc/src/context/storage.rs b/crates/ibc/src/context/storage.rs index 7aae70b5d1..4db283e567 100644 --- a/crates/ibc/src/context/storage.rs +++ b/crates/ibc/src/context/storage.rs @@ -27,12 +27,6 @@ pub trait IbcStorageContext: StorageRead + StorageWrite { amount: Amount, ) -> Result<(), Error>; - /// Handle masp tx - fn handle_masp_tx( - &mut self, - shielded: &masp_primitives::transaction::Transaction, - ) -> Result<(), Error>; - /// Mint token fn mint_token( &mut self, @@ -49,6 +43,9 @@ pub trait IbcStorageContext: StorageRead + StorageWrite { amount: Amount, ) -> Result<(), Error>; + /// Insert the verifier + fn insert_verifier(&mut self, verifier: &Address) -> Result<(), Error>; + /// Logging fn log_string(&self, message: String); } diff --git a/crates/ibc/src/lib.rs b/crates/ibc/src/lib.rs index 797a158297..fc79cfd566 100644 --- a/crates/ibc/src/lib.rs +++ b/crates/ibc/src/lib.rs @@ -97,8 +97,8 @@ pub enum Error { Trace(String), #[error("Invalid chain ID: {0}")] ChainId(IdentifierError), - #[error("Handling MASP transaction error: {0}")] - MaspTx(String), + #[error("Verifier insertion error: {0}")] + Verifier(namada_storage::Error), } /// IBC actions to handle IBC operations @@ -150,6 +150,7 @@ where self.ctx.inner.clone(), self.verifiers.clone(), ); + self.insert_verifiers()?; send_transfer_execute( &mut self.ctx, &mut token_transfer_ctx, @@ -366,6 +367,7 @@ where self.ctx.inner.clone(), verifiers.clone(), ); + self.insert_verifiers()?; send_transfer_validate( &self.ctx, &token_transfer_ctx, @@ -407,6 +409,14 @@ where } } } + + fn insert_verifiers(&self) -> Result<(), Error> { + let mut ctx = self.ctx.inner.borrow_mut(); + for verifier in self.verifiers.borrow().iter() { + ctx.insert_verifier(verifier).map_err(Error::Verifier)?; + } + Ok(()) + } } fn is_ack_successful(ack: &Acknowledgement) -> Result { diff --git a/crates/namada/src/ledger/native_vp/ibc/context.rs b/crates/namada/src/ledger/native_vp/ibc/context.rs index b7c4072c26..ab6832c92e 100644 --- a/crates/namada/src/ledger/native_vp/ibc/context.rs +++ b/crates/namada/src/ledger/native_vp/ibc/context.rs @@ -227,14 +227,6 @@ where transfer(self, token, src, dest, amount) } - fn handle_masp_tx( - &mut self, - shielded: &masp_primitives::transaction::Transaction, - ) -> Result<()> { - crate::token::utils::handle_masp_tx(self, shielded)?; - crate::token::utils::update_note_commitment_tree(self, shielded) - } - fn mint_token( &mut self, target: &Address, @@ -259,6 +251,10 @@ where burn_tokens(self, token, target, amount) } + fn insert_verifier(&mut self, _verifier: &Address) -> Result<()> { + Ok(()) + } + fn log_string(&self, message: String) { tracing::debug!("{message} in the pseudo execution for IBC VP"); } @@ -397,13 +393,6 @@ where unimplemented!("Validation doesn't transfer") } - fn handle_masp_tx( - &mut self, - _shielded: &masp_primitives::transaction::Transaction, - ) -> Result<()> { - unimplemented!("Validation doesn't handle a masp tx") - } - fn mint_token( &mut self, _target: &Address, @@ -422,6 +411,10 @@ where unimplemented!("Validation doesn't burn") } + fn insert_verifier(&mut self, _verifier: &Address) -> Result<()> { + Ok(()) + } + /// Logging fn log_string(&self, message: String) { tracing::debug!("{message} for validation in IBC VP"); diff --git a/crates/namada/src/vm/host_env.rs b/crates/namada/src/vm/host_env.rs index 1c3bf70a87..ed09242f16 100644 --- a/crates/namada/src/vm/host_env.rs +++ b/crates/namada/src/vm/host_env.rs @@ -7,7 +7,6 @@ use std::num::TryFromIntError; use borsh::BorshDeserialize; use borsh_ext::BorshSerializeExt; -use gas::IBC_TX_GAS; use masp_primitives::transaction::Transaction; use namada_core::address::ESTABLISHED_ADDRESS_BYTES_LEN; use namada_core::arith::{self, checked}; @@ -79,10 +78,6 @@ pub enum TxRuntimeError { NumConversionError(#[from] TryFromIntError), #[error("Memory error: {0}")] MemoryError(Box), - #[error("Missing tx data")] - MissingTxData, - #[error("IBC: {0}")] - Ibc(#[from] namada_ibc::Error), #[error("No value found in result buffer")] NoValueInResultBuffer, #[error("VP code is not allowed in allowlist parameter.")] @@ -2063,70 +2058,6 @@ where Ok(()) } -/// Execute IBC tx. -// Temporarily the IBC tx execution is implemented via a host function to -// workaround wasm issue. -pub fn tx_ibc_execute( - env: &TxVmEnv, -) -> TxResult -where - MEM: VmMemory, - D: 'static + DB + for<'iter> DBIter<'iter>, - H: 'static + StorageHasher, - CA: WasmCacheAccess, -{ - use std::rc::Rc; - - use namada_ibc::{IbcActions, NftTransferModule, TransferModule}; - - tx_charge_gas::(env, IBC_TX_GAS)?; - - let tx = unsafe { env.ctx.tx.get() }; - let cmt = unsafe { env.ctx.cmt.get() }; - let tx_data = tx.data(cmt).ok_or_else(|| { - let sentinel = unsafe { env.ctx.sentinel.get() }; - sentinel.borrow_mut().set_invalid_commitment(); - TxRuntimeError::MissingTxData - })?; - let state = Rc::new(RefCell::new(env.state())); - // Verifier set populated in tx execution - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - // Scoped to drop `verifiers.clone`s after `actions.execute` - let transfer = { - let mut actions = IbcActions::new(state.clone(), verifiers.clone()); - let module = TransferModule::new(state.clone(), verifiers.clone()); - actions.add_transfer_module(module); - let module = NftTransferModule::new(state); - actions.add_transfer_module(module); - actions.execute(&tx_data)? - }; - // NB: There must be no other strong references to this Rc - let verifiers = Rc::into_inner(verifiers) - .expect("There must be only one strong ref to verifiers set") - .into_inner(); - - // Insert all the verifiers from the tx into the verifier set in env - let verifiers_in_env = unsafe { env.ctx.verifiers.get_mut() }; - for addr in verifiers.into_iter() { - tx_charge_gas::( - env, - (ESTABLISHED_ADDRESS_BYTES_LEN as u64) - .checked_mul(MEMORY_ACCESS_GAS_PER_BYTE) - .expect("Consts mul that cannot overflow"), - )?; - verifiers_in_env.insert(addr); - } - - let value = transfer.serialize_to_vec(); - let len: i64 = value - .len() - .try_into() - .map_err(TxRuntimeError::NumConversionError)?; - let result_buffer = unsafe { env.ctx.result_buffer.get_mut() }; - result_buffer.replace(value); - Ok(len) -} - /// Validate a VP WASM code hash in a tx environment. fn tx_validate_vp_code_hash( env: &TxVmEnv, diff --git a/crates/namada/src/vm/wasm/host_env.rs b/crates/namada/src/vm/wasm/host_env.rs index 6b1fb84f94..1dc86a9a09 100644 --- a/crates/namada/src/vm/wasm/host_env.rs +++ b/crates/namada/src/vm/wasm/host_env.rs @@ -80,7 +80,6 @@ where "namada_tx_get_pred_epochs" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_get_pred_epochs), "namada_tx_get_native_token" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_get_native_token), "namada_tx_log_string" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_log_string), - "namada_tx_ibc_execute" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_ibc_execute), "namada_tx_set_commitment_sentinel" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_set_commitment_sentinel), "namada_tx_verify_tx_section_signature" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_verify_tx_section_signature), "namada_tx_update_masp_note_commitment_tree" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_update_masp_note_commitment_tree), diff --git a/crates/tests/src/vm_host_env/mod.rs b/crates/tests/src/vm_host_env/mod.rs index b7b52dcd3c..da322d3fa0 100644 --- a/crates/tests/src/vm_host_env/mod.rs +++ b/crates/tests/src/vm_host_env/mod.rs @@ -18,10 +18,8 @@ pub mod vp; #[cfg(test)] mod tests { - use std::cell::RefCell; use std::collections::BTreeSet; use std::panic; - use std::rc::Rc; use borsh_ext::BorshSerializeExt; use itertools::Itertools; @@ -703,8 +701,7 @@ mod tests { .sign_wrapper(keypair.clone()); // create a client with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("creating a client failed"); @@ -735,8 +732,7 @@ mod tests { .sign_raw(keypairs, pks_map, None) .sign_wrapper(keypair); // update the client with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("updating a client failed"); @@ -777,8 +773,7 @@ mod tests { .sign_raw(keypairs.clone(), pks_map.clone(), None) .sign_wrapper(keypair.clone()); // init a connection with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("creating a connection failed"); @@ -809,8 +804,7 @@ mod tests { .sign_raw(keypairs, pks_map, None) .sign_wrapper(keypair); // open the connection with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("opening the connection failed"); @@ -852,8 +846,7 @@ mod tests { .sign_raw(keypairs.clone(), pks_map.clone(), None) .sign_wrapper(keypair.clone()); // open try a connection with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("creating a connection failed"); @@ -884,8 +877,7 @@ mod tests { .sign_raw(keypairs, pks_map, None) .sign_wrapper(keypair); // open the connection with the mssage - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("opening the connection failed"); @@ -929,8 +921,7 @@ mod tests { .sign_raw(keypairs.clone(), pks_map.clone(), None) .sign_wrapper(keypair.clone()); // init a channel with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("creating a channel failed"); @@ -961,8 +952,7 @@ mod tests { .sign_raw(keypairs, pks_map, None) .sign_wrapper(keypair); // open the channel with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("opening the channel failed"); @@ -1006,8 +996,7 @@ mod tests { .sign_raw(keypairs.clone(), pks_map.clone(), None) .sign_wrapper(keypair.clone()); // try open a channel with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("creating a channel failed"); @@ -1039,8 +1028,7 @@ mod tests { .sign_raw(keypairs, pks_map, None) .sign_wrapper(keypair); // open a channel with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("opening the channel failed"); @@ -1087,8 +1075,7 @@ mod tests { .sign_raw(keypairs, pks_map, None) .sign_wrapper(keypair); // close the channel with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - let mut actions = tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers); + let mut actions = tx_host_env::ibc::ibc_actions(tx::ctx()); // the dummy module closes the channel let dummy_module = DummyTransferModule {}; actions.add_transfer_module(dummy_module); @@ -1146,8 +1133,7 @@ mod tests { .sign_wrapper(keypair); // close the channel with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("closing the channel failed"); @@ -1194,8 +1180,7 @@ mod tests { .sign_raw(keypairs.clone(), pks_map.clone(), None) .sign_wrapper(keypair.clone()); // send the token and a packet with the data - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("sending a token failed"); @@ -1243,8 +1228,7 @@ mod tests { .sign_raw(keypairs, pks_map, None) .sign_wrapper(keypair); // ack the packet with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("ack failed"); @@ -1329,8 +1313,7 @@ mod tests { .sign_raw(keypairs, pks_map, None) .sign_wrapper(keypair); // send the token and a packet with the data - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("sending a token failed"); @@ -1412,8 +1395,7 @@ mod tests { .sign_raw(keypairs, pks_map, None) .sign_wrapper(keypair); // receive a packet with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("receiving the token failed"); @@ -1504,8 +1486,7 @@ mod tests { .sign_raw(keypairs, pks_map, None) .sign_wrapper(keypair); // Receive the packet, but no token is received - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("receiving the token failed"); @@ -1598,8 +1579,7 @@ mod tests { .sign_raw(keypairs, pks_map, None) .sign_wrapper(keypair); // receive a packet with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("receiving a token failed"); @@ -1701,8 +1681,7 @@ mod tests { .sign_raw(keypairs, pks_map, None) .sign_wrapper(keypair); // receive a packet with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("receiving a token failed"); @@ -1764,8 +1743,7 @@ mod tests { ibc::set_timeout_timestamp(&mut msg.message); let tx_data = msg.serialize_to_vec(); // send a packet with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("sending a token failed"); @@ -1797,8 +1775,7 @@ mod tests { .sign_wrapper(keypair); // timeout the packet - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("timeout failed"); @@ -1850,8 +1827,7 @@ mod tests { ibc::msg_transfer(port_id, channel_id, token.to_string(), &sender); let tx_data = msg.serialize_to_vec(); // send a packet with the message - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("sending a token failed"); @@ -1883,8 +1859,7 @@ mod tests { .sign_wrapper(keypair); // timeout the packet - let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - tx_host_env::ibc::ibc_actions(tx::ctx(), verifiers) + tx_host_env::ibc::ibc_actions(tx::ctx()) .execute(&tx_data) .expect("timeout on close failed"); diff --git a/crates/tx_prelude/src/ibc.rs b/crates/tx_prelude/src/ibc.rs index fd01f928f1..a52f10038e 100644 --- a/crates/tx_prelude/src/ibc.rs +++ b/crates/tx_prelude/src/ibc.rs @@ -23,11 +23,9 @@ use crate::{Ctx, Error}; /// IBC actions to handle an IBC message. The `verifiers` inserted into the set /// must be inserted into the tx context with `Ctx::insert_verifier` after tx /// execution. -pub fn ibc_actions( - ctx: &mut Ctx, - verifiers: Rc>>, -) -> IbcActions<'_, Ctx> { +pub fn ibc_actions(ctx: &mut Ctx) -> IbcActions<'_, Ctx> { let ctx = Rc::new(RefCell::new(ctx.clone())); + let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); let mut actions = IbcActions::new(ctx.clone(), verifiers.clone()); let module = TransferModule::new(ctx.clone(), verifiers); actions.add_transfer_module(module); @@ -68,14 +66,6 @@ impl IbcStorageContext for Ctx { transfer(self, src, dest, token, amount) } - fn handle_masp_tx( - &mut self, - shielded: &masp_primitives::transaction::Transaction, - ) -> Result<(), Error> { - namada_token::utils::handle_masp_tx(self, shielded)?; - namada_token::utils::update_note_commitment_tree(self, shielded) - } - fn mint_token( &mut self, target: &Address, @@ -94,6 +84,10 @@ impl IbcStorageContext for Ctx { burn_tokens(self, target, token, amount) } + fn insert_verifier(&mut self, addr: &Address) -> Result<(), Error> { + TxEnv::insert_verifier(self, addr) + } + fn log_string(&self, message: String) { super::log_string(message); } diff --git a/crates/tx_prelude/src/lib.rs b/crates/tx_prelude/src/lib.rs index a92b326c86..eb0f6a3be9 100644 --- a/crates/tx_prelude/src/lib.rs +++ b/crates/tx_prelude/src/lib.rs @@ -431,19 +431,6 @@ impl namada_tx::action::Write for Ctx { } } -/// Execute IBC tx. -// Temp. workaround for -pub fn tx_ibc_execute() -> Result, Error> { - let result = unsafe { namada_tx_ibc_execute() }; - match read_from_buffer(result, namada_tx_result_buffer) { - Some(value) => { - Ok(Option::::try_from_slice(&value[..]) - .expect("The conversion shouldn't fail")) - } - None => Ok(None), - } -} - /// Verify section signatures against the given list of keys pub fn verify_signatures_of_pks( ctx: &Ctx, diff --git a/crates/vm_env/src/lib.rs b/crates/vm_env/src/lib.rs index 2c0a5bc130..1888715d93 100644 --- a/crates/vm_env/src/lib.rs +++ b/crates/vm_env/src/lib.rs @@ -134,10 +134,6 @@ pub mod tx { /// Charge the provided amount of gas for the current tx pub fn namada_tx_charge_gas(used_gas: u64); - /// Execute IBC tx. - // Temp. workaround for - pub fn namada_tx_ibc_execute() -> i64; - /// Set the sentinel for a wrong tx section commitment pub fn namada_tx_set_commitment_sentinel(); diff --git a/wasm/tx_ibc/src/lib.rs b/wasm/tx_ibc/src/lib.rs index 62d304d4ee..96f07dbe69 100644 --- a/wasm/tx_ibc/src/lib.rs +++ b/wasm/tx_ibc/src/lib.rs @@ -7,13 +7,9 @@ use namada_tx_prelude::*; #[transaction] fn apply_tx(ctx: &mut Ctx, tx_data: BatchedTx) -> TxResult { - // let data = ctx.get_tx_data(&tx_data)?; - - // let transfer = - // ibc::ibc_actions(ctx).execute(&data).into_storage_result()?; - - // Temp. workaround for - let transfer = tx_ibc_execute()?; + let data = ctx.get_tx_data(&tx_data)?; + let transfer = + ibc::ibc_actions(ctx).execute(&data).into_storage_result()?; if let Some(transfer) = transfer { let shielded = transfer From 1cc0be08286cba2dcf573a37a8ab7a17df75b618 Mon Sep 17 00:00:00 2001 From: yito88 Date: Wed, 22 May 2024 22:05:04 +0200 Subject: [PATCH 2/5] enable floating-point support --- crates/namada/src/vm/mod.rs | 2 +- crates/tests/src/e2e/ibc_tests.rs | 22 ---------------------- 2 files changed, 1 insertion(+), 23 deletions(-) diff --git a/crates/namada/src/vm/mod.rs b/crates/namada/src/vm/mod.rs index a7d5cccda5..9754e3c0e0 100644 --- a/crates/namada/src/vm/mod.rs +++ b/crates/namada/src/vm/mod.rs @@ -24,7 +24,7 @@ const UNTRUSTED_WASM_FEATURES: WasmFeatures = WasmFeatures { relaxed_simd: false, threads: false, tail_call: false, - floats: false, + floats: true, multi_memory: false, exceptions: false, memory64: false, diff --git a/crates/tests/src/e2e/ibc_tests.rs b/crates/tests/src/e2e/ibc_tests.rs index 0533374315..02236e6f0b 100644 --- a/crates/tests/src/e2e/ibc_tests.rs +++ b/crates/tests/src/e2e/ibc_tests.rs @@ -260,28 +260,6 @@ fn run_ledger_ibc_with_hermes() -> Result<()> { wait_for_packet_relay(&port_id_a, &channel_id_a, &test_a)?; check_balances_after_back(&port_id_b, &channel_id_b, &test_a, &test_b)?; - // Transfer a token and it will time out and refund - std::env::set_var(ENV_VAR_CHAIN_ID, test_b.net.chain_id.to_string()); - let receiver = find_address(&test_b, BERTHA)?; - // Send a token from Chain A - transfer( - &test_a, - ALBERT, - receiver.to_string(), - NAM, - 100000.0, - ALBERT_KEY, - &port_id_a, - &channel_id_a, - Some(Duration::new(0, 0)), - None, - false, - )?; - // wait for the timeout and the refund - wait_for_packet_relay(&port_id_a, &channel_id_a, &test_a)?; - // The balance should not be changed - check_balances_after_back(&port_id_b, &channel_id_b, &test_a, &test_b)?; - // Send a token to the shielded address on Chain A transfer_on_chain( &test_a, From 4da1205012df4db626909eb376ab3596b6858635 Mon Sep 17 00:00:00 2001 From: yito88 Date: Wed, 22 May 2024 22:57:33 +0200 Subject: [PATCH 3/5] add changelog --- .changelog/unreleased/improvements/1831-tx-ibc-wasm.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/unreleased/improvements/1831-tx-ibc-wasm.md diff --git a/.changelog/unreleased/improvements/1831-tx-ibc-wasm.md b/.changelog/unreleased/improvements/1831-tx-ibc-wasm.md new file mode 100644 index 0000000000..d52f350728 --- /dev/null +++ b/.changelog/unreleased/improvements/1831-tx-ibc-wasm.md @@ -0,0 +1,3 @@ +- Revert IBC transaction wasm not to use host_env function. + But it required to enable floating-point support again + ([\#1831](https://github.com/anoma/namada/issues/1831)) \ No newline at end of file From 27726cb139418e9bee106981020f609028119165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Thu, 23 May 2024 18:33:43 +0200 Subject: [PATCH 4/5] remove allowed_txs bench --- crates/benches/Cargo.toml | 5 -- crates/benches/README.md | 2 +- crates/benches/txs.rs | 129 -------------------------------------- 3 files changed, 1 insertion(+), 135 deletions(-) delete mode 100644 crates/benches/txs.rs diff --git a/crates/benches/Cargo.toml b/crates/benches/Cargo.toml index 965e67b502..ca308f9100 100644 --- a/crates/benches/Cargo.toml +++ b/crates/benches/Cargo.toml @@ -12,11 +12,6 @@ readme.workspace = true repository.workspace = true version.workspace = true -[[bench]] -name = "allowed_txs" -harness = false -path = "txs.rs" - [[bench]] name = "native_vps" harness = false diff --git a/crates/benches/README.md b/crates/benches/README.md index 62980150ac..2bdf837424 100644 --- a/crates/benches/README.md +++ b/crates/benches/README.md @@ -25,5 +25,5 @@ cargo test --bench native_vps To benchmark a selected bench with a minimum sample size use e.g.: ```shell -cargo bench --bench allowed_txs -- --sample-size 10 +cargo bench --bench native_vps -- --sample-size 10 ``` diff --git a/crates/benches/txs.rs b/crates/benches/txs.rs deleted file mode 100644 index 58ade0d01b..0000000000 --- a/crates/benches/txs.rs +++ /dev/null @@ -1,129 +0,0 @@ -use std::str::FromStr; - -use criterion::{criterion_group, criterion_main, Criterion}; -use namada::core::address::Address; -use namada::core::masp::{TransferSource, TransferTarget}; -use namada::ibc::core::channel::types::channel::Order; -use namada::ibc::core::channel::types::msgs::MsgChannelOpenInit; -use namada::ibc::core::channel::types::Version as ChannelVersion; -use namada::ibc::core::commitment_types::commitment::CommitmentPrefix; -use namada::ibc::core::connection::types::msgs::MsgConnectionOpenInit; -use namada::ibc::core::connection::types::version::Version; -use namada::ibc::core::connection::types::Counterparty; -use namada::ibc::core::host::types::identifiers::{ - ClientId, ConnectionId, PortId, -}; -use namada::ibc::primitives::ToProto; -use namada::proof_of_stake::KeySeg; -use namada::token::Amount; -use namada_apps_lib::wallet::defaults; -use namada_node::bench_utils::{ - BenchShieldedCtx, ALBERT_PAYMENT_ADDRESS, ALBERT_SPENDING_KEY, TX_IBC_WASM, -}; - -fn ibc(c: &mut Criterion) { - let mut group = c.benchmark_group("tx_ibc"); - - // NOTE: Ibc encompass a variety of different messages that can be executed, - // here we only benchmark a few of those - for bench_name in [ - "open_connection", - "open_channel", - "outgoing_transfer", - "outgoing_shielded_action", - ] { - group.bench_function(bench_name, |b| { - b.iter_batched_ref( - || { - let mut shielded_ctx = BenchShieldedCtx::default(); - // Initialize the state according to the target tx - let (shielded_ctx, signed_tx) = match bench_name { - "open_connection" => { - let _ = shielded_ctx.shell.init_ibc_client_state( - namada::core::storage::Key::from( - Address::Internal(namada::core::address::InternalAddress::Ibc).to_db_key(), - ), - ); - // Connection handshake - let msg = MsgConnectionOpenInit { - client_id_on_a: ClientId::new("07-tendermint", 1).unwrap(), - counterparty: Counterparty::new( - ClientId::from_str("07-tendermint-1").unwrap(), - None, - CommitmentPrefix::try_from(b"ibc".to_vec()).unwrap(), - ), - version: Some(Version::compatibles().first().unwrap().clone()), - delay_period: std::time::Duration::new(100, 0), - signer: defaults::albert_address().to_string().into(), - }; - let mut data = vec![]; - prost::Message::encode(&msg.to_any(), &mut data).unwrap(); - let open_connection = shielded_ctx.shell.generate_ibc_tx(TX_IBC_WASM, data); - (shielded_ctx, open_connection) - } - "open_channel" => { - let _ = shielded_ctx.shell.init_ibc_connection(); - // Channel handshake - let msg = MsgChannelOpenInit { - port_id_on_a: PortId::transfer(), - connection_hops_on_a: vec![ConnectionId::new(1)], - port_id_on_b: PortId::transfer(), - ordering: Order::Unordered, - signer: defaults::albert_address().to_string().into(), - version_proposal: ChannelVersion::new("ics20-1".to_string()), - }; - - // Avoid serializing the data again with borsh - let mut data = vec![]; - prost::Message::encode(&msg.to_any(), &mut data).unwrap(); - let open_channel = shielded_ctx.shell.generate_ibc_tx(TX_IBC_WASM, data); - (shielded_ctx, open_channel) - } - "outgoing_transfer" => { - shielded_ctx.shell.init_ibc_channel(); - let outgoing_transfer = shielded_ctx.shell.generate_ibc_transfer_tx(); - (shielded_ctx, outgoing_transfer) - } - "outgoing_shielded_action" => { - shielded_ctx.shell.init_ibc_channel(); - let albert_payment_addr = shielded_ctx - .wallet - .find_payment_addr(ALBERT_PAYMENT_ADDRESS) - .unwrap() - .to_owned(); - let albert_spending_key = shielded_ctx - .wallet - .find_spending_key(ALBERT_SPENDING_KEY, None) - .unwrap() - .to_owned(); - // Shield some tokens for Albert - let (mut shielded_ctx, shield_tx) = shielded_ctx.generate_masp_tx( - Amount::native_whole(500), - TransferSource::Address(defaults::albert_address()), - TransferTarget::PaymentAddress(albert_payment_addr), - ); - shielded_ctx.shell.execute_tx(&shield_tx.to_ref()); - shielded_ctx.shell.commit_masp_tx(shield_tx.tx); - shielded_ctx.shell.commit_block(); - - shielded_ctx.generate_shielded_action( - Amount::native_whole(10), - TransferSource::ExtendedSpendingKey(albert_spending_key), - TransferTarget::Address(defaults::bertha_address()), - ) - } - _ => panic!("Unexpected bench test"), - }; - (shielded_ctx, signed_tx) - }, - |(shielded_ctx, signed_tx)| shielded_ctx.shell.execute_tx(&signed_tx.to_ref()), - criterion::BatchSize::SmallInput, - ) - }); - } - - group.finish(); -} - -criterion_group!(allowed_txs, ibc); -criterion_main!(allowed_txs); From e1c28123cf5c4cb78c45e5a6a20038fc47a2f03f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Thu, 23 May 2024 18:33:49 +0200 Subject: [PATCH 5/5] gas: remove unused ibc tx gas --- crates/gas/src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/gas/src/lib.rs b/crates/gas/src/lib.rs index 4816a49c25..c015542008 100644 --- a/crates/gas/src/lib.rs +++ b/crates/gas/src/lib.rs @@ -84,9 +84,6 @@ pub const WASM_MEMORY_PAGE_GAS: u32 = pub const IBC_ACTION_VALIDATE_GAS: u64 = 1_472_023; /// The cost to execute an Ibc action pub const IBC_ACTION_EXECUTE_GAS: u64 = 3_678_745; -/// The cost to execute an ibc transaction TODO: remove once ibc tx goes back to -/// wasm -pub const IBC_TX_GAS: u64 = 111_825_500; /// The cost to verify a masp spend note pub const MASP_VERIFY_SPEND_GAS: u64 = 66_822_000; /// The cost to verify a masp convert note