@@ -8,6 +8,8 @@ use cuprate_consensus_context::{
88} ;
99use cuprate_types:: Chain ;
1010
11+ use proptest:: { collection:: vec, prelude:: * } ;
12+
1113pub ( crate ) const TEST_WEIGHT_CONFIG : BlockWeightsCacheConfig =
1214 BlockWeightsCacheConfig :: new ( 100 , 5000 ) ;
1315
@@ -60,12 +62,12 @@ async fn pop_blocks_greater_than_window() -> Result<(), tower::BoxError> {
6062
6163 let old_cache = weight_cache. clone ( ) ;
6264
63- weight_cache . new_block ( 5000 , 0 , 0 ) ;
64- weight_cache. new_block ( 5001 , 0 , 0 ) ;
65- weight_cache . new_block ( 5002 , 0 , 0 ) ;
65+ for i in 0 .. 4999 {
66+ weight_cache. new_block ( 5000 + i , 0 , 0 ) ;
67+ }
6668
6769 weight_cache
68- . pop_blocks_main_chain ( 3 , database)
70+ . pop_blocks_main_chain ( 4999 , database)
6971 . await
7072 . unwrap ( ) ;
7173
@@ -108,6 +110,40 @@ async fn pop_blocks_less_than_window() -> Result<(), tower::BoxError> {
108110 Ok ( ( ) )
109111}
110112
113+ #[ tokio:: test]
114+ async fn pop_blocks_on_window ( ) -> Result < ( ) , tower:: BoxError > {
115+ let mut db_builder = DummyDatabaseBuilder :: default ( ) ;
116+ for weight in 1 ..=4999 {
117+ let block = DummyBlockExtendedHeader :: default ( ) . with_weight_into ( weight, weight) ;
118+ db_builder. add_block ( block) ;
119+ }
120+
121+ let database = db_builder. finish ( None ) ;
122+
123+ let mut weight_cache = BlockWeightsCache :: init_from_chain_height (
124+ 4999 ,
125+ TEST_WEIGHT_CONFIG ,
126+ database. clone ( ) ,
127+ Chain :: Main ,
128+ )
129+ . await ?;
130+
131+ let old_cache = weight_cache. clone ( ) ;
132+
133+ weight_cache. new_block ( 4999 , 0 , 0 ) ;
134+ weight_cache. new_block ( 5000 , 0 , 0 ) ;
135+ weight_cache. new_block ( 5001 , 0 , 0 ) ;
136+
137+ weight_cache
138+ . pop_blocks_main_chain ( 3 , database)
139+ . await
140+ . unwrap ( ) ;
141+
142+ assert_eq ! ( weight_cache, old_cache) ;
143+
144+ Ok ( ( ) )
145+ }
146+
111147#[ tokio:: test]
112148async fn weight_cache_calculates_correct_median ( ) -> Result < ( ) , tower:: BoxError > {
113149 let mut db_builder = DummyDatabaseBuilder :: default ( ) ;
@@ -167,4 +203,41 @@ async fn calc_bw_ltw_2850000_3050000() {
167203 }
168204}
169205
170- // TODO: protests
206+ proptest ! {
207+ #[ test]
208+ fn pop_blocks_proptest(
209+ db_blocks in vec( any:: <( usize , usize ) >( ) , 1 ..10_000 ) ,
210+ new_blocks in vec( any:: <( usize , usize ) >( ) , 0 ..5_000 ) ,
211+ ) {
212+ let mut db_builder = DummyDatabaseBuilder :: default ( ) ;
213+ let db_len = db_blocks. len( ) ;
214+
215+ for ( weight, ltw) in db_blocks {
216+ let block = DummyBlockExtendedHeader :: default ( ) . with_weight_into( weight, ltw) ;
217+ db_builder. add_block( block) ;
218+ }
219+
220+ tokio_test:: block_on( async move {
221+ let db = db_builder. finish( None ) ;
222+ let mut weight_cache = BlockWeightsCache :: init_from_chain_height(
223+ db_len,
224+ TEST_WEIGHT_CONFIG ,
225+ db. clone( ) ,
226+ Chain :: Main ,
227+ )
228+ . await . unwrap( ) ;
229+
230+ let cloned_cache = weight_cache. clone( ) ;
231+ let new_blocks_len = new_blocks. len( ) ;
232+
233+ for ( i, ( weight, ltw) ) in new_blocks. into_iter( ) . enumerate( ) {
234+ weight_cache. new_block( db_len + i, weight, ltw) ;
235+ }
236+
237+ weight_cache. pop_blocks_main_chain( new_blocks_len, db) . await . unwrap( ) ;
238+
239+ prop_assert_eq!( weight_cache, cloned_cache) ;
240+ Ok ( ( ) )
241+ } ) ?;
242+ }
243+ }
0 commit comments