Skip to content

Commit f035175

Browse files
committed
chore: make the epoch2x state machine check testable, and extend TestPeer to check it
1 parent e667b82 commit f035175

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

stackslib/src/net/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3314,6 +3314,10 @@ pub mod test {
33143314

33153315
let old_tip = self.network.stacks_tip.clone();
33163316

3317+
// make sure the right state machines run
3318+
let epoch2_passes = self.network.epoch2_state_machine_passes;
3319+
let nakamoto_passes = self.network.nakamoto_state_machine_passes;
3320+
33173321
let ret = self.network.run(
33183322
&indexer,
33193323
&sortdb,
@@ -3326,6 +3330,19 @@ pub mod test {
33263330
&RPCHandlerArgs::default(),
33273331
);
33283332

3333+
if self.network.get_current_epoch().epoch_id >= StacksEpochId::Epoch30 {
3334+
assert_eq!(
3335+
self.network.nakamoto_state_machine_passes,
3336+
nakamoto_passes + 1
3337+
);
3338+
}
3339+
if self
3340+
.network
3341+
.need_epoch2_state_machines(self.network.get_current_epoch().epoch_id)
3342+
{
3343+
assert_eq!(self.network.epoch2_state_machine_passes, epoch2_passes + 1);
3344+
}
3345+
33293346
self.sortdb = Some(sortdb);
33303347
self.stacks_node = Some(stacks_node);
33313348
self.mempool = Some(mempool);
@@ -3394,6 +3411,10 @@ pub mod test {
33943411

33953412
let old_tip = self.network.stacks_tip.clone();
33963413

3414+
// make sure the right state machines run
3415+
let epoch2_passes = self.network.epoch2_state_machine_passes;
3416+
let nakamoto_passes = self.network.nakamoto_state_machine_passes;
3417+
33973418
let ret = self.network.run(
33983419
&indexer,
33993420
&sortdb,
@@ -3406,6 +3427,19 @@ pub mod test {
34063427
&RPCHandlerArgs::default(),
34073428
);
34083429

3430+
if self.network.get_current_epoch().epoch_id >= StacksEpochId::Epoch30 {
3431+
assert_eq!(
3432+
self.network.nakamoto_state_machine_passes,
3433+
nakamoto_passes + 1
3434+
);
3435+
}
3436+
if self
3437+
.network
3438+
.need_epoch2_state_machines(self.network.get_current_epoch().epoch_id)
3439+
{
3440+
assert_eq!(self.network.epoch2_state_machine_passes, epoch2_passes + 1);
3441+
}
3442+
34093443
self.sortdb = Some(sortdb);
34103444
self.stacks_node = Some(stacks_node);
34113445
self.mempool = Some(mempool);

stackslib/src/net/p2p.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,12 @@ pub struct PeerNetwork {
423423
// how many downloader passes have we done?
424424
pub num_downloader_passes: u64,
425425

426+
// number of epoch2 state machine passes
427+
pub(crate) epoch2_state_machine_passes: u128,
428+
429+
// number of nakamoto state machine passes
430+
pub(crate) nakamoto_state_machine_passes: u128,
431+
426432
// to whom did we send a block or microblock stream as part of our anti-entropy protocol, and
427433
// when did we send it?
428434
antientropy_blocks: HashMap<NeighborKey, HashMap<StacksBlockId, u64>>,
@@ -593,6 +599,8 @@ impl PeerNetwork {
593599
num_state_machine_passes: 0,
594600
num_inv_sync_passes: 0,
595601
num_downloader_passes: 0,
602+
epoch2_state_machine_passes: 0,
603+
nakamoto_state_machine_passes: 0,
596604

597605
antientropy_blocks: HashMap::new(),
598606
antientropy_microblocks: HashMap::new(),
@@ -3578,6 +3586,15 @@ impl PeerNetwork {
35783586
}
35793587
}
35803588

3589+
/// Check to see if we need to run the epoch 2.x state machines.
3590+
/// This will be true if we're either in epoch 2.5 or lower, OR, if we're in epoch 3.0 or
3591+
/// higher AND the Stacks tip is not yet a Nakamoto block. This latter condition indicates
3592+
/// that the epoch 2.x state machines are still needed to download the final epoch 2.x blocks.
3593+
pub(crate) fn need_epoch2_state_machines(&self, epoch_id: StacksEpochId) -> bool {
3594+
epoch_id < StacksEpochId::Epoch30
3595+
|| (epoch_id >= StacksEpochId::Epoch30 && !self.stacks_tip.is_nakamoto)
3596+
}
3597+
35813598
/// Do the actual work in the state machine.
35823599
/// Return true if we need to prune connections.
35833600
/// This will call the epoch-appropriate network worker
@@ -3606,7 +3623,7 @@ impl PeerNetwork {
36063623

36073624
// in Nakamoto epoch, but we might still be doing epoch 2.x things since Nakamoto does
36083625
// not begin on a reward cycle boundary.
3609-
if cur_epoch.epoch_id >= StacksEpochId::Epoch30 && !self.stacks_tip.is_nakamoto {
3626+
if self.need_epoch2_state_machines(cur_epoch.epoch_id) {
36103627
debug!(
36113628
"{:?}: run Epoch 2.x work loop in Nakamoto epoch",
36123629
self.get_local_peer()
@@ -3655,6 +3672,8 @@ impl PeerNetwork {
36553672
ibd: bool,
36563673
network_result: &mut NetworkResult,
36573674
) {
3675+
self.nakamoto_state_machine_passes += 1;
3676+
36583677
// always do an inv sync
36593678
let learned = self.do_network_inv_sync_nakamoto(sortdb, ibd);
36603679
debug!(
@@ -3704,6 +3723,8 @@ impl PeerNetwork {
37043723
ibd: bool,
37053724
network_result: &mut NetworkResult,
37063725
) -> bool {
3726+
self.epoch2_state_machine_passes += 1;
3727+
37073728
// do some Actual Work(tm)
37083729
let mut do_prune = false;
37093730
let mut did_cycle = false;

0 commit comments

Comments
 (0)