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
c65f10a1
Commit
c65f10a1
authored
Jul 17, 2017
by
Péter Szilágyi
Committed by
GitHub
Jul 17, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #14733 from karalabe/metro-eip100
consensus/ethash, core: implement Metropolis EIP 100
parents
a56f3dc0
8c313eed
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
12 deletions
+70
-12
consensus.go
consensus/ethash/consensus.go
+69
-12
chain_makers.go
core/chain_makers.go
+1
-0
No files found.
consensus/ethash/consensus.go
View file @
c65f10a1
...
@@ -287,8 +287,10 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
...
@@ -287,8 +287,10 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
// given the parent block's time and difficulty.
// given the parent block's time and difficulty.
// TODO (karalabe): Move the chain maker into this package and make this private!
// TODO (karalabe): Move the chain maker into this package and make this private!
func
CalcDifficulty
(
config
*
params
.
ChainConfig
,
time
uint64
,
parent
*
types
.
Header
)
*
big
.
Int
{
func
CalcDifficulty
(
config
*
params
.
ChainConfig
,
time
uint64
,
parent
*
types
.
Header
)
*
big
.
Int
{
next
:=
new
(
big
.
Int
)
.
Add
(
parent
.
Number
,
common
.
B
ig1
)
next
:=
new
(
big
.
Int
)
.
Add
(
parent
.
Number
,
b
ig1
)
switch
{
switch
{
case
config
.
IsMetropolis
(
next
)
:
return
calcDifficultyMetropolis
(
time
,
parent
)
case
config
.
IsHomestead
(
next
)
:
case
config
.
IsHomestead
(
next
)
:
return
calcDifficultyHomestead
(
time
,
parent
)
return
calcDifficultyHomestead
(
time
,
parent
)
default
:
default
:
...
@@ -299,10 +301,65 @@ func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Heade
...
@@ -299,10 +301,65 @@ func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Heade
// Some weird constants to avoid constant memory allocs for them.
// Some weird constants to avoid constant memory allocs for them.
var
(
var
(
expDiffPeriod
=
big
.
NewInt
(
100000
)
expDiffPeriod
=
big
.
NewInt
(
100000
)
big1
=
big
.
NewInt
(
1
)
big2
=
big
.
NewInt
(
2
)
big9
=
big
.
NewInt
(
9
)
big10
=
big
.
NewInt
(
10
)
big10
=
big
.
NewInt
(
10
)
bigMinus99
=
big
.
NewInt
(
-
99
)
bigMinus99
=
big
.
NewInt
(
-
99
)
)
)
// calcDifficultyMetropolis is the difficulty adjustment algorithm. It returns
// the difficulty that a new block should have when created at time given the
// parent block's time and difficulty. The calculation uses the Metropolis rules.
func
calcDifficultyMetropolis
(
time
uint64
,
parent
*
types
.
Header
)
*
big
.
Int
{
// https://github.com/ethereum/EIPs/issues/100.
// algorithm:
// diff = (parent_diff +
// (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
// ) + 2^(periodCount - 2)
bigTime
:=
new
(
big
.
Int
)
.
SetUint64
(
time
)
bigParentTime
:=
new
(
big
.
Int
)
.
Set
(
parent
.
Time
)
// holds intermediate values to make the algo easier to read & audit
x
:=
new
(
big
.
Int
)
y
:=
new
(
big
.
Int
)
// (2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9
x
.
Sub
(
bigTime
,
bigParentTime
)
x
.
Div
(
x
,
big9
)
if
parent
.
UncleHash
==
types
.
EmptyUncleHash
{
x
.
Sub
(
big1
,
x
)
}
else
{
x
.
Sub
(
big2
,
x
)
}
// max((2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9, -99)
if
x
.
Cmp
(
bigMinus99
)
<
0
{
x
.
Set
(
bigMinus99
)
}
// (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
y
.
Div
(
parent
.
Difficulty
,
params
.
DifficultyBoundDivisor
)
x
.
Mul
(
y
,
x
)
x
.
Add
(
parent
.
Difficulty
,
x
)
// minimum difficulty can ever be (before exponential factor)
if
x
.
Cmp
(
params
.
MinimumDifficulty
)
<
0
{
x
.
Set
(
params
.
MinimumDifficulty
)
}
// for the exponential factor
periodCount
:=
new
(
big
.
Int
)
.
Add
(
parent
.
Number
,
big1
)
periodCount
.
Div
(
periodCount
,
expDiffPeriod
)
// the exponential factor, commonly referred to as "the bomb"
// diff = diff + 2^(periodCount - 2)
if
periodCount
.
Cmp
(
big1
)
>
0
{
y
.
Sub
(
periodCount
,
big2
)
y
.
Exp
(
big2
,
y
,
nil
)
x
.
Add
(
x
,
y
)
}
return
x
}
// calcDifficultyHomestead is the difficulty adjustment algorithm. It returns
// calcDifficultyHomestead is the difficulty adjustment algorithm. It returns
// the difficulty that a new block should have when created at time given the
// the difficulty that a new block should have when created at time given the
// parent block's time and difficulty. The calculation uses the Homestead rules.
// parent block's time and difficulty. The calculation uses the Homestead rules.
...
@@ -320,12 +377,12 @@ func calcDifficultyHomestead(time uint64, parent *types.Header) *big.Int {
...
@@ -320,12 +377,12 @@ func calcDifficultyHomestead(time uint64, parent *types.Header) *big.Int {
x
:=
new
(
big
.
Int
)
x
:=
new
(
big
.
Int
)
y
:=
new
(
big
.
Int
)
y
:=
new
(
big
.
Int
)
// 1 - (block_timestamp -parent_timestamp) // 10
// 1 - (block_timestamp -
parent_timestamp) // 10
x
.
Sub
(
bigTime
,
bigParentTime
)
x
.
Sub
(
bigTime
,
bigParentTime
)
x
.
Div
(
x
,
big10
)
x
.
Div
(
x
,
big10
)
x
.
Sub
(
common
.
B
ig1
,
x
)
x
.
Sub
(
b
ig1
,
x
)
// max(1 - (block_timestamp - parent_timestamp) // 10, -99)
))
// max(1 - (block_timestamp - parent_timestamp) // 10, -99)
if
x
.
Cmp
(
bigMinus99
)
<
0
{
if
x
.
Cmp
(
bigMinus99
)
<
0
{
x
.
Set
(
bigMinus99
)
x
.
Set
(
bigMinus99
)
}
}
...
@@ -339,14 +396,14 @@ func calcDifficultyHomestead(time uint64, parent *types.Header) *big.Int {
...
@@ -339,14 +396,14 @@ func calcDifficultyHomestead(time uint64, parent *types.Header) *big.Int {
x
.
Set
(
params
.
MinimumDifficulty
)
x
.
Set
(
params
.
MinimumDifficulty
)
}
}
// for the exponential factor
// for the exponential factor
periodCount
:=
new
(
big
.
Int
)
.
Add
(
parent
.
Number
,
common
.
B
ig1
)
periodCount
:=
new
(
big
.
Int
)
.
Add
(
parent
.
Number
,
b
ig1
)
periodCount
.
Div
(
periodCount
,
expDiffPeriod
)
periodCount
.
Div
(
periodCount
,
expDiffPeriod
)
// the exponential factor, commonly referred to as "the bomb"
// the exponential factor, commonly referred to as "the bomb"
// diff = diff + 2^(periodCount - 2)
// diff = diff + 2^(periodCount - 2)
if
periodCount
.
Cmp
(
common
.
B
ig1
)
>
0
{
if
periodCount
.
Cmp
(
b
ig1
)
>
0
{
y
.
Sub
(
periodCount
,
common
.
B
ig2
)
y
.
Sub
(
periodCount
,
b
ig2
)
y
.
Exp
(
common
.
B
ig2
,
y
,
nil
)
y
.
Exp
(
b
ig2
,
y
,
nil
)
x
.
Add
(
x
,
y
)
x
.
Add
(
x
,
y
)
}
}
return
x
return
x
...
@@ -373,12 +430,12 @@ func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int {
...
@@ -373,12 +430,12 @@ func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int {
diff
.
Set
(
params
.
MinimumDifficulty
)
diff
.
Set
(
params
.
MinimumDifficulty
)
}
}
periodCount
:=
new
(
big
.
Int
)
.
Add
(
parent
.
Number
,
common
.
B
ig1
)
periodCount
:=
new
(
big
.
Int
)
.
Add
(
parent
.
Number
,
b
ig1
)
periodCount
.
Div
(
periodCount
,
expDiffPeriod
)
periodCount
.
Div
(
periodCount
,
expDiffPeriod
)
if
periodCount
.
Cmp
(
common
.
B
ig1
)
>
0
{
if
periodCount
.
Cmp
(
b
ig1
)
>
0
{
// diff = diff + 2^(periodCount - 2)
// diff = diff + 2^(periodCount - 2)
expDiff
:=
periodCount
.
Sub
(
periodCount
,
common
.
B
ig2
)
expDiff
:=
periodCount
.
Sub
(
periodCount
,
b
ig2
)
expDiff
.
Exp
(
common
.
B
ig2
,
expDiff
,
nil
)
expDiff
.
Exp
(
b
ig2
,
expDiff
,
nil
)
diff
.
Add
(
diff
,
expDiff
)
diff
.
Add
(
diff
,
expDiff
)
diff
=
math
.
BigMax
(
diff
,
params
.
MinimumDifficulty
)
diff
=
math
.
BigMax
(
diff
,
params
.
MinimumDifficulty
)
}
}
...
...
core/chain_makers.go
View file @
c65f10a1
...
@@ -218,6 +218,7 @@ func makeHeader(config *params.ChainConfig, parent *types.Block, state *state.St
...
@@ -218,6 +218,7 @@ func makeHeader(config *params.ChainConfig, parent *types.Block, state *state.St
Number
:
parent
.
Number
(),
Number
:
parent
.
Number
(),
Time
:
new
(
big
.
Int
)
.
Sub
(
time
,
big
.
NewInt
(
10
)),
Time
:
new
(
big
.
Int
)
.
Sub
(
time
,
big
.
NewInt
(
10
)),
Difficulty
:
parent
.
Difficulty
(),
Difficulty
:
parent
.
Difficulty
(),
UncleHash
:
parent
.
UncleHash
(),
}),
}),
GasLimit
:
CalcGasLimit
(
parent
),
GasLimit
:
CalcGasLimit
(
parent
),
GasUsed
:
new
(
big
.
Int
),
GasUsed
:
new
(
big
.
Int
),
...
...
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