3434//! finality proof (that finalizes some block C that is ancestor of the B and descendant
3535//! of the U) could be returned.
3636
37+ use std:: iter;
3738use std:: sync:: Arc ;
3839use log:: { trace, warn} ;
3940
4041use client:: {
4142 backend:: Backend , blockchain:: Backend as BlockchainBackend , CallExecutor , Client ,
4243 error:: { Error as ClientError , Result as ClientResult } ,
43- light:: fetcher:: { FetchChecker , RemoteCallRequest , StorageProof } , ExecutionStrategy ,
44+ light:: fetcher:: { FetchChecker , RemoteReadRequest , StorageProof } ,
4445} ;
4546use codec:: { Encode , Decode } ;
4647use grandpa:: BlockNumberOps ;
4748use sr_primitives:: {
4849 Justification , generic:: BlockId ,
4950 traits:: { NumberFor , Block as BlockT , Header as HeaderT , One } ,
5051} ;
51- use primitives:: { H256 , Blake2Hasher } ;
52+ use primitives:: { H256 , Blake2Hasher , storage :: StorageKey } ;
5253use substrate_telemetry:: { telemetry, CONSENSUS_INFO } ;
53- use fg_primitives:: AuthorityId ;
54+ use fg_primitives:: { AuthorityId , AuthorityList , VersionedAuthorityList , GRANDPA_AUTHORITIES_KEY } ;
5455
5556use crate :: justification:: GrandpaJustification ;
5657
@@ -59,9 +60,9 @@ const MAX_FRAGMENTS_IN_PROOF: usize = 8;
5960
6061/// GRANDPA authority set related methods for the finality proof provider.
6162pub trait AuthoritySetForFinalityProver < Block : BlockT > : Send + Sync {
62- /// Call GrandpaApi::grandpa_authorities at given block.
63- fn authorities ( & self , block : & BlockId < Block > ) -> ClientResult < Vec < ( AuthorityId , u64 ) > > ;
64- /// Prove call of GrandpaApi::grandpa_authorities at given block.
63+ /// Read GRANDPA_AUTHORITIES_KEY from storage at given block.
64+ fn authorities ( & self , block : & BlockId < Block > ) -> ClientResult < AuthorityList > ;
65+ /// Prove storage read of GRANDPA_AUTHORITIES_KEY at given block.
6566 fn prove_authorities ( & self , block : & BlockId < Block > ) -> ClientResult < StorageProof > ;
6667}
6768
@@ -72,33 +73,28 @@ impl<B, E, Block: BlockT<Hash=H256>, RA> AuthoritySetForFinalityProver<Block> fo
7273 E : CallExecutor < Block , Blake2Hasher > + ' static + Clone + Send + Sync ,
7374 RA : Send + Sync ,
7475{
75- fn authorities ( & self , block : & BlockId < Block > ) -> ClientResult < Vec < ( AuthorityId , u64 ) > > {
76- self . executor ( ) . call (
77- block,
78- "GrandpaApi_grandpa_authorities" ,
79- & [ ] ,
80- ExecutionStrategy :: NativeElseWasm ,
81- None ,
82- ) . and_then ( |call_result| Decode :: decode ( & mut & call_result[ ..] )
83- . map_err ( |err| ClientError :: CallResultDecode (
84- "failed to decode GRANDPA authorities set proof" . into ( ) , err
85- ) ) )
76+ fn authorities ( & self , block : & BlockId < Block > ) -> ClientResult < AuthorityList > {
77+ let storage_key = StorageKey ( GRANDPA_AUTHORITIES_KEY . to_vec ( ) ) ;
78+ self . storage ( block, & storage_key) ?
79+ . and_then ( |encoded| VersionedAuthorityList :: decode ( & mut encoded. 0 . as_slice ( ) ) . ok ( ) )
80+ . map ( |versioned| versioned. into ( ) )
81+ . ok_or ( ClientError :: InvalidAuthoritiesSet )
8682 }
8783
8884 fn prove_authorities ( & self , block : & BlockId < Block > ) -> ClientResult < StorageProof > {
89- self . execution_proof ( block, "GrandpaApi_grandpa_authorities" , & [ ] ) . map ( | ( _ , proof ) | proof )
85+ self . read_proof ( block, iter :: once ( GRANDPA_AUTHORITIES_KEY ) )
9086 }
9187}
9288
9389/// GRANDPA authority set related methods for the finality proof checker.
9490pub trait AuthoritySetForFinalityChecker < Block : BlockT > : Send + Sync {
95- /// Check execution proof of Grandpa::grandpa_authorities at given block.
91+ /// Check storage read proof of GRANDPA_AUTHORITIES_KEY at given block.
9692 fn check_authorities_proof (
9793 & self ,
9894 hash : Block :: Hash ,
9995 header : Block :: Header ,
10096 proof : StorageProof ,
101- ) -> ClientResult < Vec < ( AuthorityId , u64 ) > > ;
97+ ) -> ClientResult < AuthorityList > ;
10298}
10399
104100/// FetchChecker-based implementation of AuthoritySetForFinalityChecker.
@@ -108,22 +104,30 @@ impl<Block: BlockT> AuthoritySetForFinalityChecker<Block> for Arc<dyn FetchCheck
108104 hash : Block :: Hash ,
109105 header : Block :: Header ,
110106 proof : StorageProof ,
111- ) -> ClientResult < Vec < ( AuthorityId , u64 ) > > {
112- let request = RemoteCallRequest {
107+ ) -> ClientResult < AuthorityList > {
108+ let storage_key = GRANDPA_AUTHORITIES_KEY . to_vec ( ) ;
109+ let request = RemoteReadRequest {
113110 block : hash,
114111 header,
115- method : "GrandpaApi_grandpa_authorities" . into ( ) ,
116- call_data : vec ! [ ] ,
112+ keys : vec ! [ storage_key. clone( ) ] ,
117113 retry_count : None ,
118114 } ;
119115
120- self . check_execution_proof ( & request, proof)
121- . and_then ( |authorities| {
122- let authorities: Vec < ( AuthorityId , u64 ) > = Decode :: decode ( & mut & authorities[ ..] )
123- . map_err ( |err| ClientError :: CallResultDecode (
124- "failed to decode GRANDPA authorities set proof" . into ( ) , err
125- ) ) ?;
126- Ok ( authorities. into_iter ( ) . collect ( ) )
116+ self . check_read_proof ( & request, proof)
117+ . and_then ( |results| {
118+ let maybe_encoded = results. get ( & storage_key)
119+ . expect (
120+ "storage_key is listed in the request keys; \
121+ check_read_proof must return a value for each requested key;
122+ qed"
123+ ) ;
124+ maybe_encoded
125+ . as_ref ( )
126+ . and_then ( |encoded| {
127+ VersionedAuthorityList :: decode ( & mut encoded. as_slice ( ) ) . ok ( )
128+ } )
129+ . map ( |versioned| versioned. into ( ) )
130+ . ok_or ( ClientError :: InvalidAuthoritiesSet )
127131 } )
128132 }
129133}
@@ -189,7 +193,7 @@ pub struct FinalityEffects<Header: HeaderT> {
189193 /// New authorities set id that should be applied starting from block.
190194 pub new_set_id : u64 ,
191195 /// New authorities set that should be applied starting from block.
192- pub new_authorities : Vec < ( AuthorityId , u64 ) > ,
196+ pub new_authorities : AuthorityList ,
193197}
194198
195199/// Single fragment of proof-of-finality.
@@ -408,7 +412,7 @@ pub(crate) fn prove_finality<Block: BlockT<Hash=H256>, B: BlockchainBackend<Bloc
408412pub ( crate ) fn check_finality_proof < Block : BlockT < Hash =H256 > , B > (
409413 blockchain : & B ,
410414 current_set_id : u64 ,
411- current_authorities : Vec < ( AuthorityId , u64 ) > ,
415+ current_authorities : AuthorityList ,
412416 authorities_provider : & dyn AuthoritySetForFinalityChecker < Block > ,
413417 remote_proof : Vec < u8 > ,
414418) -> ClientResult < FinalityEffects < Block :: Header > >
@@ -427,7 +431,7 @@ pub(crate) fn check_finality_proof<Block: BlockT<Hash=H256>, B>(
427431fn do_check_finality_proof < Block : BlockT < Hash =H256 > , B , J > (
428432 blockchain : & B ,
429433 current_set_id : u64 ,
430- current_authorities : Vec < ( AuthorityId , u64 ) > ,
434+ current_authorities : AuthorityList ,
431435 authorities_provider : & dyn AuthoritySetForFinalityChecker < Block > ,
432436 remote_proof : Vec < u8 > ,
433437) -> ClientResult < FinalityEffects < Block :: Header > >
@@ -522,12 +526,12 @@ fn check_finality_proof_fragment<Block: BlockT<Hash=H256>, B, J>(
522526
523527/// Authorities set from initial authorities set or finality effects.
524528enum AuthoritiesOrEffects < Header : HeaderT > {
525- Authorities ( u64 , Vec < ( AuthorityId , u64 ) > ) ,
529+ Authorities ( u64 , AuthorityList ) ,
526530 Effects ( FinalityEffects < Header > ) ,
527531}
528532
529533impl < Header : HeaderT > AuthoritiesOrEffects < Header > {
530- pub fn extract_authorities ( self ) -> ( u64 , Vec < ( AuthorityId , u64 ) > ) {
534+ pub fn extract_authorities ( self ) -> ( u64 , AuthorityList ) {
531535 match self {
532536 AuthoritiesOrEffects :: Authorities ( set_id, authorities) => ( set_id, authorities) ,
533537 AuthoritiesOrEffects :: Effects ( effects) => ( effects. new_set_id , effects. new_authorities ) ,
@@ -581,10 +585,10 @@ pub(crate) mod tests {
581585
582586 impl < GetAuthorities , ProveAuthorities > AuthoritySetForFinalityProver < Block > for ( GetAuthorities , ProveAuthorities )
583587 where
584- GetAuthorities : Send + Sync + Fn ( BlockId < Block > ) -> ClientResult < Vec < ( AuthorityId , u64 ) > > ,
588+ GetAuthorities : Send + Sync + Fn ( BlockId < Block > ) -> ClientResult < AuthorityList > ,
585589 ProveAuthorities : Send + Sync + Fn ( BlockId < Block > ) -> ClientResult < StorageProof > ,
586590 {
587- fn authorities ( & self , block : & BlockId < Block > ) -> ClientResult < Vec < ( AuthorityId , u64 ) > > {
591+ fn authorities ( & self , block : & BlockId < Block > ) -> ClientResult < AuthorityList > {
588592 self . 0 ( * block)
589593 }
590594
@@ -597,14 +601,14 @@ pub(crate) mod tests {
597601
598602 impl < Closure > AuthoritySetForFinalityChecker < Block > for ClosureAuthoritySetForFinalityChecker < Closure >
599603 where
600- Closure : Send + Sync + Fn ( H256 , Header , StorageProof ) -> ClientResult < Vec < ( AuthorityId , u64 ) > > ,
604+ Closure : Send + Sync + Fn ( H256 , Header , StorageProof ) -> ClientResult < AuthorityList > ,
601605 {
602606 fn check_authorities_proof (
603607 & self ,
604608 hash : H256 ,
605609 header : Header ,
606- proof : StorageProof ,
607- ) -> ClientResult < Vec < ( AuthorityId , u64 ) > > {
610+ proof : StorageProof
611+ ) -> ClientResult < AuthorityList > {
608612 self . 0 ( hash, header, proof)
609613 }
610614 }
0 commit comments