Skip to content

Commit 96d3f78

Browse files
authored
Merge pull request #5406 from stacks-network/pause-after-rejection
feat: add pause after block rejections
2 parents f317cd1 + 6247051 commit 96d3f78

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

testnet/stacks-node/src/config.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ pub const OP_TX_ANY_ESTIM_SIZE: u64 = fmax!(
8686
const DEFAULT_MAX_RBF_RATE: u64 = 150; // 1.5x
8787
const DEFAULT_RBF_FEE_RATE_INCREMENT: u64 = 5;
8888
const INV_REWARD_CYCLES_TESTNET: u64 = 6;
89-
const DEFAULT_MIN_TIME_BETWEEN_BLOCKS_MS: u64 = 1000;
89+
const DEFAULT_MIN_TIME_BETWEEN_BLOCKS_MS: u64 = 1_000;
90+
const DEFAULT_FIRST_REJECTION_PAUSE_MS: u64 = 5_000;
91+
const DEFAULT_SUBSEQUENT_REJECTION_PAUSE_MS: u64 = 10_000;
9092

9193
#[derive(Clone, Deserialize, Default, Debug)]
9294
#[serde(deny_unknown_fields)]
@@ -2183,6 +2185,10 @@ pub struct MinerConfig {
21832185
/// The minimum time to wait between mining blocks in milliseconds. The value must be greater than or equal to 1000 ms because if a block is mined
21842186
/// within the same second as its parent, it will be rejected by the signers.
21852187
pub min_time_between_blocks_ms: u64,
2188+
/// Time in milliseconds to pause after receiving the first threshold rejection, before proposing a new block.
2189+
pub first_rejection_pause_ms: u64,
2190+
/// Time in milliseconds to pause after receiving subsequent threshold rejections, before proposing a new block.
2191+
pub subsequent_rejection_pause_ms: u64,
21862192
}
21872193

21882194
impl Default for MinerConfig {
@@ -2213,6 +2219,8 @@ impl Default for MinerConfig {
22132219
max_reorg_depth: 3,
22142220
pre_nakamoto_mock_signing: false, // Should only default true if mining key is set
22152221
min_time_between_blocks_ms: DEFAULT_MIN_TIME_BETWEEN_BLOCKS_MS,
2222+
first_rejection_pause_ms: DEFAULT_FIRST_REJECTION_PAUSE_MS,
2223+
subsequent_rejection_pause_ms: DEFAULT_SUBSEQUENT_REJECTION_PAUSE_MS,
22162224
}
22172225
}
22182226
}
@@ -2575,6 +2583,8 @@ pub struct MinerConfigFile {
25752583
pub max_reorg_depth: Option<u64>,
25762584
pub pre_nakamoto_mock_signing: Option<bool>,
25772585
pub min_time_between_blocks_ms: Option<u64>,
2586+
pub first_rejection_pause_ms: Option<u64>,
2587+
pub subsequent_rejection_pause_ms: Option<u64>,
25782588
}
25792589

25802590
impl MinerConfigFile {
@@ -2688,6 +2698,8 @@ impl MinerConfigFile {
26882698
} else {
26892699
ms
26902700
}).unwrap_or(miner_default_config.min_time_between_blocks_ms),
2701+
first_rejection_pause_ms: self.first_rejection_pause_ms.unwrap_or(miner_default_config.first_rejection_pause_ms),
2702+
subsequent_rejection_pause_ms: self.subsequent_rejection_pause_ms.unwrap_or(miner_default_config.subsequent_rejection_pause_ms),
26912703
})
26922704
}
26932705
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ impl BlockMinerThread {
283283
}
284284
let mut stackerdbs = StackerDBs::connect(&self.config.get_stacker_db_file_path(), true)
285285
.map_err(|e| NakamotoNodeError::MiningFailure(ChainstateError::NetError(e)))?;
286+
let mut last_block_rejected = false;
286287

287288
// now, actually run this tenure
288289
loop {
@@ -386,15 +387,25 @@ impl BlockMinerThread {
386387
return Err(e);
387388
}
388389
_ => {
389-
error!("Error while gathering signatures: {e:?}. Will try mining again.";
390+
// Sleep for a bit to allow signers to catch up
391+
let pause_ms = if last_block_rejected {
392+
self.config.miner.subsequent_rejection_pause_ms
393+
} else {
394+
self.config.miner.first_rejection_pause_ms
395+
};
396+
397+
error!("Error while gathering signatures: {e:?}. Will try mining again in {pause_ms}.";
390398
"signer_sighash" => %new_block.header.signer_signature_hash(),
391399
"block_height" => new_block.header.chain_length,
392400
"consensus_hash" => %new_block.header.consensus_hash,
393401
);
402+
thread::sleep(Duration::from_millis(pause_ms));
403+
last_block_rejected = true;
394404
continue;
395405
}
396406
},
397407
};
408+
last_block_rejected = false;
398409

399410
new_block.header.signer_signature = signer_signature;
400411
if let Err(e) = self.broadcast(new_block.clone(), reward_set, &stackerdbs) {

0 commit comments

Comments
 (0)