Skip to content

Commit ad92a17

Browse files
committed
reuse code
adapt change logic
1 parent 7a9f786 commit ad92a17

File tree

6 files changed

+54
-43
lines changed

6 files changed

+54
-43
lines changed

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

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use crate::{
1616
block_data_manager::BlockDataManager,
1717
consensus::BestInformation,
1818
transaction_pool::{nonce_pool::TxWithReadyInfo, pool_metrics::*},
19-
verification::{VerificationConfig, VerifyTxLocalMode, VerifyTxMode},
19+
verification::{
20+
PackingCheckResult, VerificationConfig, VerifyTxLocalMode, VerifyTxMode,
21+
},
2022
};
2123
use cfx_executor::{
2224
machine::Machine, spec::TransitionsEpochHeight, state::State,
@@ -718,6 +720,21 @@ impl TransactionPool {
718720
}
719721
}
720722

723+
fn check_validity(
724+
&self, tx: &SignedTransaction, best_epoch_height: u64,
725+
best_block_number: u64,
726+
) -> PackingCheckResult {
727+
let spec = self.machine.spec(best_block_number, best_epoch_height);
728+
let transitions = &self.machine.params().transition_heights;
729+
730+
self.verification_config.fast_recheck(
731+
tx,
732+
best_epoch_height,
733+
transitions,
734+
&spec,
735+
)
736+
}
737+
721738
pub fn pack_transactions<'a>(
722739
&self, num_txs: usize, block_gas_limit: U256, evm_gas_limit: U256,
723740
block_size_limit: usize, mut best_epoch_height: u64,
@@ -727,15 +744,17 @@ impl TransactionPool {
727744
best_epoch_height += 1;
728745
// The best block number is not necessary an exact number.
729746
best_block_number += 1;
747+
748+
let validity = |tx: &SignedTransaction| {
749+
self.check_validity(tx, best_epoch_height, best_block_number)
750+
};
751+
730752
inner.pack_transactions(
731753
num_txs,
732754
block_gas_limit,
733755
evm_gas_limit,
734756
block_size_limit,
735-
best_epoch_height,
736-
best_block_number,
737-
&self.verification_config,
738-
&self.machine,
757+
validity,
739758
)
740759
}
741760

@@ -749,16 +768,8 @@ impl TransactionPool {
749768
// The best block number is not necessary an exact number.
750769
best_block_number += 1;
751770

752-
let spec = self.machine.spec(best_block_number, best_epoch_height);
753-
let transitions = &self.machine.params().transition_heights;
754-
755771
let validity = |tx: &SignedTransaction| {
756-
self.verification_config.fast_recheck(
757-
tx,
758-
best_epoch_height,
759-
transitions,
760-
&spec,
761-
)
772+
self.check_validity(tx, best_epoch_height, best_block_number)
762773
};
763774

764775
inner.pack_transactions_1559(
@@ -800,7 +811,7 @@ impl TransactionPool {
800811
for tx in txs {
801812
gas_used[tx.space()] += *tx.gas_limit();
802813
min_gas_price[tx.space()] =
803-
min_gas_price[tx.space()].min(*tx.gas_limit());
814+
min_gas_price[tx.space()].min(*tx.gas_price());
804815
}
805816

806817
let core_gas_limit =

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ use super::{
77
TransactionPoolError,
88
};
99

10-
use crate::verification::{PackingCheckResult, VerificationConfig};
10+
use crate::verification::PackingCheckResult;
1111
use cfx_executor::machine::Machine;
1212
use cfx_packing_pool::PackingPoolConfig;
1313
use cfx_parameters::{
14-
block::cspace_block_gas_limit_after_cip1559,
14+
block::{
15+
cspace_block_gas_limit_after_cip1559,
16+
espace_block_gas_limit_of_enabled_block,
17+
},
1518
consensus_internal::ELASTICITY_MULTIPLIER,
1619
staking::DRIPS_PER_STORAGE_COLLATERAL_UNIT,
1720
};
@@ -643,35 +646,22 @@ impl TransactionPoolInner {
643646
/// pack at most num_txs transactions randomly
644647
pub fn pack_transactions<'a>(
645648
&mut self, num_txs: usize, block_gas_limit: U256, evm_gas_limit: U256,
646-
block_size_limit: usize, best_epoch_height: u64,
647-
best_block_number: u64, verification_config: &VerificationConfig,
648-
machine: &Machine,
649+
block_size_limit: usize,
650+
validity: impl Fn(&SignedTransaction) -> PackingCheckResult,
649651
) -> Vec<Arc<SignedTransaction>> {
650652
let mut packed_transactions: Vec<Arc<SignedTransaction>> = Vec::new();
651653
if num_txs == 0 {
652654
return packed_transactions;
653655
}
654656

655-
let spec = machine.spec(best_block_number, best_epoch_height);
656-
let transitions = &machine.params().transition_heights;
657-
658-
let validity = |tx: &SignedTransaction| {
659-
verification_config.fast_recheck(
660-
tx,
661-
best_epoch_height,
662-
transitions,
663-
&spec,
664-
)
665-
};
666-
667657
let (sampled_tx, used_gas, used_size) =
668658
self.deferred_pool.packing_sampler(
669659
Space::Ethereum,
670660
std::cmp::min(block_gas_limit, evm_gas_limit),
671661
block_size_limit,
672662
num_txs,
673663
U256::zero(),
674-
validity,
664+
&validity,
675665
);
676666
packed_transactions.extend_from_slice(&sampled_tx);
677667

@@ -681,7 +671,7 @@ impl TransactionPoolInner {
681671
block_size_limit - used_size,
682672
num_txs - sampled_tx.len(),
683673
U256::zero(),
684-
validity,
674+
&validity,
685675
);
686676
packed_transactions.extend_from_slice(&sampled_tx);
687677

@@ -722,7 +712,9 @@ impl TransactionPoolInner {
722712
machine.params().can_pack_evm_transaction(best_epoch_height);
723713

724714
let (evm_packed_tx_num, evm_used_size) = if can_pack_evm {
725-
let gas_target = block_gas_limit * 5 / 10 / ELASTICITY_MULTIPLIER;
715+
let gas_target =
716+
espace_block_gas_limit_of_enabled_block(block_gas_limit)
717+
/ ELASTICITY_MULTIPLIER;
726718
let parent_base_price = parent_base_price[Space::Ethereum];
727719
let min_base_price =
728720
machine.params().min_base_price()[Space::Ethereum];

crates/cfxcore/packing-pool/src/packing_batch.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub enum RemoveError {
4141

4242
#[derive(Debug)]
4343
pub(crate) struct PackInfo {
44-
pub first_gas_price: U256,
44+
pub first_priority_gas_price: U256,
4545
pub total_gas_limit: U256,
4646
}
4747

@@ -65,13 +65,18 @@ impl<TX: PackingPoolTransaction> PackingBatch<TX> {
6565
self.txs.first().unwrap().max_priority_gas_price()
6666
}
6767

68+
#[inline]
69+
pub fn first_gas_price(&self) -> U256 {
70+
self.txs.first().unwrap().gas_price()
71+
}
72+
6873
#[inline]
6974
pub fn total_gas_limit(&self) -> U256 { self.total_gas_limit }
7075

7176
#[inline]
7277
pub(crate) fn pack_info(&self) -> PackInfo {
7378
PackInfo {
74-
first_gas_price: self.first_priority_gas_price(),
79+
first_priority_gas_price: self.first_priority_gas_price(),
7580
total_gas_limit: self.total_gas_limit(),
7681
}
7782
}
@@ -266,12 +271,13 @@ impl<TX: PackingPoolTransaction> PackingBatch<TX> {
266271
self, config: &PackingPoolConfig, rng: &mut dyn RngCore,
267272
) -> treap_map::Node<PackingPoolMap<TX>> {
268273
let key = self.txs.first().unwrap().sender();
274+
let gas_price = self.first_gas_price();
269275
let priority_gas_price = self.first_priority_gas_price();
270276
let sort_key = priority_gas_price;
271277
let loss_ratio = config.loss_ratio(sort_key);
272278
let weight = PackingPoolWeight {
273279
gas_limit: self.total_gas_limit,
274-
min_gas_price: priority_gas_price,
280+
min_gas_price: gas_price,
275281
weighted_loss_ratio: loss_ratio * self.total_gas_limit,
276282
max_loss_ratio: loss_ratio,
277283
};

crates/cfxcore/packing-pool/src/pool.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,15 @@ fn make_apply_outcome<TX: PackingPoolTransaction, T>(
254254
old_info: PackInfo, new_info: PackInfo,
255255
node: &mut Node<PackingPoolMap<TX>>, config: &PackingPoolConfig, out: T,
256256
) -> ApplyOpOutcome<T> {
257-
let change_gas_price = old_info.first_gas_price != new_info.first_gas_price;
257+
let change_gas_price =
258+
old_info.first_priority_gas_price != new_info.first_priority_gas_price;
258259
let change_gas_limit = old_info.total_gas_limit != new_info.total_gas_limit;
259260

260261
let mut update_weight = false;
261262
let mut update_key = false;
262263

263264
if change_gas_price {
264-
let gas_price = new_info.first_gas_price;
265+
let gas_price = new_info.first_priority_gas_price;
265266
node.sort_key = gas_price;
266267
node.weight.max_loss_ratio = config.loss_ratio(gas_price);
267268
node.weight.gas_limit = new_info.total_gas_limit;

crates/cfxcore/packing-pool/src/sample.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ pub enum SampleTag {
2727
///
2828
/// The iterator operates in three phases:
2929
/// 1. **Random Sampling Phase**: Based on the random packing algorithm,
30-
/// addresses are selected from high to low gas prices of their first
30+
/// addresses are selected from high to low gas prices of their first
3131
/// transaction. The probability of inclusion is determined by the algorithm.
32-
/// Transactions not selected are placed in a candidate queue.
32+
/// Transactions not selected are placed in a candidate queue.
3333
/// 2. **Candidate Queue Phase**: Transactions from the candidate queue are
34-
/// output in a random order. Transactions with a higher probability in the
34+
/// output in a random order. Transactions with a higher probability in the
3535
/// first phase have a greater chance of appearing earlier in this phase.
3636
/// 3. **Remaining Transactions Phase**: Remaining addresses are output in
3737
/// descending order of their first transaction gas prices.

crates/cfxcore/packing-pool/src/transaction.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use primitives::SignedTransaction;
66
/// Trait representing a transaction processed by the `PackingPool`.
77
pub trait PackingPoolTransaction: Clone {
88
type Sender: Default + Ord + Hash + Copy + Debug;
9+
910
fn sender(&self) -> Self::Sender;
1011

1112
fn nonce(&self) -> U256;

0 commit comments

Comments
 (0)