3939pub use storage_types:: StoredAuthoritySet ;
4040
4141use bp_header_chain:: {
42- justification:: GrandpaJustification , ChainWithGrandpa , GrandpaConsensusLogReader , HeaderChain ,
43- InitializationData , StoredHeaderData , StoredHeaderDataBuilder ,
42+ justification:: GrandpaJustification , AuthoritySet , ChainWithGrandpa , GrandpaConsensusLogReader ,
43+ HeaderChain , HeaderGrandpaInfo , InitializationData , StoredHeaderData , StoredHeaderDataBuilder ,
4444} ;
4545use bp_runtime:: { BlockNumberOf , HashOf , HasherOf , HeaderId , HeaderOf , OwnedBridgeModule } ;
4646use finality_grandpa:: voter_set:: VoterSet ;
@@ -49,7 +49,7 @@ use sp_runtime::{
4949 traits:: { Header as HeaderT , Zero } ,
5050 SaturatedConversion ,
5151} ;
52- use sp_std:: { boxed:: Box , convert:: TryInto } ;
52+ use sp_std:: { boxed:: Box , convert:: TryInto , prelude :: * } ;
5353
5454mod call_ext;
5555#[ cfg( test) ]
@@ -194,11 +194,12 @@ pub mod pallet {
194194 let authority_set = <CurrentAuthoritySet < T , I > >:: get ( ) ;
195195 let unused_proof_size = authority_set. unused_proof_size ( ) ;
196196 let set_id = authority_set. set_id ;
197- verify_justification :: < T , I > ( & justification, hash, number, authority_set. into ( ) ) ?;
197+ let authority_set: AuthoritySet = authority_set. into ( ) ;
198+ verify_justification :: < T , I > ( & justification, hash, number, authority_set) ?;
198199
199- let is_authorities_change_enacted =
200+ let maybe_new_authority_set =
200201 try_enact_authority_change :: < T , I > ( & finality_target, set_id) ?;
201- let may_refund_call_fee = is_authorities_change_enacted &&
202+ let may_refund_call_fee = maybe_new_authority_set . is_some ( ) &&
202203 // if we have seen too many mandatory headers in this block, we don't want to refund
203204 Self :: free_mandatory_headers_remaining ( ) > 0 &&
204205 // if arguments out of expected bounds, we don't want to refund
@@ -237,7 +238,14 @@ pub mod pallet {
237238 let actual_weight = pre_dispatch_weight
238239 . set_proof_size ( pre_dispatch_weight. proof_size ( ) . saturating_sub ( unused_proof_size) ) ;
239240
240- Self :: deposit_event ( Event :: UpdatedBestFinalizedHeader { number, hash } ) ;
241+ Self :: deposit_event ( Event :: UpdatedBestFinalizedHeader {
242+ number,
243+ hash,
244+ grandpa_info : HeaderGrandpaInfo {
245+ justification,
246+ authority_set : maybe_new_authority_set,
247+ } ,
248+ } ) ;
241249
242250 Ok ( PostDispatchInfo { actual_weight : Some ( actual_weight) , pays_fee } )
243251 }
@@ -402,6 +410,8 @@ pub mod pallet {
402410 UpdatedBestFinalizedHeader {
403411 number : BridgedBlockNumber < T , I > ,
404412 hash : BridgedBlockHash < T , I > ,
413+ /// The Grandpa info associated to the new best finalized header.
414+ grandpa_info : HeaderGrandpaInfo < BridgedHeader < T , I > > ,
405415 } ,
406416 }
407417
@@ -437,9 +447,7 @@ pub mod pallet {
437447 pub ( crate ) fn try_enact_authority_change < T : Config < I > , I : ' static > (
438448 header : & BridgedHeader < T , I > ,
439449 current_set_id : sp_consensus_grandpa:: SetId ,
440- ) -> Result < bool , sp_runtime:: DispatchError > {
441- let mut change_enacted = false ;
442-
450+ ) -> Result < Option < AuthoritySet > , DispatchError > {
443451 // We don't support forced changes - at that point governance intervention is required.
444452 ensure ! (
445453 GrandpaConsensusLogReader :: <BridgedBlockNumber <T , I >>:: find_forced_change(
@@ -468,7 +476,6 @@ pub mod pallet {
468476 // Since our header schedules a change and we know the delay is 0, it must also enact
469477 // the change.
470478 <CurrentAuthoritySet < T , I > >:: put ( & next_authorities) ;
471- change_enacted = true ;
472479
473480 log:: info!(
474481 target: LOG_TARGET ,
@@ -477,9 +484,11 @@ pub mod pallet {
477484 current_set_id + 1 ,
478485 next_authorities,
479486 ) ;
487+
488+ return Ok ( Some ( next_authorities. into ( ) ) )
480489 } ;
481490
482- Ok ( change_enacted )
491+ Ok ( None )
483492 }
484493
485494 /// Verify a GRANDPA justification (finality proof) for a given header.
@@ -603,10 +612,22 @@ pub mod pallet {
603612 }
604613}
605614
606- impl < T : Config < I > , I : ' static > Pallet < T , I > {
607- /// Get the best finalized block number.
608- pub fn best_finalized_number ( ) -> Option < BridgedBlockNumber < T , I > > {
609- BestFinalized :: < T , I > :: get ( ) . map ( |id| id. number ( ) )
615+ impl < T : Config < I > , I : ' static > Pallet < T , I >
616+ where
617+ <T as frame_system:: Config >:: RuntimeEvent : TryInto < Event < T , I > > ,
618+ {
619+ /// Get the GRANDPA justifications accepted in the current block.
620+ pub fn synced_headers_grandpa_info ( ) -> Vec < HeaderGrandpaInfo < BridgedHeader < T , I > > > {
621+ frame_system:: Pallet :: < T > :: read_events_no_consensus ( )
622+ . filter_map ( |event| {
623+ if let Event :: < T , I > :: UpdatedBestFinalizedHeader { grandpa_info, .. } =
624+ event. event . try_into ( ) . ok ( ) ?
625+ {
626+ return Some ( grandpa_info)
627+ }
628+ None
629+ } )
630+ . collect ( )
610631 }
611632}
612633
@@ -913,10 +934,18 @@ mod tests {
913934 event: TestEvent :: Grandpa ( Event :: UpdatedBestFinalizedHeader {
914935 number: * header. number( ) ,
915936 hash: header. hash( ) ,
937+ grandpa_info: HeaderGrandpaInfo {
938+ justification: justification. clone( ) ,
939+ authority_set: None ,
940+ } ,
916941 } ) ,
917942 topics: vec![ ] ,
918943 } ] ,
919944 ) ;
945+ assert_eq ! (
946+ Pallet :: <TestRuntime >:: synced_headers_grandpa_info( ) ,
947+ vec![ HeaderGrandpaInfo { justification, authority_set: None } ]
948+ ) ;
920949 } )
921950 }
922951
@@ -1022,7 +1051,7 @@ mod tests {
10221051 let result = Pallet :: < TestRuntime > :: submit_finality_proof (
10231052 RuntimeOrigin :: signed ( 1 ) ,
10241053 Box :: new ( header. clone ( ) ) ,
1025- justification,
1054+ justification. clone ( ) ,
10261055 ) ;
10271056 assert_ok ! ( result) ;
10281057 assert_eq ! ( result. unwrap( ) . pays_fee, frame_support:: dispatch:: Pays :: No ) ;
@@ -1037,6 +1066,30 @@ mod tests {
10371066 StoredAuthoritySet :: <TestRuntime , ( ) >:: try_new( next_authorities, next_set_id)
10381067 . unwrap( ) ,
10391068 ) ;
1069+
1070+ // Here
1071+ assert_eq ! (
1072+ System :: events( ) ,
1073+ vec![ EventRecord {
1074+ phase: Phase :: Initialization ,
1075+ event: TestEvent :: Grandpa ( Event :: UpdatedBestFinalizedHeader {
1076+ number: * header. number( ) ,
1077+ hash: header. hash( ) ,
1078+ grandpa_info: HeaderGrandpaInfo {
1079+ justification: justification. clone( ) ,
1080+ authority_set: Some ( <CurrentAuthoritySet <TestRuntime >>:: get( ) . into( ) ) ,
1081+ } ,
1082+ } ) ,
1083+ topics: vec![ ] ,
1084+ } ] ,
1085+ ) ;
1086+ assert_eq ! (
1087+ Pallet :: <TestRuntime >:: synced_headers_grandpa_info( ) ,
1088+ vec![ HeaderGrandpaInfo {
1089+ justification,
1090+ authority_set: Some ( <CurrentAuthoritySet <TestRuntime >>:: get( ) . into( ) ) ,
1091+ } ]
1092+ ) ;
10401093 } )
10411094 }
10421095
0 commit comments