Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
158d6035
Unverified
Commit
158d6035
authored
Apr 06, 2017
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
consensus, core: drop all the legacy custom core error types
parent
702bef84
Changes
15
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
114 additions
and
260 deletions
+114
-260
errors.go
consensus/errors.go
+41
-0
consensus.go
consensus/ethash/consensus.go
+16
-21
block_validator.go
core/block_validator.go
+5
-5
blockchain.go
core/blockchain.go
+8
-9
blockchain_test.go
core/blockchain_test.go
+3
-3
dao.go
core/dao.go
+3
-2
error.go
core/error.go
+9
-181
gaspool.go
core/gaspool.go
+1
-1
headerchain.go
core/headerchain.go
+2
-2
state_transition.go
core/state_transition.go
+7
-17
fetcher.go
eth/fetcher/fetcher.go
+2
-2
fetcher.go
les/fetcher.go
+2
-1
lightchain_test.go
light/lightchain_test.go
+2
-3
worker.go
miner/worker.go
+12
-12
state_test_util.go
tests/state_test_util.go
+1
-1
No files found.
consensus/errors.go
0 → 100644
View file @
158d6035
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package
consensus
import
"errors"
var
(
// ErrUnknownAncestor is returned when validating a block requires an ancestor
// that is unknown.
ErrUnknownAncestor
=
errors
.
New
(
"unknown ancestor"
)
// ErrLargeBlockTime is returned if the value of the timestamp is beyond
// any reasonable value.
ErrLargeBlockTime
=
errors
.
New
(
"timestamp too big"
)
// ErrZeroBlockTime is returned if the block's timestamp is the same as the one
// its parent has.
ErrZeroBlockTime
=
errors
.
New
(
"timestamp equals parent's"
)
// ErrFutureBlock is returned when a block's timestamp is in the future according
// to the current node.
ErrFutureBlock
=
errors
.
New
(
"block in the future"
)
// ErrInvalidNumber is returned if a block's number doesn't equal it's parent's
// plus one.
ErrInvalidNumber
=
errors
.
New
(
"invalid block number"
)
)
consensus/ethash/consensus.go
View file @
158d6035
...
@@ -42,20 +42,15 @@ var (
...
@@ -42,20 +42,15 @@ var (
)
)
var
(
var
(
ErrInvalidChain
=
errors
.
New
(
"invalid header chain"
)
ErrInvalidChain
=
errors
.
New
(
"invalid header chain"
)
ErrParentUnknown
=
errors
.
New
(
"parent not known locally"
)
ErrTooManyUncles
=
errors
.
New
(
"too many uncles"
)
ErrFutureBlock
=
errors
.
New
(
"block in the future"
)
ErrDuplicateUncle
=
errors
.
New
(
"duplicate uncle"
)
ErrLargeBlockTimestamp
=
errors
.
New
(
"timestamp too big"
)
ErrUncleIsAncestor
=
errors
.
New
(
"uncle is ancestor"
)
ErrZeroBlockTime
=
errors
.
New
(
"timestamp equals parent's"
)
ErrDanglingUncle
=
errors
.
New
(
"uncle's parent is not ancestor"
)
ErrInvalidNumber
=
errors
.
New
(
"invalid block number"
)
ErrNonceOutOfRange
=
errors
.
New
(
"nonce out of range"
)
ErrTooManyUncles
=
errors
.
New
(
"too many uncles"
)
ErrInvalidDifficulty
=
errors
.
New
(
"non-positive difficulty"
)
ErrDuplicateUncle
=
errors
.
New
(
"duplicate uncle"
)
ErrInvalidMixDigest
=
errors
.
New
(
"invalid mix digest"
)
ErrUncleIsAncestor
=
errors
.
New
(
"uncle is ancestor"
)
ErrInvalidPoW
=
errors
.
New
(
"invalid proof-of-work"
)
ErrDanglingUncle
=
errors
.
New
(
"uncle's parent is not ancestor"
)
ErrNonceOutOfRange
=
errors
.
New
(
"nonce out of range"
)
ErrInvalidDifficulty
=
errors
.
New
(
"non-positive difficulty"
)
ErrInvalidMixDigest
=
errors
.
New
(
"invalid mix digest"
)
ErrInvalidPoW
=
errors
.
New
(
"invalid proof-of-work"
)
)
)
// VerifyHeader checks whether a header conforms to the consensus rules of the
// 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
...
@@ -72,7 +67,7 @@ func (ethash *Ethash) VerifyHeader(chain consensus.ChainReader, header *types.He
}
}
parent
:=
chain
.
GetHeader
(
header
.
ParentHash
,
number
-
1
)
parent
:=
chain
.
GetHeader
(
header
.
ParentHash
,
number
-
1
)
if
parent
==
nil
{
if
parent
==
nil
{
return
ErrParentUnknown
return
consensus
.
ErrUnknownAncestor
}
}
// Sanity checks passed, do a proper verification
// Sanity checks passed, do a proper verification
return
ethash
.
verifyHeader
(
chain
,
header
,
parent
,
false
,
seal
)
return
ethash
.
verifyHeader
(
chain
,
header
,
parent
,
false
,
seal
)
...
@@ -125,7 +120,7 @@ func (ethash *Ethash) VerifyHeaders(chain consensus.ChainReader, headers []*type
...
@@ -125,7 +120,7 @@ func (ethash *Ethash) VerifyHeaders(chain consensus.ChainReader, headers []*type
case
chain
.
GetHeader
(
headers
[
index
]
.
Hash
(),
headers
[
index
]
.
Number
.
Uint64
()
-
1
)
!=
nil
:
case
chain
.
GetHeader
(
headers
[
index
]
.
Hash
(),
headers
[
index
]
.
Number
.
Uint64
()
-
1
)
!=
nil
:
outputs
<-
result
{
index
:
index
,
err
:
nil
}
outputs
<-
result
{
index
:
index
,
err
:
nil
}
case
parent
==
nil
:
case
parent
==
nil
:
failure
=
ErrParentUnknown
failure
=
consensus
.
ErrUnknownAncestor
outputs
<-
result
{
index
:
index
,
err
:
failure
}
outputs
<-
result
{
index
:
index
,
err
:
failure
}
default
:
default
:
failure
=
ethash
.
verifyHeader
(
chain
,
headers
[
index
],
parent
,
false
,
seals
[
index
])
failure
=
ethash
.
verifyHeader
(
chain
,
headers
[
index
],
parent
,
false
,
seals
[
index
])
...
@@ -254,15 +249,15 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
...
@@ -254,15 +249,15 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
// Verify the header's timestamp
// Verify the header's timestamp
if
uncle
{
if
uncle
{
if
header
.
Time
.
Cmp
(
math
.
MaxBig256
)
>
0
{
if
header
.
Time
.
Cmp
(
math
.
MaxBig256
)
>
0
{
return
ErrLargeBlockTimestamp
return
consensus
.
ErrLargeBlockTime
}
}
}
else
{
}
else
{
if
header
.
Time
.
Cmp
(
big
.
NewInt
(
time
.
Now
()
.
Unix
()))
>
0
{
if
header
.
Time
.
Cmp
(
big
.
NewInt
(
time
.
Now
()
.
Unix
()))
>
0
{
return
ErrFutureBlock
return
consensus
.
ErrFutureBlock
}
}
}
}
if
header
.
Time
.
Cmp
(
parent
.
Time
)
<=
0
{
if
header
.
Time
.
Cmp
(
parent
.
Time
)
<=
0
{
return
ErrZeroBlockTime
return
consensus
.
ErrZeroBlockTime
}
}
// Verify the block's difficulty based in it's timestamp and parent's difficulty
// Verify the block's difficulty based in it's timestamp and parent's difficulty
expected
:=
CalcDifficulty
(
chain
.
Config
(),
header
.
Time
.
Uint64
(),
parent
.
Time
.
Uint64
(),
parent
.
Number
,
parent
.
Difficulty
)
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 *
...
@@ -282,7 +277,7 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
}
}
// Verify that the block number is parent's +1
// Verify that the block number is parent's +1
if
diff
:=
new
(
big
.
Int
)
.
Sub
(
header
.
Number
,
parent
.
Number
);
diff
.
Cmp
(
big
.
NewInt
(
1
))
!=
0
{
if
diff
:=
new
(
big
.
Int
)
.
Sub
(
header
.
Number
,
parent
.
Number
);
diff
.
Cmp
(
big
.
NewInt
(
1
))
!=
0
{
return
ErrInvalidNumber
return
consensus
.
ErrInvalidNumber
}
}
// Verify the engine specific seal securing the block
// Verify the engine specific seal securing the block
if
seal
{
if
seal
{
...
@@ -449,7 +444,7 @@ func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Head
...
@@ -449,7 +444,7 @@ func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Head
func
(
ethash
*
Ethash
)
Prepare
(
chain
consensus
.
ChainReader
,
header
*
types
.
Header
)
error
{
func
(
ethash
*
Ethash
)
Prepare
(
chain
consensus
.
ChainReader
,
header
*
types
.
Header
)
error
{
parent
:=
chain
.
GetHeader
(
header
.
ParentHash
,
header
.
Number
.
Uint64
()
-
1
)
parent
:=
chain
.
GetHeader
(
header
.
ParentHash
,
header
.
Number
.
Uint64
()
-
1
)
if
parent
==
nil
{
if
parent
==
nil
{
return
ErrParentUnknown
return
consensus
.
ErrUnknownAncestor
}
}
header
.
Difficulty
=
CalcDifficulty
(
chain
.
Config
(),
header
.
Time
.
Uint64
(),
header
.
Difficulty
=
CalcDifficulty
(
chain
.
Config
(),
header
.
Time
.
Uint64
(),
parent
.
Time
.
Uint64
(),
parent
.
Number
,
parent
.
Difficulty
)
parent
.
Time
.
Uint64
(),
parent
.
Number
,
parent
.
Difficulty
)
...
...
core/block_validator.go
View file @
158d6035
...
@@ -54,15 +54,15 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
...
@@ -54,15 +54,15 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
// Check whether the block's known, and if not, that it's linkable
// Check whether the block's known, and if not, that it's linkable
if
v
.
bc
.
HasBlock
(
block
.
Hash
())
{
if
v
.
bc
.
HasBlock
(
block
.
Hash
())
{
if
_
,
err
:=
state
.
New
(
block
.
Root
(),
v
.
bc
.
chainDb
);
err
==
nil
{
if
_
,
err
:=
state
.
New
(
block
.
Root
(),
v
.
bc
.
chainDb
);
err
==
nil
{
return
&
KnownBlockError
{
block
.
Number
(),
block
.
Hash
()}
return
ErrKnownBlock
}
}
}
}
parent
:=
v
.
bc
.
GetBlock
(
block
.
ParentHash
(),
block
.
NumberU64
()
-
1
)
parent
:=
v
.
bc
.
GetBlock
(
block
.
ParentHash
(),
block
.
NumberU64
()
-
1
)
if
parent
==
nil
{
if
parent
==
nil
{
return
ParentError
(
block
.
ParentHash
())
return
consensus
.
ErrUnknownAncestor
}
}
if
_
,
err
:=
state
.
New
(
parent
.
Root
(),
v
.
bc
.
chainDb
);
err
!=
nil
{
if
_
,
err
:=
state
.
New
(
parent
.
Root
(),
v
.
bc
.
chainDb
);
err
!=
nil
{
return
ParentError
(
block
.
ParentHash
())
return
consensus
.
ErrUnknownAncestor
}
}
// Header validity is known at this point, check the uncles and transactions
// Header validity is known at this point, check the uncles and transactions
header
:=
block
.
Header
()
header
:=
block
.
Header
()
...
@@ -82,10 +82,10 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
...
@@ -82,10 +82,10 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
// transition, such as amount of used gas, the receipt roots and the state root
// transition, such as amount of used gas, the receipt roots and the state root
// itself. ValidateState returns a database batch if the validation was a success
// itself. ValidateState returns a database batch if the validation was a success
// otherwise nil and an error is returned.
// otherwise nil and an error is returned.
func
(
v
*
BlockValidator
)
ValidateState
(
block
,
parent
*
types
.
Block
,
statedb
*
state
.
StateDB
,
receipts
types
.
Receipts
,
usedGas
*
big
.
Int
)
(
err
error
)
{
func
(
v
*
BlockValidator
)
ValidateState
(
block
,
parent
*
types
.
Block
,
statedb
*
state
.
StateDB
,
receipts
types
.
Receipts
,
usedGas
*
big
.
Int
)
error
{
header
:=
block
.
Header
()
header
:=
block
.
Header
()
if
block
.
GasUsed
()
.
Cmp
(
usedGas
)
!=
0
{
if
block
.
GasUsed
()
.
Cmp
(
usedGas
)
!=
0
{
return
ValidationError
(
fmt
.
Sprintf
(
"invalid gas used (remote: %v local: %v)"
,
block
.
GasUsed
(),
usedGas
)
)
return
fmt
.
Errorf
(
"invalid gas used (remote: %v local: %v)"
,
block
.
GasUsed
(),
usedGas
)
}
}
// Validate the received block's bloom with the one derived from the generated receipts.
// Validate the received block's bloom with the one derived from the generated receipts.
// For valid blocks this should always validate to true.
// For valid blocks this should always validate to true.
...
...
core/blockchain.go
View file @
158d6035
...
@@ -831,7 +831,7 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status WriteStatus, err
...
@@ -831,7 +831,7 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status WriteStatus, err
// Calculate the total difficulty of the block
// Calculate the total difficulty of the block
ptd
:=
self
.
GetTd
(
block
.
ParentHash
(),
block
.
NumberU64
()
-
1
)
ptd
:=
self
.
GetTd
(
block
.
ParentHash
(),
block
.
NumberU64
()
-
1
)
if
ptd
==
nil
{
if
ptd
==
nil
{
return
NonStatTy
,
ParentError
(
block
.
ParentHash
())
return
NonStatTy
,
consensus
.
ErrUnknownAncestor
}
}
// Make sure no inconsistent state is leaked during insertion
// Make sure no inconsistent state is leaked during insertion
self
.
mu
.
Lock
()
self
.
mu
.
Lock
()
...
@@ -918,9 +918,8 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
...
@@ -918,9 +918,8 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
}
}
// If the header is a banned one, straight out abort
// If the header is a banned one, straight out abort
if
BadHashes
[
block
.
Hash
()]
{
if
BadHashes
[
block
.
Hash
()]
{
err
:=
BadHashError
(
block
.
Hash
())
self
.
reportBlock
(
block
,
nil
,
ErrBlacklistedHash
)
self
.
reportBlock
(
block
,
nil
,
err
)
return
i
,
ErrBlacklistedHash
return
i
,
err
}
}
// Wait for the block's verification to complete
// Wait for the block's verification to complete
bstart
:=
time
.
Now
()
bstart
:=
time
.
Now
()
...
@@ -930,25 +929,25 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
...
@@ -930,25 +929,25 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
err
=
self
.
Validator
()
.
ValidateBody
(
block
)
err
=
self
.
Validator
()
.
ValidateBody
(
block
)
}
}
if
err
!=
nil
{
if
err
!=
nil
{
if
IsKnownBlockErr
(
err
)
{
if
err
==
ErrKnownBlock
{
stats
.
ignored
++
stats
.
ignored
++
continue
continue
}
}
if
err
==
BlockFutureErr
{
if
err
==
consensus
.
ErrFutureBlock
{
// Allow up to MaxFuture second in the future blocks. If this limit
// Allow up to MaxFuture second in the future blocks. If this limit
// is exceeded the chain is discarded and processed at a later time
// is exceeded the chain is discarded and processed at a later time
// if given.
// if given.
max
:=
big
.
NewInt
(
time
.
Now
()
.
Unix
()
+
maxTimeFutureBlocks
)
max
:=
big
.
NewInt
(
time
.
Now
()
.
Unix
()
+
maxTimeFutureBlocks
)
if
block
.
Time
()
.
Cmp
(
max
)
==
1
{
if
block
.
Time
()
.
Cmp
(
max
)
>
0
{
return
i
,
fmt
.
Errorf
(
"
%v: BlockFutureErr, %v > %v"
,
BlockFutureErr
,
block
.
Time
(),
max
)
return
i
,
fmt
.
Errorf
(
"
future block: %v > %v"
,
block
.
Time
(),
max
)
}
}
self
.
futureBlocks
.
Add
(
block
.
Hash
(),
block
)
self
.
futureBlocks
.
Add
(
block
.
Hash
(),
block
)
stats
.
queued
++
stats
.
queued
++
continue
continue
}
}
if
IsParentErr
(
err
)
&&
self
.
futureBlocks
.
Contains
(
block
.
ParentHash
())
{
if
err
==
consensus
.
ErrUnknownAncestor
&&
self
.
futureBlocks
.
Contains
(
block
.
ParentHash
())
{
self
.
futureBlocks
.
Add
(
block
.
Hash
(),
block
)
self
.
futureBlocks
.
Add
(
block
.
Hash
(),
block
)
stats
.
queued
++
stats
.
queued
++
continue
continue
...
...
core/blockchain_test.go
View file @
158d6035
...
@@ -126,7 +126,7 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
...
@@ -126,7 +126,7 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
err
=
blockchain
.
validator
.
ValidateBody
(
block
)
err
=
blockchain
.
validator
.
ValidateBody
(
block
)
}
}
if
err
!=
nil
{
if
err
!=
nil
{
if
IsKnownBlockErr
(
err
)
{
if
err
==
ErrKnownBlock
{
continue
continue
}
}
return
err
return
err
...
@@ -441,8 +441,8 @@ func testBadHashes(t *testing.T, full bool) {
...
@@ -441,8 +441,8 @@ func testBadHashes(t *testing.T, full bool) {
BadHashes
[
headers
[
2
]
.
Hash
()]
=
true
BadHashes
[
headers
[
2
]
.
Hash
()]
=
true
_
,
err
=
bc
.
InsertHeaderChain
(
headers
,
1
)
_
,
err
=
bc
.
InsertHeaderChain
(
headers
,
1
)
}
}
if
!
IsBadHashError
(
err
)
{
if
err
!=
ErrBlacklistedHash
{
t
.
Errorf
(
"error mismatch:
want: BadHashError, have: %v"
,
err
)
t
.
Errorf
(
"error mismatch:
have: %v, want: %v"
,
err
,
ErrBlacklistedHash
)
}
}
}
}
...
...
core/dao.go
View file @
158d6035
...
@@ -18,6 +18,7 @@ package core
...
@@ -18,6 +18,7 @@ package core
import
(
import
(
"bytes"
"bytes"
"fmt"
"math/big"
"math/big"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/state"
...
@@ -46,11 +47,11 @@ func ValidateDAOHeaderExtraData(config *params.ChainConfig, header *types.Header
...
@@ -46,11 +47,11 @@ func ValidateDAOHeaderExtraData(config *params.ChainConfig, header *types.Header
// Depending whether we support or oppose the fork, validate the extra-data contents
// Depending whether we support or oppose the fork, validate the extra-data contents
if
config
.
DAOForkSupport
{
if
config
.
DAOForkSupport
{
if
!
bytes
.
Equal
(
header
.
Extra
,
params
.
DAOForkBlockExtra
)
{
if
!
bytes
.
Equal
(
header
.
Extra
,
params
.
DAOForkBlockExtra
)
{
return
ValidationError
(
"DAO pro-fork bad block extra-data: 0x%x"
,
header
.
Extra
)
return
fmt
.
Errorf
(
"DAO pro-fork bad block extra-data: 0x%x"
,
header
.
Extra
)
}
}
}
else
{
}
else
{
if
bytes
.
Equal
(
header
.
Extra
,
params
.
DAOForkBlockExtra
)
{
if
bytes
.
Equal
(
header
.
Extra
,
params
.
DAOForkBlockExtra
)
{
return
ValidationError
(
"DAO no-fork bad block extra-data: 0x%x"
,
header
.
Extra
)
return
fmt
.
Errorf
(
"DAO no-fork bad block extra-data: 0x%x"
,
header
.
Extra
)
}
}
}
}
// All ok, header has the same extra-data we expect
// All ok, header has the same extra-data we expect
...
...
core/error.go
View file @
158d6035
...
@@ -16,188 +16,16 @@
...
@@ -16,188 +16,16 @@
package
core
package
core
import
(
import
"errors"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
)
var
(
var
(
BlockNumberErr
=
errors
.
New
(
"block number invalid"
)
// ErrKnownBlock is returned when a block to import is already known locally.
BlockFutureErr
=
errors
.
New
(
"block time is in the future"
)
ErrKnownBlock
=
errors
.
New
(
"block already known"
)
BlockTSTooBigErr
=
errors
.
New
(
"block time too big"
)
BlockEqualTSErr
=
errors
.
New
(
"block time stamp equal to previous"
)
)
// Parent error. In case a parent is unknown this error will be thrown
// by the block manager
type
ParentErr
struct
{
Message
string
}
func
(
err
*
ParentErr
)
Error
()
string
{
return
err
.
Message
}
func
ParentError
(
hash
common
.
Hash
)
error
{
return
&
ParentErr
{
Message
:
fmt
.
Sprintf
(
"Block's parent unknown %x"
,
hash
)}
}
func
IsParentErr
(
err
error
)
bool
{
_
,
ok
:=
err
.
(
*
ParentErr
)
return
ok
}
type
UncleErr
struct
{
Message
string
}
func
(
err
*
UncleErr
)
Error
()
string
{
return
err
.
Message
}
func
UncleError
(
format
string
,
v
...
interface
{})
error
{
return
&
UncleErr
{
Message
:
fmt
.
Sprintf
(
format
,
v
...
)}
}
func
IsUncleErr
(
err
error
)
bool
{
_
,
ok
:=
err
.
(
*
UncleErr
)
return
ok
}
// Block validation error. If any validation fails, this error will be thrown
type
ValidationErr
struct
{
Message
string
}
func
(
err
*
ValidationErr
)
Error
()
string
{
return
err
.
Message
}
func
ValidationError
(
format
string
,
v
...
interface
{})
*
ValidationErr
{
return
&
ValidationErr
{
Message
:
fmt
.
Sprintf
(
format
,
v
...
)}
}
func
IsValidationErr
(
err
error
)
bool
{
_
,
ok
:=
err
.
(
*
ValidationErr
)
return
ok
}
type
NonceErr
struct
{
Message
string
Is
,
Exp
uint64
}
func
(
err
*
NonceErr
)
Error
()
string
{
return
err
.
Message
}
func
NonceError
(
is
,
exp
uint64
)
*
NonceErr
{
return
&
NonceErr
{
Message
:
fmt
.
Sprintf
(
"Transaction w/ invalid nonce. tx=%d state=%d)"
,
is
,
exp
),
Is
:
is
,
Exp
:
exp
}
}
func
IsNonceErr
(
err
error
)
bool
{
_
,
ok
:=
err
.
(
*
NonceErr
)
return
ok
}
// BlockNonceErr indicates that a block's nonce is invalid.
// ErrGasLimitReached is returned by the gas pool if the amount of gas required
type
BlockNonceErr
struct
{
// by a transaction is higher than what's left in the block.
Number
*
big
.
Int
ErrGasLimitReached
=
errors
.
New
(
"gas limit reached"
)
Hash
common
.
Hash
Nonce
uint64
}
func
(
err
*
BlockNonceErr
)
Error
()
string
{
// ErrBlacklistedHash is returned if a block to import is on the blacklist.
return
fmt
.
Sprintf
(
"nonce for #%d [%x…] is invalid (got %d)"
,
err
.
Number
,
err
.
Hash
,
err
.
Nonce
)
ErrBlacklistedHash
=
errors
.
New
(
"blacklisted hash"
)
}
)
// IsBlockNonceErr returns true for invalid block nonce errors.
func
IsBlockNonceErr
(
err
error
)
bool
{
_
,
ok
:=
err
.
(
*
BlockNonceErr
)
return
ok
}
type
InvalidTxErr
struct
{
Message
string
}
func
(
err
*
InvalidTxErr
)
Error
()
string
{
return
err
.
Message
}
func
InvalidTxError
(
err
error
)
*
InvalidTxErr
{
return
&
InvalidTxErr
{
fmt
.
Sprintf
(
"%v"
,
err
)}
}
func
IsInvalidTxErr
(
err
error
)
bool
{
_
,
ok
:=
err
.
(
*
InvalidTxErr
)
return
ok
}
type
TDError
struct
{
a
,
b
*
big
.
Int
}
func
(
self
*
TDError
)
Error
()
string
{
return
fmt
.
Sprintf
(
"incoming chain has a lower or equal TD (%v <= %v)"
,
self
.
a
,
self
.
b
)
}
func
IsTDError
(
e
error
)
bool
{
_
,
ok
:=
e
.
(
*
TDError
)
return
ok
}
type
KnownBlockError
struct
{
number
*
big
.
Int
hash
common
.
Hash
}
func
(
self
*
KnownBlockError
)
Error
()
string
{
return
fmt
.
Sprintf
(
"block %v already known (%x)"
,
self
.
number
,
self
.
hash
[
0
:
4
])
}
func
IsKnownBlockErr
(
e
error
)
bool
{
_
,
ok
:=
e
.
(
*
KnownBlockError
)
return
ok
}
type
ValueTransferError
struct
{
message
string
}
func
ValueTransferErr
(
str
string
,
v
...
interface
{})
*
ValueTransferError
{
return
&
ValueTransferError
{
fmt
.
Sprintf
(
str
,
v
...
)}
}
func
(
self
*
ValueTransferError
)
Error
()
string
{
return
self
.
message
}
func
IsValueTransferErr
(
e
error
)
bool
{
_
,
ok
:=
e
.
(
*
ValueTransferError
)
return
ok
}
type
BadHashError
common
.
Hash
func
(
h
BadHashError
)
Error
()
string
{
return
fmt
.
Sprintf
(
"Found known bad hash in chain %x"
,
h
[
:
])
}
func
IsBadHashError
(
err
error
)
bool
{
_
,
ok
:=
err
.
(
BadHashError
)
return
ok
}
type
GasLimitErr
struct
{
Have
,
Want
*
big
.
Int
}
func
IsGasLimitErr
(
err
error
)
bool
{
_
,
ok
:=
err
.
(
*
GasLimitErr
)
return
ok
}
func
(
err
*
GasLimitErr
)
Error
()
string
{
return
fmt
.
Sprintf
(
"GasLimit reached. Have %d gas, transaction requires %d"
,
err
.
Have
,
err
.
Want
)
}
core/gaspool.go
View file @
158d6035
...
@@ -35,7 +35,7 @@ func (gp *GasPool) AddGas(amount *big.Int) *GasPool {
...
@@ -35,7 +35,7 @@ func (gp *GasPool) AddGas(amount *big.Int) *GasPool {
func
(
gp
*
GasPool
)
SubGas
(
amount
*
big
.
Int
)
error
{
func
(
gp
*
GasPool
)
SubGas
(
amount
*
big
.
Int
)
error
{
i
:=
(
*
big
.
Int
)(
gp
)
i
:=
(
*
big
.
Int
)(
gp
)
if
i
.
Cmp
(
amount
)
<
0
{
if
i
.
Cmp
(
amount
)
<
0
{
return
&
GasLimitErr
{
Have
:
new
(
big
.
Int
)
.
Set
(
i
),
Want
:
amount
}
return
ErrGasLimitReached
}
}
i
.
Sub
(
i
,
amount
)
i
.
Sub
(
i
,
amount
)
return
nil
return
nil
...
...
core/headerchain.go
View file @
158d6035
...
@@ -137,7 +137,7 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
...
@@ -137,7 +137,7 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
// Calculate the total difficulty of the header
// Calculate the total difficulty of the header
ptd
:=
hc
.
GetTd
(
header
.
ParentHash
,
number
-
1
)
ptd
:=
hc
.
GetTd
(
header
.
ParentHash
,
number
-
1
)
if
ptd
==
nil
{
if
ptd
==
nil
{
return
NonStatTy
,
ParentError
(
header
.
ParentHash
)
return
NonStatTy
,
consensus
.
ErrUnknownAncestor
}
}
localTd
:=
hc
.
GetTd
(
hc
.
currentHeaderHash
,
hc
.
currentHeader
.
Number
.
Uint64
())
localTd
:=
hc
.
GetTd
(
hc
.
currentHeaderHash
,
hc
.
currentHeader
.
Number
.
Uint64
())
externTd
:=
new
(
big
.
Int
)
.
Add
(
header
.
Difficulty
,
ptd
)
externTd
:=
new
(
big
.
Int
)
.
Add
(
header
.
Difficulty
,
ptd
)
...
@@ -246,7 +246,7 @@ func (hc *HeaderChain) ValidateHeaderChain(chain []*types.Header, checkFreq int)
...
@@ -246,7 +246,7 @@ func (hc *HeaderChain) ValidateHeaderChain(chain []*types.Header, checkFreq int)
}
}
// If the header is a banned one, straight out abort
// If the header is a banned one, straight out abort
if
BadHashes
[
header
.
Hash
()]
{
if
BadHashes
[
header
.
Hash
()]
{
return
i
,
BadHashError
(
header
.
Hash
())
return
i
,
ErrBlacklistedHash
}
}
// Otherwise wait for headers checks and ensure they pass
// Otherwise wait for headers checks and ensure they pass
if
err
:=
<-
results
;
err
!=
nil
{
if
err
:=
<-
results
;
err
!=
nil
{
...
...
core/state_transition.go
View file @
158d6035
...
@@ -18,6 +18,7 @@ package core
...
@@ -18,6 +18,7 @@ package core
import
(
import
(
"errors"
"errors"
"fmt"
"math/big"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
...
@@ -195,26 +196,17 @@ func (self *StateTransition) buyGas() error {
...
@@ -195,26 +196,17 @@ func (self *StateTransition) buyGas() error {
return
nil
return
nil
}
}
func
(
self
*
StateTransition
)
preCheck
()
(
err
error
)
{
func
(
self
*
StateTransition
)
preCheck
()
error
{
msg
:=
self
.
msg
msg
:=
self
.
msg
sender
:=
self
.
from
()
sender
:=
self
.
from
()
// Make sure this transaction's nonce is correct
// Make sure this transaction's nonce is correct
if
msg
.
CheckNonce
()
{
if
msg
.
CheckNonce
()
{
if
n
:=
self
.
state
.
GetNonce
(
sender
.
Address
());
n
!=
msg
.
Nonce
()
{
if
n
:=
self
.
state
.
GetNonce
(
sender
.
Address
());
n
!=
msg
.
Nonce
()
{
return
NonceError
(
msg
.
Nonce
(),
n
)
return
fmt
.
Errorf
(
"invalid nonce: have %d, expected %d"
,
msg
.
Nonce
(),
n
)
}
}
}
}
return
self
.
buyGas
()
// Pre-pay gas
if
err
=
self
.
buyGas
();
err
!=
nil
{
if
IsGasLimitErr
(
err
)
{
return
err
}
return
InvalidTxError
(
err
)
}
return
nil
}
}
// TransitionDb will transition the state by applying the current message and returning the result
// TransitionDb will transition the state by applying the current message and returning the result
...
@@ -233,11 +225,10 @@ func (self *StateTransition) TransitionDb() (ret []byte, requiredGas, usedGas *b
...
@@ -233,11 +225,10 @@ func (self *StateTransition) TransitionDb() (ret []byte, requiredGas, usedGas *b
// TODO convert to uint64
// TODO convert to uint64
intrinsicGas
:=
IntrinsicGas
(
self
.
data
,
contractCreation
,
homestead
)
intrinsicGas
:=
IntrinsicGas
(
self
.
data
,
contractCreation
,
homestead
)
if
intrinsicGas
.
BitLen
()
>
64
{
if
intrinsicGas
.
BitLen
()
>
64
{
return
nil
,
nil
,
nil
,
InvalidTxError
(
vm
.
ErrOutOfGas
)
return
nil
,
nil
,
nil
,
vm
.
ErrOutOfGas
}
}
if
err
=
self
.
useGas
(
intrinsicGas
.
Uint64
());
err
!=
nil
{
if
err
=
self
.
useGas
(
intrinsicGas
.
Uint64
());
err
!=
nil
{
return
nil
,
nil
,
nil
,
InvalidTxError
(
err
)
return
nil
,
nil
,
nil
,
err
}
}
var
(
var
(
...
@@ -260,10 +251,9 @@ func (self *StateTransition) TransitionDb() (ret []byte, requiredGas, usedGas *b
...
@@ -260,10 +251,9 @@ func (self *StateTransition) TransitionDb() (ret []byte, requiredGas, usedGas *b
// sufficient balance to make the transfer happen. The first
// sufficient balance to make the transfer happen. The first
// balance transfer may never fail.
// balance transfer may never fail.
if
vmerr
==
vm
.
ErrInsufficientBalance
{
if
vmerr
==
vm
.
ErrInsufficientBalance
{
return
nil
,
nil
,
nil
,
InvalidTxError
(
vmerr
)
return
nil
,
nil
,
nil
,
vmerr
}
}
}
}
requiredGas
=
new
(
big
.
Int
)
.
Set
(
self
.
gasUsed
())
requiredGas
=
new
(
big
.
Int
)
.
Set
(
self
.
gasUsed
())
self
.
refundGas
()
self
.
refundGas
()
...
...
eth/fetcher/fetcher.go
View file @
158d6035
...
@@ -23,7 +23,7 @@ import (
...
@@ -23,7 +23,7 @@ import (
"time"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/co
re
"
"github.com/ethereum/go-ethereum/co
nsensus
"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/log"
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
...
@@ -654,7 +654,7 @@ func (f *Fetcher) insert(peer string, block *types.Block) {
...
@@ -654,7 +654,7 @@ func (f *Fetcher) insert(peer string, block *types.Block) {
propBroadcastOutTimer
.
UpdateSince
(
block
.
ReceivedAt
)
propBroadcastOutTimer
.
UpdateSince
(
block
.
ReceivedAt
)
go
f
.
broadcastBlock
(
block
,
true
)
go
f
.
broadcastBlock
(
block
,
true
)
case
co
re
.
BlockFutureErr
:
case
co
nsensus
.
ErrFutureBlock
:
// Weird future block, don't fail, but neither propagate
// Weird future block, don't fail, but neither propagate
default
:
default
:
...
...
les/fetcher.go
View file @
158d6035
...
@@ -24,6 +24,7 @@ import (
...
@@ -24,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/light"
"github.com/ethereum/go-ethereum/light"
...
@@ -498,7 +499,7 @@ func (f *lightFetcher) processResponse(req fetchRequest, resp fetchResponse) boo
...
@@ -498,7 +499,7 @@ func (f *lightFetcher) processResponse(req fetchRequest, resp fetchResponse) boo
headers
[
int
(
req
.
amount
)
-
1
-
i
]
=
header
headers
[
int
(
req
.
amount
)
-
1
-
i
]
=
header
}
}
if
_
,
err
:=
f
.
chain
.
InsertHeaderChain
(
headers
,
1
);
err
!=
nil
{
if
_
,
err
:=
f
.
chain
.
InsertHeaderChain
(
headers
,
1
);
err
!=
nil
{
if
err
==
co
re
.
BlockFutureErr
{
if
err
==
co
nsensus
.
ErrFutureBlock
{
return
true
return
true
}
}
log
.
Debug
(
"Failed to insert header chain"
,
"err"
,
err
)
log
.
Debug
(
"Failed to insert header chain"
,
"err"
,
err
)
...
...
light/lightchain_test.go
View file @
158d6035
...
@@ -327,9 +327,8 @@ func TestBadHeaderHashes(t *testing.T) {
...
@@ -327,9 +327,8 @@ func TestBadHeaderHashes(t *testing.T) {
var
err
error
var
err
error
headers
:=
makeHeaderChainWithDiff
(
bc
.
genesisBlock
,
[]
int
{
1
,
2
,
4
},
10
)
headers
:=
makeHeaderChainWithDiff
(
bc
.
genesisBlock
,
[]
int
{
1
,
2
,
4
},
10
)
core
.
BadHashes
[
headers
[
2
]
.
Hash
()]
=
true
core
.
BadHashes
[
headers
[
2
]
.
Hash
()]
=
true
_
,
err
=
bc
.
InsertHeaderChain
(
headers
,
1
)
if
_
,
err
=
bc
.
InsertHeaderChain
(
headers
,
1
);
err
!=
core
.
ErrBlacklistedHash
{
if
!
core
.
IsBadHashError
(
err
)
{
t
.
Errorf
(
"error mismatch: have: %v, want %v"
,
err
,
core
.
ErrBlacklistedHash
)
t
.
Errorf
(
"error mismatch: want: BadHashError, have: %v"
,
err
)
}
}
}
}
...
...
miner/worker.go
View file @
158d6035
...
@@ -503,13 +503,13 @@ func (self *worker) commitNewWork() {
...
@@ -503,13 +503,13 @@ func (self *worker) commitNewWork() {
func
(
self
*
worker
)
commitUncle
(
work
*
Work
,
uncle
*
types
.
Header
)
error
{
func
(
self
*
worker
)
commitUncle
(
work
*
Work
,
uncle
*
types
.
Header
)
error
{
hash
:=
uncle
.
Hash
()
hash
:=
uncle
.
Hash
()
if
work
.
uncles
.
Has
(
hash
)
{
if
work
.
uncles
.
Has
(
hash
)
{
return
core
.
UncleError
(
"uncle not unique"
)
return
fmt
.
Errorf
(
"uncle not unique"
)
}
}
if
!
work
.
ancestors
.
Has
(
uncle
.
ParentHash
)
{
if
!
work
.
ancestors
.
Has
(
uncle
.
ParentHash
)
{
return
core
.
UncleError
(
fmt
.
Sprintf
(
"uncle's parent unknown (%x)"
,
uncle
.
ParentHash
[
0
:
4
])
)
return
fmt
.
Errorf
(
"uncle's parent unknown (%x)"
,
uncle
.
ParentHash
[
0
:
4
]
)
}
}
if
work
.
family
.
Has
(
hash
)
{
if
work
.
family
.
Has
(
hash
)
{
return
core
.
UncleError
(
fmt
.
Sprintf
(
"uncle already in family (%x)"
,
hash
)
)
return
fmt
.
Errorf
(
"uncle already in family (%x)"
,
hash
)
}
}
work
.
uncles
.
Add
(
uncle
.
Hash
())
work
.
uncles
.
Add
(
uncle
.
Hash
())
return
nil
return
nil
...
@@ -554,23 +554,23 @@ func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsB
...
@@ -554,23 +554,23 @@ func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsB
env
.
state
.
StartRecord
(
tx
.
Hash
(),
common
.
Hash
{},
env
.
tcount
)
env
.
state
.
StartRecord
(
tx
.
Hash
(),
common
.
Hash
{},
env
.
tcount
)
err
,
logs
:=
env
.
commitTransaction
(
tx
,
bc
,
gp
)
err
,
logs
:=
env
.
commitTransaction
(
tx
,
bc
,
gp
)
switch
{
switch
err
{
case
core
.
IsGasLimitErr
(
err
)
:
case
core
.
ErrGasLimitReached
:
// Pop the current out-of-gas transaction without shifting in the next from the account
// Pop the current out-of-gas transaction without shifting in the next from the account
log
.
Trace
(
"Gas limit exceeded for current block"
,
"sender"
,
from
)
log
.
Trace
(
"Gas limit exceeded for current block"
,
"sender"
,
from
)
txs
.
Pop
()
txs
.
Pop
()
case
err
!=
nil
:
case
nil
:
// Pop the current failed transaction without shifting in the next from the account
log
.
Trace
(
"Transaction failed, will be removed"
,
"hash"
,
tx
.
Hash
(),
"err"
,
err
)
env
.
failedTxs
=
append
(
env
.
failedTxs
,
tx
)
txs
.
Pop
()
default
:
// Everything ok, collect the logs and shift in the next transaction from the same account
// Everything ok, collect the logs and shift in the next transaction from the same account
coalescedLogs
=
append
(
coalescedLogs
,
logs
...
)
coalescedLogs
=
append
(
coalescedLogs
,
logs
...
)
env
.
tcount
++
env
.
tcount
++
txs
.
Shift
()
txs
.
Shift
()
default
:
// Pop the current failed transaction without shifting in the next from the account
log
.
Trace
(
"Transaction failed, will be removed"
,
"hash"
,
tx
.
Hash
(),
"err"
,
err
)
env
.
failedTxs
=
append
(
env
.
failedTxs
,
tx
)
txs
.
Pop
()
}
}
}
}
...
...
tests/state_test_util.go
View file @
158d6035
...
@@ -214,7 +214,7 @@ func RunState(chainConfig *params.ChainConfig, statedb *state.StateDB, env, tx m
...
@@ -214,7 +214,7 @@ func RunState(chainConfig *params.ChainConfig, statedb *state.StateDB, env, tx m
snapshot
:=
statedb
.
Snapshot
()
snapshot
:=
statedb
.
Snapshot
()
ret
,
gasUsed
,
err
:=
core
.
ApplyMessage
(
environment
,
msg
,
gaspool
)
ret
,
gasUsed
,
err
:=
core
.
ApplyMessage
(
environment
,
msg
,
gaspool
)
if
core
.
IsNonceErr
(
err
)
||
core
.
IsInvalidTxErr
(
err
)
||
core
.
IsGasLimitErr
(
err
)
{
if
err
!=
nil
{
statedb
.
RevertToSnapshot
(
snapshot
)
statedb
.
RevertToSnapshot
(
snapshot
)
}
}
statedb
.
Commit
(
chainConfig
.
IsEIP158
(
environment
.
Context
.
BlockNumber
))
statedb
.
Commit
(
chainConfig
.
IsEIP158
(
environment
.
Context
.
BlockNumber
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment