Skip to content

Commit 202b45c

Browse files
authored
Merge pull request #4966 from stacks-network/feat/nakamoto-mempool-sync
Nakamoto mempool sync
2 parents a13dcc0 + d47c4ab commit 202b45c

File tree

14 files changed

+2181
-1537
lines changed

14 files changed

+2181
-1537
lines changed

stackslib/src/chainstate/stacks/miner.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,6 @@ impl<'a> StacksMicroblockBuilder<'a> {
12041204
intermediate_result = mem_pool.iterate_candidates(
12051205
&mut clarity_tx,
12061206
&mut tx_events,
1207-
self.anchor_block_height,
12081207
mempool_settings.clone(),
12091208
|clarity_tx, to_consider, estimator| {
12101209
let mempool_tx = &to_consider.tx;
@@ -2211,7 +2210,6 @@ impl StacksBlockBuilder {
22112210
intermediate_result = mempool.iterate_candidates(
22122211
epoch_tx,
22132212
&mut tx_events,
2214-
tip_height,
22152213
mempool_settings.clone(),
22162214
|epoch_tx, to_consider, estimator| {
22172215
// first, have we been preempted?

stackslib/src/chainstate/stacks/tests/block_construction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,7 +3060,7 @@ fn test_build_microblock_stream_forks_with_descendants() {
30603060

30613061
// erase any pending transactions -- this is a "worse" poison-microblock,
30623062
// and we want to avoid mining the "better" one
3063-
mempool.clear_before_height(10).unwrap();
3063+
mempool.clear_before_coinbase_height(10).unwrap();
30643064

30653065
let mut tx_bytes = vec![];
30663066
poison_microblock_tx
@@ -4784,6 +4784,7 @@ fn paramaterized_mempool_walk_test(
47844784
&mut chainstate,
47854785
&b_1.0,
47864786
&b_1.1,
4787+
true,
47874788
txid,
47884789
tx_bytes,
47894790
tx_fee,
@@ -4832,7 +4833,6 @@ fn paramaterized_mempool_walk_test(
48324833
.iterate_candidates::<_, ChainstateError, _>(
48334834
clarity_conn,
48344835
&mut tx_events,
4835-
2,
48364836
mempool_settings.clone(),
48374837
|_, available_tx, _| {
48384838
count_txs += 1;

stackslib/src/core/mempool.rs

Lines changed: 148 additions & 151 deletions
Large diffs are not rendered by default.

stackslib/src/core/tests/mod.rs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ fn mempool_walk_over_fork() {
240240
&mut chainstate,
241241
&block.0,
242242
&block.1,
243+
true,
243244
txid,
244245
tx_bytes,
245246
tx_fee,
@@ -275,7 +276,6 @@ fn mempool_walk_over_fork() {
275276
.iterate_candidates::<_, ChainstateError, _>(
276277
clarity_conn,
277278
&mut tx_events,
278-
2,
279279
mempool_settings.clone(),
280280
|_, available_tx, _| {
281281
count_txs += 1;
@@ -314,7 +314,6 @@ fn mempool_walk_over_fork() {
314314
.iterate_candidates::<_, ChainstateError, _>(
315315
clarity_conn,
316316
&mut tx_events,
317-
2,
318317
mempool_settings.clone(),
319318
|_, available_tx, _| {
320319
count_txs += 1;
@@ -352,7 +351,6 @@ fn mempool_walk_over_fork() {
352351
.iterate_candidates::<_, ChainstateError, _>(
353352
clarity_conn,
354353
&mut tx_events,
355-
3,
356354
mempool_settings.clone(),
357355
|_, available_tx, _| {
358356
count_txs += 1;
@@ -395,7 +393,6 @@ fn mempool_walk_over_fork() {
395393
.iterate_candidates::<_, ChainstateError, _>(
396394
clarity_conn,
397395
&mut tx_events,
398-
2,
399396
mempool_settings.clone(),
400397
|_, available_tx, _| {
401398
count_txs += 1;
@@ -436,7 +433,6 @@ fn mempool_walk_over_fork() {
436433
.iterate_candidates::<_, ChainstateError, _>(
437434
clarity_conn,
438435
&mut tx_events,
439-
3,
440436
mempool_settings.clone(),
441437
|_, available_tx, _| {
442438
count_txs += 1;
@@ -498,6 +494,7 @@ fn mempool_walk_over_fork() {
498494
&mut chainstate,
499495
&block.0,
500496
&block.1,
497+
true,
501498
txid,
502499
tx_bytes,
503500
tx_fee,
@@ -551,6 +548,7 @@ fn mempool_walk_over_fork() {
551548
&mut chainstate,
552549
&block.0,
553550
&block.1,
551+
true,
554552
txid,
555553
tx_bytes,
556554
tx_fee,
@@ -630,6 +628,7 @@ fn test_iterate_candidates_consider_no_estimate_tx_prob() {
630628
&mut chainstate,
631629
&b_1.0,
632630
&b_1.1,
631+
true,
633632
txid,
634633
tx_bytes,
635634
tx_fee,
@@ -672,7 +671,6 @@ fn test_iterate_candidates_consider_no_estimate_tx_prob() {
672671
.iterate_candidates::<_, ChainstateError, _>(
673672
clarity_conn,
674673
&mut tx_events,
675-
2,
676674
mempool_settings.clone(),
677675
|_, available_tx, _| {
678676
count_txs += 1;
@@ -710,7 +708,6 @@ fn test_iterate_candidates_consider_no_estimate_tx_prob() {
710708
.iterate_candidates::<_, ChainstateError, _>(
711709
clarity_conn,
712710
&mut tx_events,
713-
2,
714711
mempool_settings.clone(),
715712
|_, available_tx, _| {
716713
count_txs += 1;
@@ -748,7 +745,6 @@ fn test_iterate_candidates_consider_no_estimate_tx_prob() {
748745
.iterate_candidates::<_, ChainstateError, _>(
749746
clarity_conn,
750747
&mut tx_events,
751-
2,
752748
mempool_settings.clone(),
753749
|_, available_tx, _| {
754750
count_txs += 1;
@@ -826,6 +822,7 @@ fn test_iterate_candidates_skipped_transaction() {
826822
&mut chainstate,
827823
&b_1.0,
828824
&b_1.1,
825+
true,
829826
txid,
830827
tx_bytes,
831828
tx_fee,
@@ -850,7 +847,6 @@ fn test_iterate_candidates_skipped_transaction() {
850847
.iterate_candidates::<_, ChainstateError, _>(
851848
clarity_conn,
852849
&mut tx_events,
853-
2,
854850
mempool_settings.clone(),
855851
|_, available_tx, _| {
856852
count_txs += 1;
@@ -939,6 +935,7 @@ fn test_iterate_candidates_processing_error_transaction() {
939935
&mut chainstate,
940936
&b_1.0,
941937
&b_1.1,
938+
true,
942939
txid,
943940
tx_bytes,
944941
tx_fee,
@@ -963,7 +960,6 @@ fn test_iterate_candidates_processing_error_transaction() {
963960
.iterate_candidates::<_, ChainstateError, _>(
964961
clarity_conn,
965962
&mut tx_events,
966-
2,
967963
mempool_settings.clone(),
968964
|_, available_tx, _| {
969965
count_txs += 1;
@@ -1054,6 +1050,7 @@ fn test_iterate_candidates_problematic_transaction() {
10541050
&mut chainstate,
10551051
&b_1.0,
10561052
&b_1.1,
1053+
true,
10571054
txid,
10581055
tx_bytes,
10591056
tx_fee,
@@ -1078,7 +1075,6 @@ fn test_iterate_candidates_problematic_transaction() {
10781075
.iterate_candidates::<_, ChainstateError, _>(
10791076
clarity_conn,
10801077
&mut tx_events,
1081-
2,
10821078
mempool_settings.clone(),
10831079
|_, available_tx, _| {
10841080
count_txs += 1;
@@ -1183,6 +1179,7 @@ fn test_iterate_candidates_concurrent_write_lock() {
11831179
&mut chainstate,
11841180
&b_1.0,
11851181
&b_1.1,
1182+
true,
11861183
txid,
11871184
tx_bytes,
11881185
tx_fee,
@@ -1239,7 +1236,6 @@ fn test_iterate_candidates_concurrent_write_lock() {
12391236
.iterate_candidates::<_, ChainstateError, _>(
12401237
clarity_conn,
12411238
&mut tx_events,
1242-
2,
12431239
mempool_settings.clone(),
12441240
|_, available_tx, _| {
12451241
count_txs += 1;
@@ -1342,6 +1338,7 @@ fn mempool_do_not_replace_tx() {
13421338
&mut chainstate,
13431339
&b_1.0,
13441340
&b_1.1,
1341+
true,
13451342
txid,
13461343
tx_bytes,
13471344
tx_fee,
@@ -1370,6 +1367,7 @@ fn mempool_do_not_replace_tx() {
13701367
&mut chainstate,
13711368
&b_2.0,
13721369
&b_2.1,
1370+
true,
13731371
txid,
13741372
tx_bytes,
13751373
tx_fee,
@@ -1446,6 +1444,7 @@ fn mempool_db_load_store_replace_tx(#[case] behavior: MempoolCollectionBehavior)
14461444
&mut chainstate,
14471445
&ConsensusHash([0x1; 20]),
14481446
&BlockHeaderHash([0x2; 32]),
1447+
false, // don't resolve the above chain tip since it doesn't exist
14491448
txid,
14501449
tx_bytes,
14511450
tx_fee,
@@ -1471,12 +1470,15 @@ fn mempool_db_load_store_replace_tx(#[case] behavior: MempoolCollectionBehavior)
14711470
assert_eq!(tx_info.metadata.origin_nonce, origin_nonce);
14721471
assert_eq!(tx_info.metadata.sponsor_address, sponsor_address);
14731472
assert_eq!(tx_info.metadata.sponsor_nonce, sponsor_nonce);
1474-
assert_eq!(tx_info.metadata.consensus_hash, ConsensusHash([0x1; 20]));
14751473
assert_eq!(
1476-
tx_info.metadata.block_header_hash,
1474+
tx_info.metadata.tenure_consensus_hash,
1475+
ConsensusHash([0x1; 20])
1476+
);
1477+
assert_eq!(
1478+
tx_info.metadata.tenure_block_header_hash,
14771479
BlockHeaderHash([0x2; 32])
14781480
);
1479-
assert_eq!(tx_info.metadata.block_height, height);
1481+
assert_eq!(tx_info.metadata.coinbase_height, height);
14801482

14811483
// test replace-by-fee with a higher fee
14821484
let old_txid = txid;
@@ -1503,6 +1505,7 @@ fn mempool_db_load_store_replace_tx(#[case] behavior: MempoolCollectionBehavior)
15031505
&mut chainstate,
15041506
&ConsensusHash([0x1; 20]),
15051507
&BlockHeaderHash([0x2; 32]),
1508+
false, // don't resolve the above chain tip since it doesn't exist
15061509
txid,
15071510
tx_bytes,
15081511
tx_fee,
@@ -1539,12 +1542,15 @@ fn mempool_db_load_store_replace_tx(#[case] behavior: MempoolCollectionBehavior)
15391542
assert_eq!(tx_info.metadata.origin_nonce, origin_nonce);
15401543
assert_eq!(tx_info.metadata.sponsor_address, sponsor_address);
15411544
assert_eq!(tx_info.metadata.sponsor_nonce, sponsor_nonce);
1542-
assert_eq!(tx_info.metadata.consensus_hash, ConsensusHash([0x1; 20]));
15431545
assert_eq!(
1544-
tx_info.metadata.block_header_hash,
1546+
tx_info.metadata.tenure_consensus_hash,
1547+
ConsensusHash([0x1; 20])
1548+
);
1549+
assert_eq!(
1550+
tx_info.metadata.tenure_block_header_hash,
15451551
BlockHeaderHash([0x2; 32])
15461552
);
1547-
assert_eq!(tx_info.metadata.block_height, height);
1553+
assert_eq!(tx_info.metadata.coinbase_height, height);
15481554

15491555
// test replace-by-fee with a lower fee
15501556
let old_txid = txid;
@@ -1563,6 +1569,7 @@ fn mempool_db_load_store_replace_tx(#[case] behavior: MempoolCollectionBehavior)
15631569
&mut chainstate,
15641570
&ConsensusHash([0x1; 20]),
15651571
&BlockHeaderHash([0x2; 32]),
1572+
false, // don't resolve the above chain tip since it doesn't exist
15661573
txid,
15671574
tx_bytes,
15681575
tx_fee,
@@ -1622,7 +1629,7 @@ fn mempool_db_load_store_replace_tx(#[case] behavior: MempoolCollectionBehavior)
16221629
let mut mempool_tx = mempool.tx_begin().unwrap();
16231630
match behavior {
16241631
MempoolCollectionBehavior::ByStacksHeight => {
1625-
MemPoolDB::garbage_collect_by_height(&mut mempool_tx, 101, None)
1632+
MemPoolDB::garbage_collect_by_coinbase_height(&mut mempool_tx, 101, None)
16261633
}
16271634
MempoolCollectionBehavior::ByReceiveTime => {
16281635
let test_max_age = Duration::from_secs(1);
@@ -1712,6 +1719,7 @@ fn mempool_db_test_rbf() {
17121719
&mut chainstate,
17131720
&ConsensusHash([0x1; 20]),
17141721
&BlockHeaderHash([0x2; 32]),
1722+
false, // don't resolve the above chain tip since it doesn't exist
17151723
txid,
17161724
tx_bytes,
17171725
tx_fee,
@@ -1761,6 +1769,7 @@ fn mempool_db_test_rbf() {
17611769
&mut chainstate,
17621770
&ConsensusHash([0x1; 20]),
17631771
&BlockHeaderHash([0x2; 32]),
1772+
false, // don't resolve the above chain tip since it doesn't exist
17641773
txid,
17651774
tx_bytes,
17661775
tx_fee,
@@ -1843,6 +1852,7 @@ fn test_add_txs_bloom_filter() {
18431852
&mut chainstate,
18441853
&ConsensusHash([0x1 + (block_height as u8); 20]),
18451854
&BlockHeaderHash([0x2 + (block_height as u8); 32]),
1855+
false, // don't resolve the above chain tip since it doesn't exist
18461856
txid,
18471857
tx_bytes,
18481858
tx_fee,
@@ -1953,6 +1963,7 @@ fn test_txtags() {
19531963
&mut chainstate,
19541964
&ConsensusHash([0x1 + (block_height as u8); 20]),
19551965
&BlockHeaderHash([0x2 + (block_height as u8); 32]),
1966+
false, // don't resolve the above chain tip since it doesn't exist
19561967
txid,
19571968
tx_bytes,
19581969
tx_fee,
@@ -2046,6 +2057,7 @@ fn test_make_mempool_sync_data() {
20462057
&mut chainstate,
20472058
&ConsensusHash([0x1 + (block_height as u8); 20]),
20482059
&BlockHeaderHash([0x2 + (block_height as u8); 32]),
2060+
false, // don't resolve the above chain tip since it doesn't exist
20492061
txid.clone(),
20502062
tx_bytes,
20512063
tx_fee,
@@ -2084,7 +2096,7 @@ fn test_make_mempool_sync_data() {
20842096
let recent_txids = mempool.get_bloom_txids().unwrap();
20852097
assert!(recent_txids.len() <= MAX_BLOOM_COUNTER_TXS as usize);
20862098

2087-
let max_height = MemPoolDB::get_max_height(mempool.conn())
2099+
let max_height = MemPoolDB::get_max_coinbase_height(mempool.conn())
20882100
.unwrap()
20892101
.unwrap_or(0);
20902102
eprintln!(
@@ -2223,6 +2235,7 @@ fn test_find_next_missing_transactions() {
22232235
&mut chainstate,
22242236
&ConsensusHash([0x1 + (block_height as u8); 20]),
22252237
&BlockHeaderHash([0x2 + (block_height as u8); 32]),
2238+
false, // don't resolve the above chain tip since it doesn't exist
22262239
txid.clone(),
22272240
tx_bytes,
22282241
tx_fee,
@@ -2492,6 +2505,7 @@ fn test_drop_and_blacklist_txs_by_time() {
24922505
&mut chainstate,
24932506
&ConsensusHash([0x1 + (block_height as u8); 20]),
24942507
&BlockHeaderHash([0x2 + (block_height as u8); 32]),
2508+
false, // don't resolve the above chain tip since it doesn't exist
24952509
txid.clone(),
24962510
tx_bytes,
24972511
tx_fee,
@@ -2611,6 +2625,7 @@ fn test_drop_and_blacklist_txs_by_size() {
26112625
&mut chainstate,
26122626
&ConsensusHash([0x1 + (block_height as u8); 20]),
26132627
&BlockHeaderHash([0x2 + (block_height as u8); 32]),
2628+
false, // don't resolve the above chain tip since it doesn't exist
26142629
txid.clone(),
26152630
tx_bytes,
26162631
tx_fee,
@@ -2728,6 +2743,7 @@ fn test_filter_txs_by_type() {
27282743
&mut chainstate,
27292744
&b_2.0,
27302745
&b_2.1,
2746+
true,
27312747
txid.clone(),
27322748
tx_bytes,
27332749
tx_fee,
@@ -2763,7 +2779,6 @@ fn test_filter_txs_by_type() {
27632779
.iterate_candidates::<_, ChainstateError, _>(
27642780
clarity_conn,
27652781
&mut tx_events,
2766-
2,
27672782
mempool_settings.clone(),
27682783
|_, available_tx, _| {
27692784
count_txs += 1;
@@ -2799,7 +2814,6 @@ fn test_filter_txs_by_type() {
27992814
.iterate_candidates::<_, ChainstateError, _>(
28002815
clarity_conn,
28012816
&mut tx_events,
2802-
2,
28032817
mempool_settings.clone(),
28042818
|_, available_tx, _| {
28052819
count_txs += 1;

0 commit comments

Comments
 (0)