@@ -26,122 +26,52 @@ var PolyBFTMixDigest = types.StringToHash("adce6e5230abe012342a44e4e9b6d05997d6f
2626
2727// Extra defines the structure of the extra field for Istanbul
2828type Extra struct {
29- Validators * validator.ValidatorSetDelta
30- Parent * Signature
31- Committed * Signature
32- Checkpoint * CheckpointData
29+ Validators * validator.ValidatorSetDelta
30+ Parent * Signature
31+ Committed * Signature
32+ Checkpoint * CheckpointData
33+ Dummy1 string // MyFirstFork fork
34+ Dummy2 string // MySecondFork fork
35+ BlockNumber uint64 // field used by forking manager
3336}
3437
3538// MarshalRLPTo defines the marshal function wrapper for Extra
36- func (i * Extra ) MarshalRLPTo (dst []byte ) []byte {
39+ func (e * Extra ) MarshalRLPTo (dst []byte ) []byte {
3740 ar := & fastrlp.Arena {}
3841
39- return append (make ([]byte , ExtraVanity ), i .MarshalRLPWith (ar ).MarshalTo (dst )... )
42+ return append (make ([]byte , ExtraVanity ), e .MarshalRLPWith (ar ).MarshalTo (dst )... )
4043}
4144
4245// MarshalRLPWith defines the marshal function implementation for Extra
43- func (i * Extra ) MarshalRLPWith (ar * fastrlp.Arena ) * fastrlp.Value {
44- vv := ar .NewArray ()
45-
46- // Validators
47- if i .Validators == nil {
48- vv .Set (ar .NewNullArray ())
49- } else {
50- vv .Set (i .Validators .MarshalRLPWith (ar ))
51- }
52-
53- // Parent Signatures
54- if i .Parent == nil {
55- vv .Set (ar .NewNullArray ())
56- } else {
57- vv .Set (i .Parent .MarshalRLPWith (ar ))
58- }
59-
60- // Committed Signatures
61- if i .Committed == nil {
62- vv .Set (ar .NewNullArray ())
63- } else {
64- vv .Set (i .Committed .MarshalRLPWith (ar ))
65- }
66-
67- // Checkpoint
68- if i .Checkpoint == nil {
69- vv .Set (ar .NewNullArray ())
70- } else {
71- vv .Set (i .Checkpoint .MarshalRLPWith (ar ))
72- }
73-
74- return vv
46+ func (e * Extra ) MarshalRLPWith (ar * fastrlp.Arena ) * fastrlp.Value {
47+ return GetExtraHandler (e .BlockNumber ).MarshalRLPWith (e , ar )
7548}
7649
7750// UnmarshalRLP defines the unmarshal function wrapper for Extra
78- func (i * Extra ) UnmarshalRLP (input []byte ) error {
79- return fastrlp .UnmarshalRLP (input [ExtraVanity :], i )
51+ func (e * Extra ) UnmarshalRLP (input []byte ) error {
52+ return fastrlp .UnmarshalRLP (input [ExtraVanity :], e )
8053}
8154
8255// UnmarshalRLPWith defines the unmarshal implementation for Extra
83- func (i * Extra ) UnmarshalRLPWith (v * fastrlp.Value ) error {
84- const expectedElements = 4
85-
86- elems , err := v .GetElems ()
87- if err != nil {
88- return err
89- }
90-
91- if num := len (elems ); num != expectedElements {
92- return fmt .Errorf ("incorrect elements count to decode Extra, expected %d but found %d" , expectedElements , num )
93- }
94-
95- // Validators
96- if elems [0 ].Elems () > 0 {
97- i .Validators = & validator.ValidatorSetDelta {}
98- if err := i .Validators .UnmarshalRLPWith (elems [0 ]); err != nil {
99- return err
100- }
101- }
102-
103- // Parent Signatures
104- if elems [1 ].Elems () > 0 {
105- i .Parent = & Signature {}
106- if err := i .Parent .UnmarshalRLPWith (elems [1 ]); err != nil {
107- return err
108- }
109- }
110-
111- // Committed Signatures
112- if elems [2 ].Elems () > 0 {
113- i .Committed = & Signature {}
114- if err := i .Committed .UnmarshalRLPWith (elems [2 ]); err != nil {
115- return err
116- }
117- }
118-
119- // Checkpoint
120- if elems [3 ].Elems () > 0 {
121- i .Checkpoint = & CheckpointData {}
122- if err := i .Checkpoint .UnmarshalRLPWith (elems [3 ]); err != nil {
123- return err
124- }
125- }
126-
127- return nil
56+ func (e * Extra ) UnmarshalRLPWith (v * fastrlp.Value ) error {
57+ return GetExtraHandler (e .BlockNumber ).UnmarshalRLPWith (e , v )
12858}
12959
13060// ValidateFinalizedData contains extra data validations for finalized headers
131- func (i * Extra ) ValidateFinalizedData (header * types.Header , parent * types.Header , parents []* types.Header ,
61+ func (e * Extra ) ValidateFinalizedData (header * types.Header , parent * types.Header , parents []* types.Header ,
13262 chainID uint64 , consensusBackend polybftBackend , domain []byte , logger hclog.Logger ) error {
13363 // validate committed signatures
13464 blockNumber := header .Number
135- if i .Committed == nil {
65+ if e .Committed == nil {
13666 return fmt .Errorf ("failed to verify signatures for block %d, because signatures are not present" , blockNumber )
13767 }
13868
139- if i .Checkpoint == nil {
69+ if e .Checkpoint == nil {
14070 return fmt .Errorf ("failed to verify signatures for block %d, because checkpoint data are not present" , blockNumber )
14171 }
14272
14373 // validate current block signatures
144- checkpointHash , err := i .Checkpoint .Hash (chainID , header .Number , header .Hash )
74+ checkpointHash , err := e .Checkpoint .Hash (chainID , header .Number , header .Hash )
14575 if err != nil {
14676 return fmt .Errorf ("failed to calculate proposal hash: %w" , err )
14777 }
@@ -151,34 +81,34 @@ func (i *Extra) ValidateFinalizedData(header *types.Header, parent *types.Header
15181 return fmt .Errorf ("failed to validate header for block %d. could not retrieve block validators:%w" , blockNumber , err )
15282 }
15383
154- if err := i .Committed .Verify (validators , checkpointHash , domain , logger ); err != nil {
84+ if err := e .Committed .Verify (validators , checkpointHash , domain , logger ); err != nil {
15585 return fmt .Errorf ("failed to verify signatures for block %d (proposal hash %s): %w" ,
15686 blockNumber , checkpointHash , err )
15787 }
15888
159- parentExtra , err := GetIbftExtra (parent .ExtraData )
89+ parentExtra , err := GetIbftExtra (parent .ExtraData , parent . Number )
16090 if err != nil {
16191 return fmt .Errorf ("failed to verify signatures for block %d: %w" , blockNumber , err )
16292 }
16393
16494 // validate parent signatures
165- if err := i .ValidateParentSignatures (blockNumber , consensusBackend , parents ,
95+ if err := e .ValidateParentSignatures (blockNumber , consensusBackend , parents ,
16696 parent , parentExtra , chainID , domain , logger ); err != nil {
16797 return err
16898 }
16999
170- return i .Checkpoint .ValidateBasic (parentExtra .Checkpoint )
100+ return e .Checkpoint .ValidateBasic (parentExtra .Checkpoint )
171101}
172102
173103// ValidateParentSignatures validates signatures for parent block
174- func (i * Extra ) ValidateParentSignatures (blockNumber uint64 , consensusBackend polybftBackend , parents []* types.Header ,
104+ func (e * Extra ) ValidateParentSignatures (blockNumber uint64 , consensusBackend polybftBackend , parents []* types.Header ,
175105 parent * types.Header , parentExtra * Extra , chainID uint64 , domain []byte , logger hclog.Logger ) error {
176106 // skip block 1 because genesis does not have committed signatures
177107 if blockNumber <= 1 {
178108 return nil
179109 }
180110
181- if i .Parent == nil {
111+ if e .Parent == nil {
182112 return fmt .Errorf ("failed to verify signatures for parent of block %d because signatures are not present" ,
183113 blockNumber )
184114 }
@@ -197,14 +127,18 @@ func (i *Extra) ValidateParentSignatures(blockNumber uint64, consensusBackend po
197127 return fmt .Errorf ("failed to calculate parent proposal hash: %w" , err )
198128 }
199129
200- if err := i .Parent .Verify (parentValidators , parentCheckpointHash , domain , logger ); err != nil {
130+ if err := e .Parent .Verify (parentValidators , parentCheckpointHash , domain , logger ); err != nil {
201131 return fmt .Errorf ("failed to verify signatures for parent of block %d (proposal hash: %s): %w" ,
202132 blockNumber , parentCheckpointHash , err )
203133 }
204134
205135 return nil
206136}
207137
138+ func (e * Extra ) ValidateAdditional (header * types.Header ) error {
139+ return GetExtraHandler (e .BlockNumber ).ValidateAdditional (e , header )
140+ }
141+
208142// Signature represents aggregated signatures of signers accompanied with a bitmap
209143// (in order to be able to determine identities of each signer)
210144type Signature struct {
@@ -464,29 +398,24 @@ func (c *CheckpointData) Validate(parentCheckpoint *CheckpointData,
464398
465399// GetIbftExtraClean returns unmarshaled extra field from the passed in header,
466400// but without signatures for the given header (it only includes signatures for the parent block)
467- func GetIbftExtraClean (extraRaw []byte ) ([]byte , error ) {
468- extra , err := GetIbftExtra (extraRaw )
401+ func GetIbftExtraClean (extraRaw []byte , blockNumber uint64 ) ([]byte , error ) {
402+ extra , err := GetIbftExtra (extraRaw , blockNumber )
469403 if err != nil {
470404 return nil , err
471405 }
472406
473- ibftExtra := & Extra {
474- Parent : extra .Parent ,
475- Validators : extra .Validators ,
476- Checkpoint : extra .Checkpoint ,
477- Committed : & Signature {},
478- }
479-
480- return ibftExtra .MarshalRLPTo (nil ), nil
407+ return GetExtraHandler (blockNumber ).GetIbftExtraClean (extra ).MarshalRLPTo (nil ), nil
481408}
482409
483410// GetIbftExtra returns the istanbul extra data field from the passed in header
484- func GetIbftExtra (extraRaw []byte ) (* Extra , error ) {
411+ func GetIbftExtra (extraRaw []byte , blockNumber uint64 ) (* Extra , error ) {
485412 if len (extraRaw ) < ExtraVanity {
486413 return nil , fmt .Errorf ("wrong extra size: %d" , len (extraRaw ))
487414 }
488415
489- extra := & Extra {}
416+ extra := & Extra {
417+ BlockNumber : blockNumber ,
418+ }
490419
491420 if err := extra .UnmarshalRLP (extraRaw ); err != nil {
492421 return nil , err
0 commit comments