Skip to content

Commit ef96476

Browse files
authored
Merge pull request #5735 from stacks-network/fix/5676
Fix: Wait until in nakamoto to shutdown 2.x block downloader #5676
2 parents 760891f + d5308ce commit ef96476

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

stackslib/src/net/mod.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3384,6 +3384,10 @@ pub mod test {
33843384

33853385
let old_tip = self.network.stacks_tip.clone();
33863386

3387+
// make sure the right state machines run
3388+
let epoch2_passes = self.network.epoch2_state_machine_passes;
3389+
let nakamoto_passes = self.network.nakamoto_state_machine_passes;
3390+
33873391
let ret = self.network.run(
33883392
&indexer,
33893393
&sortdb,
@@ -3396,6 +3400,30 @@ pub mod test {
33963400
&rpc_handler_args,
33973401
);
33983402

3403+
if self.network.get_current_epoch().epoch_id >= StacksEpochId::Epoch30 {
3404+
assert_eq!(
3405+
self.network.nakamoto_state_machine_passes,
3406+
nakamoto_passes + 1
3407+
);
3408+
let epoch2_expected_passes = if self.network.stacks_tip.is_nakamoto
3409+
&& !self.network.connection_opts.force_nakamoto_epoch_transition
3410+
{
3411+
epoch2_passes
3412+
} else {
3413+
epoch2_passes + 1
3414+
};
3415+
assert_eq!(
3416+
self.network.epoch2_state_machine_passes,
3417+
epoch2_expected_passes
3418+
);
3419+
}
3420+
if self
3421+
.network
3422+
.need_epoch2_state_machines(self.network.get_current_epoch().epoch_id)
3423+
{
3424+
assert_eq!(self.network.epoch2_state_machine_passes, epoch2_passes + 1);
3425+
}
3426+
33993427
self.sortdb = Some(sortdb);
34003428
self.stacks_node = Some(stacks_node);
34013429
self.mempool = Some(mempool);
@@ -3467,6 +3495,10 @@ pub mod test {
34673495
.unwrap_or(RPCHandlerArgsType::make_default());
34683496
let old_tip = self.network.stacks_tip.clone();
34693497

3498+
// make sure the right state machines run
3499+
let epoch2_passes = self.network.epoch2_state_machine_passes;
3500+
let nakamoto_passes = self.network.nakamoto_state_machine_passes;
3501+
34703502
let ret = self.network.run(
34713503
&indexer,
34723504
&sortdb,
@@ -3479,6 +3511,30 @@ pub mod test {
34793511
&rpc_handler_args,
34803512
);
34813513

3514+
if self.network.get_current_epoch().epoch_id >= StacksEpochId::Epoch30 {
3515+
assert_eq!(
3516+
self.network.nakamoto_state_machine_passes,
3517+
nakamoto_passes + 1
3518+
);
3519+
let epoch2_expected_passes = if self.network.stacks_tip.is_nakamoto
3520+
&& !self.network.connection_opts.force_nakamoto_epoch_transition
3521+
{
3522+
epoch2_passes
3523+
} else {
3524+
epoch2_passes + 1
3525+
};
3526+
assert_eq!(
3527+
self.network.epoch2_state_machine_passes,
3528+
epoch2_expected_passes
3529+
);
3530+
}
3531+
if self
3532+
.network
3533+
.need_epoch2_state_machines(self.network.get_current_epoch().epoch_id)
3534+
{
3535+
assert_eq!(self.network.epoch2_state_machine_passes, epoch2_passes + 1);
3536+
}
3537+
34823538
self.sortdb = Some(sortdb);
34833539
self.stacks_node = Some(stacks_node);
34843540
self.mempool = Some(mempool);

stackslib/src/net/p2p.rs

Lines changed: 23 additions & 5 deletions
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(),
@@ -3554,6 +3562,15 @@ impl PeerNetwork {
35543562
}
35553563
}
35563564

3565+
/// Check to see if we need to run the epoch 2.x state machines.
3566+
/// This will be true if we're either in epoch 2.5 or lower, OR, if we're in epoch 3.0 or
3567+
/// higher AND the Stacks tip is not yet a Nakamoto block. This latter condition indicates
3568+
/// that the epoch 2.x state machines are still needed to download the final epoch 2.x blocks.
3569+
pub(crate) fn need_epoch2_state_machines(&self, epoch_id: StacksEpochId) -> bool {
3570+
epoch_id < StacksEpochId::Epoch30
3571+
|| (epoch_id >= StacksEpochId::Epoch30 && !self.stacks_tip.is_nakamoto)
3572+
}
3573+
35573574
/// Do the actual work in the state machine.
35583575
/// Return true if we need to prune connections.
35593576
/// This will call the epoch-appropriate network worker
@@ -3582,11 +3599,8 @@ impl PeerNetwork {
35823599

35833600
// in Nakamoto epoch, but we might still be doing epoch 2.x things since Nakamoto does
35843601
// not begin on a reward cycle boundary.
3585-
if cur_epoch.epoch_id == StacksEpochId::Epoch30
3586-
&& (self.burnchain_tip.block_height
3587-
<= cur_epoch.start_height
3588-
+ u64::from(self.burnchain.pox_constants.reward_cycle_length)
3589-
|| self.connection_opts.force_nakamoto_epoch_transition)
3602+
if self.need_epoch2_state_machines(cur_epoch.epoch_id)
3603+
|| self.connection_opts.force_nakamoto_epoch_transition
35903604
{
35913605
debug!(
35923606
"{:?}: run Epoch 2.x work loop in Nakamoto epoch",
@@ -3636,6 +3650,8 @@ impl PeerNetwork {
36363650
ibd: bool,
36373651
network_result: &mut NetworkResult,
36383652
) {
3653+
self.nakamoto_state_machine_passes += 1;
3654+
36393655
// always do an inv sync
36403656
let learned = self.do_network_inv_sync_nakamoto(sortdb, ibd);
36413657
debug!(
@@ -3683,6 +3699,8 @@ impl PeerNetwork {
36833699
ibd: bool,
36843700
network_result: &mut NetworkResult,
36853701
) -> bool {
3702+
self.epoch2_state_machine_passes += 1;
3703+
36863704
// do some Actual Work(tm)
36873705
let mut do_prune = false;
36883706
let mut did_cycle = false;

0 commit comments

Comments
 (0)