Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional header validations #1508

Merged
merged 4 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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