Skip to content

Commit 62931b3

Browse files
authored
Merge pull request #3097 from Pana/feat/espaceTxpool
make tx-pool function name more specific and add comment
2 parents 2bbadc1 + 09cf139 commit 62931b3

File tree

7 files changed

+46
-36
lines changed

7 files changed

+46
-36
lines changed

crates/cfxcore/core/src/consensus/consensus_inner/consensus_executor/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,10 +1142,9 @@ impl ConsensusExecutionHandler {
11421142
.check_availability(pivot_block_header.height(), epoch_hash)
11431143
{
11441144
self.tx_pool
1145-
.set_best_executed_epoch(StateIndex::new_for_readonly(
1146-
epoch_hash,
1147-
&state_root,
1148-
))
1145+
.set_best_executed_state_by_epoch(
1146+
StateIndex::new_for_readonly(epoch_hash, &state_root),
1147+
)
11491148
// FIXME: propogate error.
11501149
.expect(&concat!(file!(), ":", line!(), ":", column!()));
11511150
}
@@ -1231,7 +1230,7 @@ impl ConsensusExecutionHandler {
12311230
}
12321231

12331232
self.tx_pool
1234-
.set_best_executed_epoch(StateIndex::new_for_readonly(
1233+
.set_best_executed_state_by_epoch(StateIndex::new_for_readonly(
12351234
epoch_hash,
12361235
&commit_result.state_root,
12371236
))

crates/cfxcore/core/src/consensus/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2487,7 +2487,7 @@ impl ConsensusGraphTrait for ConsensusGraph {
24872487
fn enter_normal_phase(&self) {
24882488
self.ready_for_mining.store(true, Ordering::SeqCst);
24892489
self.update_best_info(true);
2490-
self.txpool.set_ready();
2490+
self.txpool.set_ready_for_mining();
24912491
self.txpool
24922492
.notify_new_best_info(self.best_info.read_recursive().clone())
24932493
.expect("No DB error")

crates/cfxcore/core/src/transaction_pool/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl TransactionPool {
255255
config.packing_pool_degree,
256256
);
257257
let best_executed_state = Mutex::new(
258-
Self::best_executed_state(
258+
Self::get_best_executed_state_by_epoch(
259259
&data_man,
260260
StateIndex::new_for_readonly(
261261
&genesis_hash,
@@ -377,7 +377,7 @@ impl TransactionPool {
377377
sponsored_gas,
378378
sponsored_storage,
379379
)
380-
.calc_tx_cost();
380+
.get_tx_cost();
381381

382382
let outdated = match (tx_cost <= balance, &first_tx_status) {
383383
(true, Some(Pending(PendingReason::NotEnoughCash)))
@@ -1173,7 +1173,7 @@ impl TransactionPool {
11731173
)
11741174
}
11751175

1176-
fn best_executed_state(
1176+
fn get_best_executed_state_by_epoch(
11771177
data_man: &BlockDataManager, best_executed_epoch: StateIndex,
11781178
) -> StateDbResult<Arc<State>> {
11791179
let storage = data_man
@@ -1190,11 +1190,14 @@ impl TransactionPool {
11901190
Ok(Arc::new(state))
11911191
}
11921192

1193-
pub fn set_best_executed_epoch(
1193+
pub fn set_best_executed_state_by_epoch(
11941194
&self, best_executed_epoch: StateIndex,
11951195
) -> StateDbResult<()> {
11961196
*self.best_executed_state.lock() =
1197-
Self::best_executed_state(&self.data_man, best_executed_epoch)?;
1197+
Self::get_best_executed_state_by_epoch(
1198+
&self.data_man,
1199+
best_executed_epoch,
1200+
)?;
11981201

11991202
Ok(())
12001203
}
@@ -1208,7 +1211,7 @@ impl TransactionPool {
12081211
self.ready_for_mining.load(Ordering::SeqCst)
12091212
}
12101213

1211-
pub fn set_ready(&self) {
1214+
pub fn set_ready_for_mining(&self) {
12121215
self.ready_for_mining.store(true, Ordering::SeqCst);
12131216
}
12141217
}

crates/cfxcore/core/src/transaction_pool/nonce_pool/mod.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl TxWithReadyInfo {
3636
sponsored_storage: u64,
3737
) -> Self {
3838
let tx_cost =
39-
Self::make_tx_cost(&*transaction, sponsored_gas, sponsored_storage);
39+
Self::cal_tx_cost(&*transaction, sponsored_gas, sponsored_storage);
4040
Self {
4141
transaction,
4242
packed,
@@ -63,7 +63,7 @@ impl TxWithReadyInfo {
6363

6464
pub fn get_arc_tx(&self) -> &Arc<SignedTransaction> { &self.transaction }
6565

66-
pub fn calc_tx_cost(&self) -> U256 { self.tx_cost }
66+
pub fn get_tx_cost(&self) -> U256 { self.tx_cost }
6767

6868
pub fn should_replace(
6969
&self, x: &Self, force: bool,
@@ -107,15 +107,15 @@ impl TxWithReadyInfo {
107107
}
108108

109109
#[inline]
110-
pub fn compute_next_price(price: U256) -> U256 {
110+
fn compute_next_price(price: U256) -> U256 {
111111
if price < 100.into() {
112112
price + 1
113113
} else {
114114
price + (price / 100) * 2
115115
}
116116
}
117117

118-
pub fn make_tx_cost(
118+
pub fn cal_tx_cost(
119119
transaction: &SignedTransaction, sponsored_gas: U256,
120120
sponsored_storage: u64,
121121
) -> U256 {
@@ -226,6 +226,8 @@ impl NoncePool {
226226
self.remove(&nonce)
227227
}
228228

229+
// return the number of transactions whose nonce >= `nonce`
230+
// and the first transaction with nonce >= `nonce`
229231
pub fn get_pending_info(
230232
&self, nonce: &U256,
231233
) -> Option<(usize, Arc<SignedTransaction>)> {
@@ -262,7 +264,10 @@ impl NoncePool {
262264
/// 2. the balance is enough.
263265
///
264266
/// The first return value is the transaction in the first step.
265-
/// The second return value is the last nonce in the transaction series.
267+
/// i.e., the first unpacked transaction from a sequential of transactions
268+
/// starting from `nonce`, may be `nonce` itself.
269+
/// The second return value is the last nonce in the transaction series
270+
/// from the tx.nonce()
266271
pub fn recalculate_readiness_with_local_info(
267272
&self, nonce: U256, balance: U256,
268273
) -> Option<(&TxWithReadyInfo, U256)> {
@@ -277,7 +282,8 @@ impl NoncePool {
277282
// 1. b.cost - a.cost means the sum of cost of
278283
// transactions in `[nonce, tx.nonce()]`
279284
// 2. b.size - a.size means number of transactions in
280-
// `[nonce, tx.nonce()]` 3. x.nonce() - nonce + 1 means expected
285+
// `[nonce, tx.nonce()]`
286+
// 3. tx.nonce() - nonce + 1 means expected
281287
// number of transactions in `[nonce, tx.nonce()]`
282288
let size_elapsed = b.size - a.size;
283289
let cost_elapsed = b.cost - a.cost;
@@ -451,7 +457,7 @@ mod nonce_pool_test {
451457
0,
452458
false,
453459
);
454-
assert_eq!(tx.calc_tx_cost(), U256::from(10 * 50000 / 2 + 10000));
460+
assert_eq!(tx.get_tx_cost(), U256::from(10 * 50000 / 2 + 10000));
455461
// normal case with storage limit
456462
let tx = new_test_tx_with_ready_info(
457463
&me,
@@ -463,7 +469,7 @@ mod nonce_pool_test {
463469
false,
464470
);
465471
assert_eq!(
466-
tx.calc_tx_cost(),
472+
tx.get_tx_cost(),
467473
U256::from(10 * 50000 / 2 + 10000)
468474
+ U256::from(5000 / 2) * *DRIPS_PER_STORAGE_COLLATERAL_UNIT
469475
);
@@ -477,7 +483,7 @@ mod nonce_pool_test {
477483
0,
478484
false,
479485
);
480-
assert_eq!(tx.calc_tx_cost(), U256::from(10 * 50000 / 2) + value_max);
486+
assert_eq!(tx.get_tx_cost(), U256::from(10 * 50000 / 2) + value_max);
481487
// very large tx value, fit the range, #1
482488
let tx = new_test_tx_with_ready_info(
483489
&me,
@@ -488,7 +494,7 @@ mod nonce_pool_test {
488494
0,
489495
false,
490496
);
491-
assert_eq!(tx.calc_tx_cost(), U256::from(10 * 50000 / 2) + value_max);
497+
assert_eq!(tx.get_tx_cost(), U256::from(10 * 50000 / 2) + value_max);
492498
// very large tx value, fit the range, #1
493499
let tx = new_test_tx_with_ready_info(
494500
&me,
@@ -500,7 +506,7 @@ mod nonce_pool_test {
500506
false,
501507
);
502508
assert_eq!(
503-
tx.calc_tx_cost(),
509+
tx.get_tx_cost(),
504510
U256::from(10 * 50000 / 2) + value_max - U256::from(1)
505511
);
506512
// very large gas fee, not fit the range, #1
@@ -514,7 +520,7 @@ mod nonce_pool_test {
514520
false,
515521
);
516522
assert_eq!(
517-
tx.calc_tx_cost(),
523+
tx.get_tx_cost(),
518524
gas_fee_max
519525
+ U256::from(10000)
520526
+ U256::from(5000 / 2) * *DRIPS_PER_STORAGE_COLLATERAL_UNIT
@@ -530,7 +536,7 @@ mod nonce_pool_test {
530536
false,
531537
);
532538
assert_eq!(
533-
tx.calc_tx_cost(),
539+
tx.get_tx_cost(),
534540
gas_fee_max
535541
+ U256::from(10000)
536542
+ U256::from(5000 / 2) * *DRIPS_PER_STORAGE_COLLATERAL_UNIT
@@ -546,7 +552,7 @@ mod nonce_pool_test {
546552
false,
547553
);
548554
assert_eq!(
549-
tx.calc_tx_cost(),
555+
tx.get_tx_cost(),
550556
gas_fee_max
551557
+ U256::from(10000)
552558
+ U256::from(5000 / 2) * *DRIPS_PER_STORAGE_COLLATERAL_UNIT
@@ -562,7 +568,7 @@ mod nonce_pool_test {
562568
false,
563569
);
564570
assert_eq!(
565-
tx.calc_tx_cost(),
571+
tx.get_tx_cost(),
566572
gas_fee_max - U256::from(1)
567573
+ U256::from(10000)
568574
+ U256::from(5000 / 2) * *DRIPS_PER_STORAGE_COLLATERAL_UNIT
@@ -772,7 +778,7 @@ mod nonce_pool_test {
772778

773779
let first_tx = loop {
774780
let tx = nonce_pool.get(&next_nonce)?;
775-
let cost = tx.calc_tx_cost();
781+
let cost = tx.get_tx_cost();
776782
if balance_left < cost {
777783
return None;
778784
}
@@ -793,7 +799,7 @@ mod nonce_pool_test {
793799
} else {
794800
return Some((first_tx, next_nonce - 1));
795801
};
796-
let cost = tx.calc_tx_cost();
802+
let cost = tx.get_tx_cost();
797803

798804
if balance_left < cost || tx.is_already_packed() {
799805
return Some((first_tx, next_nonce - 1));

crates/cfxcore/core/src/transaction_pool/nonce_pool/nonce_pool_map.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ impl NoncePoolMap {
111111

112112
/// find an unpacked transaction `tx` where `tx.nonce() >= nonce`
113113
/// and `tx.nonce()` is minimum
114+
/// i.e. the first unpacked transaction with nonce >= `nonce`
114115
pub fn query(&self, nonce: &U256) -> Option<&TxWithReadyInfo> {
115116
let ret = self.0.search(|left_weight, node| {
116117
if left_weight.max_unpackd_nonce.map_or(false, |x| x >= *nonce) {

crates/cfxcore/core/src/transaction_pool/nonce_pool/weight.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use super::TxWithReadyInfo;
1010
pub(super) struct NoncePoolWeight {
1111
/// number of unpacked transactions
1212
pub unpacked_size: u32,
13-
/// sum of cost of transaction
13+
/// sum of cost of transactions
1414
pub cost: U256,
15-
/// number of transaction
15+
/// number of transactions
1616
pub size: u32,
1717
/// max unpacked nonce
1818
pub max_unpackd_nonce: Option<U256>,
@@ -55,14 +55,14 @@ impl NoncePoolWeight {
5555
if tx_info.packed {
5656
Self {
5757
unpacked_size: 0,
58-
cost: tx_info.calc_tx_cost(),
58+
cost: tx_info.get_tx_cost(),
5959
size: 1,
6060
max_unpackd_nonce: None,
6161
}
6262
} else {
6363
Self {
6464
unpacked_size: 1,
65-
cost: tx_info.calc_tx_cost(),
65+
cost: tx_info.get_tx_cost(),
6666
size: 1,
6767
max_unpackd_nonce: Some(*tx_info.transaction.nonce()),
6868
}

crates/cfxcore/core/src/transaction_pool/transaction_pool_inner.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ impl TransactionSet {
536536
#[derive(DeriveMallocSizeOf)]
537537
pub struct TransactionPoolInner {
538538
capacity: usize,
539+
// deprecated, this value is never updated
539540
total_received_count: usize,
540541
unpacked_transaction_count: usize,
541542
/// Tracks all transactions in the transaction pool by account and nonce.
@@ -1367,7 +1368,7 @@ impl TransactionPoolInner {
13671368
// check balance
13681369
if !packed && !force {
13691370
let mut need_balance = U256::from(0);
1370-
let estimate_gas_fee = Self::estimated_gas_fee(
1371+
let estimate_gas_fee = Self::cal_gas_fee(
13711372
transaction.gas().clone(),
13721373
transaction.gas_price().clone(),
13731374
);
@@ -1423,7 +1424,7 @@ impl TransactionPoolInner {
14231424
Ok(())
14241425
}
14251426

1426-
fn estimated_gas_fee(gas: U256, gas_price: U256) -> U256 {
1427+
fn cal_gas_fee(gas: U256, gas_price: U256) -> U256 {
14271428
let estimated_gas_u512 = gas.full_mul(gas_price);
14281429
// Normally, it is less than 2^128
14291430
let estimated_gas =
@@ -1472,7 +1473,7 @@ impl TransactionPoolInner {
14721473
}
14731474

14741475
// Detailed logics
1475-
let estimated_gas = Self::estimated_gas_fee(
1476+
let estimated_gas = Self::cal_gas_fee(
14761477
transaction.gas().clone(),
14771478
transaction.gas_price().clone(),
14781479
);

0 commit comments

Comments
 (0)