Skip to content

Commit 71cb039

Browse files
committed
test: Add integration test for mock mining
1 parent 75baf49 commit 71cb039

File tree

4 files changed

+321
-16
lines changed

4 files changed

+321
-16
lines changed

.github/workflows/bitcoin-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ jobs:
9797
- tests::nakamoto_integrations::check_block_info
9898
- tests::nakamoto_integrations::check_block_info_rewards
9999
- tests::nakamoto_integrations::continue_tenure_extend
100+
- tests::nakamoto_integrations::mock_mining
100101
# Do not run this one until we figure out why it fails in CI
101102
# - tests::neon_integrations::bitcoin_reorg_flap
102103
# - tests::neon_integrations::bitcoin_reorg_flap_with_follower

testnet/stacks-node/src/nakamoto_node/miner.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,32 @@ impl BlockMinerThread {
222222
// now, actually run this tenure
223223
loop {
224224
let new_block = loop {
225+
if self.config.get_node_config(false).mock_mining {
226+
let burn_db_path = self.config.get_burn_db_file_path();
227+
let mut burn_db = SortitionDB::open(
228+
&burn_db_path,
229+
true,
230+
self.burnchain.pox_constants.clone(),
231+
)
232+
.expect("FATAL: could not open sortition DB");
233+
let burn_tip_changed = self.check_burn_tip_changed(&burn_db);
234+
let mut chain_state = neon_node::open_chainstate_with_faults(&self.config)
235+
.expect("FATAL: could not open chainstate DB");
236+
match burn_tip_changed
237+
.and_then(|_| self.load_block_parent_info(&mut burn_db, &mut chain_state))
238+
{
239+
Ok(..) => {}
240+
Err(NakamotoNodeError::ParentNotFound) => {
241+
info!("Mock miner has not processed parent block yet, sleeping and trying again");
242+
thread::sleep(Duration::from_millis(ABORT_TRY_AGAIN_MS));
243+
continue;
244+
}
245+
Err(e) => {
246+
warn!("Mock miner failed to load parent info: {e:?}");
247+
return Err(e);
248+
}
249+
}
250+
}
225251
match self.mine_block(&stackerdbs) {
226252
Ok(x) => break Some(x),
227253
Err(NakamotoNodeError::MiningFailure(ChainstateError::MinerAborted)) => {
@@ -401,6 +427,10 @@ impl BlockMinerThread {
401427
));
402428
};
403429

430+
if self.config.get_node_config(false).mock_mining {
431+
return Ok((reward_set, Vec::new()));
432+
}
433+
404434
let miner_privkey_as_scalar = Scalar::from(miner_privkey.as_slice().clone());
405435
let mut coordinator =
406436
SignCoordinator::new(&reward_set, miner_privkey_as_scalar, &self.config).map_err(
@@ -411,10 +441,6 @@ impl BlockMinerThread {
411441
},
412442
)?;
413443

414-
if self.config.get_node_config(false).mock_mining {
415-
return Ok((reward_set, Vec::new()));
416-
}
417-
418444
*attempts += 1;
419445
let signature = coordinator.begin_sign_v0(
420446
new_block,

testnet/stacks-node/src/nakamoto_node/relayer.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ impl RelayerThread {
794794

795795
fn continue_tenure(&mut self, new_burn_view: ConsensusHash) -> Result<(), NakamotoNodeError> {
796796
if let Err(e) = self.stop_tenure() {
797-
error!("Relayer: Failed to stop tenure: {:?}", e);
797+
error!("Relayer: Failed to stop tenure: {e:?}");
798798
return Ok(());
799799
}
800800
debug!("Relayer: successfully stopped tenure.");
@@ -867,7 +867,7 @@ impl RelayerThread {
867867
debug!("Relayer: successfully started new tenure.");
868868
}
869869
Err(e) => {
870-
error!("Relayer: Failed to start new tenure: {:?}", e);
870+
error!("Relayer: Failed to start new tenure: {e:?}");
871871
}
872872
}
873873
Ok(())
@@ -879,13 +879,11 @@ impl RelayerThread {
879879
burn_hash: BurnchainHeaderHash,
880880
committed_index_hash: StacksBlockId,
881881
) -> bool {
882-
let miner_instruction =
883-
match self.process_sortition(consensus_hash, burn_hash, committed_index_hash) {
884-
Ok(mi) => mi,
885-
Err(_) => {
886-
return false;
887-
}
888-
};
882+
let Ok(miner_instruction) =
883+
self.process_sortition(consensus_hash, burn_hash, committed_index_hash)
884+
else {
885+
return false;
886+
};
889887

890888
match miner_instruction {
891889
MinerDirective::BeginTenure {
@@ -901,7 +899,7 @@ impl RelayerThread {
901899
debug!("Relayer: successfully started new tenure.");
902900
}
903901
Err(e) => {
904-
error!("Relayer: Failed to start new tenure: {:?}", e);
902+
error!("Relayer: Failed to start new tenure: {e:?}");
905903
}
906904
},
907905
MinerDirective::ContinueTenure { new_burn_view } => {
@@ -910,7 +908,7 @@ impl RelayerThread {
910908
debug!("Relayer: successfully handled continue tenure.");
911909
}
912910
Err(e) => {
913-
error!("Relayer: Failed to continue tenure: {:?}", e);
911+
error!("Relayer: Failed to continue tenure: {e:?}");
914912
return false;
915913
}
916914
}
@@ -920,7 +918,7 @@ impl RelayerThread {
920918
debug!("Relayer: successfully stopped tenure.");
921919
}
922920
Err(e) => {
923-
error!("Relayer: Failed to stop tenure: {:?}", e);
921+
error!("Relayer: Failed to stop tenure: {e:?}");
924922
}
925923
},
926924
}

0 commit comments

Comments
 (0)