Skip to content

Commit

Permalink
test: queue items sort by order
Browse files Browse the repository at this point in the history
  • Loading branch information
jjyr authored and Flouse committed Feb 21, 2022
1 parent 1b66784 commit eb70561
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions crates/mem-pool/src/fee/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,81 @@ mod tests {
}
}

#[test]
fn test_sort_txs_by_order() {
let mut queue = FeeQueue::new();

let store = Store::open_tmp().expect("open store");
setup_genesis(&store);
{
let db = store.begin_transaction();
let genesis = db.get_tip_block().expect("tip");
assert_eq!(genesis.raw().number().unpack(), 0);
let mut state = db.state_tree(StateContext::AttachBlock(1)).expect("state");

// create accounts
for i in 0..4 {
state.create_account(H256::from_u32(i)).unwrap();
}

db.commit().expect("commit");
}
let snap = store.get_snapshot();

let entry1 = FeeEntry {
item: FeeItem::Tx(Default::default()),
fee_rate: 0,
cycles_limit: 1000,
sender: 2,
order: queue.len(),
};

queue.add(entry1);

let entry2 = FeeEntry {
item: FeeItem::Tx(Default::default()),
fee_rate: 0,
cycles_limit: 100,
sender: 3,
order: queue.len(),
};

queue.add(entry2);

let entry3 = FeeEntry {
item: FeeItem::Tx(Default::default()),
fee_rate: 0,
cycles_limit: 500,
sender: 4,
order: queue.len(),
};

queue.add(entry3);

let entry4 = FeeEntry {
item: FeeItem::Withdrawal(Default::default()),
fee_rate: 1,
cycles_limit: 1001,
sender: 5,
order: queue.len(),
};

queue.add(entry4);

let mem_store = MemStore::new(snap);
let tree = mem_store.state().unwrap();

// fetch 5
{
let items = queue.fetch(&tree, 5).expect("fetch");
assert_eq!(items.len(), 4);
assert_eq!(items[0].sender, 5);
assert_eq!(items[1].sender, 2);
assert_eq!(items[2].sender, 3);
assert_eq!(items[3].sender, 4);
}
}

#[test]
fn test_insert_distinct_nonce() {
let mut queue = FeeQueue::new();
Expand Down

0 comments on commit eb70561

Please sign in to comment.