diff --git a/Cargo.lock b/Cargo.lock index 7f00acb4f09..b624a6fb021 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4146,6 +4146,7 @@ dependencies = [ "bit-set", "blake2b-rs", "borsh 1.0.0-alpha.4", + "borsh-ext", "byte-unit", "byteorder", "bytes", @@ -4380,6 +4381,7 @@ dependencies = [ "assert_cmd", "async-trait", "borsh 1.0.0-alpha.4", + "borsh-ext", "chrono", "clap 4.3.5", "color-eyre", diff --git a/apps/Cargo.toml b/apps/Cargo.toml index 16fe028be33..c64f34321ca 100644 --- a/apps/Cargo.toml +++ b/apps/Cargo.toml @@ -77,6 +77,7 @@ bech32.workspace = true bimap.workspace = true blake2b-rs.workspace = true borsh.workspace = true +borsh-ext.workspace = true byte-unit.workspace = true byteorder.workspace = true clap.workspace = true diff --git a/apps/src/lib/client/tx.rs b/apps/src/lib/client/tx.rs index c3a46d3fd7f..12964d6d8ea 100644 --- a/apps/src/lib/client/tx.rs +++ b/apps/src/lib/client/tx.rs @@ -534,7 +534,7 @@ const TMP_FILE_NAME: &str = "shielded.tmp"; #[derive(Debug, BorshSerialize, BorshDeserialize, Clone)] pub struct CLIShieldedUtils { - #[borsh_skip] + #[borsh(skip)] context_dir: PathBuf, } diff --git a/encoding_spec/src/main.rs b/encoding_spec/src/main.rs index 5889b03b8dc..cc1082f38be 100644 --- a/encoding_spec/src/main.rs +++ b/encoding_spec/src/main.rs @@ -15,13 +15,17 @@ #![deny(rustdoc::broken_intra_doc_links)] #![deny(rustdoc::private_intra_doc_links)] -use std::collections::HashSet; +use std::collections::{BTreeMap, HashMap, HashSet}; use std::io::Write; +use std::iter::Extend; -use borsh::{schema, BorshSchema}; +use borsh::{schema, BorshSchema, schema_container_of}; +use borsh::schema::{BorshSchemaContainer, Declaration, Definition}; use itertools::Itertools; use lazy_static::lazy_static; use madato::types::TableRow; +use namada::eth_bridge::ethers::core::k256::elliptic_curve::weierstrass::add; +use namada::eth_bridge::ethers::utils::rlp::decode_list; use namada::ledger::parameters::Parameters; use namada::types::address::Address; use namada::types::key::ed25519::{PublicKey, Signature}; @@ -56,179 +60,190 @@ lazy_static! { ]); } +fn btree(b: &BorshSchemaContainer) -> BTreeMap { + let mut btree = BTreeMap::new(); + for (dec, def) in b.definitions() { + btree.insert(dec.clone(), def.clone()); + } + btree +} + fn main() -> Result<(), Box> { let mut file = std::fs::File::create(OUTPUT_PATH).unwrap(); write_generated_code_notice(&mut file)?; // Top-level definitions are displayed at the top - let address_schema = Address::schema_container(); - let token_amount_schema = token::Amount::schema_container(); - let epoch_schema = Epoch::schema_container(); - let parameters_schema = Parameters::schema_container(); + + let address_schema = schema_container_of::
(); + let token_amount_schema = schema_container_of::(); + let epoch_schema = schema_container_of::(); + let parameters_schema = schema_container_of::(); // TODO update after - let public_key_schema = PublicKey::schema_container(); + let public_key_schema = schema_container_of::(); // TODO update after - let signature_schema = Signature::schema_container(); + let signature_schema = schema_container_of::(); let init_account_schema = - transaction::account::InitAccount::schema_container(); + schema_container_of::(); let init_validator_schema = - transaction::pos::InitValidator::schema_container(); - let token_transfer_schema = token::Transfer::schema_container(); + schema_container_of::(); + let token_transfer_schema = schema_container_of::(); let update_account = - transaction::account::UpdateAccount::schema_container(); - let pos_bond_schema = pos::Bond::schema_container(); - let pos_withdraw_schema = pos::Withdraw::schema_container(); - let wrapper_tx_schema = transaction::WrapperTx::schema_container(); + schema_container_of::(); + let pos_bond_schema = schema_container_of::(); + let pos_withdraw_schema = schema_container_of::(); + let wrapper_tx_schema = schema_container_of::(); // TODO derive BorshSchema after - // let tx_result_schema = transaction::TxResult::schema_container(); - let tx_type_schema = transaction::TxType::schema_container(); - let prefix_value_schema = storage::PrefixValue::schema_container(); + // let tx_result_schema = schema_container_of::(); + let tx_type_schema = schema_container_of::(); + let prefix_value_schema = schema_container_of::(); // PoS // TODO add after // TODO imported from `use namada::ledger::pos::Bonds;` - // let pos_bonds_schema = Bonds::schema_container(); + // let pos_bonds_schema = schema_container_of::(); // Merge type definitions - let mut definitions = address_schema.definitions; + + let mut definitions= btree(&address_schema); + // TODO check for conflicts (same name, different declaration) - definitions.extend(token_amount_schema.definitions); - definitions.extend(epoch_schema.definitions); - definitions.extend(parameters_schema.definitions); - definitions.extend(public_key_schema.definitions); - definitions.extend(signature_schema.definitions); - definitions.extend(init_account_schema.definitions); - definitions.extend(init_validator_schema.definitions); - definitions.extend(token_transfer_schema.definitions); - definitions.extend(update_account.definitions); - definitions.extend(pos_bond_schema.definitions); - definitions.extend(pos_withdraw_schema.definitions); - definitions.extend(wrapper_tx_schema.definitions); - // definitions.extend(tx_result_schema.definitions); - definitions.extend(tx_type_schema.definitions); - definitions.extend(prefix_value_schema.definitions); - // definitions.extend(pos_bonds_schema.definitions); + definitions.extend(btree(&token_amount_schema)); + definitions.extend(btree(&epoch_schema)); + definitions.extend(btree(¶meters_schema)); + definitions.extend(btree(&public_key_schema)); + definitions.extend(btree(&signature_schema)); + definitions.extend(btree(&init_account_schema)); + definitions.extend(btree(&init_validator_schema)); + definitions.extend(btree(&token_transfer_schema)); + definitions.extend(btree(&update_account)); + definitions.extend(btree(&pos_bond_schema)); + definitions.extend(btree(&pos_withdraw_schema)); + definitions.extend(btree(&wrapper_tx_schema)); + // definitions.extend(btree(&tx_result_schema)); + definitions.extend(btree(&tx_type_schema)); + definitions.extend(btree(&prefix_value_schema)); + // definitions.extend(btree(&pos_bonds_schema)); let mut tables: Vec = Vec::with_capacity(definitions.len()); // Add the top-level definitions first let address_definition = - definitions.remove(&address_schema.declaration).unwrap(); + definitions.remove(address_schema.declaration()).unwrap(); let address_table = - definition_to_table(address_schema.declaration, address_definition).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/address/enum.Address.html"); + definition_to_table( address_schema.declaration(), address_definition).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/address/enum.Address.html"); tables.push(address_table); let token_amount_definition = definitions - .remove(&token_amount_schema.declaration) + .remove(token_amount_schema.declaration()) .unwrap(); let token_amount_table = definition_to_table( - token_amount_schema.declaration, + token_amount_schema.declaration(), token_amount_definition, ).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/token/struct.Amount.html"); tables.push(token_amount_table); let epoch_definition = - definitions.remove(&epoch_schema.declaration).unwrap(); + definitions.remove(epoch_schema.declaration()).unwrap(); let epoch_table = - definition_to_table(epoch_schema.declaration, epoch_definition).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/storage/struct.Epoch.html"); + definition_to_table(epoch_schema.declaration(), epoch_definition).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/storage/struct.Epoch.html"); tables.push(epoch_table); let parameters_definition = - definitions.remove(¶meters_schema.declaration).unwrap(); + definitions.remove(parameters_schema.declaration()).unwrap(); let parameters_table = - definition_to_table(parameters_schema.declaration, parameters_definition).with_rust_doc_link("file:///Users/tz/dev/namada/target/doc/namada/ledger/parameters/struct.Parameters.html"); + definition_to_table(parameters_schema.declaration(), parameters_definition).with_rust_doc_link("file:///Users/tz/dev/namada/target/doc/namada/ledger/parameters/struct.Parameters.html"); tables.push(parameters_table); let public_key_definition = - definitions.remove(&public_key_schema.declaration).unwrap(); + definitions.remove(public_key_schema.declaration()).unwrap(); let public_key_table = - definition_to_table(public_key_schema.declaration, public_key_definition).with_rust_doc_link( + definition_to_table(public_key_schema.declaration(), public_key_definition).with_rust_doc_link( // TODO update after "https://dev.namada.net/master/rustdoc/namada/types/key/ed25519/struct.PublicKey.html"); tables.push(public_key_table); let signature_definition = - definitions.remove(&signature_schema.declaration).unwrap(); + definitions.remove(signature_schema.declaration()).unwrap(); let signature_table = - definition_to_table(signature_schema.declaration, signature_definition).with_rust_doc_link( + definition_to_table(signature_schema.declaration(), signature_definition).with_rust_doc_link( // TODO update after "https://dev.namada.net/master/rustdoc/namada/types/key/ed25519/struct.Signature.html"); tables.push(signature_table); let init_account_definition = definitions - .remove(&init_account_schema.declaration) + .remove(init_account_schema.declaration()) .unwrap(); let init_account_table = definition_to_table( - init_account_schema.declaration, + init_account_schema.declaration(), init_account_definition, ).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/transaction/struct.InitAccount.html"); tables.push(init_account_table); let init_validator_definition = definitions - .remove(&init_validator_schema.declaration) + .remove(init_validator_schema.declaration()) .unwrap(); let init_validator_table = definition_to_table( - init_validator_schema.declaration, + init_validator_schema.declaration(), init_validator_definition, ).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/transaction/struct.InitValidator.html"); tables.push(init_validator_table); let token_transfer_definition = definitions - .remove(&token_transfer_schema.declaration) + .remove(token_transfer_schema.declaration()) .unwrap(); let token_transfer_table = definition_to_table( - token_transfer_schema.declaration, + token_transfer_schema.declaration(), token_transfer_definition, ).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/token/struct.Transfer.html"); tables.push(token_transfer_table); let update_account_definition = - definitions.remove(&update_account.declaration).unwrap(); + definitions.remove(update_account.declaration()).unwrap(); let update_accoun_table = - definition_to_table(update_account.declaration, update_account_definition).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/transaction/struct.UpdateVp.html"); + definition_to_table(update_account.declaration(), update_account_definition).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/transaction/struct.UpdateVp.html"); tables.push(update_accoun_table); let pos_bond_definition = - definitions.remove(&pos_bond_schema.declaration).unwrap(); + definitions.remove(pos_bond_schema.declaration()).unwrap(); let pos_bond_table = - definition_to_table(pos_bond_schema.declaration, pos_bond_definition).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/transaction/pos/struct.Bond.html"); + definition_to_table(pos_bond_schema.declaration(), pos_bond_definition).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/transaction/pos/struct.Bond.html"); tables.push(pos_bond_table); let pos_withdraw_definition = definitions - .remove(&pos_withdraw_schema.declaration) + .remove(pos_withdraw_schema.declaration()) .unwrap(); let pos_withdraw_table = definition_to_table( - pos_withdraw_schema.declaration, + pos_withdraw_schema.declaration(), pos_withdraw_definition, ).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/transaction/pos/struct.Withdraw.html"); tables.push(pos_withdraw_table); let wrapper_tx_definition = - definitions.remove(&wrapper_tx_schema.declaration).unwrap(); + definitions.remove(wrapper_tx_schema.declaration()).unwrap(); let wrapper_tx_table = definition_to_table( - wrapper_tx_schema.declaration, + wrapper_tx_schema.declaration(), wrapper_tx_definition, ).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/transaction/wrapper/wrapper_tx/struct.WrapperTx.html"); tables.push(wrapper_tx_table); // let tx_result_definition = - // definitions.remove(&tx_result_schema.declaration).unwrap(); + // definitions.remove(tx_result_schema.declaration()).unwrap(); // let tx_result_table = - // definition_to_table(tx_result_schema.declaration, + // definition_to_table(tx_result_schema.declaration(), // tx_result_definition).with_rust_doc_link("TODO"); // tables.push(tx_result_table); let tx_type_definition = - definitions.remove(&tx_type_schema.declaration).unwrap(); + definitions.remove(tx_type_schema.declaration()).unwrap(); let tx_type_table = - definition_to_table(tx_type_schema.declaration, tx_type_definition).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/transaction/tx_types/enum.TxType.html"); + definition_to_table(tx_type_schema.declaration(), tx_type_definition).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/transaction/tx_types/enum.TxType.html"); tables.push(tx_type_table); let prefix_value_definition = definitions - .remove(&prefix_value_schema.declaration) + .remove(prefix_value_schema.declaration()) .unwrap(); let prefix_value_table = - definition_to_table(prefix_value_schema.declaration, prefix_value_definition).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/transaction/prefix_values/enum.TxType.html"); + definition_to_table(prefix_value_schema.declaration(), prefix_value_definition).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/transaction/prefix_values/enum.TxType.html"); tables.push(prefix_value_table); // Add PoS definitions @@ -243,7 +258,7 @@ fn main() -> Result<(), Box> { .into_iter() .sorted_by_key(|(key, _val)| key.clone()) { - tables.push(definition_to_table(declaration, defition)) + tables.push(definition_to_table(&declaration, defition)) } // Print the tables to markdown @@ -271,7 +286,7 @@ struct Table { rows: Option>, } -fn definition_to_table(name: String, def: schema::Definition) -> Table { +fn definition_to_table(name: &Declaration, def: schema::Definition) -> Table { let (desc, rows) = match def { schema::Definition::Array { length, elements } => { let rows = None; @@ -345,7 +360,7 @@ fn definition_to_table(name: String, def: schema::Definition) -> Table { } } }; - Table { name, desc, rows } + Table { name: name.to_string(), desc, rows } } /// Format a type to markdown. For internal types, adds anchors. diff --git a/ethereum_bridge/src/vp.rs b/ethereum_bridge/src/vp.rs index 180402245cc..d10425602d1 100644 --- a/ethereum_bridge/src/vp.rs +++ b/ethereum_bridge/src/vp.rs @@ -1,4 +1,4 @@ -use borsh::BorshSerialize; + use borsh_ext::BorshSerializeExt; use namada_core::ledger::storage::{self as ledger_storage, StorageHasher}; use namada_core::ledger::storage_api::StorageWrite; diff --git a/shared/src/ledger/native_vp/ethereum_bridge/nut.rs b/shared/src/ledger/native_vp/ethereum_bridge/nut.rs index cc2c2b51022..2a1ea22bdd4 100644 --- a/shared/src/ledger/native_vp/ethereum_bridge/nut.rs +++ b/shared/src/ledger/native_vp/ethereum_bridge/nut.rs @@ -121,7 +121,6 @@ mod test_nuts { use std::env::temp_dir; use assert_matches::assert_matches; - use borsh::BorshSerialize; use namada_core::ledger::storage::testing::TestWlStorage; use namada_core::ledger::storage_api::StorageWrite; use namada_core::types::address::testing::arb_non_internal_address; @@ -136,6 +135,7 @@ mod test_nuts { use crate::ledger::gas::{TxGasMeter, VpGasMeter}; use crate::vm::wasm::VpCache; use crate::vm::WasmCacheRwAccess; + use borsh_ext::BorshSerializeExt; /// Run a VP check on a NUT transfer between the two provided addresses. fn check_nut_transfer(src: Address, dst: Address) -> Option { diff --git a/shared/src/ledger/native_vp/ethereum_bridge/vp.rs b/shared/src/ledger/native_vp/ethereum_bridge/vp.rs index 68616068abc..b4a7d74715f 100644 --- a/shared/src/ledger/native_vp/ethereum_bridge/vp.rs +++ b/shared/src/ledger/native_vp/ethereum_bridge/vp.rs @@ -164,7 +164,6 @@ mod tests { use std::default::Default; use std::env::temp_dir; - use borsh::BorshSerialize; use namada_core::ledger::eth_bridge; use namada_core::ledger::eth_bridge::storage::bridge_pool::BRIDGE_POOL_ADDRESS; use namada_core::ledger::eth_bridge::storage::wrapped_erc20s; @@ -191,6 +190,7 @@ mod tests { use crate::types::transaction::TxType; use crate::vm::wasm::VpCache; use crate::vm::WasmCacheRwAccess; + use borsh_ext::BorshSerializeExt; const ARBITRARY_OWNER_A_ADDRESS: &str = "atest1d9khqw36x9zyxwfhgfpygv2pgc65gse4gy6rjs34gfzr2v69gy6y23zpggurjv2yx5m52sesu6r4y4"; diff --git a/shared/src/ledger/native_vp/multitoken.rs b/shared/src/ledger/native_vp/multitoken.rs index 9a8cb8521f7..43fcc2c99ae 100644 --- a/shared/src/ledger/native_vp/multitoken.rs +++ b/shared/src/ledger/native_vp/multitoken.rs @@ -139,7 +139,6 @@ where mod tests { use std::collections::BTreeSet; - use borsh::BorshSerialize; use namada_core::ledger::gas::TxGasMeter; use super::*; @@ -159,6 +158,7 @@ mod tests { }; use crate::types::transaction::TxType; use crate::vm::wasm::compilation_cache::common::testing::cache as wasm_cache; + use borsh_ext::BorshSerializeExt; const ADDRESS: Address = Address::Internal(InternalAddress::Multitoken); diff --git a/shared/src/ledger/queries/router.rs b/shared/src/ledger/queries/router.rs index 1e64163c69e..7f001bb49bf 100644 --- a/shared/src/ledger/queries/router.rs +++ b/shared/src/ledger/queries/router.rs @@ -6,6 +6,7 @@ //! all the `println!`s in this module. use thiserror::Error; +use borsh_ext::BorshSerializeExt; /// Router error. #[allow(missing_docs)] @@ -834,7 +835,6 @@ macro_rules! router { /// ``` #[cfg(test)] mod test_rpc_handlers { - use borsh::BorshSerialize; use crate::ledger::queries::{ EncodedResponseQuery, RequestCtx, RequestQuery, ResponseQuery, @@ -843,6 +843,7 @@ mod test_rpc_handlers { use crate::ledger::storage_api::{self, ResultExt}; use crate::types::storage::Epoch; use crate::types::token; + use borsh_ext::BorshSerializeExt; /// A little macro to generate boilerplate for RPC handler functions. /// These are implemented to return their name as a String, joined by @@ -949,7 +950,7 @@ mod test_rpc_handlers { D: 'static + DB + for<'iter> DBIter<'iter> + Sync, H: 'static + StorageHasher + Sync, { - let data = "c".to_owned().serialize_to_vec().into_storage_result()?; + let data = "c".to_owned().serialize_to_vec(); Ok(ResponseQuery { data, ..ResponseQuery::default() diff --git a/shared/src/ledger/queries/shell.rs b/shared/src/ledger/queries/shell.rs index 36842723635..a3cf1572e41 100644 --- a/shared/src/ledger/queries/shell.rs +++ b/shared/src/ledger/queries/shell.rs @@ -201,7 +201,7 @@ where data.gas_used = cumulated_gas; // NOTE: the keys changed by the wrapper transaction (if any) are not // returned from this function - let data = data.serialize_to_vec().into_storage_result()?; + let data = data.serialize_to_vec(); Ok(EncodedResponseQuery { data, proof: None, @@ -428,7 +428,7 @@ where } else { None }; - let data = data.serialize_to_vec().into_storage_result()?; + let data = data.serialize_to_vec(); Ok(EncodedResponseQuery { data, proof, @@ -586,6 +586,7 @@ mod test { use crate::types::transaction::decrypted::DecryptedTx; use crate::types::transaction::TxType; use crate::types::{address, token}; + use borsh_ext::BorshSerializeExt; #[test] fn test_shell_queries_router_paths() { diff --git a/shared/src/ledger/signing.rs b/shared/src/ledger/signing.rs index 95744e39457..955bee2aca7 100644 --- a/shared/src/ledger/signing.rs +++ b/shared/src/ledger/signing.rs @@ -3,7 +3,7 @@ use std::collections::{BTreeMap, HashMap}; use std::path::PathBuf; -use borsh::{BorshDeserialize, BorshSerialize}; +use borsh::BorshDeserialize; use borsh_ext::BorshSerializeExt; use data_encoding::HEXLOWER; use itertools::Itertools; diff --git a/shared/src/ledger/tx.rs b/shared/src/ledger/tx.rs index 0e91fea67f2..6283c3d0265 100644 --- a/shared/src/ledger/tx.rs +++ b/shared/src/ledger/tx.rs @@ -2110,8 +2110,6 @@ where } fn proposal_to_vec(proposal: OnChainProposal) -> Result> { - proposal - .content - .serialize_to_vec() + borsh::to_vec(&proposal.content) .map_err(|e| Error::from(EncodingError::Conversion(e.to_string()))) } diff --git a/shared/src/ledger/wallet/keys.rs b/shared/src/ledger/wallet/keys.rs index d6517f14847..edda27a90a6 100644 --- a/shared/src/ledger/wallet/keys.rs +++ b/shared/src/ledger/wallet/keys.rs @@ -12,6 +12,7 @@ use thiserror::Error; use zeroize::Zeroizing; use crate::ledger::wallet::WalletUtils; +use borsh_ext::BorshSerializeExt; const ENCRYPTED_KEY_PREFIX: &str = "encrypted:"; const UNENCRYPTED_KEY_PREFIX: &str = "unencrypted:"; diff --git a/shared/src/vm/host_env.rs b/shared/src/vm/host_env.rs index dca4ffb552b..25b1f360fc0 100644 --- a/shared/src/vm/host_env.rs +++ b/shared/src/vm/host_env.rs @@ -30,6 +30,7 @@ use crate::types::token::{ use crate::vm::memory::VmMemory; use crate::vm::prefix_iter::{PrefixIteratorId, PrefixIterators}; use crate::vm::{HostRef, MutHostRef}; +use borsh_ext::BorshSerializeExt; /// These runtime errors will abort tx WASM execution immediately #[allow(missing_docs)] @@ -734,11 +735,10 @@ where tx_charge_gas(env, iter_gas + log_gas)?; match log_val { Some(write_log::StorageModification::Write { ref value }) => { - let key_val = KeyVal { + let key_val = borsh::to_vec(&KeyVal { key, val: value.clone(), - } - .serialize_to_vec() + }) .map_err(TxRuntimeError::EncodingError)?; let len: i64 = key_val .len() @@ -757,11 +757,10 @@ where continue; } Some(write_log::StorageModification::Temp { ref value }) => { - let key_val = KeyVal { + let key_val = borsh::to_vec(&KeyVal { key, val: value.clone(), - } - .serialize_to_vec() + }) .map_err(TxRuntimeError::EncodingError)?; let len: i64 = key_val .len() @@ -772,8 +771,7 @@ where return Ok(len); } None => { - let key_val = KeyVal { key, val } - .serialize_to_vec() + let key_val = borsh::to_vec(&KeyVal { key, val }) .map_err(TxRuntimeError::EncodingError)?; let len: i64 = key_val .len() @@ -1001,8 +999,7 @@ where let write_log = unsafe { env.ctx.write_log.get() }; for event in write_log.get_ibc_events() { if event.event_type == event_type { - let value = event - .serialize_to_vec() + let value = borsh::to_vec(event) .map_err(TxRuntimeError::EncodingError)?; let len: i64 = value .len() @@ -1349,8 +1346,7 @@ where if let Some(iter) = iterators.get_mut(iter_id) { let gas_meter = unsafe { env.ctx.gas_meter.get() }; if let Some((key, val)) = vp_host_fns::iter_next(gas_meter, iter)? { - let key_val = KeyVal { key, val } - .serialize_to_vec() + let key_val = borsh::to_vec(&KeyVal { key, val }) .map_err(vp_host_fns::RuntimeError::EncodingError)?; let len: i64 = key_val .len() @@ -1461,8 +1457,7 @@ where .map_err(|e| TxRuntimeError::InvalidVpCodeHash(e.to_string()))?; let (addr, gas) = write_log.init_account(&storage.address_gen, code_hash); let addr_bytes = addr - .serialize_to_vec() - .map_err(TxRuntimeError::EncodingError)?; + .serialize_to_vec(); tx_charge_gas(env, gas)?; let gas = env .memory @@ -1626,8 +1621,7 @@ where Ok(match header { Some(h) => { let value = h - .serialize_to_vec() - .map_err(TxRuntimeError::EncodingError)?; + .serialize_to_vec(); let len: i64 = value .len() .try_into() @@ -1703,8 +1697,7 @@ where Ok(match header { Some(h) => { let value = h - .serialize_to_vec() - .map_err(vp_host_fns::RuntimeError::EncodingError)?; + .serialize_to_vec(); let len: i64 = value .len() .try_into() diff --git a/shared/src/vm/wasm/memory.rs b/shared/src/vm/wasm/memory.rs index 60f463c5f16..95c9e232cf6 100644 --- a/shared/src/vm/wasm/memory.rs +++ b/shared/src/vm/wasm/memory.rs @@ -5,7 +5,6 @@ use std::ptr::NonNull; use std::str::Utf8Error; use std::sync::Arc; -use borsh::BorshSerialize; use namada_core::ledger::gas::VM_MEMORY_ACCESS_GAS_PER_BYTE; use thiserror::Error; use wasmer::{ @@ -19,6 +18,7 @@ use wasmer_vm::{ use crate::proto::Tx; use crate::vm::memory::VmMemory; use crate::vm::types::VpInput; +use borsh_ext::BorshSerializeExt; #[allow(missing_docs)] #[derive(Error, Debug)] @@ -87,7 +87,7 @@ pub fn write_tx_inputs( ) -> Result { let tx_data_ptr = 0; let tx_data_bytes = - tx_data.serialize_to_vec().map_err(Error::EncodingError)?; + tx_data.serialize_to_vec(); let tx_data_len = tx_data_bytes.len() as _; write_memory_bytes(memory, tx_data_ptr, tx_data_bytes)?; @@ -130,21 +130,20 @@ pub fn write_vp_inputs( }: VpInput, ) -> Result { let addr_ptr = 0; - let addr_bytes = addr.serialize_to_vec().map_err(Error::EncodingError)?; + let addr_bytes = addr.serialize_to_vec(); let addr_len = addr_bytes.len() as _; - let data_bytes = data.serialize_to_vec().map_err(Error::EncodingError)?; + let data_bytes = data.serialize_to_vec(); let data_ptr = addr_ptr + addr_len; let data_len = data_bytes.len() as _; let keys_changed_bytes = keys_changed - .serialize_to_vec() - .map_err(Error::EncodingError)?; + .serialize_to_vec(); let keys_changed_ptr = data_ptr + data_len; let keys_changed_len = keys_changed_bytes.len() as _; let verifiers_bytes = - verifiers.serialize_to_vec().map_err(Error::EncodingError)?; + verifiers.serialize_to_vec(); let verifiers_ptr = keys_changed_ptr + keys_changed_len; let verifiers_len = verifiers_bytes.len() as _; diff --git a/shared/src/vm/wasm/run.rs b/shared/src/vm/wasm/run.rs index 3a633f6aff0..da82904154f 100644 --- a/shared/src/vm/wasm/run.rs +++ b/shared/src/vm/wasm/run.rs @@ -30,6 +30,7 @@ use crate::vm::wasm::{memory, Cache, CacheName, VpCache}; use crate::vm::{ validate_untrusted_wasm, WasmCacheAccess, WasmValidationError, }; +use borsh_ext::BorshSerializeExt; const TX_ENTRYPOINT: &str = "_apply_tx"; const VP_ENTRYPOINT: &str = "_validate_tx"; diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 674bce95385..693494d6824 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -58,6 +58,7 @@ tracing.workspace = true namada_apps = {path = "../apps", features = ["testing"]} assert_cmd.workspace = true borsh.workspace = true +borsh-ext.workspace = true color-eyre.workspace = true data-encoding.workspace = true # NOTE: enable "print" feature to see output from builds ran by e2e tests diff --git a/tests/src/vm_host_env/ibc.rs b/tests/src/vm_host_env/ibc.rs index b6caa7e9416..db890b00ec1 100644 --- a/tests/src/vm_host_env/ibc.rs +++ b/tests/src/vm_host_env/ibc.rs @@ -86,6 +86,7 @@ use namada::vm::{wasm, WasmCacheRwAccess}; use namada_core::ledger::gas::TxGasMeter; use namada_test_utils::TestWasms; use namada_tx_prelude::BorshSerialize; +use namada_tx_prelude::borsh_ext::BorshSerializeExt; use crate::tx::*; diff --git a/tests/src/vm_host_env/tx.rs b/tests/src/vm_host_env/tx.rs index 48ada73c2f4..b6adad186bd 100644 --- a/tests/src/vm_host_env/tx.rs +++ b/tests/src/vm_host_env/tx.rs @@ -23,6 +23,7 @@ use namada_vp_prelude::key::common; use tempfile::TempDir; use crate::vp::TestVpEnv; +use namada_tx_prelude::borsh_ext::BorshSerializeExt; /// Tx execution context provides access to host env functions static mut CTX: Ctx = unsafe { Ctx::new() }; diff --git a/tx_prelude/src/lib.rs b/tx_prelude/src/lib.rs index daf0d4ef767..87b94a676c2 100644 --- a/tx_prelude/src/lib.rs +++ b/tx_prelude/src/lib.rs @@ -17,6 +17,7 @@ use core::slice; use std::marker::PhantomData; pub use borsh::{BorshDeserialize, BorshSerialize}; +pub use borsh_ext; use borsh_ext::BorshSerializeExt; pub use namada_core::ledger::eth_bridge; pub use namada_core::ledger::governance::storage as gov_storage;