Skip to content

Commit

Permalink
txpool: TestApi best_block fix (paritytech#12992)
Browse files Browse the repository at this point in the history
This is a minor clean up.

There was a minor bug in TestApi::add_block: the best block flag for
given number was not cleared when the new best block with the same
number was added.

The usage of is_best_block argument in add_block method in txpool tests
were aligned with the BestBlock event.
  • Loading branch information
michalkucharczyk authored and ltfschoen committed Feb 22, 2023
1 parent 75b4da6 commit 49b3990
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
25 changes: 15 additions & 10 deletions client/transaction-pool/tests/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,18 @@ fn block_event(header: Header) -> ChainEvent<Block> {
}

fn block_event_with_retracted(
header: Header,
new_best_block_header: Header,
retracted_start: Hash,
api: &TestApi,
) -> ChainEvent<Block> {
let tree_route =
api.tree_route(retracted_start, header.parent_hash).expect("Tree route exists");
let tree_route = api
.tree_route(retracted_start, new_best_block_header.parent_hash)
.expect("Tree route exists");

ChainEvent::NewBestBlock { hash: header.hash(), tree_route: Some(Arc::new(tree_route)) }
ChainEvent::NewBestBlock {
hash: new_best_block_header.hash(),
tree_route: Some(Arc::new(tree_route)),
}
}

#[test]
Expand Down Expand Up @@ -272,7 +276,7 @@ fn should_resubmit_from_retracted_during_maintenance() {
assert_eq!(pool.status().ready, 1);

let header = api.push_block(1, vec![], true);
let fork_header = api.push_block(1, vec![], false);
let fork_header = api.push_block(1, vec![], true);

let event = block_event_with_retracted(header, fork_header.hash(), pool.api());

Expand All @@ -290,7 +294,7 @@ fn should_not_resubmit_from_retracted_during_maintenance_if_tx_is_also_in_enacte
assert_eq!(pool.status().ready, 1);

let header = api.push_block(1, vec![xt.clone()], true);
let fork_header = api.push_block(1, vec![xt], false);
let fork_header = api.push_block(1, vec![xt], true);

let event = block_event_with_retracted(header, fork_header.hash(), pool.api());

Expand All @@ -309,7 +313,7 @@ fn should_not_retain_invalid_hashes_from_retracted() {
assert_eq!(pool.status().ready, 1);

let header = api.push_block(1, vec![], true);
let fork_header = api.push_block(1, vec![xt.clone()], false);
let fork_header = api.push_block(1, vec![xt.clone()], true);
api.add_invalid(&xt);

let event = block_event_with_retracted(header, fork_header.hash(), pool.api());
Expand Down Expand Up @@ -649,7 +653,7 @@ fn prune_and_retract_tx_at_same_time() {

// Block B2
let b2 = {
let header = pool.api().push_block(2, vec![from_alice.clone()], false);
let header = pool.api().push_block(2, vec![from_alice.clone()], true);
assert_eq!(pool.status().ready, 0);

let event = block_event_with_retracted(header.clone(), b1, pool.api());
Expand Down Expand Up @@ -726,7 +730,8 @@ fn resubmit_tx_of_fork_that_is_not_part_of_retracted() {

// Block D2
{
let header = pool.api().push_block(2, vec![], false);
//push new best block
let header = pool.api().push_block(2, vec![], true);
let event = block_event_with_retracted(header, d0, pool.api());
block_on(pool.maintain(event));
assert_eq!(pool.status().ready, 2);
Expand Down Expand Up @@ -1036,7 +1041,7 @@ fn finalized_only_handled_correctly() {
.expect("1. Imported");
assert_eq!(pool.status().ready, 1);

let header = api.push_block(1, vec![xt], false);
let header = api.push_block(1, vec![xt], true);

let event =
ChainEvent::Finalized { hash: header.clone().hash(), tree_route: Arc::from(vec![]) };
Expand Down
12 changes: 12 additions & 0 deletions test-utils/runtime/transaction-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ impl TestApi {

let mut chain = self.chain.write();
chain.block_by_hash.insert(hash, block.clone());

if is_best_block {
chain
.block_by_number
.entry(*block_number)
.or_default()
.iter_mut()
.for_each(|x| {
x.1 = IsBestBlock::No;
});
}

chain
.block_by_number
.entry(*block_number)
Expand Down

0 comments on commit 49b3990

Please sign in to comment.