@@ -104,9 +104,6 @@ pub mod pallet {
104104 #[ pallet:: constant]
105105 type MaxRequests : Get < u32 > ;
106106
107- // Avoid using `HeadersToKeep` directly in the pallet code. Use `headers_to_keep` function
108- // instead.
109-
110107 /// Maximal number of finalized headers to keep in the storage.
111108 ///
112109 /// The setting is there to prevent growing the on-chain state indefinitely. Note
@@ -292,8 +289,14 @@ pub mod pallet {
292289
293290 /// A ring buffer of imported hashes. Ordered by the insertion time.
294291 #[ pallet:: storage]
295- pub ( super ) type ImportedHashes < T : Config < I > , I : ' static = ( ) > =
296- StorageMap < _ , Identity , u32 , BridgedBlockHash < T , I > > ;
292+ pub ( super ) type ImportedHashes < T : Config < I > , I : ' static = ( ) > = StorageMap <
293+ Hasher = Identity ,
294+ Key = u32 ,
295+ Value = BridgedBlockHash < T , I > ,
296+ QueryKind = OptionQuery ,
297+ OnEmpty = GetDefault ,
298+ MaxValues = MaybeHeadersToKeep < T , I > ,
299+ > ;
297300
298301 /// Current ring buffer position.
299302 #[ pallet:: storage]
@@ -302,8 +305,14 @@ pub mod pallet {
302305
303306 /// Relevant fields of imported headers.
304307 #[ pallet:: storage]
305- pub type ImportedHeaders < T : Config < I > , I : ' static = ( ) > =
306- StorageMap < _ , Identity , BridgedBlockHash < T , I > , BridgedStoredHeaderData < T , I > > ;
308+ pub type ImportedHeaders < T : Config < I > , I : ' static = ( ) > = StorageMap <
309+ Hasher = Identity ,
310+ Key = BridgedBlockHash < T , I > ,
311+ Value = BridgedStoredHeaderData < T , I > ,
312+ QueryKind = OptionQuery ,
313+ OnEmpty = GetDefault ,
314+ MaxValues = MaybeHeadersToKeep < T , I > ,
315+ > ;
307316
308317 /// The current GRANDPA Authority set.
309318 #[ pallet:: storage]
@@ -467,20 +476,6 @@ pub mod pallet {
467476 } ) ?)
468477 }
469478
470- /// Return number of headers to keep in the runtime storage.
471- #[ cfg( not( feature = "runtime-benchmarks" ) ) ]
472- pub ( crate ) fn headers_to_keep < T : Config < I > , I : ' static > ( ) -> u32 {
473- T :: HeadersToKeep :: get ( )
474- }
475-
476- /// Return number of headers to keep in the runtime storage.
477- #[ cfg( feature = "runtime-benchmarks" ) ]
478- pub ( crate ) fn headers_to_keep < T : Config < I > , I : ' static > ( ) -> u32 {
479- // every db operation (significantly) slows down benchmarks, so let's keep as min as
480- // possible
481- 2
482- }
483-
484479 /// Import a previously verified header to the storage.
485480 ///
486481 /// Note this function solely takes care of updating the storage and pruning old entries,
@@ -496,7 +491,7 @@ pub mod pallet {
496491 <ImportedHashes < T , I > >:: insert ( index, hash) ;
497492
498493 // Update ring buffer pointer and remove old header.
499- <ImportedHashesPointer < T , I > >:: put ( ( index + 1 ) % headers_to_keep :: < T , I > ( ) ) ;
494+ <ImportedHashesPointer < T , I > >:: put ( ( index + 1 ) % T :: HeadersToKeep :: get ( ) ) ;
500495 if let Ok ( hash) = pruning {
501496 log:: debug!( target: LOG_TARGET , "Pruning old header: {:?}." , hash) ;
502497 <ImportedHeaders < T , I > >:: remove ( hash) ;
@@ -535,27 +530,35 @@ pub mod pallet {
535530 Ok ( ( ) )
536531 }
537532
533+ /// Adapter for using `Config::HeadersToKeep` as `MaxValues` bound in our storage maps.
534+ pub struct MaybeHeadersToKeep < T , I > ( PhantomData < ( T , I ) > ) ;
535+
536+ // this implementation is required to use the struct as `MaxValues`
537+ impl < T : Config < I > , I : ' static > Get < Option < u32 > > for MaybeHeadersToKeep < T , I > {
538+ fn get ( ) -> Option < u32 > {
539+ Some ( T :: HeadersToKeep :: get ( ) )
540+ }
541+ }
542+
543+ /// Initialize pallet so that it is ready for inserting new header.
544+ ///
545+ /// The function makes sure that the new insertion will cause the pruning of some old header.
546+ ///
547+ /// Returns parent header for the new header.
538548 #[ cfg( feature = "runtime-benchmarks" ) ]
539549 pub ( crate ) fn bootstrap_bridge < T : Config < I > , I : ' static > (
540550 init_params : super :: InitializationData < BridgedHeader < T , I > > ,
541- ) {
542- let start_number = * init_params. header . number ( ) ;
543- let end_number = start_number + headers_to_keep :: < T , I > ( ) . into ( ) ;
551+ ) -> BridgedHeader < T , I > {
552+ let start_header = init_params. header . clone ( ) ;
544553 initialize_bridge :: < T , I > ( init_params) . expect ( "benchmarks are correct" ) ;
545554
546- let mut number = start_number;
547- while number < end_number {
548- number = number + sp_runtime:: traits:: One :: one ( ) ;
549- let header = <BridgedHeader < T , I > >:: new (
550- number,
551- Default :: default ( ) ,
552- Default :: default ( ) ,
553- Default :: default ( ) ,
554- Default :: default ( ) ,
555- ) ;
556- let hash = header. hash ( ) ;
557- insert_header :: < T , I > ( header, hash) ;
558- }
555+ // the most obvious way to cause pruning during next insertion would be to insert
556+ // `HeadersToKeep` headers. But it'll make our benchmarks slow. So we will just play with
557+ // our pruning ring-buffer.
558+ assert_eq ! ( ImportedHashesPointer :: <T , I >:: get( ) , 1 ) ;
559+ ImportedHashesPointer :: < T , I > :: put ( 0 ) ;
560+
561+ * start_header
559562 }
560563}
561564
@@ -816,13 +819,9 @@ mod tests {
816819 fn succesfully_imports_header_with_valid_finality ( ) {
817820 run_test ( || {
818821 initialize_substrate_bridge ( ) ;
819- assert_ok ! (
820- submit_finality_proof( 1 ) ,
821- PostDispatchInfo {
822- actual_weight: None ,
823- pays_fee: frame_support:: dispatch:: Pays :: Yes ,
824- } ,
825- ) ;
822+ let result = submit_finality_proof ( 1 ) ;
823+ assert_ok ! ( result) ;
824+ assert_eq ! ( result. unwrap( ) . pays_fee, frame_support:: dispatch:: Pays :: Yes ) ;
826825
827826 let header = test_header ( 1 ) ;
828827 assert_eq ! ( <BestFinalized <TestRuntime >>:: get( ) . unwrap( ) . 1 , header. hash( ) ) ;
@@ -929,17 +928,13 @@ mod tests {
929928 let justification = make_default_justification ( & header) ;
930929
931930 // Let's import our test header
932- assert_ok ! (
933- Pallet :: <TestRuntime >:: submit_finality_proof(
934- RuntimeOrigin :: signed( 1 ) ,
935- Box :: new( header. clone( ) ) ,
936- justification
937- ) ,
938- PostDispatchInfo {
939- actual_weight: None ,
940- pays_fee: frame_support:: dispatch:: Pays :: No ,
941- } ,
931+ let result = Pallet :: < TestRuntime > :: submit_finality_proof (
932+ RuntimeOrigin :: signed ( 1 ) ,
933+ Box :: new ( header. clone ( ) ) ,
934+ justification,
942935 ) ;
936+ assert_ok ! ( result) ;
937+ assert_eq ! ( result. unwrap( ) . pays_fee, frame_support:: dispatch:: Pays :: No ) ;
943938
944939 // Make sure that our header is the best finalized
945940 assert_eq ! ( <BestFinalized <TestRuntime >>:: get( ) . unwrap( ) . 1 , header. hash( ) ) ;
0 commit comments