From 07c6ac84d1bc1deddbd2da5d4508e49b76bbeef1 Mon Sep 17 00:00:00 2001 From: Jude Nelson Date: Mon, 6 May 2024 12:04:41 -0400 Subject: [PATCH] chore: add unit test --- stackslib/src/net/inv/epoch2x.rs | 10 +++--- stackslib/src/net/tests/inv/epoch2x.rs | 50 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/stackslib/src/net/inv/epoch2x.rs b/stackslib/src/net/inv/epoch2x.rs index c0dd041523..cd853f6874 100644 --- a/stackslib/src/net/inv/epoch2x.rs +++ b/stackslib/src/net/inv/epoch2x.rs @@ -1753,7 +1753,7 @@ impl PeerNetwork { } /// Determine at which reward cycle to begin scanning inventories - fn get_block_scan_start(&self, sortdb: &SortitionDB) -> u64 { + pub(crate) fn get_block_scan_start(&self, sortdb: &SortitionDB) -> u64 { // see if the stacks tip affirmation map and heaviest affirmation map diverge. If so, then // start scaning at the reward cycle just before that. let am_rescan_rc = self @@ -1786,10 +1786,12 @@ impl PeerNetwork { let rescan_rc = cmp::min(am_rescan_rc, start_reward_cycle); test_debug!( - "begin blocks inv scan at {} = min({},{})", + "begin blocks inv scan at {} = min({},{}) stacks_tip_am={} heaviest_am={}", rescan_rc, - stacks_tip_rc, - am_rescan_rc + am_rescan_rc, + start_reward_cycle, + &self.stacks_tip_affirmation_map, + &self.heaviest_affirmation_map ); rescan_rc } diff --git a/stackslib/src/net/tests/inv/epoch2x.rs b/stackslib/src/net/tests/inv/epoch2x.rs index a2b2da5bb0..bd21eee96b 100644 --- a/stackslib/src/net/tests/inv/epoch2x.rs +++ b/stackslib/src/net/tests/inv/epoch2x.rs @@ -1241,6 +1241,56 @@ fn test_sync_inv_diagnose_nack() { ); } +#[test] +fn test_inv_sync_start_reward_cycle() { + let mut peer_1_config = TestPeerConfig::new(function_name!(), 0, 0); + peer_1_config.connection_opts.inv_reward_cycles = 0; + + let mut peer_1 = TestPeer::new(peer_1_config); + + let num_blocks = (GETPOXINV_MAX_BITLEN * 2) as u64; + for i in 0..num_blocks { + let (burn_ops, stacks_block, microblocks) = peer_1.make_default_tenure(); + peer_1.next_burnchain_block(burn_ops.clone()); + peer_1.process_stacks_epoch_at_tip(&stacks_block, µblocks); + } + + let _ = peer_1.step(); + + let block_scan_start = peer_1 + .network + .get_block_scan_start(peer_1.sortdb.as_ref().unwrap()); + assert_eq!(block_scan_start, 7); + + peer_1.network.connection_opts.inv_reward_cycles = 1; + + let block_scan_start = peer_1 + .network + .get_block_scan_start(peer_1.sortdb.as_ref().unwrap()); + assert_eq!(block_scan_start, 7); + + peer_1.network.connection_opts.inv_reward_cycles = 2; + + let block_scan_start = peer_1 + .network + .get_block_scan_start(peer_1.sortdb.as_ref().unwrap()); + assert_eq!(block_scan_start, 6); + + peer_1.network.connection_opts.inv_reward_cycles = 3; + + let block_scan_start = peer_1 + .network + .get_block_scan_start(peer_1.sortdb.as_ref().unwrap()); + assert_eq!(block_scan_start, 5); + + peer_1.network.connection_opts.inv_reward_cycles = 300; + + let block_scan_start = peer_1 + .network + .get_block_scan_start(peer_1.sortdb.as_ref().unwrap()); + assert_eq!(block_scan_start, 0); +} + #[test] #[ignore] fn test_sync_inv_2_peers_plain() {