consensus, core/vm, params: fixed rebase issues

This commit is contained in:
Jeffrey Wilcke 2017-03-26 10:50:27 +02:00
parent 22ca13fa85
commit 363cfd850f
4 changed files with 22 additions and 26 deletions

View file

@ -308,8 +308,8 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
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.Big1) next := new(big.Int).Add(parent.Number, common.Big1)
switch { switch {
case config.IsEIP100(next): case config.IsMetropolis(next):
return CalcDifficultyMetropolis(time, parent) return calcDifficultyMetropolis(time, parent)
case config.IsHomestead(next): case config.IsHomestead(next):
return calcDifficultyHomestead(time, parent) return calcDifficultyHomestead(time, parent)
default: default:
@ -320,11 +320,12 @@ 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)
big9 = big.NewInt(9)
big10 = big.NewInt(10) big10 = big.NewInt(10)
bigMinus99 = big.NewInt(-99) bigMinus99 = big.NewInt(-99)
) )
func CalcDifficultyMetropolis(time uint64, parent *types.Header) *big.Int { func calcDifficultyMetropolis(time uint64, parent *types.Header) *big.Int {
bigTime := new(big.Int).SetUint64(time) bigTime := new(big.Int).SetUint64(time)
bigParentTime := new(big.Int).Set(parent.Time) bigParentTime := new(big.Int).Set(parent.Time)
@ -356,7 +357,7 @@ func CalcDifficultyMetropolis(time uint64, parent *types.Header) *big.Int {
// for the exponential factor // for the exponential factor
periodCount := new(big.Int).Add(parent.Number, common.Big1) periodCount := new(big.Int).Add(parent.Number, common.Big1)
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)
@ -372,15 +373,15 @@ func CalcDifficultyMetropolis(time uint64, parent *types.Header) *big.Int {
// 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.
func calcDifficultyHomestead(time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int { func calcDifficultyHomestead(time uint64, parent *types.Header) *big.Int {
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.mediawiki // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.mediawiki
// algorithm: // algorithm:
// diff = (parent_diff + // diff = (parent_diff +
// (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99)) // (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
// ) + 2^(periodCount - 2) // ) + 2^(periodCount - 2)
bigTime := new(big.Int).SetUint64(time) bigTime := new(big.Int).Set(parent.Time)
bigParentTime := new(big.Int).SetUint64(parentTime) bigParentTime := new(big.Int).Set(parent.Time)
// holds intermediate values to make the algo easier to read & audit // holds intermediate values to make the algo easier to read & audit
x := new(big.Int) x := new(big.Int)
@ -396,16 +397,16 @@ func calcDifficultyHomestead(time, parentTime uint64, parentNumber, parentDiff *
x.Set(bigMinus99) x.Set(bigMinus99)
} }
// (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99)) // (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
y.Div(parentDiff, params.DifficultyBoundDivisor) y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
x.Mul(y, x) x.Mul(y, x)
x.Add(parentDiff, x) x.Add(parent.Difficulty, x)
// minimum difficulty can ever be (before exponential factor) // minimum difficulty can ever be (before exponential factor)
if x.Cmp(params.MinimumDifficulty) < 0 { if x.Cmp(params.MinimumDifficulty) < 0 {
x.Set(params.MinimumDifficulty) x.Set(params.MinimumDifficulty)
} }
// for the exponential factor // for the exponential factor
periodCount := new(big.Int).Add(parentNumber, common.Big1) periodCount := new(big.Int).Add(parent.Number, common.Big1)
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"
@ -421,25 +422,25 @@ func calcDifficultyHomestead(time, parentTime uint64, parentNumber, parentDiff *
// calcDifficultyFrontier is the difficulty adjustment algorithm. It returns the // calcDifficultyFrontier is the difficulty adjustment algorithm. It returns the
// difficulty that a new block should have when created at time given the parent // difficulty that a new block should have when created at time given the parent
// block's time and difficulty. The calculation uses the Frontier rules. // block's time and difficulty. The calculation uses the Frontier rules.
func calcDifficultyFrontier(time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int { func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int {
diff := new(big.Int) diff := new(big.Int)
adjust := new(big.Int).Div(parentDiff, params.DifficultyBoundDivisor) adjust := new(big.Int).Div(parent.Difficulty, params.DifficultyBoundDivisor)
bigTime := new(big.Int) bigTime := new(big.Int)
bigParentTime := new(big.Int) bigParentTime := new(big.Int)
bigTime.SetUint64(time) bigTime.SetUint64(time)
bigParentTime.SetUint64(parentTime) bigParentTime.Set(parent.Time)
if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 { if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 {
diff.Add(parentDiff, adjust) diff.Add(parent.Difficulty, adjust)
} else { } else {
diff.Sub(parentDiff, adjust) diff.Sub(parent.Difficulty, adjust)
} }
if diff.Cmp(params.MinimumDifficulty) < 0 { if diff.Cmp(params.MinimumDifficulty) < 0 {
diff.Set(params.MinimumDifficulty) diff.Set(params.MinimumDifficulty)
} }
periodCount := new(big.Int).Add(parentNumber, common.Big1) periodCount := new(big.Int).Add(parent.Number, common.Big1)
periodCount.Div(periodCount, expDiffPeriod) periodCount.Div(periodCount, expDiffPeriod)
if periodCount.Cmp(common.Big1) > 0 { if periodCount.Cmp(common.Big1) > 0 {
// diff = diff + 2^(periodCount - 2) // diff = diff + 2^(periodCount - 2)

View file

@ -142,7 +142,7 @@ func (b *BlockGen) OffsetTime(seconds int64) {
if b.header.Time.Cmp(b.parent.Header().Time) <= 0 { if b.header.Time.Cmp(b.parent.Header().Time) <= 0 {
panic("block time out of range") panic("block time out of range")
} }
b.header.Difficulty = ethash.CalcDifficulty(b.config, b.header.Time.Uint64(), b.parent) b.header.Difficulty = ethash.CalcDifficulty(b.config, b.header.Time.Uint64(), b.parent.Header())
} }
// GenerateChain creates a chain of n blocks. The first block's // GenerateChain creates a chain of n blocks. The first block's

View file

@ -18,7 +18,6 @@ package vm
import ( import (
"fmt" "fmt"
"math/big"
"sync/atomic" "sync/atomic"
"time" "time"
@ -140,9 +139,7 @@ func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret
if err != nil && in.cfg.Debug { if err != nil && in.cfg.Debug {
// XXX For debugging // XXX For debugging
//fmt.Printf("%04d: %8v cost = %-8d stack = %-8d ERR = %v\n", pc, op, cost, stack.len(), err) //fmt.Printf("%04d: %8v cost = %-8d stack = %-8d ERR = %v\n", pc, op, cost, stack.len(), err)
// TODO update the tracer to accept uint64 instead of *big.Int in.cfg.Tracer.CaptureState(in.evm, pc, op, contract.Gas, cost, mem, stack, contract, in.evm.depth, err)
g, c := new(big.Int).SetUint64(contract.Gas), new(big.Int).SetUint64(cost)
in.cfg.Tracer.CaptureState(in.evm, pc, op, g, c, mem, stack, contract, in.evm.depth, err)
} }
}() }()
@ -203,9 +200,7 @@ func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret
} }
if in.cfg.Debug { if in.cfg.Debug {
// TODO update the tracer to accept uint64 instead of *big.Int in.cfg.Tracer.CaptureState(in.evm, pc, op, contract.Gas, cost, mem, stack, contract, in.evm.depth, err)
g, c := new(big.Int).SetUint64(contract.Gas), new(big.Int).SetUint64(cost)
in.cfg.Tracer.CaptureState(in.evm, pc, op, g, c, mem, stack, contract, in.evm.depth, err)
} }
// XXX For debugging // XXX For debugging
//fmt.Printf("%04d: %8v cost = %-8d stack = %-8d\n", pc, op, cost, stack.len()) //fmt.Printf("%04d: %8v cost = %-8d stack = %-8d\n", pc, op, cost, stack.len())

View file

@ -34,7 +34,7 @@ var (
EIP150Hash: MainNetHomesteadGasRepriceHash, EIP150Hash: MainNetHomesteadGasRepriceHash,
EIP155Block: MainNetSpuriousDragon, EIP155Block: MainNetSpuriousDragon,
EIP158Block: MainNetSpuriousDragon, EIP158Block: MainNetSpuriousDragon,
MetropolisBlock: MainNetMetropolis, MetropolisBlock: MainNetMetropolisBlock,
} }
// TestnetChainConfig is the chain parameters to run a node on the test network. // TestnetChainConfig is the chain parameters to run a node on the test network.
@ -47,7 +47,7 @@ var (
EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"), EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
EIP155Block: big.NewInt(10), EIP155Block: big.NewInt(10),
EIP158Block: big.NewInt(10), EIP158Block: big.NewInt(10),
MetropolisBlock: TestNetMetropolis, MetropolisBlock: TestNetMetropolisBlock,
} }
// AllProtocolChanges contains every protocol change (EIPs) // AllProtocolChanges contains every protocol change (EIPs)