diff --git a/src/testnet/run_loop.rs b/src/testnet/run_loop.rs index 6fb4b71c1..a378033fd 100644 --- a/src/testnet/run_loop.rs +++ b/src/testnet/run_loop.rs @@ -30,6 +30,7 @@ impl RunLoop { } Self { + config, nodes, new_burnchain_state_callback: None, @@ -91,9 +92,8 @@ impl RunLoop { Some(res) => res, None => panic!("Error while initiating genesis tenure") }; - if let Some(cb) = self.new_tenure_callback { - cb(round_index, &first_tenure); - } + + RunLoop::handle_new_tenure_cb(&self.new_tenure_callback, round_index, &first_tenure); // Run the tenure, keep the artifacts let artifacts_from_1st_tenure = match first_tenure.run() { @@ -116,9 +116,7 @@ impl RunLoop { Err(err) => panic!("Error while expecting block #2 from burnchain: {:?}", err) }; - if let Some(cb) = self.new_burnchain_state_callback { - cb(round_index, &burnchain_state); - } + RunLoop::handle_burnchain_state_cb(&self.new_burnchain_state_callback, round_index, &burnchain_state); let mut leader_tenure = None; @@ -141,11 +139,9 @@ impl RunLoop { microblocks.clone(), burnchain_state.db.clone()); - if let Some(cb) = self.new_chain_state_callback { - let index_bhh = anchored_block_1.header.index_block_hash( - &last_sortitioned_block.burn_header_hash); - cb(round_index, &mut node.chain_state, &index_bhh); - } + let index_bhh = anchored_block_1.header.index_block_hash( + &last_sortitioned_block.burn_header_hash); + RunLoop::handle_new_chain_state_cb(&self.new_chain_state_callback, round_index, &mut node.chain_state, &index_bhh); // If the node we're looping on won the sortition, initialize and configure the next tenure if won_sortition { @@ -163,9 +159,7 @@ impl RunLoop { // Run the last initialized tenure let artifacts_from_tenure = match leader_tenure { Some(mut tenure) => { - if let Some(cb) = self.new_tenure_callback { - cb(round_index, &tenure); - } + RunLoop::handle_new_tenure_cb(&self.new_tenure_callback, round_index, &tenure); tenure.run() }, None => None @@ -187,9 +181,7 @@ impl RunLoop { Ok(res) => res, Err(err) => panic!("Error while expecting block from burnchain: {:?}", err) }; - if let Some(cb) = self.new_burnchain_state_callback { - cb(round_index, &burnchain_state); - } + RunLoop::handle_burnchain_state_cb(&self.new_burnchain_state_callback, round_index, &burnchain_state); leader_tenure = None; @@ -213,11 +205,10 @@ impl RunLoop { &burnchain_state.chain_tip.parent_burn_header_hash, microblocks.to_vec(), burnchain_state.db.clone()); - if let Some(cb) = self.new_chain_state_callback { - let index_bhh = anchored_block.header.index_block_hash( - &burnchain_state.chain_tip.burn_header_hash); - cb(round_index, &mut node.chain_state, &index_bhh); - } + let index_bhh = anchored_block.header.index_block_hash( + &burnchain_state.chain_tip.burn_header_hash); + RunLoop::handle_new_chain_state_cb(&self.new_chain_state_callback, round_index, + &mut node.chain_state, &index_bhh); }, }; @@ -242,4 +233,20 @@ impl RunLoop { pub fn apply_on_new_chain_states(&mut self, f: fn(u8, &mut StacksChainState, &BlockHeaderHash)) { self.new_chain_state_callback = Some(f); } + + fn handle_new_tenure_cb(new_tenure_callback: &Option, + round_index: u8, tenure: &LeaderTenure) { + new_tenure_callback.map(|cb| cb(round_index, tenure)); + } + + fn handle_burnchain_state_cb(burn_callback: &Option, + round_index: u8, state: &BurnchainState) { + burn_callback.map(|cb| cb(round_index, state)); + } + + fn handle_new_chain_state_cb(chain_state_callback: &Option, + round_index: u8, state: &mut StacksChainState, id_hash: &BlockHeaderHash) { + chain_state_callback.map(|cb| cb(round_index, state, &id_hash)); + } + }