@@ -30,7 +30,7 @@ use sp_consensus::{
30
30
BlockCheckParams , BlockImportParams , ImportResult , JustificationImport ,
31
31
SelectChain ,
32
32
} ;
33
- use sp_finality_grandpa:: { GRANDPA_ENGINE_ID , ScheduledChange , ConsensusLog } ;
33
+ use sp_finality_grandpa:: { ConsensusLog , ScheduledChange , SetId , GRANDPA_ENGINE_ID } ;
34
34
use sp_runtime:: Justification ;
35
35
use sp_runtime:: generic:: { BlockId , OpaqueDigestItemId } ;
36
36
use sp_runtime:: traits:: {
@@ -59,6 +59,7 @@ pub struct GrandpaBlockImport<Backend, Block: BlockT, Client, SC> {
59
59
authority_set : SharedAuthoritySet < Block :: Hash , NumberFor < Block > > ,
60
60
send_voter_commands : mpsc:: UnboundedSender < VoterCommand < Block :: Hash , NumberFor < Block > > > ,
61
61
consensus_changes : SharedConsensusChanges < Block :: Hash , NumberFor < Block > > ,
62
+ authority_set_hard_forks : HashMap < Block :: Hash , PendingChange < Block :: Hash , NumberFor < Block > > > ,
62
63
_phantom : PhantomData < Backend > ,
63
64
}
64
65
@@ -72,6 +73,7 @@ impl<Backend, Block: BlockT, Client, SC: Clone> Clone for
72
73
authority_set : self . authority_set . clone ( ) ,
73
74
send_voter_commands : self . send_voter_commands . clone ( ) ,
74
75
consensus_changes : self . consensus_changes . clone ( ) ,
76
+ authority_set_hard_forks : self . authority_set_hard_forks . clone ( ) ,
75
77
_phantom : PhantomData ,
76
78
}
77
79
}
@@ -212,9 +214,16 @@ where
212
214
Client : crate :: ClientForGrandpa < Block , BE > ,
213
215
{
214
216
// check for a new authority set change.
215
- fn check_new_change ( & self , header : & Block :: Header , hash : Block :: Hash )
216
- -> Option < PendingChange < Block :: Hash , NumberFor < Block > > >
217
- {
217
+ fn check_new_change (
218
+ & self ,
219
+ header : & Block :: Header ,
220
+ hash : Block :: Hash ,
221
+ ) -> Option < PendingChange < Block :: Hash , NumberFor < Block > > > {
222
+ // check for forced authority set hard forks
223
+ if let Some ( change) = self . authority_set_hard_forks . get ( & hash) {
224
+ return Some ( change. clone ( ) ) ;
225
+ }
226
+
218
227
// check for forced change.
219
228
if let Some ( ( median_last_finalized, change) ) = find_forced_change :: < Block > ( header) {
220
229
return Some ( PendingChange {
@@ -529,13 +538,50 @@ impl<Backend, Block: BlockT, Client, SC> GrandpaBlockImport<Backend, Block, Clie
529
538
authority_set : SharedAuthoritySet < Block :: Hash , NumberFor < Block > > ,
530
539
send_voter_commands : mpsc:: UnboundedSender < VoterCommand < Block :: Hash , NumberFor < Block > > > ,
531
540
consensus_changes : SharedConsensusChanges < Block :: Hash , NumberFor < Block > > ,
541
+ authority_set_hard_forks : Vec < ( SetId , PendingChange < Block :: Hash , NumberFor < Block > > ) > ,
532
542
) -> GrandpaBlockImport < Backend , Block , Client , SC > {
543
+ // check for and apply any forced authority set hard fork that applies
544
+ // to the *current* authority set.
545
+ if let Some ( ( _, change) ) = authority_set_hard_forks
546
+ . iter ( )
547
+ . find ( |( set_id, _) | * set_id == authority_set. set_id ( ) )
548
+ {
549
+ let mut authority_set = authority_set. inner ( ) . write ( ) ;
550
+ authority_set. current_authorities = change. next_authorities . clone ( ) ;
551
+ }
552
+
553
+ // index authority set hard forks by block hash so that they can be used
554
+ // by any node syncing the chain and importing a block hard fork
555
+ // authority set changes.
556
+ let authority_set_hard_forks = authority_set_hard_forks
557
+ . into_iter ( )
558
+ . map ( |( _, change) | ( change. canon_hash , change) )
559
+ . collect :: < HashMap < _ , _ > > ( ) ;
560
+
561
+ // check for and apply any forced authority set hard fork that apply to
562
+ // any *pending* standard changes, checking by the block hash at which
563
+ // they were announced.
564
+ {
565
+ let mut authority_set = authority_set. inner ( ) . write ( ) ;
566
+
567
+ authority_set. pending_standard_changes = authority_set
568
+ . pending_standard_changes
569
+ . clone ( )
570
+ . map ( & mut |hash, _, original| {
571
+ authority_set_hard_forks
572
+ . get ( & hash)
573
+ . cloned ( )
574
+ . unwrap_or ( original)
575
+ } ) ;
576
+ }
577
+
533
578
GrandpaBlockImport {
534
579
inner,
535
580
select_chain,
536
581
authority_set,
537
582
send_voter_commands,
538
583
consensus_changes,
584
+ authority_set_hard_forks,
539
585
_phantom : PhantomData ,
540
586
}
541
587
}
0 commit comments