Skip to content

Commit

Permalink
fix: process microblock receipts with anchor block
Browse files Browse the repository at this point in the history
Fix some integrations tests related to these.
  • Loading branch information
obycode committed Jun 21, 2023
1 parent 6812075 commit 02a390c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/chainstate/stacks/db/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5648,7 +5648,7 @@ impl StacksChainState {
// Check withdrawal state merkle root
// Process withdrawal events
let withdrawal_tree =
create_withdrawal_merkle_tree(&mut tx_receipts, block.header.total_work.work);
create_withdrawal_merkle_tree(tx_receipts.iter_mut(), block.header.total_work.work);
let withdrawal_root_hash = withdrawal_tree.root();

if withdrawal_root_hash != block.header.withdrawal_merkle_root {
Expand Down
6 changes: 5 additions & 1 deletion src/chainstate/stacks/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,8 +1533,12 @@ impl StacksBlockBuilder {
self.header.tx_merkle_root = tx_merkle_root;
self.header.state_index_root = state_root_hash;

let all_receipts_iter = self
.microblock_tx_receipts
.iter_mut()
.chain(self.tx_receipts.iter_mut());
let withdrawal_tree =
create_withdrawal_merkle_tree(&mut self.tx_receipts, self.header.total_work.work);
create_withdrawal_merkle_tree(all_receipts_iter, self.header.total_work.work);
let withdrawal_merkle_root = withdrawal_tree.root();
self.header.withdrawal_merkle_root = withdrawal_merkle_root;

Expand Down
12 changes: 6 additions & 6 deletions src/clarity_vm/withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,13 @@ pub fn convert_withdrawal_key_to_bytes(key: &Value) -> Vec<u8> {
/// The order of withdrawal events in the transaction receipts will determine the withdrawal IDs
/// that correspond to each event. These IDs are used to generate the withdrawal key that is
/// ultimately inserted in the withdrawal Merkle tree.
pub fn generate_withdrawal_keys(
tx_receipts: &mut [StacksTransactionReceipt],
pub fn generate_withdrawal_keys<'a>(
tx_receipts: impl Iterator<Item = &'a mut StacksTransactionReceipt>,
block_height: u64,
) -> Vec<Vec<u8>> {
let mut items = Vec::new();
let mut withdrawal_id = 0;
for receipt in tx_receipts.iter_mut() {
for receipt in tx_receipts {
for event in receipt.events.iter_mut() {
if let Some(key) = generate_key_from_event(event, withdrawal_id, block_height) {
withdrawal_id += 1;
Expand All @@ -284,8 +284,8 @@ pub fn generate_withdrawal_keys(
/// The order of the transaction receipts will affect the final tree.
/// The generated withdrawal IDs are inserted into the supplied withdraw events
/// (this is why the receipts are supplied as a mutable argument).
pub fn create_withdrawal_merkle_tree(
tx_receipts: &mut [StacksTransactionReceipt],
pub fn create_withdrawal_merkle_tree<'a>(
tx_receipts: impl Iterator<Item = &'a mut StacksTransactionReceipt>,
block_height: u64,
) -> MerkleTree<Sha512Trunc256Sum> {
// The specific keys generated is dependent on the order of the provided transaction receipts
Expand Down Expand Up @@ -433,7 +433,7 @@ mod test {

let mut receipts = vec![withdrawal_receipt];
// supplying block height = 0 is okay in tests, because block height is only used for logging
let withdrawal_tree = create_withdrawal_merkle_tree(receipts.as_mut(), 0);
let withdrawal_tree = create_withdrawal_merkle_tree(receipts.iter_mut(), 0);
let root_hash = withdrawal_tree.root();

// manually construct the expected Merkle tree
Expand Down
36 changes: 18 additions & 18 deletions testnet/stacks-node/src/tests/l1_observer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ fn l1_deposit_and_withdraw_asset_integration_test() {
// Withdraw nft-token from subnet contract on L2
submit_tx(&l2_rpc_origin, &l2_withdraw_nft_tx);

wait_for_next_stacks_block(&sortition_db);
wait_for_next_stacks_block(&sortition_db);
wait_for_next_stacks_block(&sortition_db);

Expand Down Expand Up @@ -1134,7 +1135,7 @@ fn l1_deposit_and_withdraw_asset_integration_test() {
tx_index: 0,
};
let mut receipts = vec![withdrawal_receipt];
let withdrawal_tree = create_withdrawal_merkle_tree(&mut receipts, withdrawal_height);
let withdrawal_tree = create_withdrawal_merkle_tree(receipts.iter_mut(), withdrawal_height);
let root_hash = withdrawal_tree.root().as_bytes().to_vec();

let ft_withdrawal_key =
Expand Down Expand Up @@ -1516,12 +1517,11 @@ fn l1_deposit_and_withdraw_stx_integration_test() {
// withdraw stx from L2
submit_tx(&l2_rpc_origin, &l2_withdraw_stx_tx);

// Wait to give the run loop time to mine a block
// Wait to give the run loop time to mine an anchor block
wait_for_next_stacks_block(&sortition_db);
wait_for_next_stacks_block(&sortition_db);
wait_for_next_stacks_block(&sortition_db);

// TODO: here, read the withdrawal events to get the withdrawal ID, and figure out the
// block height to query.
let block_data = test_observer::get_blocks();
let mut withdraw_events = filter_map_events(&block_data, |height, event| {
let ev_type = event.get("type").unwrap().as_str().unwrap();
Expand Down Expand Up @@ -1644,7 +1644,7 @@ fn l1_deposit_and_withdraw_stx_integration_test() {
let mut receipts = vec![withdrawal_receipt];

// okay to pass a zero block height in tests: the block height parameter is only used for logging
let withdrawal_tree = create_withdrawal_merkle_tree(&mut receipts, withdrawal_height);
let withdrawal_tree = create_withdrawal_merkle_tree(receipts.iter_mut(), withdrawal_height);
let root_hash = withdrawal_tree.root().as_bytes().to_vec();

// okay to pass a zero block height in tests: the block height parameter is only used for logging
Expand Down Expand Up @@ -1827,16 +1827,16 @@ fn l2_simple_contract_calls() {
wait_for_next_stacks_block(&sortition_db);

// Check for two calls to "return-one".
let small_contract_calls = select_transactions_where(
&test_observer::get_blocks(),
|transaction| match &transaction.payload {
TransactionPayload::ContractCall(contract) => {
contract.contract_name == ContractName::try_from("small-contract").unwrap()
&& contract.function_name == ClarityName::try_from("return-one").unwrap()
let small_contract_calls =
select_transactions_where(&test_observer::get_microblocks(), |transaction| {
match &transaction.payload {
TransactionPayload::ContractCall(contract) => {
contract.contract_name == ContractName::try_from("small-contract").unwrap()
&& contract.function_name == ClarityName::try_from("return-one").unwrap()
}
_ => false,
}
_ => false,
},
);
});
assert_eq!(small_contract_calls.len(), 2);
termination_switch.store(false, Ordering::SeqCst);
stacks_l1_controller.kill_process();
Expand Down Expand Up @@ -2439,7 +2439,7 @@ fn nft_deposit_and_withdraw_integration_test() {
tx_index: 0,
};
let withdrawal_tree =
create_withdrawal_merkle_tree(&mut vec![withdrawal_receipt], withdrawal_height);
create_withdrawal_merkle_tree(vec![withdrawal_receipt].iter_mut(), withdrawal_height);
let root_hash = withdrawal_tree.root().as_bytes().to_vec();

let l1_native_nft_withdrawal_key =
Expand Down Expand Up @@ -3041,7 +3041,7 @@ fn nft_deposit_failure_and_refund_integration_test() {
tx_index: 0,
};
let withdrawal_tree =
create_withdrawal_merkle_tree(&mut vec![withdrawal_receipt], withdrawal_height);
create_withdrawal_merkle_tree(vec![withdrawal_receipt].iter_mut(), withdrawal_height);
let root_hash = withdrawal_tree.root().as_bytes().to_vec();

let l1_native_nft_withdrawal_key =
Expand Down Expand Up @@ -3624,7 +3624,7 @@ fn ft_deposit_and_withdraw_integration_test() {
tx_index: 0,
};
let withdrawal_tree =
create_withdrawal_merkle_tree(&mut vec![withdrawal_receipt], withdrawal_height);
create_withdrawal_merkle_tree(vec![withdrawal_receipt].iter_mut(), withdrawal_height);
let root_hash = withdrawal_tree.root().as_bytes().to_vec();

let ft_withdrawal_key =
Expand Down Expand Up @@ -4137,7 +4137,7 @@ fn ft_deposit_failure_and_refund_integration_test() {
tx_index: 0,
};
let withdrawal_tree =
create_withdrawal_merkle_tree(&mut vec![withdrawal_receipt], withdrawal_height);
create_withdrawal_merkle_tree(vec![withdrawal_receipt].iter_mut(), withdrawal_height);
let root_hash = withdrawal_tree.root().as_bytes().to_vec();

let ft_withdrawal_key =
Expand Down

0 comments on commit 02a390c

Please sign in to comment.