@@ -58,15 +58,15 @@ fn process_a_trivial_block(#[case] seed: Seed) {
5858#[ rstest]
5959#[ trace]
6060#[ case( Seed :: from_entropy( ) ) ]
61- fn get_locator ( #[ case] seed : Seed ) {
61+ fn get_locator_from_best_block ( #[ case] seed : Seed ) {
6262 utils:: concurrency:: model ( move || {
6363 let mut rng = make_seedable_rng ( seed) ;
6464 let mut btf = TestFramework :: builder ( & mut rng)
6565 // With the heavy checks enabled, this test takes several minutes to complete in debug builds.
6666 . with_chainstate_config ( ChainstateConfig :: new ( ) . with_heavy_checks_enabled ( false ) )
6767 . build ( ) ;
6868
69- let locator = btf. chainstate . get_locator ( ) . unwrap ( ) ;
69+ let locator = btf. chainstate . get_locator_from_best_block ( ) . unwrap ( ) ;
7070 assert_eq ! ( locator. len( ) , 1 ) ;
7171 assert_eq ! ( & locator[ 0 ] , & btf. genesis( ) . get_id( ) ) ;
7272
@@ -79,7 +79,7 @@ fn get_locator(#[case] seed: Seed) {
7979 blocks += new_blocks;
8080
8181 // Check the locator length.
82- let locator = btf. chainstate . get_locator ( ) . unwrap ( ) ;
82+ let locator = btf. chainstate . get_locator_from_best_block ( ) . unwrap ( ) ;
8383 assert_eq ! (
8484 locator. len( ) ,
8585 blocks. next_power_of_two( ) . trailing_zeros( ) as usize + 1
@@ -95,6 +95,12 @@ fn get_locator(#[case] seed: Seed) {
9595 btf. chainstate . get_block_id_from_height ( idx. unwrap ( ) ) . unwrap ( ) . unwrap ( ) ;
9696 assert_eq ! ( & expected, header) ;
9797 }
98+
99+ // Check that get_locator_from_block_id returns the same locator.
100+ let best_block_id = btf. to_chain_block_id ( & btf. chainstate . get_best_block_id ( ) . unwrap ( ) ) ;
101+ let locator_from_block_id =
102+ btf. chainstate . get_locator_from_block_id ( & best_block_id) . unwrap ( ) ;
103+ assert_eq ! ( locator, locator_from_block_id) ;
98104 }
99105 } ) ;
100106}
@@ -134,6 +140,88 @@ fn get_locator_from_height(#[case] seed: Seed) {
134140 btf. chainstate . get_block_id_from_height ( idx. unwrap ( ) ) . unwrap ( ) . unwrap ( ) ;
135141 assert_eq ! ( & expected, header) ;
136142 }
143+
144+ // Check that get_locator_from_block_id returns the same locator.
145+ let block_id = btf. to_chain_block_id (
146+ & btf. chainstate . get_block_id_from_height ( height. into ( ) ) . unwrap ( ) . unwrap ( ) ,
147+ ) ;
148+ let locator_from_block_id =
149+ btf. chainstate . get_locator_from_block_id ( & block_id) . unwrap ( ) ;
150+ assert_eq ! ( locator, locator_from_block_id) ;
151+ }
152+ } ) ;
153+ }
154+
155+ #[ rstest]
156+ #[ trace]
157+ #[ case( Seed :: from_entropy( ) ) ]
158+ fn get_locator_from_block_id ( #[ case] seed : Seed ) {
159+ utils:: concurrency:: model ( move || {
160+ let mut rng = make_seedable_rng ( seed) ;
161+
162+ let mut tf1 = TestFramework :: builder ( & mut rng)
163+ // Heavy checks are useless here and they increase test run time noticeably
164+ . with_chainstate_config ( ChainstateConfig :: new ( ) . with_heavy_checks_enabled ( false ) )
165+ . with_chain_config (
166+ chain:: config:: create_unit_test_config_builder ( )
167+ // Increase max_depth_for_reorg so that we can create a fork more than 1000
168+ // blocks deep.
169+ . max_depth_for_reorg ( 10_000 . into ( ) )
170+ . build ( ) ,
171+ )
172+ . build ( ) ;
173+
174+ let mainchain_len: usize = rng. gen_range ( 1000 ..2000 ) ;
175+ let stalechain_len = rng. gen_range ( mainchain_len / 2 ..mainchain_len) ;
176+ let fork_height = rng. gen_range ( 0 ..stalechain_len) ;
177+
178+ let mainchain_block_ids = tf1
179+ . create_chain_return_ids ( & tf1. genesis ( ) . get_id ( ) . into ( ) , mainchain_len, & mut rng)
180+ . unwrap ( ) ;
181+ let stale_branch_parent_id = if fork_height == 0 {
182+ tf1. genesis ( ) . get_id ( ) . into ( )
183+ } else {
184+ mainchain_block_ids[ fork_height - 1 ]
185+ } ;
186+
187+ let stale_branch_block_ids = tf1
188+ . create_chain_return_ids (
189+ & stale_branch_parent_id,
190+ stalechain_len - fork_height,
191+ & mut rng,
192+ )
193+ . unwrap ( ) ;
194+
195+ let whole_stalechain = mainchain_block_ids[ 0 ..fork_height]
196+ . iter ( )
197+ . chain ( & stale_branch_block_ids[ ..] )
198+ . map ( |block_id| {
199+ tf1. chainstate . get_block ( & tf1. to_chain_block_id ( block_id) ) . unwrap ( ) . unwrap ( )
200+ } ) ;
201+
202+ let mut tf2 = TestFramework :: builder ( & mut rng)
203+ . with_chainstate_config ( ChainstateConfig :: new ( ) . with_heavy_checks_enabled ( false ) )
204+ . build ( ) ;
205+
206+ for block in whole_stalechain {
207+ tf2. chainstate . process_block ( block, BlockSource :: Local ) . unwrap ( ) ;
208+ }
209+
210+ assert_eq ! ( tf1. best_block_id( ) , * mainchain_block_ids. last( ) . unwrap( ) ) ;
211+ assert_eq ! ( tf2. best_block_id( ) , * stale_branch_block_ids. last( ) . unwrap( ) ) ;
212+
213+ for _ in 0 ..8 {
214+ let stale_block_idx = rng. gen_range ( 0 ..stale_branch_block_ids. len ( ) ) ;
215+ let height = ( stale_block_idx + fork_height + 1 ) as u64 ;
216+
217+ let stale_block_id = tf1. to_chain_block_id ( & stale_branch_block_ids[ stale_block_idx] ) ;
218+
219+ let locator_from_id =
220+ tf1. chainstate . get_locator_from_block_id ( & stale_block_id) . unwrap ( ) ;
221+ let locator_from_height_in_tf2 =
222+ tf2. chainstate . get_locator_from_height ( height. into ( ) ) . unwrap ( ) ;
223+
224+ assert_eq ! ( locator_from_id, locator_from_height_in_tf2) ;
137225 }
138226 } ) ;
139227}
@@ -158,7 +246,7 @@ fn get_mainchain_headers_by_locator(#[case] seed: Seed) {
158246
159247 // The locator is from this exact chain, so get_mainchain_headers_by_locator
160248 // should return an empty sequence.
161- let locator = tf. chainstate . get_locator ( ) . unwrap ( ) ;
249+ let locator = tf. chainstate . get_locator_from_best_block ( ) . unwrap ( ) ;
162250 assert_eq ! (
163251 tf. chainstate. get_mainchain_headers_by_locator( & locator, header_limit) . unwrap( ) ,
164252 vec![ ] ,
@@ -211,11 +299,11 @@ fn get_headers_genesis(#[case] seed: Seed) {
211299 let genesis_id: Id < GenBlock > = btf. genesis ( ) . get_id ( ) . into ( ) ;
212300
213301 btf. create_chain ( & genesis_id, rng. gen_range ( 64 ..128 ) , & mut rng) . unwrap ( ) ;
214- let locator_1 = btf. chainstate . get_locator ( ) . unwrap ( ) ;
302+ let locator_1 = btf. chainstate . get_locator_from_best_block ( ) . unwrap ( ) ;
215303
216304 let chain_length = rng. gen_range ( 1200 ..2000 ) ;
217305 btf. create_chain ( & genesis_id, chain_length, & mut rng) . unwrap ( ) ;
218- let locator_2 = btf. chainstate . get_locator ( ) . unwrap ( ) ;
306+ let locator_2 = btf. chainstate . get_locator_from_best_block ( ) . unwrap ( ) ;
219307 assert_ne ! ( locator_1, locator_2) ;
220308 assert ! ( locator_1. len( ) < locator_2. len( ) ) ;
221309
@@ -254,7 +342,7 @@ fn get_headers_branching_chains(#[case] seed: Seed) {
254342 tf. create_chain ( & tf. genesis ( ) . get_id ( ) . into ( ) , common_height, & mut rng) . unwrap ( ) ;
255343
256344 tf. create_chain ( & common_block_id, rng. gen_range ( 100 ..250 ) , & mut rng) . unwrap ( ) ;
257- let locator = tf. chainstate . get_locator ( ) . unwrap ( ) ;
345+ let locator = tf. chainstate . get_locator_from_best_block ( ) . unwrap ( ) ;
258346 tf. create_chain ( & common_block_id, rng. gen_range ( 250 ..500 ) , & mut rng) . unwrap ( ) ;
259347
260348 let headers = tf. chainstate . get_mainchain_headers_by_locator ( & locator, 2000 ) . unwrap ( ) ;
@@ -540,15 +628,15 @@ fn get_headers_different_chains(#[case] seed: Seed) {
540628 tf2. create_chain ( & prev_id, rng. gen_range ( 256 ..512 ) , & mut rng) . unwrap ( ) ;
541629
542630 let header_count_limit = rng. gen_range ( 1000 ..3000 ) ;
543- let locator = tf1. chainstate . get_locator ( ) . unwrap ( ) ;
631+ let locator = tf1. chainstate . get_locator_from_best_block ( ) . unwrap ( ) ;
544632 let headers = tf2
545633 . chainstate
546634 . get_mainchain_headers_by_locator ( & locator, header_count_limit)
547635 . unwrap ( ) ;
548636 let id = * headers[ 0 ] . prev_block_id ( ) ;
549637 tf1. gen_block_index ( & id) ; // This panics if the ID is not found
550638
551- let locator = tf2. chainstate . get_locator ( ) . unwrap ( ) ;
639+ let locator = tf2. chainstate . get_locator_from_best_block ( ) . unwrap ( ) ;
552640 let headers = tf1
553641 . chainstate
554642 . get_mainchain_headers_by_locator ( & locator, header_count_limit)
@@ -898,7 +986,7 @@ fn get_block_ids_as_checkpoints(#[case] seed: Seed) {
898986 let mut rng = make_seedable_rng ( seed) ;
899987 let mut btf = TestFramework :: builder ( & mut rng) . build ( ) ;
900988
901- let locator = btf. chainstate . get_locator ( ) . unwrap ( ) ;
989+ let locator = btf. chainstate . get_locator_from_best_block ( ) . unwrap ( ) ;
902990 assert_eq ! ( locator. len( ) , 1 ) ;
903991 assert_eq ! ( & locator[ 0 ] , & btf. genesis( ) . get_id( ) ) ;
904992
0 commit comments