1
- use anyhow:: { anyhow, Context , Result } ;
1
+ use anyhow:: { anyhow, bail , Context , Result } ;
2
2
use log:: { debug, error, info, warn} ;
3
3
use phala_node_rpc_ext:: MakeInto ;
4
4
use phala_trie_storage:: ser:: StorageChanges ;
@@ -7,7 +7,6 @@ use sp_core::{crypto::AccountId32, H256};
7
7
use std:: cmp;
8
8
use std:: convert:: TryFrom ;
9
9
use std:: str:: FromStr ;
10
- use std:: sync:: atomic:: { AtomicBool , Ordering } ;
11
10
use std:: time:: Duration ;
12
11
use tokio:: time:: sleep;
13
12
@@ -20,11 +19,12 @@ use phaxt::{
20
19
subxt:: { self , tx:: TxPayload } ,
21
20
RpcClient ,
22
21
} ;
23
- use sp_consensus_grandpa:: { AuthorityList , SetId } ;
22
+ use sp_consensus_grandpa:: SetId ;
24
23
use subxt:: config:: { substrate:: Era , Header as _} ;
25
24
26
- type VersionedAuthorityList = ( u8 , AuthorityList ) ;
25
+ pub use authority :: get_authority_with_proof_at ;
27
26
27
+ mod authority;
28
28
mod endpoint;
29
29
mod error;
30
30
mod msg_sync;
@@ -41,14 +41,13 @@ use crate::types::{
41
41
RelaychainApi , SrSigner ,
42
42
} ;
43
43
use phactory_api:: blocks:: {
44
- self , AuthoritySet , AuthoritySetChange , BlockHeader , BlockHeaderWithChanges , HeaderToSync ,
45
- StorageProof ,
44
+ self , AuthoritySetChange , BlockHeader , BlockHeaderWithChanges , HeaderToSync , StorageProof ,
46
45
} ;
47
46
use phactory_api:: prpc:: { self , InitRuntimeResponse , PhactoryInfo } ;
48
47
use phactory_api:: pruntime_client;
49
48
50
49
use clap:: Parser ;
51
- use headers_cache:: Client as CacheClient ;
50
+ use headers_cache:: { fetch_genesis_info , Client as CacheClient } ;
52
51
use msg_sync:: { Error as MsgSyncError , Receiver , Sender } ;
53
52
use notify_client:: NotifyClient ;
54
53
use phala_types:: { AttestationProvider , AttestationReport , Collateral } ;
@@ -434,57 +433,6 @@ pub async fn batch_sync_storage_changes(
434
433
Ok ( ( ) )
435
434
}
436
435
437
- pub async fn get_authority_with_proof_at (
438
- api : & RelaychainApi ,
439
- hash : Hash ,
440
- ) -> Result < AuthoritySetChange > {
441
- // Storage
442
- let id_key = phaxt:: dynamic:: storage_key ( "Grandpa" , "CurrentSetId" ) ;
443
- let alt_authorities_key = phaxt:: dynamic:: storage_key ( "Grandpa" , "Authorities" ) ;
444
- let old_authorities_key: & [ u8 ] = b":grandpa_authorities" ;
445
-
446
- // Try the old grandpa storage key first if true. Otherwise try the new key first.
447
- static OLD_AUTH_KEY_PRIOR : AtomicBool = AtomicBool :: new ( true ) ;
448
- let old_prior = OLD_AUTH_KEY_PRIOR . load ( Ordering :: Relaxed ) ;
449
- let authorities_key_candidates = if old_prior {
450
- [ old_authorities_key, & alt_authorities_key]
451
- } else {
452
- [ & alt_authorities_key, old_authorities_key]
453
- } ;
454
- let mut authorities_key: & [ u8 ] = & [ ] ;
455
- let mut value = None ;
456
- for key in authorities_key_candidates {
457
- if let Some ( data) = api. rpc ( ) . storage ( key, Some ( hash) ) . await ? {
458
- authorities_key = key;
459
- value = Some ( data. 0 ) ;
460
- break ;
461
- }
462
- OLD_AUTH_KEY_PRIOR . store ( !old_prior, Ordering :: Relaxed ) ;
463
- }
464
- let value = value. ok_or_else ( || anyhow ! ( "No grandpa authorities found" ) ) ?;
465
- let list: AuthorityList = if authorities_key == old_authorities_key {
466
- VersionedAuthorityList :: decode ( & mut value. as_slice ( ) )
467
- . expect ( "Failed to decode VersionedAuthorityList" )
468
- . 1
469
- } else {
470
- AuthorityList :: decode ( & mut value. as_slice ( ) ) . expect ( "Failed to decode AuthorityList" )
471
- } ;
472
-
473
- // Set id
474
- let id = api. current_set_id ( Some ( hash) ) . await ?;
475
- // Proof
476
- let proof = chain_client:: read_proofs (
477
- api,
478
- Some ( hash) ,
479
- vec ! [ old_authorities_key, & alt_authorities_key, & id_key] ,
480
- )
481
- . await ?;
482
- Ok ( AuthoritySetChange {
483
- authority_set : AuthoritySet { list, id } ,
484
- authority_proof : proof,
485
- } )
486
- }
487
-
488
436
async fn try_load_handover_proof ( pr : & PrClient , api : & ParachainApi ) -> Result < ( ) > {
489
437
let info = pr. get_info ( ( ) ) . await ?;
490
438
if info. safe_mode_level < 2 {
@@ -716,7 +664,8 @@ async fn batch_sync_block(
716
664
let mut authrotiy_change: Option < AuthoritySetChange > = None ;
717
665
if let Some ( change_at) = set_id_change_at {
718
666
if change_at == last_header_number {
719
- authrotiy_change = Some ( get_authority_with_proof_at ( api, last_header_hash) . await ?) ;
667
+ authrotiy_change =
668
+ Some ( get_authority_with_proof_at ( api, & last_header. header ) . await ?) ;
720
669
}
721
670
}
722
671
@@ -738,6 +687,15 @@ async fn batch_sync_block(
738
687
header. justification = None ;
739
688
}
740
689
}
690
+ if let Some ( last_header) = header_batch. last ( ) {
691
+ let Some ( justification) = & last_header. justification else {
692
+ bail ! (
693
+ "No justification found for header {}" ,
694
+ last_header. header. number
695
+ ) ;
696
+ } ;
697
+ authority:: verify ( api, & last_header. header , justification) . await ?;
698
+ }
741
699
let r = req_sync_header ( pr, header_batch, authrotiy_change) . await ?;
742
700
info ! ( " ..sync_header: {:?}" , r) ;
743
701
next_headernum = r. synced_to + 1 ;
@@ -982,22 +940,7 @@ async fn init_runtime(
982
940
} ;
983
941
let genesis_info = match genesis_info {
984
942
Some ( genesis_info) => genesis_info,
985
- None => {
986
- let genesis_block = get_block_at ( api, Some ( start_header) ) . await ?. 0 . block ;
987
- let hash = api
988
- . rpc ( )
989
- . block_hash ( Some ( subxt:: rpc:: types:: BlockNumber :: from (
990
- NumberOrHex :: Number ( start_header as _ ) ,
991
- ) ) )
992
- . await ?
993
- . expect ( "No genesis block?" ) ;
994
- let set_proof = get_authority_with_proof_at ( api, hash) . await ?;
995
- blocks:: GenesisBlockInfo {
996
- block_header : genesis_block. header . clone ( ) ,
997
- authority_set : set_proof. authority_set ,
998
- proof : set_proof. authority_proof ,
999
- }
1000
- }
943
+ None => fetch_genesis_info ( api, start_header) . await ?,
1001
944
} ;
1002
945
let genesis_state = chain_client:: fetch_genesis_storage ( para_api) . await ?;
1003
946
let mut debug_set_key = None ;
@@ -1032,7 +975,10 @@ pub async fn attestation_to_report(
1032
975
pccs_url : & str ,
1033
976
pccs_timeout_secs : u64 ,
1034
977
) -> Result < Vec < u8 > > {
1035
- info ! ( "Processing attestation report provider={}" , attestation. provider) ;
978
+ info ! (
979
+ "Processing attestation report, provider={}" ,
980
+ attestation. provider
981
+ ) ;
1036
982
let report = match attestation. payload {
1037
983
Some ( payload) => Attestation :: SgxIas {
1038
984
ra_report : payload. report . as_bytes ( ) . to_vec ( ) ,
0 commit comments