Skip to content

Commit

Permalink
Merge pull request #1718 from WeiLoy/feature-mainnet-launch
Browse files Browse the repository at this point in the history
Limit the length of the Extra field in the block header
  • Loading branch information
benbaley authored Mar 5, 2021
2 parents 983c60c + 6592d2d commit 62b9a90
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
5 changes: 5 additions & 0 deletions consensus/cbft/cbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,11 @@ func (cbft *Cbft) VerifyHeader(chain consensus.ChainReader, header *types.Header
return fmt.Errorf("verify header fail, missing signature, number:%d, hash:%s", header.Number.Uint64(), header.Hash().String())
}

if header.IsInvalid() {
cbft.log.Error("Verify header fail, Extra field is too long", "number", header.Number, "hash", header.CacheHash())
return fmt.Errorf("verify header fail, Extra field is too long, number:%d, hash:%s", header.Number.Uint64(), header.CacheHash().String())
}

if err := cbft.validatorPool.VerifyHeader(header); err != nil {
cbft.log.Error("Verify header fail", "number", header.Number, "hash", header.Hash(), "err", err)
return fmt.Errorf("verify header fail, number:%d, hash:%s, err:%s", header.Number.Uint64(), header.Hash().String(), err.Error())
Expand Down
4 changes: 2 additions & 2 deletions consensus/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func SigHash(header *types.Header) (hash common.Hash) {
header.GasLimit,
header.GasUsed,
header.Time,
header.Extra[:len(header.Extra)-65], // Yes, this will panic if extra is too short
header.ExtraData(),
header.Nonce,
})
hasher.Sum(hash[:0])
Expand All @@ -74,7 +74,7 @@ func Ecrecover(header *types.Header, sigcache *lru.ARCCache) (common.Address, er
if len(header.Extra) < ExtraSeal {
return common.Address{}, ErrMissingSignature
}
signature := header.Extra[len(header.Extra)-ExtraSeal:]
signature := header.Signature()

// Recover the public key and the Ethereum address
pubkey, err := crypto.Ecrecover(SigHash(header).Bytes(), signature)
Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func DefaultGenesisBlock() *Genesis {
Config: params.MainnetChainConfig,
Nonce: hexutil.MustDecode("0x024c6378c176ef6c717cd37a74c612c9abd615d13873ff6651e3d352b31cb0b2e1"),
Timestamp: 0,
ExtraData: hexutil.MustDecode("0xd782070186706c61746f6e86676f312e3131856c696e757800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
ExtraData: []byte("Let us compute"),
GasLimit: params.GenesisGasLimit,
Alloc: map[common.Address]GenesisAccount{
vm.RewardManagerPoolAddr: {Balance: rewardMgrPoolIssue},
Expand Down
16 changes: 15 additions & 1 deletion core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import (

var (
EmptyRootHash = DeriveSha(Transactions{})
// Extra field in the block header, maximum length
ExtraMaxSize = 97
)

// BlockNonce is an 81-byte vrf proof containing random numbers
Expand Down Expand Up @@ -180,7 +182,19 @@ func (h *Header) Signature() []byte {
if len(h.Extra) < 32 {
return []byte{}
}
return h.Extra[32:]
return h.Extra[32:97]
}

func (h *Header) ExtraData() []byte {
if len(h.Extra) < 32 {
return []byte{}
}
return h.Extra[:32]
}

// Check whether the Extra field exceeds the limit size
func (h *Header) IsInvalid() bool {
return len(h.Extra) > ExtraMaxSize
}

// hasherPool holds Keccak hashers.
Expand Down

0 comments on commit 62b9a90

Please sign in to comment.