Skip to content

Commit 158d603

Browse files
committed
consensus, core: drop all the legacy custom core error types
1 parent 702bef8 commit 158d603

15 files changed

Lines changed: 114 additions & 260 deletions

File tree

‎consensus/errors.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2017 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package consensus
18+
19+
import "errors"
20+
21+
var (
22+
// ErrUnknownAncestor is returned when validating a block requires an ancestor
23+
// that is unknown.
24+
ErrUnknownAncestor = errors.New("unknown ancestor")
25+
26+
// ErrLargeBlockTime is returned if the value of the timestamp is beyond
27+
// any reasonable value.
28+
ErrLargeBlockTime = errors.New("timestamp too big")
29+
30+
// ErrZeroBlockTime is returned if the block's timestamp is the same as the one
31+
// its parent has.
32+
ErrZeroBlockTime = errors.New("timestamp equals parent's")
33+
34+
// ErrFutureBlock is returned when a block's timestamp is in the future according
35+
// to the current node.
36+
ErrFutureBlock = errors.New("block in the future")
37+
38+
// ErrInvalidNumber is returned if a block's number doesn't equal it's parent's
39+
// plus one.
40+
ErrInvalidNumber = errors.New("invalid block number")
41+
)

‎consensus/ethash/consensus.go‎

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,15 @@ var (
4242
)
4343

4444
var (
45-
ErrInvalidChain = errors.New("invalid header chain")
46-
ErrParentUnknown = errors.New("parent not known locally")
47-
ErrFutureBlock = errors.New("block in the future")
48-
ErrLargeBlockTimestamp = errors.New("timestamp too big")
49-
ErrZeroBlockTime = errors.New("timestamp equals parent's")
50-
ErrInvalidNumber = errors.New("invalid block number")
51-
ErrTooManyUncles = errors.New("too many uncles")
52-
ErrDuplicateUncle = errors.New("duplicate uncle")
53-
ErrUncleIsAncestor = errors.New("uncle is ancestor")
54-
ErrDanglingUncle = errors.New("uncle's parent is not ancestor")
55-
ErrNonceOutOfRange = errors.New("nonce out of range")
56-
ErrInvalidDifficulty = errors.New("non-positive difficulty")
57-
ErrInvalidMixDigest = errors.New("invalid mix digest")
58-
ErrInvalidPoW = errors.New("invalid proof-of-work")
45+
ErrInvalidChain = errors.New("invalid header chain")
46+
ErrTooManyUncles = errors.New("too many uncles")
47+
ErrDuplicateUncle = errors.New("duplicate uncle")
48+
ErrUncleIsAncestor = errors.New("uncle is ancestor")
49+
ErrDanglingUncle = errors.New("uncle's parent is not ancestor")
50+
ErrNonceOutOfRange = errors.New("nonce out of range")
51+
ErrInvalidDifficulty = errors.New("non-positive difficulty")
52+
ErrInvalidMixDigest = errors.New("invalid mix digest")
53+
ErrInvalidPoW = errors.New("invalid proof-of-work")
5954
)
6055

6156
// VerifyHeader checks whether a header conforms to the consensus rules of the
@@ -72,7 +67,7 @@ func (ethash *Ethash) VerifyHeader(chain consensus.ChainReader, header *types.He
7267
}
7368
parent := chain.GetHeader(header.ParentHash, number-1)
7469
if parent == nil {
75-
return ErrParentUnknown
70+
return consensus.ErrUnknownAncestor
7671
}
7772
// Sanity checks passed, do a proper verification
7873
return ethash.verifyHeader(chain, header, parent, false, seal)
@@ -125,7 +120,7 @@ func (ethash *Ethash) VerifyHeaders(chain consensus.ChainReader, headers []*type
125120
case chain.GetHeader(headers[index].Hash(), headers[index].Number.Uint64()-1) != nil:
126121
outputs <- result{index: index, err: nil}
127122
case parent == nil:
128-
failure = ErrParentUnknown
123+
failure = consensus.ErrUnknownAncestor
129124
outputs <- result{index: index, err: failure}
130125
default:
131126
failure = ethash.verifyHeader(chain, headers[index], parent, false, seals[index])
@@ -254,15 +249,15 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
254249
// Verify the header's timestamp
255250
if uncle {
256251
if header.Time.Cmp(math.MaxBig256) > 0 {
257-
return ErrLargeBlockTimestamp
252+
return consensus.ErrLargeBlockTime
258253
}
259254
} else {
260255
if header.Time.Cmp(big.NewInt(time.Now().Unix())) > 0 {
261-
return ErrFutureBlock
256+
return consensus.ErrFutureBlock
262257
}
263258
}
264259
if header.Time.Cmp(parent.Time) <= 0 {
265-
return ErrZeroBlockTime
260+
return consensus.ErrZeroBlockTime
266261
}
267262
// Verify the block's difficulty based in it's timestamp and parent's difficulty
268263
expected := CalcDifficulty(chain.Config(), header.Time.Uint64(), parent.Time.Uint64(), parent.Number, parent.Difficulty)
@@ -282,7 +277,7 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
282277
}
283278
// Verify that the block number is parent's +1
284279
if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(big.NewInt(1)) != 0 {
285-
return ErrInvalidNumber
280+
return consensus.ErrInvalidNumber
286281
}
287282
// Verify the engine specific seal securing the block
288283
if seal {
@@ -449,7 +444,7 @@ func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Head
449444
func (ethash *Ethash) Prepare(chain consensus.ChainReader, header *types.Header) error {
450445
parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
451446
if parent == nil {
452-
return ErrParentUnknown
447+
return consensus.ErrUnknownAncestor
453448
}
454449
header.Difficulty = CalcDifficulty(chain.Config(), header.Time.Uint64(),
455450
parent.Time.Uint64(), parent.Number, parent.Difficulty)

‎core/block_validator.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
5454
// Check whether the block's known, and if not, that it's linkable
5555
if v.bc.HasBlock(block.Hash()) {
5656
if _, err := state.New(block.Root(), v.bc.chainDb); err == nil {
57-
return &KnownBlockError{block.Number(), block.Hash()}
57+
return ErrKnownBlock
5858
}
5959
}
6060
parent := v.bc.GetBlock(block.ParentHash(), block.NumberU64()-1)
6161
if parent == nil {
62-
return ParentError(block.ParentHash())
62+
return consensus.ErrUnknownAncestor
6363
}
6464
if _, err := state.New(parent.Root(), v.bc.chainDb); err != nil {
65-
return ParentError(block.ParentHash())
65+
return consensus.ErrUnknownAncestor
6666
}
6767
// Header validity is known at this point, check the uncles and transactions
6868
header := block.Header()
@@ -82,10 +82,10 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
8282
// transition, such as amount of used gas, the receipt roots and the state root
8383
// itself. ValidateState returns a database batch if the validation was a success
8484
// otherwise nil and an error is returned.
85-
func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas *big.Int) (err error) {
85+
func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas *big.Int) error {
8686
header := block.Header()
8787
if block.GasUsed().Cmp(usedGas) != 0 {
88-
return ValidationError(fmt.Sprintf("invalid gas used (remote: %v local: %v)", block.GasUsed(), usedGas))
88+
return fmt.Errorf("invalid gas used (remote: %v local: %v)", block.GasUsed(), usedGas)
8989
}
9090
// Validate the received block's bloom with the one derived from the generated receipts.
9191
// For valid blocks this should always validate to true.

‎core/blockchain.go‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status WriteStatus, err
831831
// Calculate the total difficulty of the block
832832
ptd := self.GetTd(block.ParentHash(), block.NumberU64()-1)
833833
if ptd == nil {
834-
return NonStatTy, ParentError(block.ParentHash())
834+
return NonStatTy, consensus.ErrUnknownAncestor
835835
}
836836
// Make sure no inconsistent state is leaked during insertion
837837
self.mu.Lock()
@@ -918,9 +918,8 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
918918
}
919919
// If the header is a banned one, straight out abort
920920
if BadHashes[block.Hash()] {
921-
err := BadHashError(block.Hash())
922-
self.reportBlock(block, nil, err)
923-
return i, err
921+
self.reportBlock(block, nil, ErrBlacklistedHash)
922+
return i, ErrBlacklistedHash
924923
}
925924
// Wait for the block's verification to complete
926925
bstart := time.Now()
@@ -930,25 +929,25 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
930929
err = self.Validator().ValidateBody(block)
931930
}
932931
if err != nil {
933-
if IsKnownBlockErr(err) {
932+
if err == ErrKnownBlock {
934933
stats.ignored++
935934
continue
936935
}
937936

938-
if err == BlockFutureErr {
937+
if err == consensus.ErrFutureBlock {
939938
// Allow up to MaxFuture second in the future blocks. If this limit
940939
// is exceeded the chain is discarded and processed at a later time
941940
// if given.
942941
max := big.NewInt(time.Now().Unix() + maxTimeFutureBlocks)
943-
if block.Time().Cmp(max) == 1 {
944-
return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max)
942+
if block.Time().Cmp(max) > 0 {
943+
return i, fmt.Errorf("future block: %v > %v", block.Time(), max)
945944
}
946945
self.futureBlocks.Add(block.Hash(), block)
947946
stats.queued++
948947
continue
949948
}
950949

951-
if IsParentErr(err) && self.futureBlocks.Contains(block.ParentHash()) {
950+
if err == consensus.ErrUnknownAncestor && self.futureBlocks.Contains(block.ParentHash()) {
952951
self.futureBlocks.Add(block.Hash(), block)
953952
stats.queued++
954953
continue

‎core/blockchain_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
126126
err = blockchain.validator.ValidateBody(block)
127127
}
128128
if err != nil {
129-
if IsKnownBlockErr(err) {
129+
if err == ErrKnownBlock {
130130
continue
131131
}
132132
return err
@@ -441,8 +441,8 @@ func testBadHashes(t *testing.T, full bool) {
441441
BadHashes[headers[2].Hash()] = true
442442
_, err = bc.InsertHeaderChain(headers, 1)
443443
}
444-
if !IsBadHashError(err) {
445-
t.Errorf("error mismatch: want: BadHashError, have: %v", err)
444+
if err != ErrBlacklistedHash {
445+
t.Errorf("error mismatch: have: %v, want: %v", err, ErrBlacklistedHash)
446446
}
447447
}
448448

‎core/dao.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package core
1818

1919
import (
2020
"bytes"
21+
"fmt"
2122
"math/big"
2223

2324
"github.com/ethereum/go-ethereum/core/state"
@@ -46,11 +47,11 @@ func ValidateDAOHeaderExtraData(config *params.ChainConfig, header *types.Header
4647
// Depending whether we support or oppose the fork, validate the extra-data contents
4748
if config.DAOForkSupport {
4849
if !bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
49-
return ValidationError("DAO pro-fork bad block extra-data: 0x%x", header.Extra)
50+
return fmt.Errorf("DAO pro-fork bad block extra-data: 0x%x", header.Extra)
5051
}
5152
} else {
5253
if bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
53-
return ValidationError("DAO no-fork bad block extra-data: 0x%x", header.Extra)
54+
return fmt.Errorf("DAO no-fork bad block extra-data: 0x%x", header.Extra)
5455
}
5556
}
5657
// All ok, header has the same extra-data we expect

0 commit comments

Comments
 (0)