From c95bbbf355abbc6d5854ab1d30b6e07ceda5ed97 Mon Sep 17 00:00:00 2001 From: Jude Nelson Date: Mon, 28 Jun 2021 14:36:24 -0400 Subject: [PATCH] fix: address #2713 by starting processing from the first burnchain block of the reward cycle in which the canonical Stacks chain state starts --- testnet/stacks-node/src/run_loop/neon.rs | 29 +++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/testnet/stacks-node/src/run_loop/neon.rs b/testnet/stacks-node/src/run_loop/neon.rs index 613b4552b..dbecf22ba 100644 --- a/testnet/stacks-node/src/run_loop/neon.rs +++ b/testnet/stacks-node/src/run_loop/neon.rs @@ -314,7 +314,27 @@ impl RunLoop { }; // TODO (hack) instantiate the sortdb in the burnchain - let _ = burnchain.sortdb_mut(); + let sortdb = burnchain.sortdb_mut(); + let mut block_height = { + let (stacks_ch, _) = SortitionDB::get_canonical_stacks_chain_tip_hash(sortdb.conn()) + .expect("BUG: failed to load canonical stacks chain tip hash"); + + match SortitionDB::get_block_snapshot_consensus(sortdb.conn(), &stacks_ch) + .expect("BUG: failed to query sortition DB") + { + Some(sn) => burnchain_config.reward_cycle_to_block_height( + burnchain_config + .block_height_to_reward_cycle(sn.block_height) + .expect("BUG: snapshot preceeds first reward cycle"), + ), + None => { + let sn = SortitionDB::get_first_block_snapshot(&sortdb.conn()) + .expect("BUG: failed to get first-ever block snapshot"); + + sn.block_height + } + } + }; // Start the runloop trace!("Begin run loop"); @@ -330,8 +350,6 @@ impl RunLoop { .unwrap(); } - let mut block_height = 1.max(burnchain_config.first_block_height); - let mut burnchain_height = block_height; let mut num_sortitions_in_last_cycle = 1; let mut learned_burnchain_height = false; @@ -339,6 +357,11 @@ impl RunLoop { // prepare to fetch the first reward cycle! target_burnchain_block_height = burnchain_height + pox_constants.reward_cycle_length as u64; + debug!( + "Begin main runloop starting a burnchain block {}", + block_height + ); + loop { // Orchestrating graceful termination if !should_keep_running.load(Ordering::SeqCst) {