Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemanja0x committed May 15, 2023
1 parent 2dbdab2 commit 76b225e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion consensus/polybft/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ func validateHeaderFields(parent *types.Header, header *types.Header) error {
}
// verify that the gasUsed is <= gasLimit
if header.GasUsed > header.GasLimit {
return fmt.Errorf("invalid gasLimit: have %v, max %v", 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 {
Expand Down
18 changes: 17 additions & 1 deletion consensus/polybft/fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ func TestFSM_ValidateHeader(t *testing.T) {

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, ExtraData: extra}
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")
Expand All @@ -43,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
6 changes: 1 addition & 5 deletions types/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (h *Header) HasReceipts() bool {
}

func (h *Header) SetNonce(i uint64) {
h.Nonce.EncodeNonce(i)
binary.BigEndian.PutUint64(h.Nonce[:], i)
}

func (h *Header) IsGenesis() bool {
Expand All @@ -53,10 +53,6 @@ func (h *Header) IsGenesis() bool {

type Nonce [8]byte

func (n Nonce) EncodeNonce(i uint64) {
binary.BigEndian.PutUint64(n[:], i)
}

func (n Nonce) String() string {
return hex.EncodeToHex(n[:])
}
Expand Down

0 comments on commit 76b225e

Please sign in to comment.