diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index 0e36d12903..49929be7fb 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -43,6 +43,7 @@ var ( allowedFutureBlockTime = 15 * time.Second // Max time from current time allowed for blocks, before they're considered future blocks DisinflationRateQuotient = big.NewInt(4) // Disinflation rate quotient for ECIP1017 DisinflationRateDivisor = big.NewInt(5) // Disinflation rate divisor for ECIP1017 + ExpDiffPeriod = big.NewInt(100000) // Exponential diff period for ECIP1010 ) // Various error messages to mark blocks invalid. These should be private to @@ -304,6 +305,8 @@ func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Heade return calcDifficultyBombDisposal(time, parent) case config.IsByzantium(next): return calcDifficultyByzantium(time, parent) + case config.IsECIP1010(next): + return calcDifficultyECIP1010(time, parent, next, config.ECIP1010PauseBlock, config.ECIP1010Length) case config.IsHomestead(next): return calcDifficultyHomestead(time, parent) default: @@ -421,6 +424,115 @@ func calcDifficultyBombDisposal(time uint64, parent *types.Header) *big.Int { return x } +func calcDifficultyECIP1010(time uint64, parent *types.Header, nextBlock *big.Int, pauseBlock *big.Int, length *big.Int) *big.Int { + explosionBlock := big.NewInt(0).Add(pauseBlock, length) + if nextBlock.Cmp(explosionBlock) < 0 { + return calcDifficultyDiehard(time, parent.Time.Uint64(), parent.Difficulty, pauseBlock) + } else { + return calcDifficultyExplosion(time, parent.Time.Uint64(), parent.Number, parent.Difficulty, pauseBlock, explosionBlock) + } +} + +func calcDifficultyDiehard(time, parentTime uint64, parentDiff *big.Int, diehardBlock *big.Int) *big.Int { + // https://github.com/ethereumproject/ECIPs/blob/master/ECIPS/ECIP-1010.md + // algorithm: + // diff = (parent_diff + + // (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99)) + // ) + 2^(fixed_diff) + + bigTime := new(big.Int).SetUint64(time) + bigParentTime := new(big.Int).SetUint64(parentTime) + + // holds intermediate values to make the algo easier to read & audit + x := new(big.Int) + y := new(big.Int) + + // 1 - (block_timestamp -parent_timestamp) // 10 + x.Sub(bigTime, bigParentTime) + x.Div(x, big10) + x.Sub(common.Big1, x) + + // max(1 - (block_timestamp - parent_timestamp) // 10, -99))) + if x.Cmp(bigMinus99) < 0 { + x.Set(bigMinus99) + } + + // (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99)) + y.Div(parentDiff, params.DifficultyBoundDivisor) + x.Mul(y, x) + x.Add(parentDiff, x) + + // minimum difficulty can ever be (before exponential factor) + if x.Cmp(params.MinimumDifficulty) < 0 { + x.Set(params.MinimumDifficulty) + } + + // for the exponential factor + fixedCount := new(big.Int).Div(diehardBlock, ExpDiffPeriod) + + // the exponential factor, commonly referred to as "the bomb" + // diff = diff + 2^(periodCount - 2) + if fixedCount.Cmp(common.Big1) > 0 { + y.Sub(fixedCount, common.Big2) + y.Exp(common.Big2, y, nil) + x.Add(x, y) + } + + return x +} + +func calcDifficultyExplosion(time, parentTime uint64, parentNumber, parentDiff *big.Int, delayBlock *big.Int, continueBlock *big.Int) *big.Int { + // https://github.com/ethereumproject/ECIPs/blob/master/ECIPs/ECIP-1010.md + // algorithm: + // diff = (parent_diff + + // (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99)) + // ) + 2^(delayedCount - 2) + + bigTime := new(big.Int).SetUint64(time) + bigParentTime := new(big.Int).SetUint64(parentTime) + + // holds intermediate values to make the algo easier to read & audit + x := new(big.Int) + y := new(big.Int) + + // 1 - (block_timestamp -parent_timestamp) // 10 + x.Sub(bigTime, bigParentTime) + x.Div(x, big10) + x.Sub(common.Big1, x) + + // max(1 - (block_timestamp - parent_timestamp) // 10, -99))) + if x.Cmp(bigMinus99) < 0 { + x.Set(bigMinus99) + } + + // (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99)) + y.Div(parentDiff, params.DifficultyBoundDivisor) + x.Mul(y, x) + x.Add(parentDiff, x) + + // minimum difficulty can ever be (before exponential factor) + if x.Cmp(params.MinimumDifficulty) < 0 { + x.Set(params.MinimumDifficulty) + } + + // for the exponential factor... + + delayedCount := new(big.Int).Add(parentNumber, common.Big1) + delayedCount.Sub(delayedCount, continueBlock) + delayedCount.Add(delayedCount, delayBlock) + delayedCount.Div(delayedCount, ExpDiffPeriod) + + // the exponential factor, commonly referred to as "the bomb" + // diff = diff + 2^(periodCount - 2) + if delayedCount.Cmp(common.Big1) > 0 { + y.Sub(delayedCount, common.Big2) + y.Exp(common.Big2, y, nil) + x.Add(x, y) + } + + return x +} + // calcDifficultyHomestead 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 Homestead rules. diff --git a/params/config.go b/params/config.go index ef7a89827f..f0aa130fc5 100644 --- a/params/config.go +++ b/params/config.go @@ -83,6 +83,8 @@ var ( ConstantinopleBlock: nil, ECIP1017EraRounds: big.NewInt(5000000), EIP160Block: big.NewInt(3000000), + ECIP1010PauseBlock: big.NewInt(3000000), + ECIP1010Length: big.NewInt(2000000), Ethash: new(EthashConfig), } @@ -147,16 +149,16 @@ var ( // // This configuration is intentionally not using keyed fields to force anyone // adding flags to the config to also have to set these fields. - AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, new(EthashConfig), nil} + AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, nil, nil, new(EthashConfig), nil} // AllCliqueProtocolChanges contains every protocol change (EIPs) introduced // and accepted by the Ethereum core developers into the Clique consensus. // // This configuration is intentionally not using keyed fields to force anyone // adding flags to the config to also have to set these fields. - AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}} + AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}} - TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, new(EthashConfig), nil} + TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, nil, nil, new(EthashConfig), nil} TestRules = TestChainConfig.Rules(new(big.Int)) ) @@ -188,6 +190,9 @@ type ChainConfig struct { ECIP1017EraRounds *big.Int `json:"ecip1017EraRounds,omitempty"` // ECIP1017 era rounds EIP160Block *big.Int `json:"eip160Block,omitempty"` // EIP160 HF block + ECIP1010PauseBlock *big.Int `json:"ecip1010PauseBlock,omitempty"` // ECIP1010 pause HF block + ECIP1010Length *big.Int `json:"ecip1010Length,omitempty"` // ECIP1010 length + // Various consensus engines Ethash *EthashConfig `json:"ethash,omitempty"` Clique *CliqueConfig `json:"clique,omitempty"` @@ -223,7 +228,7 @@ func (c *ChainConfig) String() string { default: engine = "unknown" } - return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v, Disposal: %v, Social: %v, ECIP1017: %v, EIP160: %v, Constantinople: %v, Engine: %v}", + return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v, Disposal: %v, Social: %v, ECIP1017: %v, EIP160: %v, ECIP1010PauseBlock: %v, ECIP1010Length: %v, Constantinople: %v, Engine: %v}", c.ChainId, c.HomesteadBlock, c.DAOForkBlock, @@ -236,6 +241,8 @@ func (c *ChainConfig) String() string { c.SocialBlock, c.ECIP1017EraRounds, c.EIP160Block, + c.ECIP1010PauseBlock, + c.ECIP1010Length, c.ConstantinopleBlock, engine, ) @@ -293,6 +300,10 @@ func (c *ChainConfig) IsSocial(num *big.Int) bool { return isForked(c.SocialBlock, num) } +func (c *ChainConfig) IsECIP1010(num *big.Int) bool { + return isForked(c.ECIP1010PauseBlock, num) +} + // GasTable returns the gas table corresponding to the current phase (homestead or homestead reprice). // // The returned GasTable's fields shouldn't, under any circumstances, be changed.