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
84a4f761
Commit
84a4f761
authored
Mar 04, 2015
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
uncle validation
parent
d4d505c8
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
46 additions
and
30 deletions
+46
-30
block_processor.go
core/block_processor.go
+38
-22
chain_makers.go
core/chain_makers.go
+1
-1
chain_manager.go
core/chain_manager.go
+6
-6
filter.go
core/filter.go
+1
-1
No files found.
core/block_processor.go
View file @
84a4f761
...
@@ -176,10 +176,15 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
...
@@ -176,10 +176,15 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
state
:=
state
.
New
(
parent
.
Root
(),
sm
.
db
)
state
:=
state
.
New
(
parent
.
Root
(),
sm
.
db
)
// Block validation
// Block validation
if
err
=
sm
.
Validate
Block
(
block
,
parent
);
err
!=
nil
{
if
err
=
sm
.
Validate
Header
(
block
.
Header
(),
parent
.
Header
()
);
err
!=
nil
{
return
return
}
}
// There can be at most two uncles
if
len
(
block
.
Uncles
())
>
2
{
return
nil
,
ValidationError
(
"Block can only contain one uncle (contained %v)"
,
len
(
block
.
Uncles
()))
}
receipts
,
err
:=
sm
.
TransitionState
(
state
,
parent
,
block
,
false
)
receipts
,
err
:=
sm
.
TransitionState
(
state
,
parent
,
block
,
false
)
if
err
!=
nil
{
if
err
!=
nil
{
return
return
...
@@ -238,46 +243,41 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
...
@@ -238,46 +243,41 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
// Validates the current block. Returns an error if the block was invalid,
// Validates the current block. Returns an error if the block was invalid,
// an uncle or anything that isn't on the current block chain.
// an uncle or anything that isn't on the current block chain.
// Validation validates easy over difficult (dagger takes longer time = difficult)
// Validation validates easy over difficult (dagger takes longer time = difficult)
func
(
sm
*
BlockProcessor
)
Validate
Block
(
block
,
parent
*
types
.
Block
)
error
{
func
(
sm
*
BlockProcessor
)
Validate
Header
(
block
,
parent
*
types
.
Header
)
error
{
if
len
(
block
.
Header
()
.
Extra
)
>
1024
{
if
len
(
block
.
Extra
)
>
1024
{
return
fmt
.
Errorf
(
"Block extra data too long (%d)"
,
len
(
block
.
Header
()
.
Extra
))
return
fmt
.
Errorf
(
"Block extra data too long (%d)"
,
len
(
block
.
Extra
))
}
}
expd
:=
CalcDifficulty
(
block
,
parent
)
expd
:=
CalcDifficulty
(
block
,
parent
)
if
expd
.
Cmp
(
block
.
Header
()
.
Difficulty
)
!=
0
{
if
expd
.
Cmp
(
block
.
Difficulty
)
!=
0
{
return
fmt
.
Errorf
(
"Difficulty check failed for block %v, %v"
,
block
.
Header
()
.
Difficulty
,
expd
)
return
fmt
.
Errorf
(
"Difficulty check failed for block %v, %v"
,
block
.
Difficulty
,
expd
)
}
}
//expl := CalcGasLimit(parent, block)
//expl := CalcGasLimit(parent, block)
//if expl.Cmp(block.Header().GasLimit) != 0 {
//if expl.Cmp(block.Header().GasLimit) != 0 {
// block.gasLimit - parent.gasLimit <= parent.gasLimit / 1024
// block.gasLimit - parent.gasLimit <= parent.gasLimit / 1024
a
:=
new
(
big
.
Int
)
.
Sub
(
block
.
Header
()
.
GasLimit
,
parent
.
Header
()
.
GasLimit
)
a
:=
new
(
big
.
Int
)
.
Sub
(
block
.
GasLimit
,
parent
.
GasLimit
)
b
:=
new
(
big
.
Int
)
.
Div
(
parent
.
Header
()
.
GasLimit
,
big
.
NewInt
(
1024
))
b
:=
new
(
big
.
Int
)
.
Div
(
parent
.
GasLimit
,
big
.
NewInt
(
1024
))
if
a
.
Cmp
(
b
)
>
0
{
if
a
.
Cmp
(
b
)
>
0
{
return
fmt
.
Errorf
(
"GasLimit check failed for block %v (%v > %v)"
,
block
.
Header
()
.
GasLimit
,
a
,
b
)
return
fmt
.
Errorf
(
"GasLimit check failed for block %v (%v > %v)"
,
block
.
GasLimit
,
a
,
b
)
}
// There can be at most one uncle
if
len
(
block
.
Uncles
())
>
1
{
return
ValidationError
(
"Block can only contain one uncle (contained %v)"
,
len
(
block
.
Uncles
()))
}
}
if
block
.
Time
()
<
parent
.
Time
()
{
if
block
.
Time
<
parent
.
Time
{
return
ValidationError
(
"Block timestamp not after prev block (%v - %v)"
,
block
.
Header
()
.
Time
,
parent
.
Header
()
.
Time
)
return
ValidationError
(
"Block timestamp not after prev block (%v - %v)"
,
block
.
Time
,
parent
.
Time
)
}
}
if
block
.
Time
(
)
>
time
.
Now
()
.
Unix
()
{
if
int64
(
block
.
Time
)
>
time
.
Now
()
.
Unix
()
{
return
BlockFutureErr
return
BlockFutureErr
}
}
if
new
(
big
.
Int
)
.
Sub
(
block
.
Number
(),
parent
.
Number
()
)
.
Cmp
(
big
.
NewInt
(
1
))
!=
0
{
if
new
(
big
.
Int
)
.
Sub
(
block
.
Number
,
parent
.
Number
)
.
Cmp
(
big
.
NewInt
(
1
))
!=
0
{
return
BlockNumberErr
return
BlockNumberErr
}
}
// Verify the nonce of the block. Return an error if it's not valid
// Verify the nonce of the block. Return an error if it's not valid
if
!
sm
.
Pow
.
Verify
(
block
)
{
if
!
sm
.
Pow
.
Verify
(
types
.
NewBlockWithHeader
(
block
)
)
{
return
ValidationError
(
"Block's nonce is invalid (= %x)"
,
block
.
Header
()
.
Nonce
)
return
ValidationError
(
"Block's nonce is invalid (= %x)"
,
block
.
Nonce
)
}
}
return
nil
return
nil
...
@@ -287,23 +287,39 @@ func (sm *BlockProcessor) AccumulateRewards(statedb *state.StateDB, block, paren
...
@@ -287,23 +287,39 @@ func (sm *BlockProcessor) AccumulateRewards(statedb *state.StateDB, block, paren
reward
:=
new
(
big
.
Int
)
.
Set
(
BlockReward
)
reward
:=
new
(
big
.
Int
)
.
Set
(
BlockReward
)
ancestors
:=
set
.
New
()
ancestors
:=
set
.
New
()
uncles
:=
set
.
New
()
ancestorHeaders
:=
make
(
map
[
string
]
*
types
.
Header
)
for
_
,
ancestor
:=
range
sm
.
bc
.
GetAncestors
(
block
,
7
)
{
for
_
,
ancestor
:=
range
sm
.
bc
.
GetAncestors
(
block
,
7
)
{
ancestors
.
Add
(
string
(
ancestor
.
Hash
()))
hash
:=
string
(
ancestor
.
Hash
())
ancestorHeaders
[
hash
]
=
ancestor
.
Header
()
ancestors
.
Add
(
hash
)
// Include ancestors uncles in the uncle set. Uncles must be unique.
for
_
,
uncle
:=
range
ancestor
.
Uncles
()
{
uncles
.
Add
(
string
(
uncle
.
Hash
()))
}
}
}
uncles
:=
set
.
New
()
uncles
.
Add
(
string
(
block
.
Hash
()))
uncles
.
Add
(
string
(
block
.
Hash
()))
for
_
,
uncle
:=
range
block
.
Uncles
()
{
for
_
,
uncle
:=
range
block
.
Uncles
()
{
if
uncles
.
Has
(
string
(
uncle
.
Hash
()))
{
if
uncles
.
Has
(
string
(
uncle
.
Hash
()))
{
// Error not unique
// Error not unique
return
UncleError
(
"Uncle not unique"
)
return
UncleError
(
"Uncle not unique"
)
}
}
uncles
.
Add
(
string
(
uncle
.
Hash
()))
uncles
.
Add
(
string
(
uncle
.
Hash
()))
if
ancestors
.
Has
(
string
(
uncle
.
Hash
()))
{
return
UncleError
(
"Uncle is ancestor"
)
}
if
!
ancestors
.
Has
(
string
(
uncle
.
ParentHash
))
{
if
!
ancestors
.
Has
(
string
(
uncle
.
ParentHash
))
{
return
UncleError
(
fmt
.
Sprintf
(
"Uncle's parent unknown (%x)"
,
uncle
.
ParentHash
[
0
:
4
]))
return
UncleError
(
fmt
.
Sprintf
(
"Uncle's parent unknown (%x)"
,
uncle
.
ParentHash
[
0
:
4
]))
}
}
if
err
:=
sm
.
ValidateHeader
(
uncle
,
ancestorHeaders
[
string
(
uncle
.
ParentHash
)]);
err
!=
nil
{
return
ValidationError
(
fmt
.
Sprintf
(
"%v"
,
err
))
}
if
!
sm
.
Pow
.
Verify
(
types
.
NewBlockWithHeader
(
uncle
))
{
if
!
sm
.
Pow
.
Verify
(
types
.
NewBlockWithHeader
(
uncle
))
{
return
ValidationError
(
"Uncle's nonce is invalid (= %x)"
,
uncle
.
Nonce
)
return
ValidationError
(
"Uncle's nonce is invalid (= %x)"
,
uncle
.
Nonce
)
}
}
...
...
core/chain_makers.go
View file @
84a4f761
...
@@ -61,7 +61,7 @@ func newBlockFromParent(addr []byte, parent *types.Block) *types.Block {
...
@@ -61,7 +61,7 @@ func newBlockFromParent(addr []byte, parent *types.Block) *types.Block {
block
.
SetReceipts
(
nil
)
block
.
SetReceipts
(
nil
)
header
:=
block
.
Header
()
header
:=
block
.
Header
()
header
.
Difficulty
=
CalcDifficulty
(
block
,
parent
)
header
.
Difficulty
=
CalcDifficulty
(
block
.
Header
(),
parent
.
Header
()
)
header
.
Number
=
new
(
big
.
Int
)
.
Add
(
parent
.
Header
()
.
Number
,
ethutil
.
Big1
)
header
.
Number
=
new
(
big
.
Int
)
.
Add
(
parent
.
Header
()
.
Number
,
ethutil
.
Big1
)
header
.
Time
=
parent
.
Header
()
.
Time
+
10
header
.
Time
=
parent
.
Header
()
.
Time
+
10
header
.
GasLimit
=
CalcGasLimit
(
parent
,
block
)
header
.
GasLimit
=
CalcGasLimit
(
parent
,
block
)
...
...
core/chain_manager.go
View file @
84a4f761
...
@@ -28,16 +28,16 @@ type StateQuery interface {
...
@@ -28,16 +28,16 @@ type StateQuery interface {
GetAccount
(
addr
[]
byte
)
*
state
.
StateObject
GetAccount
(
addr
[]
byte
)
*
state
.
StateObject
}
}
func
CalcDifficulty
(
block
,
parent
*
types
.
Block
)
*
big
.
Int
{
func
CalcDifficulty
(
block
,
parent
*
types
.
Header
)
*
big
.
Int
{
diff
:=
new
(
big
.
Int
)
diff
:=
new
(
big
.
Int
)
//adjust := new(big.Int).Rsh(parent.Difficulty(), 10)
//adjust := new(big.Int).Rsh(parent.Difficulty(), 10)
//if block.Time() >= parent.Time()+8 {
//if block.Time() >= parent.Time()+8 {
adjust
:=
new
(
big
.
Int
)
.
Div
(
parent
.
Difficulty
()
,
big
.
NewInt
(
2048
))
adjust
:=
new
(
big
.
Int
)
.
Div
(
parent
.
Difficulty
,
big
.
NewInt
(
2048
))
if
(
block
.
Time
()
-
parent
.
Time
()
)
<
8
{
if
(
block
.
Time
-
parent
.
Time
)
<
8
{
diff
.
Add
(
parent
.
Difficulty
()
,
adjust
)
diff
.
Add
(
parent
.
Difficulty
,
adjust
)
}
else
{
}
else
{
diff
.
Sub
(
parent
.
Difficulty
()
,
adjust
)
diff
.
Sub
(
parent
.
Difficulty
,
adjust
)
}
}
return
diff
return
diff
...
@@ -206,7 +206,7 @@ func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block {
...
@@ -206,7 +206,7 @@ func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block {
parent
:=
bc
.
currentBlock
parent
:=
bc
.
currentBlock
if
parent
!=
nil
{
if
parent
!=
nil
{
header
:=
block
.
Header
()
header
:=
block
.
Header
()
header
.
Difficulty
=
CalcDifficulty
(
block
,
parent
)
header
.
Difficulty
=
CalcDifficulty
(
block
.
Header
(),
parent
.
Header
()
)
header
.
Number
=
new
(
big
.
Int
)
.
Add
(
parent
.
Header
()
.
Number
,
ethutil
.
Big1
)
header
.
Number
=
new
(
big
.
Int
)
.
Add
(
parent
.
Header
()
.
Number
,
ethutil
.
Big1
)
header
.
GasLimit
=
CalcGasLimit
(
parent
,
block
)
header
.
GasLimit
=
CalcGasLimit
(
parent
,
block
)
...
...
core/filter.go
View file @
84a4f761
...
@@ -147,7 +147,7 @@ func (self *Filter) FilterLogs(logs state.Logs) state.Logs {
...
@@ -147,7 +147,7 @@ func (self *Filter) FilterLogs(logs state.Logs) state.Logs {
// Filter the logs for interesting stuff
// Filter the logs for interesting stuff
Logs
:
Logs
:
for
_
,
log
:=
range
logs
{
for
_
,
log
:=
range
logs
{
if
!
includes
(
self
.
address
,
log
.
Address
())
{
if
len
(
self
.
address
)
>
0
&&
!
includes
(
self
.
address
,
log
.
Address
())
{
continue
continue
}
}
...
...
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