Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
whqwjb
go-ethereum
提交
3df7df03
G
go-ethereum
项目概览
whqwjb
/
go-ethereum
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
go-ethereum
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
3df7df03
编写于
9月 15, 2018
作者:
M
Martin Holst Swende
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ethash: less copy-paste for EIP 1234
上级
0732617b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
67 addition
and
116 deletion
+67
-116
consensus/ethash/consensus.go
consensus/ethash/consensus.go
+67
-116
未找到文件。
consensus/ethash/consensus.go
浏览文件 @
3df7df03
...
...
@@ -43,6 +43,16 @@ var (
ConstantinopleBlockReward
=
big
.
NewInt
(
2e+18
)
// Block reward in wei for successfully mining a block upward from Constantinople
maxUncles
=
2
// Maximum number of uncles allowed in a single block
allowedFutureBlockTime
=
15
*
time
.
Second
// Max time from current time allowed for blocks, before they're considered future blocks
// calcDifficultyConstantinople is the difficulty adjustment algorithm for Constantinople.
// 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 Byzantium rules, but with bomb offset 5M.
calcDifficultyConstantinople
=
makeDifficultyCalculator
(
big
.
NewInt
(
4999999
))
// calcDifficultyByzantium 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 Byzantium rules.
calcDifficultyByzantium
=
makeDifficultyCalculator
(
big
.
NewInt
(
2999999
))
)
// Various error messages to mark blocks invalid. These should be private to
...
...
@@ -319,126 +329,67 @@ var (
big9
=
big
.
NewInt
(
9
)
big10
=
big
.
NewInt
(
10
)
bigMinus99
=
big
.
NewInt
(
-
99
)
big2999999
=
big
.
NewInt
(
2999999
)
big4999999
=
big
.
NewInt
(
4999999
)
)
// calcDifficultyConstantinople 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 Constantinople rules.
func
calcDifficultyConstantinople
(
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((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -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
)
}
// calculate a fake block number for the ice-age delay:
// https://github.com/ethereum/EIPs/pull/1234
// fake_block_number = max(0, block.number - 5_000_000)
fakeBlockNumber
:=
new
(
big
.
Int
)
if
parent
.
Number
.
Cmp
(
big4999999
)
>=
0
{
fakeBlockNumber
=
fakeBlockNumber
.
Sub
(
parent
.
Number
,
big4999999
)
// Note, parent is 1 less than the actual block number
}
// for the exponential factor
periodCount
:=
fakeBlockNumber
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
}
// calcDifficultyByzantium 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 Byzantium rules.
func
calcDifficultyByzantium
(
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((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -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
)
}
// calculate a fake block number for the ice-age delay:
// https://github.com/ethereum/EIPs/pull/669
// fake_block_number = max(0, block.number - 3_000_000)
fakeBlockNumber
:=
new
(
big
.
Int
)
if
parent
.
Number
.
Cmp
(
big2999999
)
>=
0
{
fakeBlockNumber
=
fakeBlockNumber
.
Sub
(
parent
.
Number
,
big2999999
)
// Note, parent is 1 less than the actual block number
}
// for the exponential factor
periodCount
:=
fakeBlockNumber
periodCount
.
Div
(
periodCount
,
expDiffPeriod
)
// makeDifficultyCalculator creates a difficultyCalculator with the given bomb-delay.
// the difficulty is calculated with Byzantium rules, which differs from Homestead in
// how uncles affect the calculation
func
makeDifficultyCalculator
(
bombDelay
*
big
.
Int
)
func
(
time
uint64
,
parent
*
types
.
Header
)
*
big
.
Int
{
return
func
(
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((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -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
)
}
// calculate a fake block number for the ice-age delay:
// https://github.com/ethereum/EIPs/pull/669
// fake_block_number = max(0, block.number - 3_000_000)
fakeBlockNumber
:=
new
(
big
.
Int
)
if
parent
.
Number
.
Cmp
(
bombDelay
)
>=
0
{
fakeBlockNumber
=
fakeBlockNumber
.
Sub
(
parent
.
Number
,
bombDelay
)
// Note, parent is 1 less than the actual block number
}
// for the exponential factor
periodCount
:=
fakeBlockNumber
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
)
// 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
}
return
x
}
// calcDifficultyHomestead is the difficulty adjustment algorithm. It returns
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录