Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable mock mining in Epoch 3.0 #5029

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/bitcoin-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ jobs:
- tests::nakamoto_integrations::check_block_info
- tests::nakamoto_integrations::check_block_info_rewards
- tests::nakamoto_integrations::continue_tenure_extend
- tests::nakamoto_integrations::mock_mining
- tests::nakamoto_integrations::multiple_miners
# Do not run this one until we figure out why it fails in CI
# - tests::neon_integrations::bitcoin_reorg_flap
Expand Down
34 changes: 34 additions & 0 deletions testnet/stacks-node/src/nakamoto_node/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,35 @@ impl BlockMinerThread {
// now, actually run this tenure
loop {
let new_block = loop {
// If we're mock mining, we may not have processed the block that the
// actual tenure winner committed to yet. So, before attempting to
// mock mine, check if the parent is processed.
if self.config.get_node_config(false).mock_mining {
kantai marked this conversation as resolved.
Show resolved Hide resolved
let burn_db_path = self.config.get_burn_db_file_path();
let mut burn_db = SortitionDB::open(
&burn_db_path,
true,
self.burnchain.pox_constants.clone(),
)
.expect("FATAL: could not open sortition DB");
let burn_tip_changed = self.check_burn_tip_changed(&burn_db);
let mut chain_state = neon_node::open_chainstate_with_faults(&self.config)
.expect("FATAL: could not open chainstate DB");
match burn_tip_changed
.and_then(|_| self.load_block_parent_info(&mut burn_db, &mut chain_state))
{
Ok(..) => {}
Err(NakamotoNodeError::ParentNotFound) => {
info!("Mock miner has not processed parent block yet, sleeping and trying again");
thread::sleep(Duration::from_millis(ABORT_TRY_AGAIN_MS));
continue;
}
Err(e) => {
warn!("Mock miner failed to load parent info: {e:?}");
return Err(e);
}
}
}
match self.mine_block(&stackerdbs) {
Ok(x) => break Some(x),
Err(NakamotoNodeError::MiningFailure(ChainstateError::MinerAborted)) => {
Expand Down Expand Up @@ -430,6 +459,11 @@ impl BlockMinerThread {

let miner_privkey_as_scalar = Scalar::from(miner_privkey.as_slice().clone());
let reward_set = self.load_signer_set()?;

if self.config.get_node_config(false).mock_mining {
return Ok((reward_set, Vec::new()));
}

let mut coordinator =
SignCoordinator::new(&reward_set, miner_privkey_as_scalar, &self.config).map_err(
|e| {
Expand Down
24 changes: 11 additions & 13 deletions testnet/stacks-node/src/nakamoto_node/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ impl RelayerThread {
}

let directive = if sn.sortition {
if won_sortition {
if won_sortition || self.config.get_node_config(false).mock_mining {
MinerDirective::BeginTenure {
parent_tenure_start: committed_index_hash,
burnchain_tip: sn,
Expand Down Expand Up @@ -789,7 +789,7 @@ impl RelayerThread {

fn continue_tenure(&mut self, new_burn_view: ConsensusHash) -> Result<(), NakamotoNodeError> {
if let Err(e) = self.stop_tenure() {
error!("Relayer: Failed to stop tenure: {:?}", e);
error!("Relayer: Failed to stop tenure: {e:?}");
return Ok(());
}
debug!("Relayer: successfully stopped tenure.");
Expand Down Expand Up @@ -862,7 +862,7 @@ impl RelayerThread {
debug!("Relayer: successfully started new tenure.");
}
Err(e) => {
error!("Relayer: Failed to start new tenure: {:?}", e);
error!("Relayer: Failed to start new tenure: {e:?}");
}
}
Ok(())
Expand All @@ -874,13 +874,11 @@ impl RelayerThread {
burn_hash: BurnchainHeaderHash,
committed_index_hash: StacksBlockId,
) -> bool {
let miner_instruction =
match self.process_sortition(consensus_hash, burn_hash, committed_index_hash) {
Ok(mi) => mi,
Err(_) => {
return false;
}
};
let Ok(miner_instruction) =
self.process_sortition(consensus_hash, burn_hash, committed_index_hash)
else {
return false;
};

match miner_instruction {
MinerDirective::BeginTenure {
Expand All @@ -896,7 +894,7 @@ impl RelayerThread {
debug!("Relayer: successfully started new tenure.");
}
Err(e) => {
error!("Relayer: Failed to start new tenure: {:?}", e);
error!("Relayer: Failed to start new tenure: {e:?}");
}
},
MinerDirective::ContinueTenure { new_burn_view } => {
Expand All @@ -905,7 +903,7 @@ impl RelayerThread {
debug!("Relayer: successfully handled continue tenure.");
}
Err(e) => {
error!("Relayer: Failed to continue tenure: {:?}", e);
error!("Relayer: Failed to continue tenure: {e:?}");
return false;
}
}
Expand All @@ -915,7 +913,7 @@ impl RelayerThread {
debug!("Relayer: successfully stopped tenure.");
}
Err(e) => {
error!("Relayer: Failed to stop tenure: {:?}", e);
error!("Relayer: Failed to stop tenure: {e:?}");
}
},
}
Expand Down
Loading