Skip to content

Commit 3d03b04

Browse files
authored
Extraction of chain configuration (ethereum#92)
Extraction of chain configuration
2 parents 1bde114 + 109484a commit 3d03b04

37 files changed

+1096
-456
lines changed

consensus/ethash/consensus.go

Lines changed: 14 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,11 @@ import (
3838

3939
// Ethash proof-of-work protocol constants.
4040
var (
41-
FrontierBlockReward = big.NewInt(5e+18) // Block reward in wei for successfully mining a block
42-
EIP649FBlockReward = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Byzantium
43-
EIP1234FBlockReward = big.NewInt(2e+18) // Block reward in wei for successfully mining a block upward from Constantinople
44-
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
45-
EthersocialBlockReward = big.NewInt(5e+18) // Block reward in wei for successfully mining a block upward for Ethersocial Network
46-
maxUncles = 2 // Maximum number of uncles allowed in a single block
47-
allowedFutureBlockTime = 15 * time.Second // Max time from current time allowed for blocks, before they're considered future blocks
48-
DisinflationRateQuotient = big.NewInt(4) // Disinflation rate quotient for ECIP1017
49-
DisinflationRateDivisor = big.NewInt(5) // Disinflation rate divisor for ECIP1017
50-
ExpDiffPeriod = big.NewInt(100000) // Exponential diff period for diff bomb & ECIP1010
51-
52-
// Musicoin
53-
Mcip0BlockReward = new(big.Int).Mul(big.NewInt(314), big.NewInt(1e+18)) // In musicoin code as 'FrontierBlockReward'
54-
Mcip3BlockReward = new(big.Int).Mul(big.NewInt(250), big.NewInt(1e+18))
55-
Mcip8BlockReward = new(big.Int).Mul(big.NewInt(50), big.NewInt(1e+18))
56-
MusicoinUbiBlockReward = new(big.Int).Mul(big.NewInt(50), big.NewInt(1e+18))
57-
MusicoinDevBlockReward = new(big.Int).Mul(big.NewInt(14), big.NewInt(1e+18))
41+
FrontierBlockReward = big.NewInt(5e+18) // Block reward in wei for successfully mining a block
42+
EIP649FBlockReward = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Byzantium
43+
EIP1234FBlockReward = big.NewInt(2e+18) // Block reward in wei for successfully mining a block upward from Constantinople
44+
maxUncles = 2 // Maximum number of uncles allowed in a single block
45+
allowedFutureBlockTime = 15 * time.Second // Max time from current time allowed for blocks, before they're considered future blocks
5846
)
5947

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

409397
} else if config.IsECIP1010(next) {
410-
// https://github.com/ethereumproject/ECIPs/blob/master/ECIPs/ECIP-1010.md
411-
412-
explosionBlock := new(big.Int).Add(config.ECIP1010PauseBlock, config.ECIP1010Length)
413-
if next.Cmp(explosionBlock) < 0 {
414-
exPeriodRef.Set(config.ECIP1010PauseBlock)
415-
} else {
416-
exPeriodRef.Sub(exPeriodRef, config.ECIP1010Length)
417-
}
418-
398+
ecip1010Explosion(config, next, exPeriodRef)
419399
}
420400

421401
// EXPLOSION
@@ -426,8 +406,8 @@ func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Heade
426406
// 2^(( periodRef // EDP) - 2)
427407
//
428408
x := new(big.Int)
429-
x.Div(exPeriodRef, ExpDiffPeriod) // (periodRef // EDP)
430-
if x.Cmp(big1) > 0 { // if result large enough (not in algo explicitly)
409+
x.Div(exPeriodRef, params.ExpDiffPeriod) // (periodRef // EDP)
410+
if x.Cmp(big1) > 0 { // if result large enough (not in algo explicitly)
431411
x.Sub(x, big2) // - 2
432412
x.Exp(big2, x, nil) // 2^
433413
} else {
@@ -590,65 +570,17 @@ func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header
590570
blockReward = EIP1234FBlockReward
591571
}
592572
if config.IsSocial(header.Number) {
593-
blockReward = SocialBlockReward
573+
blockReward = params.SocialBlockReward
594574
}
595575
if config.IsEthersocial(header.Number) {
596-
blockReward = EthersocialBlockReward
576+
blockReward = params.EthersocialBlockReward
597577
}
598578
if config.IsMCIP0(header.Number) {
599-
// Select the correct block reward based on chain progression
600-
blockReward := Mcip0BlockReward
601-
mcip3Reward := Mcip3BlockReward
602-
mcip8Reward := Mcip8BlockReward
603-
ubiReservoir := MusicoinUbiBlockReward
604-
devReservoir := MusicoinDevBlockReward
605-
606-
reward := new(big.Int).Set(blockReward)
607-
608-
if config.IsMCIP8(header.Number) {
609-
state.AddBalance(header.Coinbase, mcip8Reward)
610-
state.AddBalance(common.HexToAddress("0x00eFdd5883eC628983E9063c7d969fE268BBf310"), ubiReservoir)
611-
state.AddBalance(common.HexToAddress("0x00756cF8159095948496617F5FB17ED95059f536"), devReservoir)
612-
blockReward := mcip8Reward
613-
reward := new(big.Int).Set(blockReward)
614-
_ = reward
615-
} else if config.IsMCIP3(header.Number) {
616-
state.AddBalance(header.Coinbase, mcip3Reward)
617-
state.AddBalance(common.HexToAddress("0x00eFdd5883eC628983E9063c7d969fE268BBf310"), ubiReservoir)
618-
state.AddBalance(common.HexToAddress("0x00756cF8159095948496617F5FB17ED95059f536"), devReservoir)
619-
// no change to uncle reward during UBI fork, a mistake but now a legacy
620-
} else {
621-
state.AddBalance(header.Coinbase, reward)
622-
}
623-
624-
// Accumulate the rewards for the miner and any included uncles
625-
r := new(big.Int)
626-
for _, uncle := range uncles {
627-
r.Add(uncle.Number, big8)
628-
r.Sub(r, header.Number)
629-
r.Mul(r, blockReward)
630-
r.Div(r, big8)
631-
state.AddBalance(uncle.Coinbase, r)
632-
633-
r.Div(blockReward, big32)
634-
reward.Add(reward, r)
635-
}
579+
musicoinBlockReward(config, state, header, uncles)
636580
return
637581
}
638582
if config.HasECIP1017() {
639-
// Ensure value 'era' is configured.
640-
eraLen := config.ECIP1017EraRounds
641-
era := GetBlockEra(header.Number, eraLen)
642-
wr := GetBlockWinnerRewardByEra(era, blockReward) // wr "winner reward". 5, 4, 3.2, 2.56, ...
643-
wurs := GetBlockWinnerRewardForUnclesByEra(era, uncles, blockReward) // wurs "winner uncle rewards"
644-
wr.Add(wr, wurs)
645-
state.AddBalance(header.Coinbase, wr) // $$
646-
647-
// Reward uncle miners.
648-
for _, uncle := range uncles {
649-
ur := GetBlockUncleRewardByEra(era, header, uncle, blockReward)
650-
state.AddBalance(uncle.Coinbase, ur) // $$
651-
}
583+
ecip1017BlockReward(config, state, header, uncles)
652584
} else {
653585
// Accumulate the rewards for the miner and any included uncles
654586
reward := new(big.Int).Set(blockReward)
@@ -712,28 +644,11 @@ func GetBlockWinnerRewardByEra(era *big.Int, blockReward *big.Int) *big.Int {
712644
// qed
713645
var q, d, r *big.Int = new(big.Int), new(big.Int), new(big.Int)
714646

715-
q.Exp(DisinflationRateQuotient, era, nil)
716-
d.Exp(DisinflationRateDivisor, era, nil)
647+
q.Exp(params.DisinflationRateQuotient, era, nil)
648+
d.Exp(params.DisinflationRateDivisor, era, nil)
717649

718650
r.Mul(blockReward, q)
719651
r.Div(r, d)
720652

721653
return r
722654
}
723-
724-
// GetBlockEra gets which "Era" a given block is within, given an era length (ecip-1017 has era=5,000,000 blocks)
725-
// Returns a zero-index era number, so "Era 1": 0, "Era 2": 1, "Era 3": 2 ...
726-
func GetBlockEra(blockNum, eraLength *big.Int) *big.Int {
727-
// If genesis block or impossible negative-numbered block, return zero-val.
728-
if blockNum.Sign() < 1 {
729-
return new(big.Int)
730-
}
731-
732-
remainder := big.NewInt(0).Mod(big.NewInt(0).Sub(blockNum, big.NewInt(1)), eraLength)
733-
base := big.NewInt(0).Sub(blockNum, remainder)
734-
735-
d := big.NewInt(0).Div(base, eraLength)
736-
dremainder := big.NewInt(0).Mod(d, big.NewInt(1))
737-
738-
return new(big.Int).Sub(d, dremainder)
739-
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2019 The multi-geth Authors
2+
// This file is part of the multi-geth library.
3+
//
4+
// The multi-geth library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The multi-geth library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the multi-geth library. If not, see <http://www.gnu.org/licenses/>.
16+
package ethash
17+
18+
import (
19+
"math/big"
20+
21+
"github.com/ethereum/go-ethereum/core/state"
22+
"github.com/ethereum/go-ethereum/core/types"
23+
"github.com/ethereum/go-ethereum/params"
24+
)
25+
26+
func ecip1017BlockReward(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
27+
blockReward := FrontierBlockReward
28+
29+
// Ensure value 'era' is configured.
30+
eraLen := config.ECIP1017EraRounds
31+
era := GetBlockEra(header.Number, eraLen)
32+
wr := GetBlockWinnerRewardByEra(era, blockReward) // wr "winner reward". 5, 4, 3.2, 2.56, ...
33+
wurs := GetBlockWinnerRewardForUnclesByEra(era, uncles, blockReward) // wurs "winner uncle rewards"
34+
wr.Add(wr, wurs)
35+
state.AddBalance(header.Coinbase, wr) // $$
36+
37+
// Reward uncle miners.
38+
for _, uncle := range uncles {
39+
ur := GetBlockUncleRewardByEra(era, header, uncle, blockReward)
40+
state.AddBalance(uncle.Coinbase, ur) // $$
41+
}
42+
}
43+
44+
func ecip1010Explosion(config *params.ChainConfig, next *big.Int, exPeriodRef *big.Int) {
45+
// https://github.com/ethereumproject/ECIPs/blob/master/ECIPs/ECIP-1010.md
46+
47+
explosionBlock := new(big.Int).Add(config.ECIP1010PauseBlock, config.ECIP1010Length)
48+
if next.Cmp(explosionBlock) < 0 {
49+
exPeriodRef.Set(config.ECIP1010PauseBlock)
50+
} else {
51+
exPeriodRef.Sub(exPeriodRef, config.ECIP1010Length)
52+
}
53+
}
54+
55+
// GetBlockEra gets which "Era" a given block is within, given an era length (ecip-1017 has era=5,000,000 blocks)
56+
// Returns a zero-index era number, so "Era 1": 0, "Era 2": 1, "Era 3": 2 ...
57+
func GetBlockEra(blockNum, eraLength *big.Int) *big.Int {
58+
// If genesis block or impossible negative-numbered block, return zero-val.
59+
if blockNum.Sign() < 1 {
60+
return new(big.Int)
61+
}
62+
63+
remainder := big.NewInt(0).Mod(big.NewInt(0).Sub(blockNum, big.NewInt(1)), eraLength)
64+
base := big.NewInt(0).Sub(blockNum, remainder)
65+
66+
d := big.NewInt(0).Div(base, eraLength)
67+
dremainder := big.NewInt(0).Mod(d, big.NewInt(1))
68+
69+
return new(big.Int).Sub(d, dremainder)
70+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2019 The multi-geth Authors
2+
// This file is part of the multi-geth library.
3+
//
4+
// The multi-geth library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The multi-geth library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the multi-geth library. If not, see <http://www.gnu.org/licenses/>.
16+
package ethash
17+
18+
import (
19+
"math/big"
20+
21+
"github.com/ethereum/go-ethereum/common"
22+
"github.com/ethereum/go-ethereum/core/state"
23+
"github.com/ethereum/go-ethereum/core/types"
24+
"github.com/ethereum/go-ethereum/params"
25+
)
26+
27+
func musicoinBlockReward(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
28+
// Select the correct block reward based on chain progression
29+
blockReward := params.Mcip0BlockReward
30+
mcip3Reward := params.Mcip3BlockReward
31+
mcip8Reward := params.Mcip8BlockReward
32+
ubiReservoir := params.MusicoinUbiBlockReward
33+
devReservoir := params.MusicoinDevBlockReward
34+
35+
reward := new(big.Int).Set(blockReward)
36+
37+
if config.IsMCIP8(header.Number) {
38+
state.AddBalance(header.Coinbase, mcip8Reward)
39+
state.AddBalance(common.HexToAddress("0x00eFdd5883eC628983E9063c7d969fE268BBf310"), ubiReservoir)
40+
state.AddBalance(common.HexToAddress("0x00756cF8159095948496617F5FB17ED95059f536"), devReservoir)
41+
blockReward := mcip8Reward
42+
reward := new(big.Int).Set(blockReward)
43+
_ = reward
44+
} else if config.IsMCIP3(header.Number) {
45+
state.AddBalance(header.Coinbase, mcip3Reward)
46+
state.AddBalance(common.HexToAddress("0x00eFdd5883eC628983E9063c7d969fE268BBf310"), ubiReservoir)
47+
state.AddBalance(common.HexToAddress("0x00756cF8159095948496617F5FB17ED95059f536"), devReservoir)
48+
// no change to uncle reward during UBI fork, a mistake but now a legacy
49+
} else {
50+
state.AddBalance(header.Coinbase, reward)
51+
}
52+
53+
// Accumulate the rewards for the miner and any included uncles
54+
r := new(big.Int)
55+
for _, uncle := range uncles {
56+
r.Add(uncle.Number, big8)
57+
r.Sub(r, header.Number)
58+
r.Mul(r, blockReward)
59+
r.Div(r, big8)
60+
state.AddBalance(uncle.Coinbase, r)
61+
62+
r.Div(blockReward, big32)
63+
reward.Add(reward, r)
64+
}
65+
}

core/genesis.go

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
227227
return params.EthersocialChainConfig
228228
case ghash == params.MusicoinGenesisHash:
229229
return params.MusicoinChainConfig
230+
case ghash == params.RinkebyGenesisHash:
231+
return params.RinkebyChainConfig
232+
case ghash == params.GoerliGenesisHash:
233+
return params.GoerliChainConfig
230234
case ghash == params.KottiGenesisHash:
231235
return params.KottiChainConfig
232236
default:
@@ -325,68 +329,6 @@ func DefaultGenesisBlock() *Genesis {
325329
}
326330
}
327331

328-
// ClassicGenesisBlock returns the Ethereum Classic genesis block.
329-
func DefaultClassicGenesisBlock() *Genesis {
330-
return &Genesis{
331-
Config: params.ClassicChainConfig,
332-
Nonce: 66,
333-
ExtraData: hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"),
334-
GasLimit: 5000,
335-
Difficulty: big.NewInt(17179869184),
336-
Alloc: decodePrealloc(mainnetAllocData),
337-
}
338-
}
339-
340-
// SocialGenesisBlock returns the Ethereum Social genesis block.
341-
func DefaultSocialGenesisBlock() *Genesis {
342-
return &Genesis{
343-
Config: params.SocialChainConfig,
344-
Nonce: 66,
345-
ExtraData: hexutil.MustDecode("0x3230313820457468657265756d20536f6369616c2050726f6a656374"),
346-
GasLimit: 5000,
347-
Difficulty: big.NewInt(17179869184),
348-
Alloc: decodePrealloc(socialAllocData),
349-
}
350-
}
351-
352-
// MixGenesisBlock returns the Mix genesis block.
353-
func DefaultMixGenesisBlock() *Genesis {
354-
return &Genesis{
355-
Config: params.MixChainConfig,
356-
Nonce: 0x1391eaa92b871f91,
357-
ExtraData: hexutil.MustDecode("0x77656c636f6d65746f7468656c696e6b6564776f726c64000000000000000000"),
358-
GasLimit: 3000000,
359-
Difficulty: big.NewInt(1048576),
360-
Alloc: decodePrealloc(mixAllocData),
361-
}
362-
}
363-
364-
// EthersocialGenesisBlock returns the Ethersocial main net genesis block.
365-
func DefaultEthersocialGenesisBlock() *Genesis {
366-
return &Genesis{
367-
Config: params.EthersocialChainConfig,
368-
Nonce: 66,
369-
ExtraData: hexutil.MustDecode("0x"),
370-
GasLimit: 3141592,
371-
Difficulty: big.NewInt(131072),
372-
Alloc: decodePrealloc(ethersocialAllocData),
373-
}
374-
}
375-
376-
// MusicoinGenesisBlock returns the Musicoin main net genesis block.
377-
func DefaultMusicoinGenesisBlock() *Genesis {
378-
return &Genesis{
379-
Config: params.MusicoinChainConfig,
380-
Timestamp: 0,
381-
Nonce: 42,
382-
ExtraData: nil,
383-
Mixhash: common.HexToHash("0x00000000000000000000000000000000000000647572616c65787365646c6578"),
384-
GasLimit: 8000000,
385-
Difficulty: big.NewInt(4000000),
386-
Alloc: decodePrealloc(musicoinAllocData),
387-
}
388-
}
389-
390332
// DefaultTestnetGenesisBlock returns the Ropsten network genesis block.
391333
func DefaultTestnetGenesisBlock() *Genesis {
392334
return &Genesis{
@@ -411,18 +353,6 @@ func DefaultRinkebyGenesisBlock() *Genesis {
411353
}
412354
}
413355

414-
// DefaultKottiGenesisBlock returns the Kotti network genesis block.
415-
func DefaultKottiGenesisBlock() *Genesis {
416-
return &Genesis{
417-
Config: params.KottiChainConfig,
418-
Timestamp: 1546461831,
419-
ExtraData: hexutil.MustDecode("0x000000000000000000000000000000000000000000000000000000000000000025b7955e43adf9c2a01a9475908702cce67f302a6aaf8cba3c9255a2b863415d4db7bae4f4bbca020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
420-
GasLimit: 10485760,
421-
Difficulty: big.NewInt(1),
422-
Alloc: decodePrealloc(kottiAllocData),
423-
}
424-
}
425-
426356
// DefaultGoerliGenesisBlock returns the Görli network genesis block.
427357
func DefaultGoerliGenesisBlock() *Genesis {
428358
return &Genesis{

core/genesis_alloc.go

Lines changed: 0 additions & 5 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)