Skip to content

Commit ca9b5a2

Browse files
Change priority bucket expected time to block target time (kaspanet#735)
* change priority bucket expected time to block target time * clippy --------- Co-authored-by: coderofstuff <[email protected]>
1 parent 11ffbcc commit ca9b5a2

File tree

4 files changed

+37
-25
lines changed

4 files changed

+37
-25
lines changed

mining/benches/bench.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub fn bench_mempool_sampling(c: &mut Criterion) {
102102
}
103103

104104
let len = cap;
105-
let mut frontier = Frontier::default();
105+
let mut frontier = Frontier::new(1.0);
106106
for item in map.values().take(len).cloned() {
107107
frontier.insert(item).then_some(()).unwrap();
108108
}
@@ -183,7 +183,7 @@ pub fn bench_mempool_selectors(c: &mut Criterion) {
183183
}
184184

185185
for len in [100, 300, 350, 500, 1000, 2000, 5000, 10_000, 100_000, 500_000, 1_000_000].into_iter().rev() {
186-
let mut frontier = Frontier::default();
186+
let mut frontier = Frontier::new(1.0);
187187
for item in map.values().take(len).cloned() {
188188
frontier.insert(item).then_some(()).unwrap();
189189
}
@@ -252,7 +252,7 @@ pub fn bench_inplace_sampling_worst_case(c: &mut Criterion) {
252252
map.insert(key.tx.id(), key);
253253
}
254254

255-
let mut frontier = Frontier::default();
255+
let mut frontier = Frontier::new(1.0);
256256
for item in map.values().cloned() {
257257
frontier.insert(item).then_some(()).unwrap();
258258
}

mining/src/feerate/mod.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,15 @@ pub struct FeerateEstimator {
8585
/// other words, the inverse of the transaction inclusion rate. For instance, if the average transaction mass is 2500 grams,
8686
/// the block mass limit is 500,000 and the network has 10 BPS, then this number would be 1/2000 seconds.
8787
inclusion_interval: f64,
88+
89+
target_time_per_block_seconds: f64,
8890
}
8991

9092
impl FeerateEstimator {
91-
pub fn new(total_weight: f64, inclusion_interval: f64) -> Self {
93+
pub fn new(total_weight: f64, inclusion_interval: f64, target_time_per_block_seconds: f64) -> Self {
9294
assert!(total_weight >= 0.0);
9395
assert!((0f64..1f64).contains(&inclusion_interval));
94-
Self { total_weight, inclusion_interval }
96+
Self { total_weight, inclusion_interval, target_time_per_block_seconds }
9597
}
9698

9799
pub(crate) fn feerate_to_time(&self, feerate: f64) -> f64 {
@@ -132,8 +134,8 @@ impl FeerateEstimator {
132134

133135
pub fn calc_estimations(&self, minimum_standard_feerate: f64) -> FeerateEstimations {
134136
let min = minimum_standard_feerate;
135-
// Choose `high` such that it provides sub-second waiting time
136-
let high = self.time_to_feerate(1f64).max(min);
137+
// Choose `high` such that the transaction is expected to be included in the next block.
138+
let high = self.time_to_feerate(self.target_time_per_block_seconds).max(min);
137139
// Choose `low` feerate such that it provides sub-hour waiting time AND it covers (at least) the 0.25 quantile
138140
let low = self.time_to_feerate(3600f64).max(self.quantile(min, high, 0.25));
139141
// Choose `normal` feerate such that it provides sub-minute waiting time AND it covers (at least) the 0.66 quantile between low and high.
@@ -176,7 +178,8 @@ mod tests {
176178

177179
#[test]
178180
fn test_feerate_estimations() {
179-
let estimator = FeerateEstimator { total_weight: 1002283.659, inclusion_interval: 0.004f64 };
181+
let estimator =
182+
FeerateEstimator { total_weight: 1002283.659, inclusion_interval: 0.004f64, target_time_per_block_seconds: 1.0 };
180183
let estimations = estimator.calc_estimations(1.0);
181184
let buckets = estimations.ordered_buckets();
182185
for (i, j) in buckets.into_iter().tuple_windows() {
@@ -187,7 +190,7 @@ mod tests {
187190

188191
#[test]
189192
fn test_min_feerate_estimations() {
190-
let estimator = FeerateEstimator { total_weight: 0.00659, inclusion_interval: 0.004f64 };
193+
let estimator = FeerateEstimator { total_weight: 0.00659, inclusion_interval: 0.004f64, target_time_per_block_seconds: 1.0 };
191194
let minimum_feerate = 0.755;
192195
let estimations = estimator.calc_estimations(minimum_feerate);
193196
println!("{estimations}");
@@ -201,7 +204,7 @@ mod tests {
201204

202205
#[test]
203206
fn test_zero_values() {
204-
let estimator = FeerateEstimator { total_weight: 0.0, inclusion_interval: 0.0 };
207+
let estimator = FeerateEstimator { total_weight: 0.0, inclusion_interval: 0.0, target_time_per_block_seconds: 1.0 };
205208
let minimum_feerate = 0.755;
206209
let estimations = estimator.calc_estimations(minimum_feerate);
207210
let buckets = estimations.ordered_buckets();
@@ -210,7 +213,7 @@ mod tests {
210213
assert_eq!(0.0, bucket.estimated_seconds);
211214
}
212215

213-
let estimator = FeerateEstimator { total_weight: 0.0, inclusion_interval: 0.1 };
216+
let estimator = FeerateEstimator { total_weight: 0.0, inclusion_interval: 0.1, target_time_per_block_seconds: 1.0 };
214217
let minimum_feerate = 0.755;
215218
let estimations = estimator.calc_estimations(minimum_feerate);
216219
let buckets = estimations.ordered_buckets();
@@ -219,7 +222,7 @@ mod tests {
219222
assert_eq!(estimator.inclusion_interval, bucket.estimated_seconds);
220223
}
221224

222-
let estimator = FeerateEstimator { total_weight: 0.1, inclusion_interval: 0.0 };
225+
let estimator = FeerateEstimator { total_weight: 0.1, inclusion_interval: 0.0, target_time_per_block_seconds: 1.0 };
223226
let minimum_feerate = 0.755;
224227
let estimations = estimator.calc_estimations(minimum_feerate);
225228
let buckets = estimations.ordered_buckets();

mining/src/mempool/model/frontier.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,18 @@ pub struct Frontier {
4343

4444
/// Tracks the average transaction mass throughout the mempool's lifespan using a decayed weighting mechanism
4545
average_transaction_mass: f64,
46+
47+
target_time_per_block_seconds: f64,
4648
}
4749

48-
impl Default for Frontier {
49-
fn default() -> Self {
50-
Self { search_tree: Default::default(), total_mass: Default::default(), average_transaction_mass: INITIAL_AVG_MASS }
50+
impl Frontier {
51+
pub fn new(target_time_per_block_seconds: f64) -> Self {
52+
Self {
53+
search_tree: Default::default(),
54+
total_mass: Default::default(),
55+
average_transaction_mass: INITIAL_AVG_MASS,
56+
target_time_per_block_seconds,
57+
}
5158
}
5259
}
5360

@@ -229,7 +236,7 @@ impl Frontier {
229236
let bps = args.network_blocks_per_second as f64;
230237
let mut mass_per_block = args.maximum_mass_per_block as f64;
231238
let mut inclusion_interval = average_transaction_mass / (mass_per_block * bps);
232-
let mut estimator = FeerateEstimator::new(self.total_weight(), inclusion_interval);
239+
let mut estimator = FeerateEstimator::new(self.total_weight(), inclusion_interval, self.target_time_per_block_seconds);
233240

234241
// Search for better estimators by possibly removing extremely high outliers
235242
let mut down_iter = self.search_tree.descending_iter().peekable();
@@ -250,7 +257,7 @@ impl Frontier {
250257

251258
// Compute the weight up to, and excluding, current key (which translates to zero weight if peek() is none)
252259
let prefix_weight = down_iter.peek().map(|key| self.search_tree.prefix_weight(key)).unwrap_or_default();
253-
let pending_estimator = FeerateEstimator::new(prefix_weight, inclusion_interval);
260+
let pending_estimator = FeerateEstimator::new(prefix_weight, inclusion_interval, self.target_time_per_block_seconds);
254261

255262
// Test the pending estimator vs. the current one
256263
if pending_estimator.feerate_to_time(1.0) < estimator.feerate_to_time(1.0) {
@@ -294,7 +301,7 @@ mod tests {
294301
map.insert(key.tx.id(), key);
295302
}
296303

297-
let mut frontier = Frontier::default();
304+
let mut frontier = Frontier::new(1.0);
298305
for item in map.values().cloned() {
299306
frontier.insert(item).then_some(()).unwrap();
300307
}
@@ -314,7 +321,7 @@ mod tests {
314321
map.insert(key.tx.id(), key);
315322
}
316323

317-
let mut frontier = Frontier::default();
324+
let mut frontier = Frontier::new(1.0);
318325
for item in map.values().cloned() {
319326
frontier.insert(item).then_some(()).unwrap();
320327
}
@@ -348,7 +355,7 @@ mod tests {
348355
}
349356

350357
let len = cap / 2;
351-
let mut frontier = Frontier::default();
358+
let mut frontier = Frontier::new(1.0);
352359
for item in map.values().take(len).cloned() {
353360
frontier.insert(item).then_some(()).unwrap();
354361
}
@@ -402,7 +409,7 @@ mod tests {
402409
}
403410

404411
for len in [0, 1, 10, 100, 200, 300, 500, 750, cap / 2, (cap * 2) / 3, (cap * 4) / 5, (cap * 5) / 6, cap] {
405-
let mut frontier = Frontier::default();
412+
let mut frontier = Frontier::new(1.0);
406413
for item in map.values().take(len).cloned() {
407414
frontier.insert(item).then_some(()).unwrap();
408415
}
@@ -444,7 +451,7 @@ mod tests {
444451
for len in [0, 1, 10, 100, 200, 300, 500, 750, cap / 2, (cap * 2) / 3, (cap * 4) / 5, (cap * 5) / 6, cap] {
445452
println!();
446453
println!("Testing a frontier with {} txs...", len.min(cap));
447-
let mut frontier = Frontier::default();
454+
let mut frontier = Frontier::new(1.0);
448455
for item in map.values().take(len).cloned() {
449456
frontier.insert(item).then_some(()).unwrap();
450457
}
@@ -477,7 +484,7 @@ mod tests {
477484
const HIGH_FEERATE: f64 = 1000.0;
478485

479486
let cap = 20_000;
480-
let mut frontier = Frontier::default();
487+
let mut frontier = Frontier::new(1.0);
481488
for i in 0..cap as u64 {
482489
let (mass, fee) = if i < 200 {
483490
let mass = 1650;
@@ -533,7 +540,7 @@ mod tests {
533540

534541
// All lens make for less than block capacity (given the mass used)
535542
for len in [0, 1, 10, 100, 200, 250, 300] {
536-
let mut frontier = Frontier::default();
543+
let mut frontier = Frontier::new(1.0);
537544
for item in map.values().take(len).cloned() {
538545
frontier.insert(item).then_some(()).unwrap();
539546
}

mining/src/mempool/model/transactions_pool.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,14 @@ pub(crate) struct TransactionsPool {
8181

8282
impl TransactionsPool {
8383
pub(crate) fn new(config: Arc<Config>) -> Self {
84+
// [Crescendo] Delete `after()` after cleanup.
85+
let target_time_per_block = 1.0 / (config.network_blocks_per_second.after() as f64);
8486
Self {
8587
config,
8688
all_transactions: MempoolTransactionCollection::default(),
8789
parent_transactions: TransactionsEdges::default(),
8890
chained_transactions: TransactionsEdges::default(),
89-
ready_transactions: Default::default(),
91+
ready_transactions: Frontier::new(target_time_per_block),
9092
last_expire_scan_daa_score: 0,
9193
last_expire_scan_time: unix_now(),
9294
utxo_set: MempoolUtxoSet::new(),

0 commit comments

Comments
 (0)