Skip to content

Commit f5df7ef

Browse files
committed
refactor: separate fault injection into its own function
1 parent c9ec4fb commit f5df7ef

File tree

1 file changed

+21
-16
lines changed
  • testnet/stacks-node/src/nakamoto_node

1 file changed

+21
-16
lines changed

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,24 @@ impl BlockMinerThread {
678678
Ok(filtered_transactions.into_values().collect())
679679
}
680680

681+
/// Fault injection -- possibly fail to broadcast
682+
/// Return true to drop the block
683+
fn fault_injection_broadcast_fail(&self) -> bool {
684+
let drop_prob = self
685+
.config
686+
.node
687+
.fault_injection_block_push_fail_probability
688+
.unwrap_or(0)
689+
.min(100);
690+
let will_drop = if drop_prob > 0 {
691+
let throw: u8 = thread_rng().gen_range(0..100);
692+
throw < drop_prob
693+
} else {
694+
false
695+
};
696+
will_drop
697+
}
698+
681699
/// Store a block to the chainstate, and if successful (it should be since we mined it),
682700
/// broadcast it via the p2p network.
683701
fn broadcast_p2p(
@@ -717,25 +735,12 @@ impl BlockMinerThread {
717735
}
718736

719737
// forward to p2p thread, but do fault injection
720-
let block_id = block.block_id();
721-
let drop_prob = self
722-
.config
723-
.node
724-
.fault_injection_block_push_fail_probability
725-
.unwrap_or(0)
726-
.min(100);
727-
let will_drop = if drop_prob > 0 {
728-
let throw: u8 = thread_rng().gen_range(0..100);
729-
throw < drop_prob
730-
} else {
731-
false
732-
};
733-
734-
if will_drop {
735-
info!("Fault injection: drop block {}", &block_id);
738+
if self.fault_injection_broadcast_fail() {
739+
info!("Fault injection: drop block {}", &block.block_id());
736740
return Ok(());
737741
}
738742

743+
let block_id = block.block_id();
739744
debug!("Broadcasting block {}", &block_id);
740745
if let Err(e) = self.p2p_handle.broadcast_message(
741746
vec![],

0 commit comments

Comments
 (0)