Skip to content

Commit c1d9640

Browse files
Seulgi Kimsgkim126
authored andcommitted
Test whether the mempool categorizes transactions on removing
1 parent 932e6d1 commit c1d9640

File tree

1 file changed

+73
-3
lines changed

1 file changed

+73
-3
lines changed

core/src/miner/mem_pool.rs

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ impl MemPool {
979979
pub mod test {
980980
use std::cmp::Ordering;
981981

982-
use crate::client::{Balance, RegularKeyOwner, Seq, TestBlockChainClient};
982+
use crate::client::{Balance, ChainInfo, RegularKeyOwner, Seq, TestBlockChainClient};
983983
use ckey::{Generator, KeyPair, Random};
984984
use ctypes::transaction::{Action, AssetMintOutput, Transaction};
985985
use primitives::H160;
@@ -1490,7 +1490,7 @@ pub mod test {
14901490
assert_eq!(mem_pool_recovered.future, mem_pool.future);
14911491
}
14921492

1493-
fn create_mempool_input_with_pay(seq: u64, keypair: KeyPair, timelock: TxTimelock) -> MemPoolInput {
1493+
fn create_signed_pay(seq: u64, keypair: KeyPair) -> SignedTransaction {
14941494
let receiver = 1u64.into();
14951495
let tx = Transaction {
14961496
seq,
@@ -1501,8 +1501,11 @@ pub mod test {
15011501
quantity: 100_000,
15021502
},
15031503
};
1504-
let signed = SignedTransaction::new_with_sign(tx, keypair.private());
1504+
SignedTransaction::new_with_sign(tx, keypair.private())
1505+
}
15051506

1507+
fn create_mempool_input_with_pay(seq: u64, keypair: KeyPair, timelock: TxTimelock) -> MemPoolInput {
1508+
let signed = create_signed_pay(seq, keypair);
15061509
MemPoolInput::new(signed, TxOrigin::Local, timelock)
15071510
}
15081511

@@ -1535,4 +1538,71 @@ pub mod test {
15351538
let item = MemPoolItem::new(signed, TxOrigin::Local, 0, 0, 0, timelock);
15361539
TransactionOrder::for_transaction(&item, 0)
15371540
}
1541+
1542+
#[test]
1543+
fn transactions_are_moved_to_future_queue_if_the_preceding_one_removed() {
1544+
//setup test_client
1545+
let test_client = TestBlockChainClient::new();
1546+
1547+
let db = Arc::new(kvdb_memorydb::create(crate::db::NUM_COLUMNS.unwrap_or(0)));
1548+
let mut mem_pool = MemPool::with_limits(8192, usize::max_value(), 3, db.clone());
1549+
1550+
let fetch_account = |p: &Public| -> AccountDetails {
1551+
let address = public_to_address(p);
1552+
let a = test_client.latest_regular_key_owner(&address).unwrap_or(address);
1553+
AccountDetails {
1554+
seq: test_client.latest_seq(&a),
1555+
balance: test_client.latest_balance(&a),
1556+
}
1557+
};
1558+
let keypair = Random.generate().unwrap();
1559+
let address = public_to_address(keypair.public());
1560+
println!("! {}", address);
1561+
test_client.set_balance(address, 1_000_000_000_000);
1562+
assert_eq!(1_000_000_000_000, test_client.latest_balance(&address));
1563+
let no_timelock = TxTimelock {
1564+
block: None,
1565+
timestamp: None,
1566+
};
1567+
1568+
let inserted_block_number = 1;
1569+
let inserted_timestamp = 100;
1570+
let inputs = vec![
1571+
create_mempool_input_with_pay(0, keypair, no_timelock),
1572+
create_mempool_input_with_pay(1, keypair, no_timelock),
1573+
create_mempool_input_with_pay(2, keypair, no_timelock),
1574+
];
1575+
let result = mem_pool.add(inputs, inserted_block_number, inserted_timestamp, &fetch_account);
1576+
assert_eq!(
1577+
vec![
1578+
Ok(TransactionImportResult::Current),
1579+
Ok(TransactionImportResult::Current),
1580+
Ok(TransactionImportResult::Current)
1581+
],
1582+
result
1583+
);
1584+
1585+
assert_eq!(
1586+
vec![create_signed_pay(0, keypair), create_signed_pay(1, keypair), create_signed_pay(2, keypair),],
1587+
mem_pool.top_transactions(std::usize::MAX, None, 0..std::u64::MAX).transactions
1588+
);
1589+
1590+
assert_eq!(Vec::<SignedTransaction>::default(), mem_pool.future_transactions());
1591+
1592+
let best_block_number = test_client.chain_info().best_block_number;
1593+
let best_block_timestamp = test_client.chain_info().best_block_timestamp;
1594+
let fetch_seq = |p: &Public| -> u64 {
1595+
let address = public_to_address(p);
1596+
let a = test_client.latest_regular_key_owner(&address).unwrap_or(address);
1597+
test_client.latest_seq(&a)
1598+
};
1599+
mem_pool.remove(&[create_signed_pay(1, keypair).hash()], &fetch_seq, best_block_number, best_block_timestamp);
1600+
1601+
assert_eq!(
1602+
vec![create_signed_pay(0, keypair),],
1603+
mem_pool.top_transactions(std::usize::MAX, None, 0..std::u64::MAX).transactions
1604+
);
1605+
1606+
assert_eq!(vec![create_signed_pay(2, keypair),], mem_pool.future_transactions());
1607+
}
15381608
}

0 commit comments

Comments
 (0)