Skip to content

Commit 1d61f35

Browse files
authored
Merge pull request #5029 from jbencin/feat/naka-mocka
feat: Enable mock mining in Epoch 3.0
2 parents f49e69a + fe7b1a6 commit 1d61f35

File tree

4 files changed

+326
-13
lines changed

4 files changed

+326
-13
lines changed

.github/workflows/bitcoin-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ jobs:
101101
- tests::nakamoto_integrations::check_block_info
102102
- tests::nakamoto_integrations::check_block_info_rewards
103103
- tests::nakamoto_integrations::continue_tenure_extend
104+
- tests::nakamoto_integrations::mock_mining
104105
- tests::nakamoto_integrations::multiple_miners
105106
# Do not run this one until we figure out why it fails in CI
106107
# - tests::neon_integrations::bitcoin_reorg_flap

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,35 @@ impl BlockMinerThread {
224224
// now, actually run this tenure
225225
loop {
226226
let new_block = loop {
227+
// If we're mock mining, we may not have processed the block that the
228+
// actual tenure winner committed to yet. So, before attempting to
229+
// mock mine, check if the parent is processed.
230+
if self.config.get_node_config(false).mock_mining {
231+
let burn_db_path = self.config.get_burn_db_file_path();
232+
let mut burn_db = SortitionDB::open(
233+
&burn_db_path,
234+
true,
235+
self.burnchain.pox_constants.clone(),
236+
)
237+
.expect("FATAL: could not open sortition DB");
238+
let burn_tip_changed = self.check_burn_tip_changed(&burn_db);
239+
let mut chain_state = neon_node::open_chainstate_with_faults(&self.config)
240+
.expect("FATAL: could not open chainstate DB");
241+
match burn_tip_changed
242+
.and_then(|_| self.load_block_parent_info(&mut burn_db, &mut chain_state))
243+
{
244+
Ok(..) => {}
245+
Err(NakamotoNodeError::ParentNotFound) => {
246+
info!("Mock miner has not processed parent block yet, sleeping and trying again");
247+
thread::sleep(Duration::from_millis(ABORT_TRY_AGAIN_MS));
248+
continue;
249+
}
250+
Err(e) => {
251+
warn!("Mock miner failed to load parent info: {e:?}");
252+
return Err(e);
253+
}
254+
}
255+
}
227256
match self.mine_block(&stackerdbs) {
228257
Ok(x) => break Some(x),
229258
Err(NakamotoNodeError::MiningFailure(ChainstateError::MinerAborted)) => {
@@ -430,6 +459,11 @@ impl BlockMinerThread {
430459

431460
let miner_privkey_as_scalar = Scalar::from(miner_privkey.as_slice().clone());
432461
let reward_set = self.load_signer_set()?;
462+
463+
if self.config.get_node_config(false).mock_mining {
464+
return Ok((reward_set, Vec::new()));
465+
}
466+
433467
let mut coordinator =
434468
SignCoordinator::new(&reward_set, miner_privkey_as_scalar, &self.config).map_err(
435469
|e| {

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl RelayerThread {
407407
}
408408

409409
let directive = if sn.sortition {
410-
if won_sortition {
410+
if won_sortition || self.config.get_node_config(false).mock_mining {
411411
MinerDirective::BeginTenure {
412412
parent_tenure_start: committed_index_hash,
413413
burnchain_tip: sn,
@@ -789,7 +789,7 @@ impl RelayerThread {
789789

790790
fn continue_tenure(&mut self, new_burn_view: ConsensusHash) -> Result<(), NakamotoNodeError> {
791791
if let Err(e) = self.stop_tenure() {
792-
error!("Relayer: Failed to stop tenure: {:?}", e);
792+
error!("Relayer: Failed to stop tenure: {e:?}");
793793
return Ok(());
794794
}
795795
debug!("Relayer: successfully stopped tenure.");
@@ -862,7 +862,7 @@ impl RelayerThread {
862862
debug!("Relayer: successfully started new tenure.");
863863
}
864864
Err(e) => {
865-
error!("Relayer: Failed to start new tenure: {:?}", e);
865+
error!("Relayer: Failed to start new tenure: {e:?}");
866866
}
867867
}
868868
Ok(())
@@ -874,13 +874,11 @@ impl RelayerThread {
874874
burn_hash: BurnchainHeaderHash,
875875
committed_index_hash: StacksBlockId,
876876
) -> bool {
877-
let miner_instruction =
878-
match self.process_sortition(consensus_hash, burn_hash, committed_index_hash) {
879-
Ok(mi) => mi,
880-
Err(_) => {
881-
return false;
882-
}
883-
};
877+
let Ok(miner_instruction) =
878+
self.process_sortition(consensus_hash, burn_hash, committed_index_hash)
879+
else {
880+
return false;
881+
};
884882

885883
match miner_instruction {
886884
MinerDirective::BeginTenure {
@@ -896,7 +894,7 @@ impl RelayerThread {
896894
debug!("Relayer: successfully started new tenure.");
897895
}
898896
Err(e) => {
899-
error!("Relayer: Failed to start new tenure: {:?}", e);
897+
error!("Relayer: Failed to start new tenure: {e:?}");
900898
}
901899
},
902900
MinerDirective::ContinueTenure { new_burn_view } => {
@@ -905,7 +903,7 @@ impl RelayerThread {
905903
debug!("Relayer: successfully handled continue tenure.");
906904
}
907905
Err(e) => {
908-
error!("Relayer: Failed to continue tenure: {:?}", e);
906+
error!("Relayer: Failed to continue tenure: {e:?}");
909907
return false;
910908
}
911909
}
@@ -915,7 +913,7 @@ impl RelayerThread {
915913
debug!("Relayer: successfully stopped tenure.");
916914
}
917915
Err(e) => {
918-
error!("Relayer: Failed to stop tenure: {:?}", e);
916+
error!("Relayer: Failed to stop tenure: {e:?}");
919917
}
920918
},
921919
}

0 commit comments

Comments
 (0)