Skip to content

Commit

Permalink
Extraction of chain configuration (ethereum#92)
Browse files Browse the repository at this point in the history
Extraction of chain configuration
  • Loading branch information
whilei authored Jun 3, 2019
2 parents 1bde114 + 109484a commit 3d03b04
Show file tree
Hide file tree
Showing 37 changed files with 1,096 additions and 456 deletions.
113 changes: 14 additions & 99 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,11 @@ import (

// Ethash proof-of-work protocol constants.
var (
FrontierBlockReward = big.NewInt(5e+18) // Block reward in wei for successfully mining a block
EIP649FBlockReward = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Byzantium
EIP1234FBlockReward = big.NewInt(2e+18) // Block reward in wei for successfully mining a block upward from Constantinople
SocialBlockReward = new(big.Int).Mul(big.NewInt(50), big.NewInt(1e+18)) // Block reward in wei for successfully mining a block upward for Ethereum Social
EthersocialBlockReward = big.NewInt(5e+18) // Block reward in wei for successfully mining a block upward for Ethersocial Network
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
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 diff bomb & ECIP1010

// Musicoin
Mcip0BlockReward = new(big.Int).Mul(big.NewInt(314), big.NewInt(1e+18)) // In musicoin code as 'FrontierBlockReward'
Mcip3BlockReward = new(big.Int).Mul(big.NewInt(250), big.NewInt(1e+18))
Mcip8BlockReward = new(big.Int).Mul(big.NewInt(50), big.NewInt(1e+18))
MusicoinUbiBlockReward = new(big.Int).Mul(big.NewInt(50), big.NewInt(1e+18))
MusicoinDevBlockReward = new(big.Int).Mul(big.NewInt(14), big.NewInt(1e+18))
FrontierBlockReward = big.NewInt(5e+18) // Block reward in wei for successfully mining a block
EIP649FBlockReward = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Byzantium
EIP1234FBlockReward = 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
)

// Various error messages to mark blocks invalid. These should be private to
Expand Down Expand Up @@ -407,15 +395,7 @@ func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Heade
exPeriodRef.Set(fakeBlockNumber)

} else if config.IsECIP1010(next) {
// https://github.com/ethereumproject/ECIPs/blob/master/ECIPs/ECIP-1010.md

explosionBlock := new(big.Int).Add(config.ECIP1010PauseBlock, config.ECIP1010Length)
if next.Cmp(explosionBlock) < 0 {
exPeriodRef.Set(config.ECIP1010PauseBlock)
} else {
exPeriodRef.Sub(exPeriodRef, config.ECIP1010Length)
}

ecip1010Explosion(config, next, exPeriodRef)
}

// EXPLOSION
Expand All @@ -426,8 +406,8 @@ func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Heade
// 2^(( periodRef // EDP) - 2)
//
x := new(big.Int)
x.Div(exPeriodRef, ExpDiffPeriod) // (periodRef // EDP)
if x.Cmp(big1) > 0 { // if result large enough (not in algo explicitly)
x.Div(exPeriodRef, params.ExpDiffPeriod) // (periodRef // EDP)
if x.Cmp(big1) > 0 { // if result large enough (not in algo explicitly)
x.Sub(x, big2) // - 2
x.Exp(big2, x, nil) // 2^
} else {
Expand Down Expand Up @@ -590,65 +570,17 @@ func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header
blockReward = EIP1234FBlockReward
}
if config.IsSocial(header.Number) {
blockReward = SocialBlockReward
blockReward = params.SocialBlockReward
}
if config.IsEthersocial(header.Number) {
blockReward = EthersocialBlockReward
blockReward = params.EthersocialBlockReward
}
if config.IsMCIP0(header.Number) {
// Select the correct block reward based on chain progression
blockReward := Mcip0BlockReward
mcip3Reward := Mcip3BlockReward
mcip8Reward := Mcip8BlockReward
ubiReservoir := MusicoinUbiBlockReward
devReservoir := MusicoinDevBlockReward

reward := new(big.Int).Set(blockReward)

if config.IsMCIP8(header.Number) {
state.AddBalance(header.Coinbase, mcip8Reward)
state.AddBalance(common.HexToAddress("0x00eFdd5883eC628983E9063c7d969fE268BBf310"), ubiReservoir)
state.AddBalance(common.HexToAddress("0x00756cF8159095948496617F5FB17ED95059f536"), devReservoir)
blockReward := mcip8Reward
reward := new(big.Int).Set(blockReward)
_ = reward
} else if config.IsMCIP3(header.Number) {
state.AddBalance(header.Coinbase, mcip3Reward)
state.AddBalance(common.HexToAddress("0x00eFdd5883eC628983E9063c7d969fE268BBf310"), ubiReservoir)
state.AddBalance(common.HexToAddress("0x00756cF8159095948496617F5FB17ED95059f536"), devReservoir)
// no change to uncle reward during UBI fork, a mistake but now a legacy
} else {
state.AddBalance(header.Coinbase, reward)
}

// Accumulate the rewards for the miner and any included uncles
r := new(big.Int)
for _, uncle := range uncles {
r.Add(uncle.Number, big8)
r.Sub(r, header.Number)
r.Mul(r, blockReward)
r.Div(r, big8)
state.AddBalance(uncle.Coinbase, r)

r.Div(blockReward, big32)
reward.Add(reward, r)
}
musicoinBlockReward(config, state, header, uncles)
return
}
if config.HasECIP1017() {
// Ensure value 'era' is configured.
eraLen := config.ECIP1017EraRounds
era := GetBlockEra(header.Number, eraLen)
wr := GetBlockWinnerRewardByEra(era, blockReward) // wr "winner reward". 5, 4, 3.2, 2.56, ...
wurs := GetBlockWinnerRewardForUnclesByEra(era, uncles, blockReward) // wurs "winner uncle rewards"
wr.Add(wr, wurs)
state.AddBalance(header.Coinbase, wr) // $$

// Reward uncle miners.
for _, uncle := range uncles {
ur := GetBlockUncleRewardByEra(era, header, uncle, blockReward)
state.AddBalance(uncle.Coinbase, ur) // $$
}
ecip1017BlockReward(config, state, header, uncles)
} else {
// Accumulate the rewards for the miner and any included uncles
reward := new(big.Int).Set(blockReward)
Expand Down Expand Up @@ -712,28 +644,11 @@ func GetBlockWinnerRewardByEra(era *big.Int, blockReward *big.Int) *big.Int {
// qed
var q, d, r *big.Int = new(big.Int), new(big.Int), new(big.Int)

q.Exp(DisinflationRateQuotient, era, nil)
d.Exp(DisinflationRateDivisor, era, nil)
q.Exp(params.DisinflationRateQuotient, era, nil)
d.Exp(params.DisinflationRateDivisor, era, nil)

r.Mul(blockReward, q)
r.Div(r, d)

return r
}

// GetBlockEra gets which "Era" a given block is within, given an era length (ecip-1017 has era=5,000,000 blocks)
// Returns a zero-index era number, so "Era 1": 0, "Era 2": 1, "Era 3": 2 ...
func GetBlockEra(blockNum, eraLength *big.Int) *big.Int {
// If genesis block or impossible negative-numbered block, return zero-val.
if blockNum.Sign() < 1 {
return new(big.Int)
}

remainder := big.NewInt(0).Mod(big.NewInt(0).Sub(blockNum, big.NewInt(1)), eraLength)
base := big.NewInt(0).Sub(blockNum, remainder)

d := big.NewInt(0).Div(base, eraLength)
dremainder := big.NewInt(0).Mod(d, big.NewInt(1))

return new(big.Int).Sub(d, dremainder)
}
70 changes: 70 additions & 0 deletions consensus/ethash/consensus_classic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2019 The multi-geth Authors
// This file is part of the multi-geth library.
//
// The multi-geth library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The multi-geth library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the multi-geth library. If not, see <http://www.gnu.org/licenses/>.
package ethash

import (
"math/big"

"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
)

func ecip1017BlockReward(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
blockReward := FrontierBlockReward

// Ensure value 'era' is configured.
eraLen := config.ECIP1017EraRounds
era := GetBlockEra(header.Number, eraLen)
wr := GetBlockWinnerRewardByEra(era, blockReward) // wr "winner reward". 5, 4, 3.2, 2.56, ...
wurs := GetBlockWinnerRewardForUnclesByEra(era, uncles, blockReward) // wurs "winner uncle rewards"
wr.Add(wr, wurs)
state.AddBalance(header.Coinbase, wr) // $$

// Reward uncle miners.
for _, uncle := range uncles {
ur := GetBlockUncleRewardByEra(era, header, uncle, blockReward)
state.AddBalance(uncle.Coinbase, ur) // $$
}
}

func ecip1010Explosion(config *params.ChainConfig, next *big.Int, exPeriodRef *big.Int) {
// https://github.com/ethereumproject/ECIPs/blob/master/ECIPs/ECIP-1010.md

explosionBlock := new(big.Int).Add(config.ECIP1010PauseBlock, config.ECIP1010Length)
if next.Cmp(explosionBlock) < 0 {
exPeriodRef.Set(config.ECIP1010PauseBlock)
} else {
exPeriodRef.Sub(exPeriodRef, config.ECIP1010Length)
}
}

// GetBlockEra gets which "Era" a given block is within, given an era length (ecip-1017 has era=5,000,000 blocks)
// Returns a zero-index era number, so "Era 1": 0, "Era 2": 1, "Era 3": 2 ...
func GetBlockEra(blockNum, eraLength *big.Int) *big.Int {
// If genesis block or impossible negative-numbered block, return zero-val.
if blockNum.Sign() < 1 {
return new(big.Int)
}

remainder := big.NewInt(0).Mod(big.NewInt(0).Sub(blockNum, big.NewInt(1)), eraLength)
base := big.NewInt(0).Sub(blockNum, remainder)

d := big.NewInt(0).Div(base, eraLength)
dremainder := big.NewInt(0).Mod(d, big.NewInt(1))

return new(big.Int).Sub(d, dremainder)
}
65 changes: 65 additions & 0 deletions consensus/ethash/consensus_musicoin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2019 The multi-geth Authors
// This file is part of the multi-geth library.
//
// The multi-geth library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The multi-geth library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the multi-geth library. If not, see <http://www.gnu.org/licenses/>.
package ethash

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
)

func musicoinBlockReward(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
// Select the correct block reward based on chain progression
blockReward := params.Mcip0BlockReward
mcip3Reward := params.Mcip3BlockReward
mcip8Reward := params.Mcip8BlockReward
ubiReservoir := params.MusicoinUbiBlockReward
devReservoir := params.MusicoinDevBlockReward

reward := new(big.Int).Set(blockReward)

if config.IsMCIP8(header.Number) {
state.AddBalance(header.Coinbase, mcip8Reward)
state.AddBalance(common.HexToAddress("0x00eFdd5883eC628983E9063c7d969fE268BBf310"), ubiReservoir)
state.AddBalance(common.HexToAddress("0x00756cF8159095948496617F5FB17ED95059f536"), devReservoir)
blockReward := mcip8Reward
reward := new(big.Int).Set(blockReward)
_ = reward
} else if config.IsMCIP3(header.Number) {
state.AddBalance(header.Coinbase, mcip3Reward)
state.AddBalance(common.HexToAddress("0x00eFdd5883eC628983E9063c7d969fE268BBf310"), ubiReservoir)
state.AddBalance(common.HexToAddress("0x00756cF8159095948496617F5FB17ED95059f536"), devReservoir)
// no change to uncle reward during UBI fork, a mistake but now a legacy
} else {
state.AddBalance(header.Coinbase, reward)
}

// Accumulate the rewards for the miner and any included uncles
r := new(big.Int)
for _, uncle := range uncles {
r.Add(uncle.Number, big8)
r.Sub(r, header.Number)
r.Mul(r, blockReward)
r.Div(r, big8)
state.AddBalance(uncle.Coinbase, r)

r.Div(blockReward, big32)
reward.Add(reward, r)
}
}
78 changes: 4 additions & 74 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
return params.EthersocialChainConfig
case ghash == params.MusicoinGenesisHash:
return params.MusicoinChainConfig
case ghash == params.RinkebyGenesisHash:
return params.RinkebyChainConfig
case ghash == params.GoerliGenesisHash:
return params.GoerliChainConfig
case ghash == params.KottiGenesisHash:
return params.KottiChainConfig
default:
Expand Down Expand Up @@ -325,68 +329,6 @@ func DefaultGenesisBlock() *Genesis {
}
}

// ClassicGenesisBlock returns the Ethereum Classic genesis block.
func DefaultClassicGenesisBlock() *Genesis {
return &Genesis{
Config: params.ClassicChainConfig,
Nonce: 66,
ExtraData: hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"),
GasLimit: 5000,
Difficulty: big.NewInt(17179869184),
Alloc: decodePrealloc(mainnetAllocData),
}
}

// SocialGenesisBlock returns the Ethereum Social genesis block.
func DefaultSocialGenesisBlock() *Genesis {
return &Genesis{
Config: params.SocialChainConfig,
Nonce: 66,
ExtraData: hexutil.MustDecode("0x3230313820457468657265756d20536f6369616c2050726f6a656374"),
GasLimit: 5000,
Difficulty: big.NewInt(17179869184),
Alloc: decodePrealloc(socialAllocData),
}
}

// MixGenesisBlock returns the Mix genesis block.
func DefaultMixGenesisBlock() *Genesis {
return &Genesis{
Config: params.MixChainConfig,
Nonce: 0x1391eaa92b871f91,
ExtraData: hexutil.MustDecode("0x77656c636f6d65746f7468656c696e6b6564776f726c64000000000000000000"),
GasLimit: 3000000,
Difficulty: big.NewInt(1048576),
Alloc: decodePrealloc(mixAllocData),
}
}

// EthersocialGenesisBlock returns the Ethersocial main net genesis block.
func DefaultEthersocialGenesisBlock() *Genesis {
return &Genesis{
Config: params.EthersocialChainConfig,
Nonce: 66,
ExtraData: hexutil.MustDecode("0x"),
GasLimit: 3141592,
Difficulty: big.NewInt(131072),
Alloc: decodePrealloc(ethersocialAllocData),
}
}

// MusicoinGenesisBlock returns the Musicoin main net genesis block.
func DefaultMusicoinGenesisBlock() *Genesis {
return &Genesis{
Config: params.MusicoinChainConfig,
Timestamp: 0,
Nonce: 42,
ExtraData: nil,
Mixhash: common.HexToHash("0x00000000000000000000000000000000000000647572616c65787365646c6578"),
GasLimit: 8000000,
Difficulty: big.NewInt(4000000),
Alloc: decodePrealloc(musicoinAllocData),
}
}

// DefaultTestnetGenesisBlock returns the Ropsten network genesis block.
func DefaultTestnetGenesisBlock() *Genesis {
return &Genesis{
Expand All @@ -411,18 +353,6 @@ func DefaultRinkebyGenesisBlock() *Genesis {
}
}

// DefaultKottiGenesisBlock returns the Kotti network genesis block.
func DefaultKottiGenesisBlock() *Genesis {
return &Genesis{
Config: params.KottiChainConfig,
Timestamp: 1546461831,
ExtraData: hexutil.MustDecode("0x000000000000000000000000000000000000000000000000000000000000000025b7955e43adf9c2a01a9475908702cce67f302a6aaf8cba3c9255a2b863415d4db7bae4f4bbca020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
GasLimit: 10485760,
Difficulty: big.NewInt(1),
Alloc: decodePrealloc(kottiAllocData),
}
}

// DefaultGoerliGenesisBlock returns the Görli network genesis block.
func DefaultGoerliGenesisBlock() *Genesis {
return &Genesis{
Expand Down
5 changes: 0 additions & 5 deletions core/genesis_alloc.go

Large diffs are not rendered by default.

Loading

0 comments on commit 3d03b04

Please sign in to comment.