33
33
use std:: { fmt:: Debug , hash:: Hash , marker:: PhantomData , pin:: Pin , sync:: Arc } ;
34
34
35
35
use futures:: prelude:: * ;
36
- use log:: { debug, trace} ;
37
36
38
37
use codec:: { Codec , Decode , Encode } ;
39
38
40
- use sc_client_api:: { backend:: AuxStore , BlockOf , UsageProvider } ;
39
+ use sc_client_api:: { backend:: AuxStore , BlockOf } ;
41
40
use sc_consensus:: { BlockImport , BlockImportParams , ForkChoiceStrategy , StateAction } ;
42
41
use sc_consensus_slots:: {
43
42
BackoffAuthoringBlocksStrategy , InherentDataProviderExt , SimpleSlotWorkerToSlotWorker ,
44
43
SlotInfo , StorageChanges ,
45
44
} ;
46
45
use sc_telemetry:: TelemetryHandle ;
47
46
use sp_api:: { Core , ProvideRuntimeApi } ;
48
- use sp_application_crypto:: { AppCrypto , AppPublic } ;
49
- use sp_blockchain:: { HeaderBackend , Result as CResult } ;
47
+ use sp_application_crypto:: AppPublic ;
48
+ use sp_blockchain:: HeaderBackend ;
50
49
use sp_consensus:: { BlockOrigin , Environment , Error as ConsensusError , Proposer , SelectChain } ;
51
50
use sp_consensus_slots:: Slot ;
52
- use sp_core:: crypto:: { ByteArray , Pair , Public } ;
51
+ use sp_core:: crypto:: { Pair , Public } ;
53
52
use sp_inherents:: CreateInherentDataProviders ;
54
53
use sp_keystore:: KeystorePtr ;
55
- use sp_runtime:: {
56
- traits:: { Block as BlockT , Header , Member , NumberFor , Zero } ,
57
- DigestItem ,
58
- } ;
54
+ use sp_runtime:: traits:: { Block as BlockT , Header , Member , NumberFor } ;
59
55
60
56
mod import_queue;
57
+ pub mod standalone;
61
58
59
+ pub use crate :: standalone:: { find_pre_digest, slot_duration} ;
62
60
pub use import_queue:: {
63
61
build_verifier, import_queue, AuraVerifier , BuildVerifierParams , CheckForEquivocation ,
64
62
ImportQueueParams ,
@@ -112,39 +110,6 @@ impl<N> Default for CompatibilityMode<N> {
112
110
}
113
111
}
114
112
115
- /// Get the slot duration for Aura.
116
- pub fn slot_duration < A , B , C > ( client : & C ) -> CResult < SlotDuration >
117
- where
118
- A : Codec ,
119
- B : BlockT ,
120
- C : AuxStore + ProvideRuntimeApi < B > + UsageProvider < B > ,
121
- C :: Api : AuraApi < B , A > ,
122
- {
123
- client
124
- . runtime_api ( )
125
- . slot_duration ( client. usage_info ( ) . chain . best_hash )
126
- . map_err ( |err| err. into ( ) )
127
- }
128
-
129
- /// Get slot author for given block along with authorities.
130
- fn slot_author < P : Pair > ( slot : Slot , authorities : & [ AuthorityId < P > ] ) -> Option < & AuthorityId < P > > {
131
- if authorities. is_empty ( ) {
132
- return None
133
- }
134
-
135
- let idx = * slot % ( authorities. len ( ) as u64 ) ;
136
- assert ! (
137
- idx <= usize :: MAX as u64 ,
138
- "It is impossible to have a vector with length beyond the address space; qed" ,
139
- ) ;
140
-
141
- let current_author = authorities. get ( idx as usize ) . expect (
142
- "authorities not empty; index constrained to list length;this is a valid index; qed" ,
143
- ) ;
144
-
145
- Some ( current_author)
146
- }
147
-
148
113
/// Parameters of [`start_aura`].
149
114
pub struct StartAuraParams < C , SC , I , PF , SO , L , CIDP , BS , N > {
150
115
/// The duration of a slot.
@@ -412,21 +377,11 @@ where
412
377
slot : Slot ,
413
378
authorities : & Self :: AuxData ,
414
379
) -> Option < Self :: Claim > {
415
- let expected_author = slot_author :: < P > ( slot, authorities) ;
416
- expected_author. and_then ( |p| {
417
- if self
418
- . keystore
419
- . has_keys ( & [ ( p. to_raw_vec ( ) , sp_application_crypto:: key_types:: AURA ) ] )
420
- {
421
- Some ( p. clone ( ) )
422
- } else {
423
- None
424
- }
425
- } )
380
+ crate :: standalone:: claim_slot :: < P > ( slot, authorities, & self . keystore ) . await
426
381
}
427
382
428
383
fn pre_digest_data ( & self , slot : Slot , _claim : & Self :: Claim ) -> Vec < sp_runtime:: DigestItem > {
429
- vec ! [ < DigestItem as CompatibleDigestItem < P :: Signature >> :: aura_pre_digest ( slot) ]
384
+ vec ! [ crate :: standalone :: pre_digest :: < P > ( slot) ]
430
385
}
431
386
432
387
async fn block_import_params (
@@ -441,28 +396,8 @@ where
441
396
sc_consensus:: BlockImportParams < B , <Self :: BlockImport as BlockImport < B > >:: Transaction > ,
442
397
ConsensusError ,
443
398
> {
444
- let signature = self
445
- . keystore
446
- . sign_with (
447
- <AuthorityId < P > as AppCrypto >:: ID ,
448
- <AuthorityId < P > as AppCrypto >:: CRYPTO_ID ,
449
- public. as_slice ( ) ,
450
- header_hash. as_ref ( ) ,
451
- )
452
- . map_err ( |e| ConsensusError :: CannotSign ( format ! ( "{}. Key: {:?}" , e, public) ) ) ?
453
- . ok_or_else ( || {
454
- ConsensusError :: CannotSign ( format ! (
455
- "Could not find key in keystore. Key: {:?}" ,
456
- public
457
- ) )
458
- } ) ?;
459
- let signature = signature
460
- . clone ( )
461
- . try_into ( )
462
- . map_err ( |_| ConsensusError :: InvalidSignature ( signature, public. to_raw_vec ( ) ) ) ?;
463
-
464
399
let signature_digest_item =
465
- < DigestItem as CompatibleDigestItem < P :: Signature > > :: aura_seal ( signature ) ;
400
+ crate :: standalone :: seal :: < _ , P > ( header_hash , & public , & self . keystore ) ? ;
466
401
467
402
let mut import_block = BlockImportParams :: new ( BlockOrigin :: Own , header) ;
468
403
import_block. post_digests . push ( signature_digest_item) ;
@@ -526,11 +461,6 @@ where
526
461
}
527
462
}
528
463
529
- fn aura_err < B : BlockT > ( error : Error < B > ) -> Error < B > {
530
- debug ! ( target: LOG_TARGET , "{}" , error) ;
531
- error
532
- }
533
-
534
464
/// Aura Errors
535
465
#[ derive( Debug , thiserror:: Error ) ]
536
466
pub enum Error < B : BlockT > {
@@ -569,22 +499,13 @@ impl<B: BlockT> From<Error<B>> for String {
569
499
}
570
500
}
571
501
572
- /// Get pre-digests from the header
573
- pub fn find_pre_digest < B : BlockT , Signature : Codec > ( header : & B :: Header ) -> Result < Slot , Error < B > > {
574
- if header. number ( ) . is_zero ( ) {
575
- return Ok ( 0 . into ( ) )
576
- }
577
-
578
- let mut pre_digest: Option < Slot > = None ;
579
- for log in header. digest ( ) . logs ( ) {
580
- trace ! ( target: LOG_TARGET , "Checking log {:?}" , log) ;
581
- match ( CompatibleDigestItem :: < Signature > :: as_aura_pre_digest ( log) , pre_digest. is_some ( ) ) {
582
- ( Some ( _) , true ) => return Err ( aura_err ( Error :: MultipleHeaders ) ) ,
583
- ( None , _) => trace ! ( target: LOG_TARGET , "Ignoring digest not meant for us" ) ,
584
- ( s, false ) => pre_digest = s,
502
+ impl < B : BlockT > From < crate :: standalone:: PreDigestLookupError > for Error < B > {
503
+ fn from ( e : crate :: standalone:: PreDigestLookupError ) -> Self {
504
+ match e {
505
+ crate :: standalone:: PreDigestLookupError :: MultipleHeaders => Error :: MultipleHeaders ,
506
+ crate :: standalone:: PreDigestLookupError :: NoDigestFound => Error :: NoDigestFound ,
585
507
}
586
508
}
587
- pre_digest. ok_or_else ( || aura_err ( Error :: NoDigestFound ) )
588
509
}
589
510
590
511
fn authorities < A , B , C > (
@@ -637,7 +558,7 @@ mod tests {
637
558
use sc_consensus_slots:: { BackoffAuthoringOnFinalizedHeadLagging , SimpleSlotWorker } ;
638
559
use sc_keystore:: LocalKeystore ;
639
560
use sc_network_test:: { Block as TestBlock , * } ;
640
- use sp_application_crypto:: key_types:: AURA ;
561
+ use sp_application_crypto:: { key_types:: AURA , AppCrypto } ;
641
562
use sp_consensus:: { DisableProofRecording , NoNetwork as DummyOracle , Proposal } ;
642
563
use sp_consensus_aura:: sr25519:: AuthorityPair ;
643
564
use sp_inherents:: InherentData ;
@@ -851,22 +772,6 @@ mod tests {
851
772
. await ;
852
773
}
853
774
854
- #[ test]
855
- fn authorities_call_works ( ) {
856
- let client = substrate_test_runtime_client:: new ( ) ;
857
-
858
- assert_eq ! ( client. chain_info( ) . best_number, 0 ) ;
859
- assert_eq ! (
860
- authorities( & client, client. chain_info( ) . best_hash, 1 , & CompatibilityMode :: None )
861
- . unwrap( ) ,
862
- vec![
863
- Keyring :: Alice . public( ) . into( ) ,
864
- Keyring :: Bob . public( ) . into( ) ,
865
- Keyring :: Charlie . public( ) . into( )
866
- ]
867
- ) ;
868
- }
869
-
870
775
#[ tokio:: test]
871
776
async fn current_node_authority_should_claim_slot ( ) {
872
777
let net = AuraTestNet :: new ( 4 ) ;
0 commit comments