From 3d2bd33f3eda18d7a4d2f1145cf67881a4aaf428 Mon Sep 17 00:00:00 2001 From: Murisi Tarusenga Date: Mon, 20 Feb 2023 17:56:56 +0200 Subject: [PATCH] Add an extra data field to Txs that is hashed before signing. --- apps/src/lib/client/tx.rs | 8 ++- .../lib/node/ledger/shell/process_proposal.rs | 1 + core/src/ledger/storage/mod.rs | 10 +--- core/src/ledger/storage_api/mod.rs | 5 +- core/src/ledger/tx_env.rs | 7 +++ core/src/proto/mod.rs | 1 + core/src/proto/types.rs | 8 +++ core/src/types/transaction/mod.rs | 4 +- proto/types.proto | 3 +- shared/src/ledger/native_vp/mod.rs | 8 --- shared/src/ledger/protocol/mod.rs | 5 +- shared/src/vm/host_env.rs | 52 +++++++++++++++++-- shared/src/vm/wasm/host_env.rs | 2 + shared/src/vm/wasm/run.rs | 25 ++++----- tests/src/vm_host_env/mod.rs | 28 ++++++++++ tests/src/vm_host_env/tx.rs | 10 ++-- tx_prelude/src/lib.rs | 20 +++++-- vm_env/src/lib.rs | 8 ++- vp_prelude/src/lib.rs | 8 --- wasm/checksums.json | 36 ++++++------- wasm/wasm_source/src/tx_init_account.rs | 2 +- 21 files changed, 163 insertions(+), 88 deletions(-) diff --git a/apps/src/lib/client/tx.rs b/apps/src/lib/client/tx.rs index 9ae1d0562f..5e98e8992d 100644 --- a/apps/src/lib/client/tx.rs +++ b/apps/src/lib/client/tx.rs @@ -196,13 +196,11 @@ pub async fn submit_init_account(mut ctx: Context, args: args::TxInitAccount) { } let tx_code = ctx.read_wasm(TX_INIT_ACCOUNT_WASM); - let data = InitAccount { - public_key, - vp_code, - }; + let data = InitAccount { public_key }; let data = data.try_to_vec().expect("Encoding tx data shouldn't fail"); - let tx = Tx::new(tx_code, Some(data)); + let mut tx = Tx::new(tx_code, Some(data)); + tx.extra = Some(vp_code); let (ctx, initialized_accounts) = process_tx( ctx, &args.tx, diff --git a/apps/src/lib/node/ledger/shell/process_proposal.rs b/apps/src/lib/node/ledger/shell/process_proposal.rs index 2888b54ea4..1b4308e5e3 100644 --- a/apps/src/lib/node/ledger/shell/process_proposal.rs +++ b/apps/src/lib/node/ledger/shell/process_proposal.rs @@ -339,6 +339,7 @@ mod test_process_proposal { ), timestamp, inner_tx: tx.inner_tx, + extra: None, } } else { panic!("Test failed"); diff --git a/core/src/ledger/storage/mod.rs b/core/src/ledger/storage/mod.rs index 45f145ec87..74198e8a86 100644 --- a/core/src/ledger/storage/mod.rs +++ b/core/src/ledger/storage/mod.rs @@ -47,7 +47,7 @@ use crate::types::chain::{ChainId, CHAIN_ID_LENGTH}; use crate::types::internal::TxQueue; use crate::types::storage::{ BlockHash, BlockHeight, BlockResults, Epoch, Epochs, Header, Key, KeySeg, - TxIndex, BLOCK_HASH_LENGTH, + BLOCK_HASH_LENGTH, }; use crate::types::time::DateTimeUtc; use crate::types::token; @@ -97,8 +97,6 @@ where pub next_epoch_min_start_time: DateTimeUtc, /// The current established address generator pub address_gen: EstablishedAddressGen, - /// The shielded transaction index - pub tx_index: TxIndex, /// The currently saved conversion state pub conversion_state: ConversionState, /// Wrapper txs to be decrypted in the next block proposal @@ -354,7 +352,6 @@ where address_gen: EstablishedAddressGen::new( "Privacy is a function of liberty.", ), - tx_index: TxIndex::default(), conversion_state: ConversionState::default(), #[cfg(feature = "ferveo-tpke")] tx_queue: TxQueue::default(), @@ -1053,10 +1050,6 @@ where Ok(self.block.epoch) } - fn get_tx_index(&self) -> std::result::Result { - Ok(self.tx_index) - } - fn get_native_token( &self, ) -> std::result::Result { @@ -1142,7 +1135,6 @@ pub mod testing { address_gen: EstablishedAddressGen::new( "Test address generator seed", ), - tx_index: TxIndex::default(), conversion_state: ConversionState::default(), #[cfg(feature = "ferveo-tpke")] tx_queue: TxQueue::default(), diff --git a/core/src/ledger/storage_api/mod.rs b/core/src/ledger/storage_api/mod.rs index c929aec03b..a78751d80f 100644 --- a/core/src/ledger/storage_api/mod.rs +++ b/core/src/ledger/storage_api/mod.rs @@ -10,7 +10,7 @@ use borsh::{BorshDeserialize, BorshSerialize}; pub use error::{CustomError, Error, OptionExt, Result, ResultExt}; use crate::types::address::Address; -use crate::types::storage::{self, BlockHash, BlockHeight, Epoch, TxIndex}; +use crate::types::storage::{self, BlockHash, BlockHeight, Epoch}; /// Common storage read interface /// @@ -85,9 +85,6 @@ pub trait StorageRead { /// current transaction is being applied. fn get_block_epoch(&self) -> Result; - /// Get the transaction index. - fn get_tx_index(&self) -> Result; - /// Get the native token address fn get_native_token(&self) -> Result
; } diff --git a/core/src/ledger/tx_env.rs b/core/src/ledger/tx_env.rs index 6ca47bb9d9..36ca28604b 100644 --- a/core/src/ledger/tx_env.rs +++ b/core/src/ledger/tx_env.rs @@ -7,6 +7,7 @@ use crate::ledger::storage_api::{self, StorageRead, StorageWrite}; use crate::types::address::Address; use crate::types::ibc::IbcEvent; use crate::types::storage; +use crate::types::storage::TxIndex; use crate::types::time::Rfc3339String; /// Transaction host functions @@ -60,4 +61,10 @@ pub trait TxEnv: StorageRead + StorageWrite { /// Get time of the current block header as rfc 3339 string fn get_block_time(&self) -> Result; + + /// Get the transaction index + fn get_tx_index(&self) -> Result; + + /// Get the transaction extra data + fn get_tx_extra(&self) -> Result, storage_api::Error>; } diff --git a/core/src/proto/mod.rs b/core/src/proto/mod.rs index d36ebd8b18..b2eb0e534b 100644 --- a/core/src/proto/mod.rs +++ b/core/src/proto/mod.rs @@ -22,6 +22,7 @@ mod tests { data: Some("arbitrary data".as_bytes().to_owned()), timestamp: Some(std::time::SystemTime::now().into()), inner_tx: Some(inner_tx), + extra: None, }; let mut tx_bytes = vec![]; tx.encode(&mut tx_bytes).unwrap(); diff --git a/core/src/proto/types.rs b/core/src/proto/types.rs index 599ae6409b..975d5ea9b4 100644 --- a/core/src/proto/types.rs +++ b/core/src/proto/types.rs @@ -146,6 +146,7 @@ pub struct Tx { pub code: Vec, pub data: Option>, pub timestamp: DateTimeUtc, + pub extra: Option>, /// the encrypted inner transaction if data contains a WrapperTx #[cfg(feature = "ferveo-tpke")] pub inner_tx: Option, @@ -172,6 +173,7 @@ impl TryFrom<&[u8]> for Tx { Ok(Tx { code: tx.code, data: tx.data, + extra: tx.extra, timestamp, inner_tx, }) @@ -188,6 +190,7 @@ impl From for types::Tx { types::Tx { code: tx.code, data: tx.data, + extra: tx.extra, timestamp, inner_tx, } @@ -287,6 +290,7 @@ impl Tx { data, timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } } @@ -305,6 +309,7 @@ impl Tx { let mut bytes = vec![]; types::Tx { code: hash_tx(&self.code).0.to_vec(), + extra: self.extra.as_ref().map(|x| hash_tx(x).0.to_vec()), data: self.data.clone(), timestamp, inner_tx: None, @@ -332,6 +337,7 @@ impl Tx { Tx { code: self.code, data: Some(signed), + extra: self.extra, timestamp: self.timestamp, inner_tx: self.inner_tx, } @@ -351,6 +357,7 @@ impl Tx { let data = signed_tx_data.data; let tx = Tx { code: self.code.clone(), + extra: self.extra.clone(), data, timestamp: self.timestamp, inner_tx: self.inner_tx.clone(), @@ -478,6 +485,7 @@ mod tests { data: Some(data), timestamp: None, inner_tx: None, + extra: None, }; let mut bytes = vec![]; types_tx.encode(&mut bytes).expect("encoding failed"); diff --git a/core/src/types/transaction/mod.rs b/core/src/types/transaction/mod.rs index 338578b764..3cd0a77ce1 100644 --- a/core/src/types/transaction/mod.rs +++ b/core/src/types/transaction/mod.rs @@ -163,8 +163,6 @@ pub struct InitAccount { /// for signature verification of transactions for the newly created /// account. pub public_key: common::PublicKey, - /// The VP code - pub vp_code: Vec, } /// A tx data type to initialize a new validator account. @@ -297,6 +295,7 @@ pub mod tx_types { data: Some(data.clone()), timestamp: tx.timestamp, inner_tx: tx.inner_tx.clone(), + extra: tx.extra.clone(), } .partial_hash(); match TxType::try_from(Tx { @@ -304,6 +303,7 @@ pub mod tx_types { data: Some(data), timestamp: tx.timestamp, inner_tx: tx.inner_tx, + extra: tx.extra, }) .map_err(|err| TxError::Deserialization(err.to_string()))? { diff --git a/proto/types.proto b/proto/types.proto index a9478aaf5b..ec2a8aaf12 100644 --- a/proto/types.proto +++ b/proto/types.proto @@ -9,7 +9,8 @@ message Tx { // TODO this optional is useless because it's default on proto3 optional bytes data = 2; google.protobuf.Timestamp timestamp = 3; - optional bytes inner_tx = 4; + optional bytes extra = 4; + optional bytes inner_tx = 5; } message Dkg { string data = 1; } diff --git a/shared/src/ledger/native_vp/mod.rs b/shared/src/ledger/native_vp/mod.rs index e14378c3d3..8fd6a9c259 100644 --- a/shared/src/ledger/native_vp/mod.rs +++ b/shared/src/ledger/native_vp/mod.rs @@ -240,10 +240,6 @@ where self.ctx.get_block_epoch() } - fn get_tx_index(&self) -> Result { - self.ctx.get_tx_index().into_storage_result() - } - fn get_native_token(&self) -> Result { self.ctx.get_native_token() } @@ -322,10 +318,6 @@ where self.ctx.get_block_epoch() } - fn get_tx_index(&self) -> Result { - self.ctx.get_tx_index().into_storage_result() - } - fn get_native_token(&self) -> Result { Ok(self.ctx.storage.native_token.clone()) } diff --git a/shared/src/ledger/protocol/mod.rs b/shared/src/ledger/protocol/mod.rs index cfd416c14d..bdd8aa0427 100644 --- a/shared/src/ledger/protocol/mod.rs +++ b/shared/src/ledger/protocol/mod.rs @@ -164,15 +164,12 @@ where gas_meter .add_compiling_fee(tx.code.len()) .map_err(Error::GasError)?; - let empty = vec![]; - let tx_data = tx.data.as_ref().unwrap_or(&empty); wasm::run::tx( storage, write_log, gas_meter, tx_index, - &tx.code, - tx_data, + tx, vp_wasm_cache, tx_wasm_cache, ) diff --git a/shared/src/vm/host_env.rs b/shared/src/vm/host_env.rs index ee8e8a3601..d4b99b339d 100644 --- a/shared/src/vm/host_env.rs +++ b/shared/src/vm/host_env.rs @@ -91,6 +91,8 @@ where pub iterators: MutHostRef<'a, &'a PrefixIterators<'a, DB>>, /// Transaction gas meter. pub gas_meter: MutHostRef<'a, &'a BlockGasMeter>, + /// The transaction code is used for signature verification + pub tx: HostRef<'a, &'a Tx>, /// The transaction index is used to identify a shielded transaction's /// parent pub tx_index: HostRef<'a, &'a TxIndex>, @@ -131,6 +133,7 @@ where write_log: &mut WriteLog, iterators: &mut PrefixIterators<'a, DB>, gas_meter: &mut BlockGasMeter, + tx: &Tx, tx_index: &TxIndex, verifiers: &mut BTreeSet
, result_buffer: &mut Option>, @@ -141,6 +144,7 @@ where let write_log = unsafe { MutHostRef::new(write_log) }; let iterators = unsafe { MutHostRef::new(iterators) }; let gas_meter = unsafe { MutHostRef::new(gas_meter) }; + let tx = unsafe { HostRef::new(tx) }; let tx_index = unsafe { HostRef::new(tx_index) }; let verifiers = unsafe { MutHostRef::new(verifiers) }; let result_buffer = unsafe { MutHostRef::new(result_buffer) }; @@ -153,6 +157,7 @@ where write_log, iterators, gas_meter, + tx, tx_index, verifiers, result_buffer, @@ -195,6 +200,7 @@ where write_log: self.write_log.clone(), iterators: self.iterators.clone(), gas_meter: self.gas_meter.clone(), + tx: self.tx.clone(), tx_index: self.tx_index.clone(), verifiers: self.verifiers.clone(), result_buffer: self.result_buffer.clone(), @@ -1481,9 +1487,9 @@ where Ok(height.0) } -/// Getting the block height function exposed to the wasm VM Tx -/// environment. The height is that of the block to which the current -/// transaction is being applied. +/// Getting the transaction index function exposed to the wasm VM Tx +/// environment. The index is that of the transaction being applied +/// in the current block. pub fn tx_get_tx_index( env: &TxVmEnv, ) -> TxResult @@ -1498,6 +1504,44 @@ where Ok(tx_index.0) } +/// Getting the transaction extra data function exposed to the wasm VM Tx +/// environment. The extra data is that of the transaction being applied. +pub fn tx_get_tx_extra( + env: &TxVmEnv, + result_ptr: u64, +) -> TxResult<()> +where + MEM: VmMemory, + DB: storage::DB + for<'iter> storage::DBIter<'iter>, + H: StorageHasher, + CA: WasmCacheAccess, +{ + let tx = unsafe { env.ctx.tx.get() }; + tx_add_gas(env, crate::vm::host_env::gas::MIN_STORAGE_GAS)?; + let gas = env + .memory + .write_bytes(result_ptr, tx.extra.as_ref().unwrap_or(&vec![])) + .map_err(|e| TxRuntimeError::MemoryError(Box::new(e)))?; + tx_add_gas(env, gas) +} + +/// Getting the transaction extra data length function exposed to the wasm +/// VM Tx environment. The extra data length is that of the transaction +/// being applied. +pub fn tx_get_tx_extra_len( + env: &TxVmEnv, +) -> TxResult +where + MEM: VmMemory, + DB: storage::DB + for<'iter> storage::DBIter<'iter>, + H: StorageHasher, + CA: WasmCacheAccess, +{ + let tx = unsafe { env.ctx.tx.get() }; + tx_add_gas(env, crate::vm::host_env::gas::MIN_STORAGE_GAS)?; + Ok(tx.extra.as_ref().map(|x| Vec::len(x) as u64).unwrap_or(0)) +} + /// Getting the block height function exposed to the wasm VM VP /// environment. The height is that of the block to which the current /// transaction is being applied. @@ -1953,6 +1997,7 @@ pub mod testing { iterators: &mut PrefixIterators<'static, DB>, verifiers: &mut BTreeSet
, gas_meter: &mut BlockGasMeter, + tx: &Tx, tx_index: &TxIndex, result_buffer: &mut Option>, #[cfg(feature = "wasm-runtime")] vp_wasm_cache: &mut VpCache, @@ -1969,6 +2014,7 @@ pub mod testing { write_log, iterators, gas_meter, + tx, tx_index, verifiers, result_buffer, diff --git a/shared/src/vm/wasm/host_env.rs b/shared/src/vm/wasm/host_env.rs index 28fb2cc12a..efa3c283a8 100644 --- a/shared/src/vm/wasm/host_env.rs +++ b/shared/src/vm/wasm/host_env.rs @@ -74,6 +74,8 @@ where "namada_tx_emit_ibc_event" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_emit_ibc_event), "namada_tx_get_chain_id" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_get_chain_id), "namada_tx_get_tx_index" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_get_tx_index), + "namada_tx_get_tx_extra" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_get_tx_extra), + "namada_tx_get_tx_extra_len" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_get_tx_extra_len), "namada_tx_get_block_height" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_get_block_height), "namada_tx_get_block_time" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_get_block_time), "namada_tx_get_block_hash" => Function::new_native_with_env(wasm_store, env.clone(), host_env::tx_get_block_hash), diff --git a/shared/src/vm/wasm/run.rs b/shared/src/vm/wasm/run.rs index 233a5f72a0..246bc40996 100644 --- a/shared/src/vm/wasm/run.rs +++ b/shared/src/vm/wasm/run.rs @@ -77,8 +77,7 @@ pub fn tx( write_log: &mut WriteLog, gas_meter: &mut BlockGasMeter, tx_index: &TxIndex, - tx_code: impl AsRef<[u8]>, - tx_data: impl AsRef<[u8]>, + tx: &Tx, vp_wasm_cache: &mut VpCache, tx_wasm_cache: &mut TxCache, ) -> Result> @@ -89,9 +88,11 @@ where { // let wasm_store = untrusted_wasm_store(memory::tx_limit()); - validate_untrusted_wasm(&tx_code).map_err(Error::ValidationError)?; + let empty = vec![]; + let tx_data = tx.data.as_ref().unwrap_or(&empty); + validate_untrusted_wasm(&tx.code).map_err(Error::ValidationError)?; - let (module, store) = tx_wasm_cache.fetch_or_compile(&tx_code)?; + let (module, store) = tx_wasm_cache.fetch_or_compile(&tx.code)?; let mut iterators: PrefixIterators<'_, DB> = PrefixIterators::default(); let mut verifiers = BTreeSet::new(); @@ -103,6 +104,7 @@ where write_log, &mut iterators, gas_meter, + tx, tx_index, &mut verifiers, &mut result_buffer, @@ -495,8 +497,7 @@ mod tests { &mut write_log, &mut gas_meter, &tx_index, - tx_code.clone(), - tx_data, + &Tx::new(tx_code.clone(), Some(tx_data)), &mut vp_cache, &mut tx_cache, ); @@ -510,8 +511,7 @@ mod tests { &mut write_log, &mut gas_meter, &tx_index, - tx_code, - tx_data, + &Tx::new(tx_code, Some(tx_data)), &mut vp_cache, &mut tx_cache, ) @@ -692,8 +692,7 @@ mod tests { &mut write_log, &mut gas_meter, &tx_index, - tx_no_op, - tx_data, + &Tx::new(tx_no_op, Some(tx_data)), &mut vp_cache, &mut tx_cache, ); @@ -809,8 +808,7 @@ mod tests { &mut write_log, &mut gas_meter, &tx_index, - tx_read_key, - tx_data, + &Tx::new(tx_read_key, Some(tx_data)), &mut vp_cache, &mut tx_cache, ) @@ -974,8 +972,7 @@ mod tests { &mut write_log, &mut gas_meter, &tx_index, - tx_code, - tx_data, + &Tx::new(tx_code, Some(tx_data)), &mut vp_cache, &mut tx_cache, ) diff --git a/tests/src/vm_host_env/mod.rs b/tests/src/vm_host_env/mod.rs index 53caa22bea..3c8d6dd2de 100644 --- a/tests/src/vm_host_env/mod.rs +++ b/tests/src/vm_host_env/mod.rs @@ -541,6 +541,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // get and increment the connection counter @@ -579,6 +580,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); @@ -613,6 +615,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // get and update the client without a header @@ -659,6 +662,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // update the client with the message @@ -689,6 +693,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // upgrade the client with the message @@ -728,6 +733,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // get and increment the connection counter @@ -766,6 +772,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // init a connection with the message @@ -793,6 +800,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // open the connection with the message @@ -830,6 +838,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // open try a connection with the message @@ -858,6 +867,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // open the connection with the mssage @@ -900,6 +910,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // not bind a port @@ -942,6 +953,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // bind a port @@ -987,6 +999,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // init a channel with the message @@ -1012,6 +1025,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // open the channle with the message @@ -1051,6 +1065,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // try open a channel with the message @@ -1077,6 +1092,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // open a channel with the message @@ -1118,6 +1134,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // close the channel with the message @@ -1159,6 +1176,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); @@ -1205,6 +1223,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // send the token and a packet with the data @@ -1246,6 +1265,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // ack the packet with the message @@ -1296,6 +1316,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // send the token and a packet with the data @@ -1362,6 +1383,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // receive a packet with the message @@ -1440,6 +1462,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // receive a packet with the message @@ -1488,6 +1511,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // send a packet with the message @@ -1518,6 +1542,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // ack the packet with the message @@ -1570,6 +1595,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); // receive a packet with the message @@ -1633,6 +1659,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); @@ -1706,6 +1733,7 @@ mod tests { data: Some(tx_data.clone()), timestamp: DateTimeUtc::now(), inner_tx: None, + extra: None, } .sign(&key::testing::keypair_1()); diff --git a/tests/src/vm_host_env/tx.rs b/tests/src/vm_host_env/tx.rs index 0f7040941d..8483bfc57d 100644 --- a/tests/src/vm_host_env/tx.rs +++ b/tests/src/vm_host_env/tx.rs @@ -185,14 +185,12 @@ impl TestTxEnv { /// Apply the tx changes to the write log. pub fn execute_tx(&mut self) -> Result<(), Error> { - let empty_data = vec![]; wasm::run::tx( &self.storage, &mut self.write_log, &mut self.gas_meter, &self.tx_index, - &self.tx.code, - self.tx.data.as_ref().unwrap_or(&empty_data), + &self.tx, &mut self.vp_wasm_cache, &mut self.tx_wasm_cache, ) @@ -317,7 +315,7 @@ mod native_tx_host_env { vp_cache_dir: _, tx_wasm_cache, tx_cache_dir: _, - tx: _, + tx, }: &mut TestTxEnv| { let tx_env = vm::host_env::testing::tx_env( @@ -326,6 +324,7 @@ mod native_tx_host_env { iterators, verifiers, gas_meter, + tx, tx_index, result_buffer, vp_wasm_cache, @@ -357,7 +356,7 @@ mod native_tx_host_env { vp_cache_dir: _, tx_wasm_cache, tx_cache_dir: _, - tx: _, + tx, }: &mut TestTxEnv| { let tx_env = vm::host_env::testing::tx_env( @@ -366,6 +365,7 @@ mod native_tx_host_env { iterators, verifiers, gas_meter, + tx, tx_index, result_buffer, vp_wasm_cache, diff --git a/tx_prelude/src/lib.rs b/tx_prelude/src/lib.rs index 9d66dcb73e..84f1dfdaa5 100644 --- a/tx_prelude/src/lib.rs +++ b/tx_prelude/src/lib.rs @@ -199,11 +199,6 @@ impl StorageRead for Ctx { namada_tx_result_buffer, )) } - - fn get_tx_index(&self) -> Result { - let tx_index = unsafe { namada_tx_get_tx_index() }; - Ok(TxIndex(tx_index)) - } } impl StorageWrite for Ctx { @@ -324,4 +319,19 @@ impl TxEnv for Ctx { }; Ok(()) } + + fn get_tx_index(&self) -> Result { + let tx_index = unsafe { namada_tx_get_tx_index() }; + Ok(TxIndex(tx_index)) + } + + fn get_tx_extra(&self) -> Result, Error> { + let capacity = unsafe { namada_tx_get_tx_extra_len() } as usize; + let result = Vec::with_capacity(capacity); + unsafe { + namada_tx_get_tx_extra(result.as_ptr() as _); + } + let slice = unsafe { slice::from_raw_parts(result.as_ptr(), capacity) }; + Ok(slice.to_vec()) + } } diff --git a/vm_env/src/lib.rs b/vm_env/src/lib.rs index b38feb83f2..0aa7ca6f71 100644 --- a/vm_env/src/lib.rs +++ b/vm_env/src/lib.rs @@ -93,9 +93,15 @@ pub mod tx { // Get the current block epoch pub fn namada_tx_get_block_epoch() -> u64; - // Get the current tx id + // Get the current tx index pub fn namada_tx_get_tx_index() -> u32; + // Get the current tx extra + pub fn namada_tx_get_tx_extra(result_ptr: u64); + + // Get the current tx extra length + pub fn namada_tx_get_tx_extra_len() -> u64; + // Get the native token address pub fn namada_tx_get_native_token(result_ptr: u64); diff --git a/vp_prelude/src/lib.rs b/vp_prelude/src/lib.rs index 33cb5f900c..7f3a06ced7 100644 --- a/vp_prelude/src/lib.rs +++ b/vp_prelude/src/lib.rs @@ -370,10 +370,6 @@ impl StorageRead for CtxPreStorageRead<'_> { get_block_epoch() } - fn get_tx_index(&self) -> Result { - get_tx_index() - } - fn get_native_token(&self) -> Result { get_native_token() } @@ -433,10 +429,6 @@ impl StorageRead for CtxPostStorageRead<'_> { get_block_epoch() } - fn get_tx_index(&self) -> Result { - get_tx_index() - } - fn get_native_token(&self) -> Result { get_native_token() } diff --git a/wasm/checksums.json b/wasm/checksums.json index 3ffc525c0a..8ae7d47a1b 100644 --- a/wasm/checksums.json +++ b/wasm/checksums.json @@ -1,20 +1,20 @@ { - "tx_bond.wasm": "tx_bond.749ff231716200c7d2ec509fa3e7d447e7ed63b76d92a465deffb5fcfc952332.wasm", - "tx_change_validator_commission.wasm": "tx_change_validator_commission.97b0d6f07c9db41320f1006e12e726098c93aad75eb843a575222c8f305462e7.wasm", - "tx_ibc.wasm": "tx_ibc.1d8969bf70235452e4e0254fb410ccd84d4cd1ea795f9cc608c3f9e76c45cd40.wasm", - "tx_init_account.wasm": "tx_init_account.bc398b6063fc1cb34c58f1a31793cf85f6e7a1305ef7937036d44fe0f3ca4dc0.wasm", - "tx_init_proposal.wasm": "tx_init_proposal.4a977c3d205b68114c6ec8f4ae8d933b768f146759a35de21796395626ca5d43.wasm", - "tx_init_validator.wasm": "tx_init_validator.a2da8bf373a314e59bfd48fba567ea1dfb56d616cade9f5a585e5bf5a59fb8d3.wasm", - "tx_reveal_pk.wasm": "tx_reveal_pk.fce1576f83731b281d7035a283adcec67779ea38f392148a06544821f7e49d0f.wasm", - "tx_transfer.wasm": "tx_transfer.3fda6e26b50e7aa4b1d6e37fc631d5c55bb9370e6fac71f64f2be137b42df549.wasm", - "tx_unbond.wasm": "tx_unbond.ed61e0b0289765f51c642aa7440304ac0c1468c3a2c587641c9c30f7eb545971.wasm", - "tx_update_vp.wasm": "tx_update_vp.db4d8c11658c5f8e99fc39f0112be1ee480ab1f0045010d323ee3081a7afe802.wasm", - "tx_vote_proposal.wasm": "tx_vote_proposal.01e5ade00d8c91d6db0a6e228edc1561143ee792f510ecbd37c36b18485c4872.wasm", - "tx_withdraw.wasm": "tx_withdraw.df583eb3c1529adb2bcd15946e5f9e9a735c406fb76baf5d0b1a48d1049b3213.wasm", - "vp_implicit.wasm": "vp_implicit.d94a52837ef3ae47c8574c549935758776ede4db155a0ea258a4e1beeaeafafa.wasm", - "vp_masp.wasm": "vp_masp.08137fd20390a40f106c4ab8ae3a6e548002dff189042aee858c6e4bf10f94e5.wasm", - "vp_testnet_faucet.wasm": "vp_testnet_faucet.e4409f3803ce7c15cd3852e6912342e594e71983e33fdf324788d98b529f0a66.wasm", - "vp_token.wasm": "vp_token.b9622cb39e0141c3a8b9c6d5cba395ca2baf335f1b376079f66ba2bf6b8cd770.wasm", - "vp_user.wasm": "vp_user.fb999a383e36081abdf80587c857a169e1fac5cc75823fe2bda5b3ace40d57fb.wasm", - "vp_validator.wasm": "vp_validator.c2d33de976f5ad5e75841292c9c59d6ebbc629647f35a50a6cda02f595a4addf.wasm" + "tx_bond.wasm": "tx_bond.43c2741554f360af694aea5377f6dbd5052ae70243fecdfbea7a0f9ae6edbe17.wasm", + "tx_change_validator_commission.wasm": "tx_change_validator_commission.452a9fc50686cbe33997e36c7d1cafff375a0154cff7dc91352676f10cd03674.wasm", + "tx_ibc.wasm": "tx_ibc.96a4c71ccbbd9a9862e06a127826d0016c0bdef5a3ced42a75bef4abf24a98e1.wasm", + "tx_init_account.wasm": "tx_init_account.17a78ac03dab6ef33a442808872e9c2a5114ce04d985584cbc4a7d1de95ae834.wasm", + "tx_init_proposal.wasm": "tx_init_proposal.a9c21c6399da0468b764e56323c05e8bdf13fa61ada12b1c68c0d49be9246d13.wasm", + "tx_init_validator.wasm": "tx_init_validator.7e4fbbe84968b68bda9f9c2efef45e533a36765bded138299b5e35698dc9df2b.wasm", + "tx_reveal_pk.wasm": "tx_reveal_pk.fa72b50b057b109b3c8cca3fd055b1a474bee8d2fe28f9fea09733cddf57ae1e.wasm", + "tx_transfer.wasm": "tx_transfer.749d1566d30c02de7c6e1d7ddbdc7c0f6e4978737ce0da05c3fa21c592568348.wasm", + "tx_unbond.wasm": "tx_unbond.11bbad04508057e298c8a1437f028b6402f1815535b76e6069268c23c56cdc00.wasm", + "tx_update_vp.wasm": "tx_update_vp.502dba0b096f73a37a6f4751536272796f64721ab83bf3b6997e4e829a9637c7.wasm", + "tx_vote_proposal.wasm": "tx_vote_proposal.3c555ab6b101932525cb3cb1324fb07fbff3ff7473d17008f0c441e9c123cd37.wasm", + "tx_withdraw.wasm": "tx_withdraw.e20097fa7c49d622532b3815135af2eb09002b2b49633ca0bff639d4dad31aff.wasm", + "vp_implicit.wasm": "vp_implicit.a1007cc2ca9dc374cadde172f4abfa31f17cef827ee249c5fd344b9245de4f24.wasm", + "vp_masp.wasm": "vp_masp.ba1927cf3b965b92953c9a4173543043c3de52f9700179572fa8049632f6d715.wasm", + "vp_testnet_faucet.wasm": "vp_testnet_faucet.c3b1e880356d814e7502698f53c7d962f585f4da6cd51cdecce208b009ebf297.wasm", + "vp_token.wasm": "vp_token.832a2000e57209ac5baf6b335645944acabb517d280d812341cf8e17af1e6a2d.wasm", + "vp_user.wasm": "vp_user.5b00d09db6b0037576302ec3e49f296ea7555c4dbb93a41fa4bc21cba0eda879.wasm", + "vp_validator.wasm": "vp_validator.b12e449695c24a15a9446908551312a2a6353d484b8baf9c91d41c29d1fbffde.wasm" } \ No newline at end of file diff --git a/wasm/wasm_source/src/tx_init_account.rs b/wasm/wasm_source/src/tx_init_account.rs index e0fe700d63..9bbf3b1119 100644 --- a/wasm/wasm_source/src/tx_init_account.rs +++ b/wasm/wasm_source/src/tx_init_account.rs @@ -12,7 +12,7 @@ fn apply_tx(ctx: &mut Ctx, tx_data: Vec) -> TxResult { .wrap_err("failed to decode InitAccount")?; debug_log!("apply_tx called to init a new established account"); - let address = ctx.init_account(&tx_data.vp_code)?; + let address = ctx.init_account(&ctx.get_tx_extra()?)?; let pk_key = key::pk_key(&address); ctx.write(&pk_key, &tx_data.public_key)?; Ok(())