@@ -74,8 +74,14 @@ fn test_storage_manipulation() {
7474
7575 // Storage version manipulation
7676 assert_eq ! ( store. get_storage_version( ) , Ok ( None ) ) ;
77- assert_eq ! ( store. set_storage_version( 2 ) , Ok ( ( ) ) ) ;
78- assert_eq ! ( store. get_storage_version( ) , Ok ( Some ( 2 ) ) ) ;
77+ assert_eq ! (
78+ store. set_storage_version( ChainstateStorageVersion :: V0 ) ,
79+ Ok ( ( ) )
80+ ) ;
81+ assert_eq ! (
82+ store. get_storage_version( ) ,
83+ Ok ( Some ( ChainstateStorageVersion :: V0 ) )
84+ ) ;
7985
8086 // Store is now empty, the block is not there
8187 assert_eq ! ( store. get_block( block0. get_id( ) ) , Ok ( None ) ) ;
@@ -150,17 +156,19 @@ fn test_storage_manipulation() {
150156#[ test]
151157fn get_set_transactions ( ) {
152158 utils:: concurrency:: model ( || {
153- // Set up the store and initialize the version to 2
159+ // Set up the store and initialize the version to 0
154160 let mut store = TestStore :: new_empty ( ) . unwrap ( ) ;
155- assert_eq ! ( store. set_storage_version( 2 ) , Ok ( ( ) ) ) ;
161+ assert_eq ! (
162+ store. set_storage_version( ChainstateStorageVersion :: V0 ) ,
163+ Ok ( ( ) )
164+ ) ;
156165
157166 // Concurrently bump version and run a transaction that reads the version twice.
158167 let thr1 = {
159168 let store = Store :: clone ( & store) ;
160169 utils:: thread:: spawn ( move || {
161170 let mut tx = store. transaction_rw ( None ) . unwrap ( ) ;
162- let v = tx. get_storage_version ( ) . unwrap ( ) . unwrap ( ) ;
163- tx. set_storage_version ( v + 1 ) . unwrap ( ) ;
171+ tx. set_storage_version ( ChainstateStorageVersion :: V1 ) . unwrap ( ) ;
164172 tx. commit ( ) . unwrap ( ) ;
165173 } )
166174 } ;
@@ -170,80 +178,101 @@ fn get_set_transactions() {
170178 let tx = store. transaction_ro ( ) . unwrap ( ) ;
171179 let v1 = tx. get_storage_version ( ) . unwrap ( ) . unwrap ( ) ;
172180 let v2 = tx. get_storage_version ( ) . unwrap ( ) . unwrap ( ) ;
173- assert ! ( [ 2 , 3 ] . contains( & v1) ) ;
181+ assert ! ( [ ChainstateStorageVersion :: V0 , ChainstateStorageVersion :: V1 ] . contains( & v1) ) ;
174182 assert_eq ! ( v1, v2, "Version query in a transaction inconsistent" ) ;
175183 } )
176184 } ;
177185
178186 let _ = thr0. join ( ) ;
179187 let _ = thr1. join ( ) ;
180- assert_eq ! ( store. get_storage_version( ) , Ok ( Some ( 3 ) ) ) ;
188+ assert_eq ! (
189+ store. get_storage_version( ) ,
190+ Ok ( Some ( ChainstateStorageVersion :: V1 ) )
191+ ) ;
181192 } )
182193}
183194
184- #[ test]
185- fn test_storage_transactions ( ) {
186- utils:: concurrency:: model ( || {
187- // Set up the store and initialize the version to 2
188- let mut store = TestStore :: new_empty ( ) . unwrap ( ) ;
189- assert_eq ! ( store. set_storage_version( 2 ) , Ok ( ( ) ) ) ;
195+ #[ rstest]
196+ #[ trace]
197+ #[ case( Seed :: from_entropy( ) ) ]
198+ fn test_storage_transactions ( #[ case] seed : Seed ) {
199+ utils:: concurrency:: model ( move || {
200+ // Set up the store with empty utxo set
201+ let mut rng = make_seedable_rng ( seed) ;
202+ let store = TestStore :: new_empty ( ) . unwrap ( ) ;
203+ assert ! ( store. read_utxo_set( ) . unwrap( ) . is_empty( ) ) ;
190204
191- // Concurrently bump version by 3 and 5 in two separate threads
205+ let ( utxo1, outpoint1) = create_rand_utxo ( & mut rng, 1 ) ;
206+ let ( utxo2, outpoint2) = create_rand_utxo ( & mut rng, 1 ) ;
207+
208+ let expected_utxo_set = BTreeMap :: from_iter ( [
209+ ( outpoint1. clone ( ) , utxo1. clone ( ) ) ,
210+ ( outpoint2. clone ( ) , utxo2. clone ( ) ) ,
211+ ] ) ;
212+
213+ // Concurrently insert 2 utxo in two separate threads
192214 let thr0 = {
193215 let store = Store :: clone ( & store) ;
194216 utils:: thread:: spawn ( move || {
195217 let mut tx = store. transaction_rw ( None ) . unwrap ( ) ;
196- let v = tx. get_storage_version ( ) . unwrap ( ) . unwrap ( ) ;
197- tx. set_storage_version ( v + 3 ) . unwrap ( ) ;
218+ tx. set_utxo ( & outpoint1, utxo1) . unwrap ( ) ;
198219 tx. commit ( ) . unwrap ( ) ;
199220 } )
200221 } ;
201222 let thr1 = {
202223 let store = Store :: clone ( & store) ;
203224 utils:: thread:: spawn ( move || {
204225 let mut tx = store. transaction_rw ( None ) . unwrap ( ) ;
205- let v = tx. get_storage_version ( ) . unwrap ( ) . unwrap ( ) ;
206- tx. set_storage_version ( v + 5 ) . unwrap ( ) ;
226+ tx. set_utxo ( & outpoint2, utxo2) . unwrap ( ) ;
207227 tx. commit ( ) . unwrap ( ) ;
208228 } )
209229 } ;
210230
211231 let _ = thr0. join ( ) ;
212232 let _ = thr1. join ( ) ;
213- assert_eq ! ( store. get_storage_version ( ) , Ok ( Some ( 10 ) ) ) ;
233+ assert_eq ! ( store. read_utxo_set ( ) , Ok ( expected_utxo_set ) ) ;
214234 } )
215235}
216236
217- #[ test]
218- fn test_storage_transactions_with_result_check ( ) {
219- utils:: concurrency:: model ( || {
220- // Set up the store and initialize the version to 2
221- let mut store = TestStore :: new_empty ( ) . unwrap ( ) ;
222- assert_eq ! ( store. set_storage_version( 2 ) , Ok ( ( ) ) ) ;
237+ #[ rstest]
238+ #[ trace]
239+ #[ case( Seed :: from_entropy( ) ) ]
240+ fn test_storage_transactions_with_result_check ( #[ case] seed : Seed ) {
241+ utils:: concurrency:: model ( move || {
242+ // Set up the store with empty utxo set
243+ let mut rng = make_seedable_rng ( seed) ;
244+ let store = TestStore :: new_empty ( ) . unwrap ( ) ;
245+ assert ! ( store. read_utxo_set( ) . unwrap( ) . is_empty( ) ) ;
246+
247+ let ( utxo1, outpoint1) = create_rand_utxo ( & mut rng, 1 ) ;
248+ let ( utxo2, outpoint2) = create_rand_utxo ( & mut rng, 1 ) ;
249+
250+ let expected_utxo_set = BTreeMap :: from_iter ( [
251+ ( outpoint1. clone ( ) , utxo1. clone ( ) ) ,
252+ ( outpoint2. clone ( ) , utxo2. clone ( ) ) ,
253+ ] ) ;
223254
224- // Concurrently bump version by 3 and 5 in two separate threads
255+ // Concurrently insert 2 utxo in two separate threads
225256 let thr0 = {
226257 let store = Store :: clone ( & store) ;
227258 utils:: thread:: spawn ( move || {
228259 let mut tx = store. transaction_rw ( None ) . unwrap ( ) ;
229- let v = tx. get_storage_version ( ) . unwrap ( ) . unwrap ( ) ;
230- assert ! ( tx. set_storage_version( v + 3 ) . is_ok( ) ) ;
260+ assert ! ( tx. set_utxo( & outpoint1, utxo1) . is_ok( ) ) ;
231261 assert ! ( tx. commit( ) . is_ok( ) ) ;
232262 } )
233263 } ;
234264 let thr1 = {
235265 let store = Store :: clone ( & store) ;
236266 utils:: thread:: spawn ( move || {
237267 let mut tx = store. transaction_rw ( None ) . unwrap ( ) ;
238- let v = tx. get_storage_version ( ) . unwrap ( ) . unwrap ( ) ;
239- assert ! ( tx. set_storage_version( v + 5 ) . is_ok( ) ) ;
268+ assert ! ( tx. set_utxo( & outpoint2, utxo2) . is_ok( ) ) ;
240269 assert ! ( tx. commit( ) . is_ok( ) ) ;
241270 } )
242271 } ;
243272
244273 let _ = thr0. join ( ) ;
245274 let _ = thr1. join ( ) ;
246- assert_eq ! ( store. get_storage_version ( ) , Ok ( Some ( 10 ) ) ) ;
275+ assert_eq ! ( store. read_utxo_set ( ) , Ok ( expected_utxo_set ) ) ;
247276 } )
248277}
249278
0 commit comments