Skip to content

Commit

Permalink
test/shell/finalize_block: add some txs to DB commit test
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Jul 13, 2023
1 parent f1669e7 commit f30e158
Showing 1 changed file with 115 additions and 96 deletions.
211 changes: 115 additions & 96 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,36 +1044,9 @@ mod test_finalize_block {

// create some wrapper txs
for i in 1u64..5 {
let mut wrapper =
Tx::new(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount: MIN_FEE_AMOUNT,
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Epoch(0),
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
))));
wrapper.header.chain_id = shell.chain_id.clone();
wrapper.set_data(Data::new("wasm_code".as_bytes().to_owned()));
wrapper.set_code(Code::new(
format!("transaction data: {}", i).as_bytes().to_owned(),
));
wrapper.add_section(Section::Signature(Signature::new(
wrapper.sechashes(),
&keypair,
)));
let (wrapper, processed_tx) = mk_wrapper_tx(&shell, &keypair);
if i > 1 {
processed_txs.push(ProcessedTx {
tx: wrapper.to_bytes(),
result: TxResult {
code: u32::try_from(i.rem_euclid(2))
.expect("Test failed"),
info: "".into(),
},
});
processed_txs.push(processed_tx);
} else {
shell.enqueue_tx(wrapper.clone());
}
Expand Down Expand Up @@ -1239,75 +1212,14 @@ mod test_finalize_block {
.unwrap();

// create two decrypted txs
let tx_code = TestWasms::TxNoOp.read_bytes();
for i in 0..2 {
let mut outer_tx =
Tx::new(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount: MIN_FEE_AMOUNT,
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Epoch(0),
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
))));
outer_tx.header.chain_id = shell.chain_id.clone();
outer_tx.set_code(Code::new(tx_code.clone()));
outer_tx.set_data(Data::new(
format!("Decrypted transaction data: {}", i)
.as_bytes()
.to_owned(),
));
shell.enqueue_tx(outer_tx.clone());
outer_tx.update_header(TxType::Decrypted(DecryptedTx::Decrypted {
#[cfg(not(feature = "mainnet"))]
has_valid_pow: false,
}));
outer_tx.decrypt(<EllipticCurve as PairingEngine>::G2Affine::prime_subgroup_generator())
.expect("Test failed");
processed_txs.push(ProcessedTx {
tx: outer_tx.to_bytes(),
result: TxResult {
code: ErrorCodes::Ok.into(),
info: "".into(),
},
});
for _ in 0..2 {
processed_txs.push(mk_decrypted_tx(&mut shell, &keypair));
}
// create two wrapper txs
for i in 0..2 {
let mut wrapper_tx =
Tx::new(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount: MIN_FEE_AMOUNT,
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Epoch(0),
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
))));
wrapper_tx.header.chain_id = shell.chain_id.clone();
wrapper_tx.set_code(Code::new("wasm_code".as_bytes().to_owned()));
wrapper_tx.set_data(Data::new(
format!("Encrypted transaction data: {}", i)
.as_bytes()
.to_owned(),
));
wrapper_tx.add_section(Section::Signature(Signature::new(
wrapper_tx.sechashes(),
&keypair,
)));
valid_txs.push(wrapper_tx.clone());
processed_txs.push(ProcessedTx {
tx: wrapper_tx.to_bytes(),
result: TxResult {
code: ErrorCodes::Ok.into(),
info: "".into(),
},
});
for _ in 0..2 {
let (tx, processed_tx) = mk_wrapper_tx(&shell, &keypair);
valid_txs.push(tx.clone());
processed_txs.push(processed_tx);
}
// Put the wrapper txs in front of the decrypted txs
processed_txs.rotate_left(2);
Expand Down Expand Up @@ -1726,6 +1638,21 @@ mod test_finalize_block {
shell.wl_storage.storage.next_epoch_min_start_height = BlockHeight(5);
shell.wl_storage.storage.next_epoch_min_start_time = DateTimeUtc::now();

let txs_key = gen_keypair();
// Add unshielded balance for fee payment
let balance_key = token::balance_key(
&shell.wl_storage.storage.native_token,
&Address::from(&txs_key.ref_to()),
);
shell
.wl_storage
.storage
.write(
&balance_key,
Amount::native_whole(1000).try_to_vec().unwrap(),
)
.unwrap();

// Add a proposal to be executed on next epoch change.
let mut add_proposal = |proposal_id, vote| {
let validator = shell.mode.get_validator_address().unwrap().clone();
Expand Down Expand Up @@ -1821,7 +1748,20 @@ mod test_finalize_block {
// Need to supply a proposer address and votes to flow through the
// inflation code
for _ in 0..20 {
// Add some txs
let mut txs = vec![];
// create two decrypted txs
for _ in 0..2 {
txs.push(mk_decrypted_tx(&mut shell, &txs_key));
}
// create two wrapper txs
for _ in 0..2 {
let (_tx, processed_tx) = mk_wrapper_tx(&shell, &txs_key);
txs.push(processed_tx);
}

let req = FinalizeBlock {
txs,
proposer_address: proposer_address.clone(),
votes: votes.clone(),
..Default::default()
Expand Down Expand Up @@ -3548,4 +3488,83 @@ mod test_finalize_block {
control_receiver.recv().await.expect("Test failed");
assert_eq!(u64::from(cmd.min_confirmations), 42);
}

/// Make a wrapper tx and a processed tx from the wrapped tx that can be
/// added to `FinalizeBlock` request.
fn mk_wrapper_tx(
shell: &TestShell,
keypair: &common::SecretKey,
) -> (Tx, ProcessedTx) {
let mut wrapper_tx =
Tx::new(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount: MIN_FEE_AMOUNT,
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Epoch(0),
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
))));
wrapper_tx.header.chain_id = shell.chain_id.clone();
wrapper_tx.set_code(Code::new("wasm_code".as_bytes().to_owned()));
wrapper_tx.set_data(Data::new(
"Encrypted transaction data".as_bytes().to_owned(),
));
wrapper_tx.add_section(Section::Signature(Signature::new(
wrapper_tx.sechashes(),
&keypair,
)));
let tx = wrapper_tx.to_bytes();
(
wrapper_tx,
ProcessedTx {
tx,
result: TxResult {
code: ErrorCodes::Ok.into(),
info: "".into(),
},
},
)
}

/// Make a wrapper tx and a processed tx from the wrapped tx that can be
/// added to `FinalizeBlock` request.
fn mk_decrypted_tx(
shell: &mut TestShell,
keypair: &common::SecretKey,
) -> ProcessedTx {
let tx_code = TestWasms::TxNoOp.read_bytes();
let mut outer_tx = Tx::new(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount: MIN_FEE_AMOUNT,
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Epoch(0),
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
))));
outer_tx.header.chain_id = shell.chain_id.clone();
outer_tx.set_code(Code::new(tx_code.clone()));
outer_tx.set_data(Data::new(
"Decrypted transaction data".as_bytes().to_owned(),
));
shell.enqueue_tx(outer_tx.clone());
outer_tx.update_header(TxType::Decrypted(DecryptedTx::Decrypted {
#[cfg(not(feature = "mainnet"))]
has_valid_pow: false,
}));
outer_tx.decrypt(<EllipticCurve as PairingEngine>::G2Affine::prime_subgroup_generator())
.expect("Test failed");
ProcessedTx {
tx: outer_tx.to_bytes(),
result: TxResult {
code: ErrorCodes::Ok.into(),
info: "".into(),
},
}
}
}

0 comments on commit f30e158

Please sign in to comment.