Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit aa09746

Browse files
committed
extra data forking poc
1 parent fb7a3f3 commit aa09746

26 files changed

+389
-163
lines changed

chain/params.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ const (
8585
EIP150 = "EIP150"
8686
EIP158 = "EIP158"
8787
EIP155 = "EIP155"
88+
MyFirstFork = "myfirstfork"
89+
MySecondFork = "mysecondfork"
8890
)
8991

9092
// Forks is map which contains all forks and their starting blocks from genesis
@@ -114,6 +116,8 @@ func (f *Forks) At(block uint64) ForksInTime {
114116
EIP150: f.IsActive(EIP150, block),
115117
EIP158: f.IsActive(EIP158, block),
116118
EIP155: f.IsActive(EIP155, block),
119+
MyFirstFork: f.IsActive(MyFirstFork, block),
120+
MySecondFork: f.IsActive(MySecondFork, block),
117121
}
118122
}
119123

@@ -143,7 +147,9 @@ type ForksInTime struct {
143147
London,
144148
EIP150,
145149
EIP158,
146-
EIP155 bool
150+
EIP155,
151+
MyFirstFork,
152+
MySecondFork bool
147153
}
148154

149155
// AllForksEnabled should contain all supported forks by current edge version
@@ -157,4 +163,6 @@ var AllForksEnabled = &Forks{
157163
Petersburg: NewFork(0),
158164
Istanbul: NewFork(0),
159165
London: NewFork(0),
166+
MyFirstFork: NewFork(10),
167+
MySecondFork: NewFork(20),
160168
}

consensus/consensus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type Consensus interface {
3838
GetBridgeProvider() BridgeDataProvider
3939

4040
// FilterExtra filters extra data in header that is not a part of block hash
41-
FilterExtra(extra []byte) ([]byte, error)
41+
FilterExtra(*types.Header) ([]byte, error)
4242

4343
// Initialize initializes the consensus (e.g. setup data)
4444
Initialize() error

consensus/dev/dev.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,6 @@ func (d *Dev) GetBridgeProvider() consensus.BridgeDataProvider {
250250
return nil
251251
}
252252

253-
func (d *Dev) FilterExtra(extra []byte) ([]byte, error) {
254-
return extra, nil
253+
func (d *Dev) FilterExtra(header *types.Header) ([]byte, error) {
254+
return header.ExtraData, nil
255255
}

consensus/dummy/dummy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func (d *Dummy) GetBridgeProvider() consensus.BridgeDataProvider {
7979
return nil
8080
}
8181

82-
func (d *Dummy) FilterExtra(extra []byte) ([]byte, error) {
83-
return extra, nil
82+
func (d *Dummy) FilterExtra(header *types.Header) ([]byte, error) {
83+
return header.ExtraData, nil
8484
}
8585

8686
func (d *Dummy) run() {

consensus/ibft/ibft.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,8 @@ func (i *backendIBFT) GetBridgeProvider() consensus.BridgeDataProvider {
552552
}
553553

554554
// FilterExtra is the implementation of Consensus interface
555-
func (i *backendIBFT) FilterExtra(extra []byte) ([]byte, error) {
556-
return extra, nil
555+
func (i *backendIBFT) FilterExtra(header *types.Header) ([]byte, error) {
556+
return header.ExtraData, nil
557557
}
558558

559559
// updateCurrentModules updates Signer, Hooks, and Validators

consensus/polybft/checkpoint_manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (c *checkpointManager) submitCheckpoint(latestHeader *types.Header, isEndOf
142142
return fmt.Errorf("block %d was not found", initialBlockNumber)
143143
}
144144

145-
parentExtra, err = GetIbftExtra(parentHeader.ExtraData)
145+
parentExtra, err = GetIbftExtra(parentHeader.ExtraData, parentHeader.Number)
146146
if err != nil {
147147
return err
148148
}
@@ -155,7 +155,7 @@ func (c *checkpointManager) submitCheckpoint(latestHeader *types.Header, isEndOf
155155
return fmt.Errorf("block %d was not found", blockNumber)
156156
}
157157

158-
currentExtra, err = GetIbftExtra(currentHeader.ExtraData)
158+
currentExtra, err = GetIbftExtra(currentHeader.ExtraData, currentHeader.Number)
159159
if err != nil {
160160
return err
161161
}
@@ -182,7 +182,7 @@ func (c *checkpointManager) submitCheckpoint(latestHeader *types.Header, isEndOf
182182
// (in case there were pending checkpoint blocks)
183183
if currentExtra == nil {
184184
// we need to send checkpoint for the latest block
185-
currentExtra, err = GetIbftExtra(latestHeader.ExtraData)
185+
currentExtra, err = GetIbftExtra(latestHeader.ExtraData, latestHeader.Number)
186186
if err != nil {
187187
return err
188188
}

consensus/polybft/consensus_metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func updateBlockMetrics(currentBlock *types.Block, parentHeader *types.Header) e
2525
// update the number of transactions in the block metric
2626
metrics.SetGauge([]string{consensusMetricsPrefix, "num_txs"}, float32(len(currentBlock.Transactions)))
2727

28-
extra, err := GetIbftExtra(currentBlock.Header.ExtraData)
28+
extra, err := GetIbftExtra(currentBlock.Header.ExtraData, currentBlock.Number())
2929
if err != nil {
3030
return err
3131
}

consensus/polybft/consensus_runtime.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ func (c *consensusRuntime) calculateCommitEpochInput(
520520
return nil
521521
}
522522

523-
blockExtra, err := GetIbftExtra(currentBlock.ExtraData)
523+
blockExtra, err := GetIbftExtra(currentBlock.ExtraData, currentBlock.Number)
524524
if err != nil {
525525
return nil, nil, err
526526
}
@@ -694,7 +694,7 @@ func (c *consensusRuntime) IsValidProposalHash(proposal *proto.Proposal, hash []
694694
return false
695695
}
696696

697-
extra, err := GetIbftExtra(block.Header.ExtraData)
697+
extra, err := GetIbftExtra(block.Header.ExtraData, block.Number())
698698
if err != nil {
699699
c.logger.Error("failed to retrieve extra", "block number", block.Number(), "error", err)
700700

@@ -800,7 +800,7 @@ func (c *consensusRuntime) BuildPrePrepareMessage(
800800
return nil
801801
}
802802

803-
extra, err := GetIbftExtra(block.Header.ExtraData)
803+
extra, err := GetIbftExtra(block.Header.ExtraData, block.Number())
804804
if err != nil {
805805
c.logger.Error("failed to retrieve extra for block %d: %w", block.Number(), err)
806806

@@ -932,7 +932,7 @@ func (c *consensusRuntime) getFirstBlockOfEpoch(epochNumber uint64, latestHeader
932932

933933
blockHeader := latestHeader
934934

935-
blockExtra, err := GetIbftExtra(latestHeader.ExtraData)
935+
blockExtra, err := GetIbftExtra(latestHeader.ExtraData, latestHeader.Number)
936936
if err != nil {
937937
return 0, err
938938
}

consensus/polybft/extra.go

Lines changed: 37 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -26,122 +26,52 @@ var PolyBFTMixDigest = types.StringToHash("adce6e5230abe012342a44e4e9b6d05997d6f
2626

2727
// Extra defines the structure of the extra field for Istanbul
2828
type 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)
210144
type 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

Comments
 (0)