Skip to content

Commit

Permalink
Merge branch 'tiago/test-batch-tx-events' (#3401)
Browse files Browse the repository at this point in the history
* tiago/test-batch-tx-events:
  Changelog for #3401
  Test inner tx results from batch tx
  Remove redundant TODO
  • Loading branch information
brentstone committed Jun 12, 2024
2 parents 2089cf1 + 486e404 commit 3a02034
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/unreleased/testing/3401-test-batch-tx-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Adds additional test coverage to batch tx events emission, to make
sure we correctly build a batch of inner tx events from a batched tx.
([\#3401](https://github.com/anoma/namada/pull/3401))
161 changes: 161 additions & 0 deletions crates/node/src/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5796,4 +5796,165 @@ mod test_finalize_block {
assert!(!shell.state.has_key(&key.parse().unwrap()).unwrap());
}
}

#[test]
fn test_multiple_events_from_batch_tx_all_valid() {
let (mut shell, _, _, _) = setup();

let sk = wallet::defaults::bertha_keypair();

let batch_tx = {
let mut batch =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: DenominatedAmount::native(
1.into(),
),
token: shell.state.in_mem().native_token.clone(),
},
sk.ref_to(),
WRAPPER_GAS_LIMIT.into(),
))));
batch.header.chain_id = shell.chain_id.clone();
batch.header.atomic = false;

// append first inner tx to batch
batch.set_code(Code::new(TestWasms::TxNoOp.read_bytes(), None));
batch.set_data(Data::new("bing".as_bytes().to_owned()));

// append second inner tx to batch
batch.push_default_inner_tx();

batch.set_code(Code::new(TestWasms::TxNoOp.read_bytes(), None));
batch.set_data(Data::new("bong".as_bytes().to_owned()));

// sign the batch of txs
batch.sign_raw(
vec![sk.clone()],
vec![sk.ref_to()].into_iter().collect(),
None,
);
batch.sign_wrapper(sk);

batch
};

let processed_txs = vec![ProcessedTx {
tx: batch_tx.to_bytes().into(),
result: TxResult {
code: ResultCode::Ok.into(),
info: "".into(),
},
}];

let mut events = shell
.finalize_block(FinalizeBlock {
txs: processed_txs,
..Default::default()
})
.expect("Test failed");

// one top level event
assert_eq!(events.len(), 1);
let event = events.remove(0);

// multiple tx results (2)
let tx_results = event.read_attribute::<Batch<'_>>().unwrap();
assert_eq!(tx_results.batch_results.0.len(), 2);

// all txs should have succeeded
assert!(
tx_results
.batch_results
.0
.values()
.all(|result| result.is_ok())
);
}

#[test]
fn test_multiple_events_from_batch_tx_one_valid_other_invalid() {
let (mut shell, _, _, _) = setup();

let sk = wallet::defaults::bertha_keypair();

let batch_tx = {
let mut batch =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: DenominatedAmount::native(
1.into(),
),
token: shell.state.in_mem().native_token.clone(),
},
sk.ref_to(),
WRAPPER_GAS_LIMIT.into(),
))));
batch.header.chain_id = shell.chain_id.clone();
batch.header.atomic = false;

// append first inner tx to batch (this one is valid)
batch.set_code(Code::new(TestWasms::TxNoOp.read_bytes(), None));
batch.set_data(Data::new("bing".as_bytes().to_owned()));

// append second inner tx to batch (this one is invalid, because
// we pass the wrong data)
batch.push_default_inner_tx();

batch.set_code(Code::new(
TestWasms::TxWriteStorageKey.read_bytes(),
None,
));
batch.set_data(Data::new("bong".as_bytes().to_owned()));

// sign the batch of txs
batch.sign_raw(
vec![sk.clone()],
vec![sk.ref_to()].into_iter().collect(),
None,
);
batch.sign_wrapper(sk);

batch
};

let processed_txs = vec![ProcessedTx {
tx: batch_tx.to_bytes().into(),
result: TxResult {
code: ResultCode::Ok.into(),
info: "".into(),
},
}];

let mut events = shell
.finalize_block(FinalizeBlock {
txs: processed_txs,
..Default::default()
})
.expect("Test failed");

// one top level event
assert_eq!(events.len(), 1);
let event = events.remove(0);

// multiple tx results (2)
let tx_results = event.read_attribute::<Batch<'_>>().unwrap();
assert_eq!(tx_results.batch_results.0.len(), 2);

// check one succeeded and the other failed
assert!(
tx_results
.batch_results
.0
.values()
.any(|result| result.is_ok())
);
assert!(
tx_results
.batch_results
.0
.values()
.any(|result| result.is_err())
);
}
}
1 change: 0 additions & 1 deletion crates/tx/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ impl<T> Default for ExtendedTxResult<T> {
}

/// Transaction application result
// TODO derive BorshSchema after <https://github.com/near/borsh-rs/issues/82>
#[derive(
Clone, Debug, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
Expand Down

0 comments on commit 3a02034

Please sign in to comment.