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
821619e1
Commit
821619e1
authored
9 years ago
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core, eth, miner: use pure header validation
parent
e9a80518
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
26 additions
and
28 deletions
+26
-28
block_processor.go
core/block_processor.go
+22
-24
block_processor_test.go
core/block_processor_test.go
+2
-2
handler.go
eth/handler.go
+1
-1
worker.go
miner/worker.go
+1
-1
No files found.
core/block_processor.go
View file @
821619e1
...
...
@@ -213,7 +213,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
txs
:=
block
.
Transactions
()
// Block validation
if
err
=
ValidateHeader
(
sm
.
Pow
,
header
,
parent
,
false
,
false
);
err
!=
nil
{
if
err
=
ValidateHeader
(
sm
.
Pow
,
header
,
parent
.
Header
()
,
false
,
false
);
err
!=
nil
{
return
}
...
...
@@ -337,7 +337,7 @@ func (sm *BlockProcessor) VerifyUncles(statedb *state.StateDB, block, parent *ty
return
UncleError
(
"uncle[%d](%x)'s parent is not ancestor (%x)"
,
i
,
hash
[
:
4
],
uncle
.
ParentHash
[
0
:
4
])
}
if
err
:=
ValidateHeader
(
sm
.
Pow
,
uncle
,
ancestors
[
uncle
.
ParentHash
],
true
,
true
);
err
!=
nil
{
if
err
:=
ValidateHeader
(
sm
.
Pow
,
uncle
,
ancestors
[
uncle
.
ParentHash
]
.
Header
()
,
true
,
true
);
err
!=
nil
{
return
ValidationError
(
fmt
.
Sprintf
(
"uncle[%d](%x) header invalid: %v"
,
i
,
hash
[
:
4
],
err
))
}
}
...
...
@@ -367,52 +367,50 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err erro
}
// See YP section 4.3.4. "Block Header Validity"
// Validates a
block. Returns an error if the block
is invalid.
func
ValidateHeader
(
pow
pow
.
PoW
,
block
*
types
.
Header
,
parent
*
types
.
Block
,
checkPow
,
uncle
bool
)
error
{
if
big
.
NewInt
(
int64
(
len
(
block
.
Extra
)))
.
Cmp
(
params
.
MaximumExtraDataSize
)
==
1
{
return
fmt
.
Errorf
(
"
Block extra data too long (%d)"
,
len
(
block
.
Extra
))
// Validates a
header. Returns an error if the header
is invalid.
func
ValidateHeader
(
pow
pow
.
PoW
,
header
*
types
.
Header
,
parent
*
types
.
Header
,
checkPow
,
uncle
bool
)
error
{
if
big
.
NewInt
(
int64
(
len
(
header
.
Extra
)))
.
Cmp
(
params
.
MaximumExtraDataSize
)
==
1
{
return
fmt
.
Errorf
(
"
Header extra data too long (%d)"
,
len
(
header
.
Extra
))
}
if
uncle
{
if
block
.
Time
.
Cmp
(
common
.
MaxBig
)
==
1
{
if
header
.
Time
.
Cmp
(
common
.
MaxBig
)
==
1
{
return
BlockTSTooBigErr
}
}
else
{
if
block
.
Time
.
Cmp
(
big
.
NewInt
(
time
.
Now
()
.
Unix
()))
==
1
{
if
header
.
Time
.
Cmp
(
big
.
NewInt
(
time
.
Now
()
.
Unix
()))
==
1
{
return
BlockFutureErr
}
}
if
block
.
Time
.
Cmp
(
parent
.
Time
()
)
!=
1
{
if
header
.
Time
.
Cmp
(
parent
.
Time
)
!=
1
{
return
BlockEqualTSErr
}
expd
:=
CalcDifficulty
(
block
.
Time
.
Uint64
(),
parent
.
Time
()
.
Uint64
(),
parent
.
Number
(),
parent
.
Difficulty
()
)
if
expd
.
Cmp
(
block
.
Difficulty
)
!=
0
{
return
fmt
.
Errorf
(
"Difficulty check failed for
block %v, %v"
,
block
.
Difficulty
,
expd
)
expd
:=
CalcDifficulty
(
header
.
Time
.
Uint64
(),
parent
.
Time
.
Uint64
(),
parent
.
Number
,
parent
.
Difficulty
)
if
expd
.
Cmp
(
header
.
Difficulty
)
!=
0
{
return
fmt
.
Errorf
(
"Difficulty check failed for
header %v, %v"
,
header
.
Difficulty
,
expd
)
}
var
a
,
b
*
big
.
Int
a
=
parent
.
GasLimit
()
a
=
a
.
Sub
(
a
,
block
.
GasLimit
)
a
:=
new
(
big
.
Int
)
.
Set
(
parent
.
GasLimit
)
a
=
a
.
Sub
(
a
,
header
.
GasLimit
)
a
.
Abs
(
a
)
b
=
parent
.
GasLimit
(
)
b
:=
new
(
big
.
Int
)
.
Set
(
parent
.
GasLimit
)
b
=
b
.
Div
(
b
,
params
.
GasLimitBoundDivisor
)
if
!
(
a
.
Cmp
(
b
)
<
0
)
||
(
block
.
GasLimit
.
Cmp
(
params
.
MinGasLimit
)
==
-
1
)
{
return
fmt
.
Errorf
(
"GasLimit check failed for
block %v (%v > %v)"
,
block
.
GasLimit
,
a
,
b
)
if
!
(
a
.
Cmp
(
b
)
<
0
)
||
(
header
.
GasLimit
.
Cmp
(
params
.
MinGasLimit
)
==
-
1
)
{
return
fmt
.
Errorf
(
"GasLimit check failed for
header %v (%v > %v)"
,
header
.
GasLimit
,
a
,
b
)
}
num
:=
parent
.
Number
(
)
num
.
Sub
(
block
.
Number
,
num
)
num
:=
new
(
big
.
Int
)
.
Set
(
parent
.
Number
)
num
.
Sub
(
header
.
Number
,
num
)
if
num
.
Cmp
(
big
.
NewInt
(
1
))
!=
0
{
return
BlockNumberErr
}
if
checkPow
{
// Verify the nonce of the
block
. Return an error if it's not valid
if
!
pow
.
Verify
(
types
.
NewBlockWithHeader
(
block
))
{
return
ValidationError
(
"
Block's nonce is invalid (= %x)"
,
block
.
Nonce
)
// Verify the nonce of the
header
. Return an error if it's not valid
if
!
pow
.
Verify
(
types
.
NewBlockWithHeader
(
header
))
{
return
ValidationError
(
"
Header's nonce is invalid (= %x)"
,
header
.
Nonce
)
}
}
return
nil
}
This diff is collapsed.
Click to expand it.
core/block_processor_test.go
View file @
821619e1
...
...
@@ -48,13 +48,13 @@ func TestNumber(t *testing.T) {
statedb
:=
state
.
New
(
chain
.
Genesis
()
.
Root
(),
chain
.
chainDb
)
header
:=
makeHeader
(
chain
.
Genesis
(),
statedb
)
header
.
Number
=
big
.
NewInt
(
3
)
err
:=
ValidateHeader
(
pow
,
header
,
chain
.
Genesis
(),
false
,
false
)
err
:=
ValidateHeader
(
pow
,
header
,
chain
.
Genesis
()
.
Header
()
,
false
,
false
)
if
err
!=
BlockNumberErr
{
t
.
Errorf
(
"expected block number error, got %q"
,
err
)
}
header
=
makeHeader
(
chain
.
Genesis
(),
statedb
)
err
=
ValidateHeader
(
pow
,
header
,
chain
.
Genesis
(),
false
,
false
)
err
=
ValidateHeader
(
pow
,
header
,
chain
.
Genesis
()
.
Header
()
,
false
,
false
)
if
err
==
BlockNumberErr
{
t
.
Errorf
(
"didn't expect block number error"
)
}
...
...
This diff is collapsed.
Click to expand it.
eth/handler.go
View file @
821619e1
...
...
@@ -118,7 +118,7 @@ func NewProtocolManager(networkId int, mux *event.TypeMux, txpool txPool, pow po
manager
.
downloader
=
downloader
.
New
(
manager
.
eventMux
,
manager
.
chainman
.
HasBlock
,
manager
.
chainman
.
GetBlock
,
manager
.
chainman
.
CurrentBlock
,
manager
.
chainman
.
GetTd
,
manager
.
chainman
.
InsertChain
,
manager
.
removePeer
)
validator
:=
func
(
block
*
types
.
Block
,
parent
*
types
.
Block
)
error
{
return
core
.
ValidateHeader
(
pow
,
block
.
Header
(),
parent
,
true
,
false
)
return
core
.
ValidateHeader
(
pow
,
block
.
Header
(),
parent
.
Header
()
,
true
,
false
)
}
heighter
:=
func
()
uint64
{
return
manager
.
chainman
.
CurrentBlock
()
.
NumberU64
()
...
...
This diff is collapsed.
Click to expand it.
miner/worker.go
View file @
821619e1
...
...
@@ -278,7 +278,7 @@ func (self *worker) wait() {
glog
.
V
(
logger
.
Error
)
.
Infoln
(
"Invalid block found during mining"
)
continue
}
if
err
:=
core
.
ValidateHeader
(
self
.
eth
.
BlockProcessor
()
.
Pow
,
block
.
Header
(),
parent
,
true
,
false
);
err
!=
nil
&&
err
!=
core
.
BlockFutureErr
{
if
err
:=
core
.
ValidateHeader
(
self
.
eth
.
BlockProcessor
()
.
Pow
,
block
.
Header
(),
parent
.
Header
()
,
true
,
false
);
err
!=
nil
&&
err
!=
core
.
BlockFutureErr
{
glog
.
V
(
logger
.
Error
)
.
Infoln
(
"Invalid header on mined block:"
,
err
)
continue
}
...
...
This diff is collapsed.
Click to expand it.
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