From 30bb889ae96429ca891244052e01758e16b155d4 Mon Sep 17 00:00:00 2001 From: brentstone Date: Thu, 16 May 2024 12:58:43 -0700 Subject: [PATCH] fixup! Merge branch 'grarco/tx-batch' (#3103) --- crates/apps/src/lib/bench_utils.rs | 2 +- .../lib/node/ledger/shell/finalize_block.rs | 100 ++++++++++-------- crates/namada/src/ledger/governance/mod.rs | 7 +- crates/namada/src/ledger/mod.rs | 1 + .../ethereum_bridge/bridge_pool_vp.rs | 2 +- .../ledger/native_vp/ethereum_bridge/nut.rs | 2 +- .../ledger/native_vp/ethereum_bridge/vp.rs | 2 +- crates/namada/src/ledger/native_vp/ibc/mod.rs | 2 +- crates/namada/src/ledger/native_vp/masp.rs | 2 +- crates/namada/src/ledger/native_vp/mod.rs | 2 +- .../namada/src/ledger/native_vp/multitoken.rs | 7 +- .../namada/src/ledger/native_vp/parameters.rs | 2 +- crates/namada/src/ledger/pgf/mod.rs | 4 +- crates/namada/src/ledger/pos/vp.rs | 2 +- crates/namada/src/ledger/protocol/mod.rs | 6 +- crates/namada/src/ledger/vp_host_fns.rs | 5 +- crates/namada/src/vm/host_env.rs | 2 +- crates/namada/src/vm/wasm/memory.rs | 2 +- crates/namada/src/vm/wasm/run.rs | 9 +- crates/sdk/src/rpc.rs | 2 +- crates/state/src/write_log.rs | 22 +++- crates/tx/src/data/mod.rs | 4 +- crates/tx/src/data/wrapper.rs | 2 +- crates/tx/src/types.rs | 18 +++- crates/vp_env/src/lib.rs | 4 +- crates/vp_prelude/src/lib.rs | 2 +- 26 files changed, 131 insertions(+), 84 deletions(-) diff --git a/crates/apps/src/lib/bench_utils.rs b/crates/apps/src/lib/bench_utils.rs index 029fc44537..e1d0f15d53 100644 --- a/crates/apps/src/lib/bench_utils.rs +++ b/crates/apps/src/lib/bench_utils.rs @@ -404,7 +404,7 @@ impl BenchShell { /// Execute the tx and return a set of verifiers inserted by the tx. pub fn execute_tx( &mut self, - batched_tx: &BatchedTxRef, + batched_tx: &BatchedTxRef<'_>, ) -> BTreeSet
{ let gas_meter = RefCell::new(TxGasMeter::new_from_sub_limit(u64::MAX.into())); diff --git a/crates/apps/src/lib/node/ledger/shell/finalize_block.rs b/crates/apps/src/lib/node/ledger/shell/finalize_block.rs index 115eaaafd2..6072379a91 100644 --- a/crates/apps/src/lib/node/ledger/shell/finalize_block.rs +++ b/crates/apps/src/lib/node/ledger/shell/finalize_block.rs @@ -214,29 +214,27 @@ where continue; } - let (mut tx_event, tx_gas_meter, mut wrapper_args) = - match &tx_header.tx_type { - TxType::Wrapper(wrapper) => { - stats.increment_wrapper_txs(); - let tx_event = new_tx_event(&tx, height.0); - let gas_limit = match Gas::try_from(wrapper.gas_limit) { - Ok(value) => value, - Err(_) => { - response.events.emit( - new_tx_event(&tx, height.0) - .with(Code(ResultCode::InvalidTx)) - .with(Info( - "The wrapper gas limit overflowed \ - gas representation" - .to_owned(), - )) - .with(GasUsed(0.into())), - ); - continue; - } - }; - let gas_meter = TxGasMeter::new(gas_limit); - for cmt in tx.commitments() { + let (tx_gas_meter, mut wrapper_args) = match &tx_header.tx_type { + TxType::Wrapper(wrapper) => { + stats.increment_wrapper_txs(); + let gas_limit = match Gas::try_from(wrapper.gas_limit) { + Ok(value) => value, + Err(_) => { + response.events.emit( + new_tx_event(&tx, height.0) + .with(Code(ResultCode::InvalidTx)) + .with(Info( + "The wrapper gas limit overflowed gas \ + representation" + .to_owned(), + )) + .with(GasUsed(0.into())), + ); + continue; + } + }; + let gas_meter = TxGasMeter::new(gas_limit); + for cmt in tx.commitments() { if let Some(code_sec) = tx .get_section(cmt.code_sechash()) .and_then(|x| Section::code_sec(x.as_ref())) @@ -245,8 +243,8 @@ where code_sec.code.hash().to_string(), ); } - } } + ( gas_meter, Some(WrapperArgs { @@ -544,8 +542,8 @@ where namada::tx::data::TxResult, DispatchError, >, - tx_data: TxData, - mut tx_logs: TxLogs, + tx_data: TxData<'_>, + mut tx_logs: TxLogs<'_>, ) { // Check the commitment of the fee unshielding regardless of the // result, it could be committed even in case of errors @@ -628,8 +626,8 @@ where &mut self, response: &mut shim::response::FinalizeBlock, tx_result: namada::tx::data::TxResult, - tx_data: TxData, - tx_logs: &mut TxLogs, + tx_data: TxData<'_>, + tx_logs: &mut TxLogs<'_>, ) { let mut temp_log = TempTxLogs::new_from_tx_logs(tx_logs); @@ -646,8 +644,13 @@ where if tx_data.is_atomic_batch && is_any_tx_invalid { // Atomic batches need custom handling when even a single tx fails, // since we need to drop everything - let unrun_txs = tx_data.commitments_len - - tx_result.batch_results.0.len() as u64; + let unrun_txs = tx_data + .commitments_len + .checked_sub( + u64::try_from(tx_result.batch_results.0.len()) + .expect("Should be able to convert to u64"), + ) + .expect("Shouldn't underflow"); temp_log.stats.set_failing_atomic_batch(unrun_txs); temp_log.commit_stats_only(tx_logs); self.state.write_log_mut().drop_batch(); @@ -687,8 +690,8 @@ where response: &mut shim::response::FinalizeBlock, msg: &Error, tx_result: namada::tx::data::TxResult, - tx_data: TxData, - tx_logs: &mut TxLogs, + tx_data: TxData<'_>, + tx_logs: &mut TxLogs<'_>, ) { let mut temp_log = TempTxLogs::new_from_tx_logs(tx_logs); @@ -702,8 +705,13 @@ where tx_data.height, ); - let unrun_txs = - tx_data.commitments_len - tx_result.batch_results.0.len() as u64; + let unrun_txs = tx_data + .commitments_len + .checked_sub( + u64::try_from(tx_result.batch_results.0.len()) + .expect("Should be able to convert to u64"), + ) + .expect("Shouldn't underflow"); if tx_data.is_atomic_batch { tx_logs.stats.set_failing_atomic_batch(unrun_txs); @@ -735,7 +743,7 @@ where .extend(Batch(&tx_result.to_result_string())); } - fn handle_batch_error_reprot(&mut self, err: &Error, tx_data: TxData) { + fn handle_batch_error_reprot(&mut self, err: &Error, tx_data: TxData<'_>) { // If user transaction didn't fail because of out of gas nor // invalid section commitment, commit its hash to prevent // replays @@ -801,7 +809,7 @@ struct TempTxLogs { } impl TempTxLogs { - fn new_from_tx_logs(tx_logs: &TxLogs) -> Self { + fn new_from_tx_logs(tx_logs: &TxLogs<'_>) -> Self { Self { tx_event: Event::new( tx_logs.tx_event.kind().to_owned(), @@ -3095,7 +3103,7 @@ mod test_finalize_block { assert_eq!(*event[0].kind(), APPLIED_TX); let code = event[0].read_attribute::().expect("Test failed"); assert_eq!(code, ResultCode::Ok); - let inner_tx_result = event[0].read_attribute::().unwrap(); + let inner_tx_result = event[0].read_attribute::>().unwrap(); let first_tx_result = inner_tx_result .batch_results .0 @@ -3246,7 +3254,7 @@ mod test_finalize_block { assert_eq!(*event[1].kind(), APPLIED_TX); let code = event[1].read_attribute::().expect("Test failed"); assert_eq!(code, ResultCode::Ok); - let inner_tx_result = event[1].read_attribute::().unwrap(); + let inner_tx_result = event[1].read_attribute::>().unwrap(); let inner_result = inner_tx_result .batch_results .0 @@ -3256,7 +3264,7 @@ mod test_finalize_block { assert_eq!(*event[2].kind(), APPLIED_TX); let code = event[2].read_attribute::().expect("Test failed"); assert_eq!(code, ResultCode::Ok); - let inner_tx_result = event[2].read_attribute::().unwrap(); + let inner_tx_result = event[2].read_attribute::>().unwrap(); let inner_result = inner_tx_result .batch_results .0 @@ -3271,7 +3279,7 @@ mod test_finalize_block { assert_eq!(*event[3].kind(), APPLIED_TX); let code = event[3].read_attribute::().expect("Test failed"); assert_eq!(code, ResultCode::Ok); - let inner_tx_result = event[3].read_attribute::().unwrap(); + let inner_tx_result = event[3].read_attribute::>().unwrap(); let inner_result = inner_tx_result .batch_results .0 @@ -3476,7 +3484,7 @@ mod test_finalize_block { assert_eq!(*event.kind(), APPLIED_TX); let code = event.read_attribute::().expect("Test failed"); assert_eq!(code, ResultCode::Ok); - let inner_tx_result = event.read_attribute::().unwrap(); + let inner_tx_result = event.read_attribute::>().unwrap(); let inner_result = inner_tx_result .batch_results .0 @@ -5399,7 +5407,7 @@ mod test_finalize_block { let code = event[0].read_attribute::().unwrap(); assert_eq!(code, ResultCode::Ok); - let inner_tx_result = event[0].read_attribute::().unwrap(); + let inner_tx_result = event[0].read_attribute::>().unwrap(); let inner_results = inner_tx_result.batch_results.0; for cmt in batch.commitments() { @@ -5444,7 +5452,7 @@ mod test_finalize_block { let code = event[0].read_attribute::().unwrap(); assert_eq!(code, ResultCode::WasmRuntimeError); - let inner_tx_result = event[0].read_attribute::().unwrap(); + let inner_tx_result = event[0].read_attribute::>().unwrap(); let inner_results = inner_tx_result.batch_results.0; assert!( @@ -5494,7 +5502,7 @@ mod test_finalize_block { let code = event[0].read_attribute::().unwrap(); assert_eq!(code, ResultCode::Ok); - let inner_tx_result = event[0].read_attribute::().unwrap(); + let inner_tx_result = event[0].read_attribute::>().unwrap(); let inner_results = inner_tx_result.batch_results.0; assert!( @@ -5562,7 +5570,7 @@ mod test_finalize_block { let code = event[0].read_attribute::().unwrap(); assert_eq!(code, ResultCode::WasmRuntimeError); - let inner_tx_result = event[0].read_attribute::().unwrap(); + let inner_tx_result = event[0].read_attribute::>().unwrap(); let inner_results = inner_tx_result.batch_results.0; assert!( @@ -5611,7 +5619,7 @@ mod test_finalize_block { let code = event[0].read_attribute::().unwrap(); assert_eq!(code, ResultCode::WasmRuntimeError); - let inner_tx_result = event[0].read_attribute::().unwrap(); + let inner_tx_result = event[0].read_attribute::>().unwrap(); let inner_results = inner_tx_result.batch_results.0; assert!( diff --git a/crates/namada/src/ledger/governance/mod.rs b/crates/namada/src/ledger/governance/mod.rs index 563be01e72..5840f1a488 100644 --- a/crates/namada/src/ledger/governance/mod.rs +++ b/crates/namada/src/ledger/governance/mod.rs @@ -69,7 +69,7 @@ where fn validate_tx( &self, - tx_data: &BatchedTxRef, + tx_data: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, ) -> Result<()> { @@ -1119,7 +1119,10 @@ where } /// Validate a governance parameter - pub fn is_valid_parameter(&self, batched_tx: &BatchedTxRef) -> Result<()> { + pub fn is_valid_parameter( + &self, + batched_tx: &BatchedTxRef<'_>, + ) -> Result<()> { let BatchedTxRef { tx, cmt } = batched_tx; tx.data(cmt).map_or_else( || { diff --git a/crates/namada/src/ledger/mod.rs b/crates/namada/src/ledger/mod.rs index e412fb1d53..4556007637 100644 --- a/crates/namada/src/ledger/mod.rs +++ b/crates/namada/src/ledger/mod.rs @@ -23,6 +23,7 @@ pub use { mod dry_run_tx { use std::cell::RefCell; + use namada_gas::Gas; use namada_sdk::queries::{EncodedResponseQuery, RequestCtx, RequestQuery}; use namada_state::{DBIter, ResultExt, StorageHasher, DB}; use namada_tx::data::{GasLimit, TxResult}; diff --git a/crates/namada/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs b/crates/namada/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs index 86f1767291..897c096ea7 100644 --- a/crates/namada/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs +++ b/crates/namada/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs @@ -543,7 +543,7 @@ where fn validate_tx( &self, - batched_tx: &BatchedTxRef, + batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, _verifiers: &BTreeSet
, ) -> Result<(), Error> { diff --git a/crates/namada/src/ledger/native_vp/ethereum_bridge/nut.rs b/crates/namada/src/ledger/native_vp/ethereum_bridge/nut.rs index a09fee2d50..e97a7cd92c 100644 --- a/crates/namada/src/ledger/native_vp/ethereum_bridge/nut.rs +++ b/crates/namada/src/ledger/native_vp/ethereum_bridge/nut.rs @@ -41,7 +41,7 @@ where fn validate_tx( &self, - _: &BatchedTxRef, + _: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, ) -> Result<(), Self::Error> { diff --git a/crates/namada/src/ledger/native_vp/ethereum_bridge/vp.rs b/crates/namada/src/ledger/native_vp/ethereum_bridge/vp.rs index 060422d3da..e9d9592331 100644 --- a/crates/namada/src/ledger/native_vp/ethereum_bridge/vp.rs +++ b/crates/namada/src/ledger/native_vp/ethereum_bridge/vp.rs @@ -93,7 +93,7 @@ where /// no wasm transactions should be able to modify those keys. fn validate_tx( &self, - _: &BatchedTxRef, + _: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, ) -> Result<(), Self::Error> { diff --git a/crates/namada/src/ledger/native_vp/ibc/mod.rs b/crates/namada/src/ledger/native_vp/ibc/mod.rs index 6e0547feeb..a0e1f41bbd 100644 --- a/crates/namada/src/ledger/native_vp/ibc/mod.rs +++ b/crates/namada/src/ledger/native_vp/ibc/mod.rs @@ -79,7 +79,7 @@ where fn validate_tx( &self, - batched_tx: &BatchedTxRef, + batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, _verifiers: &BTreeSet
, ) -> VpResult<()> { diff --git a/crates/namada/src/ledger/native_vp/masp.rs b/crates/namada/src/ledger/native_vp/masp.rs index 4d24b6f8c8..0cd8d16304 100644 --- a/crates/namada/src/ledger/native_vp/masp.rs +++ b/crates/namada/src/ledger/native_vp/masp.rs @@ -387,7 +387,7 @@ where fn validate_tx( &self, - tx_data: &BatchedTxRef, + tx_data: &BatchedTxRef<'_>, keys_changed: &BTreeSet, _verifiers: &BTreeSet
, ) -> Result<()> { diff --git a/crates/namada/src/ledger/native_vp/mod.rs b/crates/namada/src/ledger/native_vp/mod.rs index 9257ef8b74..e03f187de9 100644 --- a/crates/namada/src/ledger/native_vp/mod.rs +++ b/crates/namada/src/ledger/native_vp/mod.rs @@ -43,7 +43,7 @@ pub trait NativeVp { /// Run the validity predicate fn validate_tx( &self, - batched_tx: &BatchedTxRef, + batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, ) -> std::result::Result<(), Self::Error>; diff --git a/crates/namada/src/ledger/native_vp/multitoken.rs b/crates/namada/src/ledger/native_vp/multitoken.rs index fc08affb15..e8f130d437 100644 --- a/crates/namada/src/ledger/native_vp/multitoken.rs +++ b/crates/namada/src/ledger/native_vp/multitoken.rs @@ -51,7 +51,7 @@ where fn validate_tx( &self, - tx_data: &BatchedTxRef, + tx_data: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, ) -> Result<()> { @@ -280,7 +280,10 @@ where } /// Return if the parameter change was done via a governance proposal - pub fn is_valid_parameter(&self, batched_tx: &BatchedTxRef) -> Result<()> { + pub fn is_valid_parameter( + &self, + batched_tx: &BatchedTxRef<'_>, + ) -> Result<()> { batched_tx.tx.data(batched_tx.cmt).map_or_else( || { Err(native_vp::Error::new_const( diff --git a/crates/namada/src/ledger/native_vp/parameters.rs b/crates/namada/src/ledger/native_vp/parameters.rs index 4747f4e236..1529e7ca3f 100644 --- a/crates/namada/src/ledger/native_vp/parameters.rs +++ b/crates/namada/src/ledger/native_vp/parameters.rs @@ -41,7 +41,7 @@ where fn validate_tx( &self, - batched_tx: &BatchedTxRef, + batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, _verifiers: &BTreeSet
, ) -> Result<()> { diff --git a/crates/namada/src/ledger/pgf/mod.rs b/crates/namada/src/ledger/pgf/mod.rs index 9b268f0a2f..4c29c13b3b 100644 --- a/crates/namada/src/ledger/pgf/mod.rs +++ b/crates/namada/src/ledger/pgf/mod.rs @@ -52,7 +52,7 @@ where fn validate_tx( &self, - batched_tx: &BatchedTxRef, + batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, ) -> Result<()> { @@ -202,7 +202,7 @@ where /// Validate a governance parameter pub fn is_valid_parameter_change( &self, - batched_tx: &BatchedTxRef, + batched_tx: &BatchedTxRef<'_>, ) -> Result<()> { batched_tx.tx.data(batched_tx.cmt).map_or_else( || { diff --git a/crates/namada/src/ledger/pos/vp.rs b/crates/namada/src/ledger/pos/vp.rs index a088676370..5090636058 100644 --- a/crates/namada/src/ledger/pos/vp.rs +++ b/crates/namada/src/ledger/pos/vp.rs @@ -55,7 +55,7 @@ where fn validate_tx( &self, - batched_tx: &BatchedTxRef, + batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, ) -> Result<()> { diff --git a/crates/namada/src/ledger/protocol/mod.rs b/crates/namada/src/ledger/protocol/mod.rs index 8f300f6dc1..0fd4ede9f7 100644 --- a/crates/namada/src/ledger/protocol/mod.rs +++ b/crates/namada/src/ledger/protocol/mod.rs @@ -817,7 +817,7 @@ where /// Apply a transaction going via the wasm environment. Gas will be metered and /// validity predicates will be triggered in the normal way. pub fn apply_wasm_tx<'a, S, D, H, CA>( - batched_tx: BatchedTxRef, + batched_tx: BatchedTxRef<'_>, tx_index: &TxIndex, shell_params: ShellParams<'a, S, D, H, CA>, ) -> Result @@ -939,7 +939,7 @@ where /// Execute a transaction code. Returns verifiers requested by the transaction. #[allow(clippy::too_many_arguments)] fn execute_tx( - batched_tx: &BatchedTxRef, + batched_tx: &BatchedTxRef<'_>, tx_index: &TxIndex, state: &mut S, tx_gas_meter: &RefCell, @@ -1024,7 +1024,7 @@ where fn execute_vps( verifiers: BTreeSet
, keys_changed: BTreeSet, - batched_tx: &BatchedTxRef, + batched_tx: &BatchedTxRef<'_>, tx_index: &TxIndex, state: &S, tx_gas_meter: &TxGasMeter, diff --git a/crates/namada/src/ledger/vp_host_fns.rs b/crates/namada/src/ledger/vp_host_fns.rs index a207309d21..37c2e3c05c 100644 --- a/crates/namada/src/ledger/vp_host_fns.rs +++ b/crates/namada/src/ledger/vp_host_fns.rs @@ -261,7 +261,7 @@ where /// current transaction is being applied. pub fn get_tx_code_hash( gas_meter: &RefCell, - batched_tx: &BatchedTxRef, + batched_tx: &BatchedTxRef<'_>, ) -> EnvResult> { add_gas( gas_meter, @@ -269,7 +269,8 @@ pub fn get_tx_code_hash( .checked_mul(MEMORY_ACCESS_GAS_PER_BYTE) .expect("Consts mul that cannot overflow"), )?; - let hash = batched_tx.tx + let hash = batched_tx + .tx .get_section(batched_tx.cmt.code_sechash()) .and_then(|x| Section::code_sec(x.as_ref())) .map(|x| x.code.hash()); diff --git a/crates/namada/src/vm/host_env.rs b/crates/namada/src/vm/host_env.rs index 5dc5c2c679..c0cda09388 100644 --- a/crates/namada/src/vm/host_env.rs +++ b/crates/namada/src/vm/host_env.rs @@ -389,7 +389,7 @@ pub trait VpEvaluator { &self, ctx: VpCtx<'static, Self::Db, Self::H, Self::Eval, Self::CA>, vp_code_hash: Hash, - input_data: BatchedTxRef, + input_data: BatchedTxRef<'_>, ) -> HostEnvResult; } diff --git a/crates/namada/src/vm/wasm/memory.rs b/crates/namada/src/vm/wasm/memory.rs index 9359c8d7c1..451301ec9b 100644 --- a/crates/namada/src/vm/wasm/memory.rs +++ b/crates/namada/src/vm/wasm/memory.rs @@ -90,7 +90,7 @@ pub struct TxCallInput { /// Write transaction inputs into wasm memory pub fn write_tx_inputs( memory: &wasmer::Memory, - tx_data: &BatchedTxRef, + tx_data: &BatchedTxRef<'_>, ) -> Result { let tx_data_ptr = 0; let tx_data_bytes = tx_data.serialize_to_vec(); diff --git a/crates/namada/src/vm/wasm/run.rs b/crates/namada/src/vm/wasm/run.rs index a80ac954b6..168529a5b3 100644 --- a/crates/namada/src/vm/wasm/run.rs +++ b/crates/namada/src/vm/wasm/run.rs @@ -107,7 +107,10 @@ pub type Result = std::result::Result; /// Returns [`Error::DisallowedTx`] when the given tx is a user tx and its code /// `Hash` is not included in the `tx_allowlist` parameter. -pub fn check_tx_allowed(batched_tx: &BatchedTxRef, storage: &S) -> Result<()> +pub fn check_tx_allowed( + batched_tx: &BatchedTxRef<'_>, + storage: &S, +) -> Result<()> where S: StorageRead, { @@ -282,7 +285,7 @@ where #[allow(clippy::too_many_arguments)] pub fn vp( vp_code_hash: Hash, - batched_tx: &BatchedTxRef, + batched_tx: &BatchedTxRef<'_>, tx_index: &TxIndex, address: &Address, state: &S, @@ -353,7 +356,7 @@ fn run_vp( module: wasmer::Module, vp_imports: wasmer::ImportObject, vp_code_hash: &Hash, - input_data: &BatchedTxRef, + input_data: &BatchedTxRef<'_>, address: &Address, keys_changed: &BTreeSet, verifiers: &BTreeSet
, diff --git a/crates/sdk/src/rpc.rs b/crates/sdk/src/rpc.rs index e63fad1c19..736374ee48 100644 --- a/crates/sdk/src/rpc.rs +++ b/crates/sdk/src/rpc.rs @@ -620,7 +620,7 @@ impl TryFrom for TxResponse { type Error = String; fn try_from(event: Event) -> Result { - let batch = event.read_attribute::().ok(); + let batch = event.read_attribute::>().ok(); let hash = event .read_attribute::() .map_err(|err| err.to_string())?; diff --git a/crates/state/src/write_log.rs b/crates/state/src/write_log.rs index e6af3696f1..82929e02f4 100644 --- a/crates/state/src/write_log.rs +++ b/crates/state/src/write_log.rs @@ -238,7 +238,8 @@ impl WriteLog { pub fn read_pre( &self, key: &storage::Key, - ) -> std::result::Result<((Option<&StorageModification>, u64), arith::Error> { + ) -> std::result::Result<(Option<&StorageModification>, u64), arith::Error> + { for bucket in self .batch_write_log .iter() @@ -256,7 +257,10 @@ impl WriteLog { checked!(key.len() + vp_code_hash.len())? } } as u64; - return Ok((Some(v), checked!(gas * MEMORY_ACCESS_GAS_PER_BYTE)?)); + return Ok(( + Some(v), + checked!(gas * MEMORY_ACCESS_GAS_PER_BYTE)?, + )); } } let gas = key.len() as u64; @@ -295,7 +299,6 @@ impl WriteLog { value: Vec, ) -> Result<(u64, i64)> { let len = value.len(); - let gas = key.len() + len; if self.tx_write_log.tx_temp_log.contains_key(key) { return Err(Error::UpdateTemporaryValue); } @@ -512,9 +515,17 @@ impl WriteLog { if gas_cost.as_ref().is_some() { let event_type = event.kind().to_string(); if !self.tx_write_log.events.tree.contains_key(&event_type) { - self.tx_write_log.events.tree.insert(&event_type, HashSet::new()); + self.tx_write_log + .events + .tree + .insert(&event_type, HashSet::new()); } - self.tx_write_log.events.tree.get_mut(&event_type).unwrap().insert(event); + self.tx_write_log + .events + .tree + .get_mut(&event_type) + .unwrap() + .insert(event); } gas_cost } @@ -681,6 +692,7 @@ impl WriteLog { self.batch_write_log = Default::default(); } + /// Commit the tx write log to the block write log. pub fn commit_tx(&mut self) { // First precommit everything self.precommit_tx(); diff --git a/crates/tx/src/data/mod.rs b/crates/tx/src/data/mod.rs index 939f08381b..0525ccf0db 100644 --- a/crates/tx/src/data/mod.rs +++ b/crates/tx/src/data/mod.rs @@ -163,6 +163,7 @@ pub fn hash_tx(tx_bytes: &[u8]) -> Hash { Hash(*digest.as_ref()) } +/// The set of inner tx results indexed by the inner tx hash // The generic is only used to return typed errors in protocol for error // management with regards to replay protection, whereas for logging we use // strings @@ -207,7 +208,7 @@ where { type Value = BatchResults; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("BatchResult") } @@ -263,6 +264,7 @@ impl Default for TxResult { } impl TxResult { + /// Convert the batched result to a string pub fn to_result_string(self) -> TxResult { let mut batch_results: BTreeMap> = BTreeMap::new(); diff --git a/crates/tx/src/data/wrapper.rs b/crates/tx/src/data/wrapper.rs index 700728e2df..97d7a24566 100644 --- a/crates/tx/src/data/wrapper.rs +++ b/crates/tx/src/data/wrapper.rs @@ -146,7 +146,7 @@ impl TryFrom for Gas { BorshSchema, Serialize, Deserialize, - PartialEq + PartialEq, )] pub struct WrapperTx { /// The fee to be paid for including the tx diff --git a/crates/tx/src/types.rs b/crates/tx/src/types.rs index 28f20e4e3c..e559c90a30 100644 --- a/crates/tx/src/types.rs +++ b/crates/tx/src/types.rs @@ -935,6 +935,7 @@ impl TxCommitments { &self.memo_hash } + /// Hash the commitments to the transaction's sections pub fn hash<'a>(&self, hasher: &'a mut Sha256) -> &'a mut Sha256 { hasher.update(self.serialize_to_vec()); hasher @@ -1691,26 +1692,31 @@ impl Tx { self } + /// Get the references to the inner transactions pub fn commitments(&self) -> &HashSet { &self.header.batch } + /// Get the reference to the first inner transaction pub fn first_commitments(&self) -> Option<&TxCommitments> { self.header.batch.first() } + /// Creates a batched tx from one or more inner transactions pub fn batch_tx(self, cmt: TxCommitments) -> BatchedTx { BatchedTx { tx: self, cmt } } + /// Creates a batched tx along with the reference to the first inner tx #[cfg(any(test, feature = "testing"))] - pub fn batch_ref_first_tx(&self) -> BatchedTxRef { + pub fn batch_ref_first_tx(&self) -> BatchedTxRef<'_> { BatchedTxRef { tx: self, cmt: self.first_commitments().unwrap(), } } + /// Creates a batched tx along with a copy of the first inner tx #[cfg(any(test, feature = "testing"))] pub fn batch_first_tx(self) -> BatchedTx { let cmt = self.first_commitments().unwrap().to_owned(); @@ -1719,6 +1725,7 @@ impl Tx { } impl<'tx> Tx { + /// Creates a batched tx along with the reference to one or more inner txs pub fn batch_ref_tx( &'tx self, cmt: &'tx TxCommitments, @@ -1742,7 +1749,9 @@ impl<'tx> Tx { Hash, )] pub enum IndexedTxType { + /// Wrapper (outer) tx Wrapper, + /// Inner tx with commitment Inner(TxCommitments), } @@ -1784,7 +1793,9 @@ impl Default for IndexedTx { /// transaction of the batch #[derive(Debug, BorshSerialize)] pub struct BatchedTxRef<'tx> { + /// The transaction pub tx: &'tx Tx, + /// The reference to the inner transaction pub cmt: &'tx TxCommitments, } @@ -1794,12 +1805,15 @@ pub struct BatchedTxRef<'tx> { Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize, )] pub struct BatchedTx { + /// The transaction pub tx: Tx, + /// The reference to the inner transaction pub cmt: TxCommitments, } impl BatchedTx { - pub fn to_ref(&self) -> BatchedTxRef { + /// Convert owned version to a referenced one + pub fn to_ref(&self) -> BatchedTxRef<'_> { BatchedTxRef { tx: &self.tx, cmt: &self.cmt, diff --git a/crates/vp_env/src/lib.rs b/crates/vp_env/src/lib.rs index 9b1e87147c..b3115443dc 100644 --- a/crates/vp_env/src/lib.rs +++ b/crates/vp_env/src/lib.rs @@ -117,7 +117,7 @@ where fn eval( &self, vp_code: Hash, - input_data: BatchedTxRef, + input_data: BatchedTxRef<'_>, ) -> Result<(), namada_storage::Error>; /// Get a tx hash @@ -126,7 +126,7 @@ where /// Get the masp tx part of the shielded action fn get_shielded_action( &self, - batched_tx: &BatchedTxRef, + batched_tx: &BatchedTxRef<'_>, ) -> Result { let data = batched_tx .tx diff --git a/crates/vp_prelude/src/lib.rs b/crates/vp_prelude/src/lib.rs index c75b792a23..80dada3f19 100644 --- a/crates/vp_prelude/src/lib.rs +++ b/crates/vp_prelude/src/lib.rs @@ -367,7 +367,7 @@ impl<'view> VpEnv<'view> for Ctx { fn eval( &self, vp_code_hash: Hash, - input_data: BatchedTxRef, + input_data: BatchedTxRef<'_>, ) -> Result<(), StorageError> { let input_data_bytes = input_data.serialize_to_vec();