Skip to content

Commit

Permalink
Additional header validations (#1508)
Browse files Browse the repository at this point in the history
Additional header validations
Co-authored-by: Stefan Negovanović <93934272+Stefan-Ethernal@users.noreply.github.com>
  • Loading branch information
Nemanja0x committed May 15, 2023
1 parent 2dcc318 commit 56eea51
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions consensus/polybft/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,10 @@ func (f *fsm) verifyDistributeRewardsTx(distributeRewardsTx *types.Transaction)
}

func validateHeaderFields(parent *types.Header, header *types.Header) error {
// header extra data must be higher or equal to ExtraVanity = 32 in order to be compliant with Ethereum blocks
if len(header.ExtraData) < ExtraVanity {
return fmt.Errorf("extra-data shorter than %d bytes (%d)", ExtraVanity, len(header.ExtraData))
}
// verify parent hash
if parent.Hash != header.ParentHash {
return fmt.Errorf("incorrect header parent hash (parent=%s, header parent=%s)", parent.Hash, header.ParentHash)
Expand All @@ -640,6 +644,14 @@ func validateHeaderFields(parent *types.Header, header *types.Header) error {
if header.Number != parent.Number+1 {
return fmt.Errorf("invalid number")
}
// verify header nonce is zero
if header.Nonce != types.ZeroNonce {
return fmt.Errorf("invalid nonce")
}
// verify that the gasUsed is <= gasLimit
if header.GasUsed > header.GasLimit {
return fmt.Errorf("invalid gas limit: have %v, max %v", header.GasUsed, header.GasLimit)
}
// verify time has passed
if header.Timestamp <= parent.Timestamp {
return fmt.Errorf("timestamp older than parent")
Expand Down
17 changes: 17 additions & 0 deletions consensus/polybft/fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ import (
func TestFSM_ValidateHeader(t *testing.T) {
t.Parallel()

extra := createTestExtra(validator.AccountSet{}, validator.AccountSet{}, 0, 0, 0)
parent := &types.Header{Number: 0, Hash: types.BytesToHash([]byte{1, 2, 3})}
header := &types.Header{Number: 0}

// parent extra data
require.ErrorContains(t, validateHeaderFields(parent, header), "extra-data shorter than")
header.ExtraData = extra

// parent hash
require.ErrorContains(t, validateHeaderFields(parent, header), "incorrect header parent hash")
header.ParentHash = parent.Hash
Expand All @@ -42,6 +47,18 @@ func TestFSM_ValidateHeader(t *testing.T) {
require.ErrorContains(t, validateHeaderFields(parent, header), "timestamp older than parent")
header.Timestamp = 10

// failed nonce
header.SetNonce(1)
require.ErrorContains(t, validateHeaderFields(parent, header), "invalid nonce")
header.SetNonce(0)

// failed gas
header.GasLimit = 10
header.GasUsed = 11
require.ErrorContains(t, validateHeaderFields(parent, header), "invalid gas limit")
header.GasLimit = 10
header.GasUsed = 10

// mix digest
require.ErrorContains(t, validateHeaderFields(parent, header), "mix digest is not correct")
header.MixHash = PolyBFTMixDigest
Expand Down
3 changes: 3 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var (
// ZeroHash is the default zero hash
ZeroHash = Hash{}

// ZeroNonce is the default empty nonce
ZeroNonce = Nonce{}

// EmptyRootHash is the root when there are no transactions
EmptyRootHash = StringToHash("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")

Expand Down

0 comments on commit 56eea51

Please sign in to comment.