Use new logic for Ethereum Social block reward

This commit is contained in:
Akira Takizawa 2018-04-09 12:27:11 +09:00
parent 0e434ff4e8
commit e7fb746750
No known key found for this signature in database
GPG key ID: 4BB42E31C79111B8
2 changed files with 49 additions and 19 deletions

View file

@ -657,9 +657,16 @@ func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Head
return nil return nil
} }
// default accumulateRewards()
var accumulateRewards func(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) = defaultAccumulateRewards
// Prepare implements consensus.Engine, initializing the difficulty field of a // Prepare implements consensus.Engine, initializing the difficulty field of a
// header to conform to the ethash protocol. The changes are done inline. // header to conform to the ethash protocol. The changes are done inline.
func (ethash *Ethash) Prepare(chain consensus.ChainReader, header *types.Header) error { func (ethash *Ethash) Prepare(chain consensus.ChainReader, header *types.Header) error {
if chain.GetHeaderByNumber(0).Hash() == params.SocialGenesisHash {
// setup accumulateRewards for Ethereum Social
accumulateRewards = socialAccumulateRewards
}
parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
if parent == nil { if parent == nil {
return consensus.ErrUnknownAncestor return consensus.ErrUnknownAncestor
@ -688,15 +695,50 @@ var (
// AccumulateRewards credits the coinbase of the given block with the mining // AccumulateRewards credits the coinbase of the given block with the mining
// reward. The total reward consists of the static block reward and rewards for // reward. The total reward consists of the static block reward and rewards for
// included uncles. The coinbase of each uncle block is also rewarded. // included uncles. The coinbase of each uncle block is also rewarded.
func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) { func defaultAccumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
// Select the correct block reward based on chain progression // Select the correct block reward based on chain progression
blockReward := FrontierBlockReward blockReward := FrontierBlockReward
if config.IsByzantium(header.Number) { if config.IsByzantium(header.Number) {
blockReward = ByzantiumBlockReward blockReward = ByzantiumBlockReward
} }
if config.IsSocial(header.Number) { if config.HasECIP1017() {
blockReward = SocialBlockReward // 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) // $$
}
} else {
// Accumulate the rewards for the miner and any included uncles
reward := new(big.Int).Set(blockReward)
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)
}
state.AddBalance(header.Coinbase, reward)
} }
}
// socialAccumulateRewards credits the coinbase of the given block with the mining
// reward. The total reward consists of the static block reward and rewards for
// included uncles. The coinbase of each uncle block is also rewarded.
func socialAccumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
// Select the correct block reward based on chain progression
blockReward := SocialBlockReward
if config.HasECIP1017() { if config.HasECIP1017() {
// Ensure value 'era' is configured. // Ensure value 'era' is configured.
eraLen := config.ECIP1017EraRounds eraLen := config.ECIP1017EraRounds

View file

@ -44,7 +44,6 @@ var (
EIP158Block: big.NewInt(2675000), EIP158Block: big.NewInt(2675000),
ByzantiumBlock: big.NewInt(4370000), ByzantiumBlock: big.NewInt(4370000),
DisposalBlock: nil, DisposalBlock: nil,
SocialBlock: nil,
ConstantinopleBlock: nil, ConstantinopleBlock: nil,
Ethash: new(EthashConfig), Ethash: new(EthashConfig),
} }
@ -61,7 +60,6 @@ var (
EIP158Block: nil, EIP158Block: nil,
ByzantiumBlock: nil, ByzantiumBlock: nil,
DisposalBlock: big.NewInt(0), DisposalBlock: big.NewInt(0),
SocialBlock: nil,
ConstantinopleBlock: nil, ConstantinopleBlock: nil,
ECIP1017EraRounds: big.NewInt(10000000), ECIP1017EraRounds: big.NewInt(10000000),
EIP160Block: big.NewInt(0), EIP160Block: big.NewInt(0),
@ -80,7 +78,6 @@ var (
EIP158Block: nil, EIP158Block: nil,
ByzantiumBlock: nil, ByzantiumBlock: nil,
DisposalBlock: big.NewInt(5900000), DisposalBlock: big.NewInt(5900000),
SocialBlock: nil,
ConstantinopleBlock: nil, ConstantinopleBlock: nil,
ECIP1017EraRounds: big.NewInt(5000000), ECIP1017EraRounds: big.NewInt(5000000),
EIP160Block: big.NewInt(3000000), EIP160Block: big.NewInt(3000000),
@ -101,7 +98,6 @@ var (
EIP158Block: nil, EIP158Block: nil,
ByzantiumBlock: nil, ByzantiumBlock: nil,
DisposalBlock: big.NewInt(0), DisposalBlock: big.NewInt(0),
SocialBlock: big.NewInt(0),
ConstantinopleBlock: nil, ConstantinopleBlock: nil,
ECIP1017EraRounds: big.NewInt(5000000), ECIP1017EraRounds: big.NewInt(5000000),
EIP160Block: big.NewInt(0), EIP160Block: big.NewInt(0),
@ -120,7 +116,6 @@ var (
EIP158Block: big.NewInt(10), EIP158Block: big.NewInt(10),
ByzantiumBlock: big.NewInt(1700000), ByzantiumBlock: big.NewInt(1700000),
DisposalBlock: nil, DisposalBlock: nil,
SocialBlock: nil,
ConstantinopleBlock: nil, ConstantinopleBlock: nil,
Ethash: new(EthashConfig), Ethash: new(EthashConfig),
} }
@ -137,7 +132,6 @@ var (
EIP158Block: big.NewInt(3), EIP158Block: big.NewInt(3),
ByzantiumBlock: big.NewInt(1035301), ByzantiumBlock: big.NewInt(1035301),
DisposalBlock: nil, DisposalBlock: nil,
SocialBlock: nil,
ConstantinopleBlock: nil, ConstantinopleBlock: nil,
Clique: &CliqueConfig{ Clique: &CliqueConfig{
Period: 15, Period: 15,
@ -150,16 +144,16 @@ var (
// //
// This configuration is intentionally not using keyed fields to force anyone // This configuration is intentionally not using keyed fields to force anyone
// adding flags to the config to also have to set these fields. // 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, 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, new(EthashConfig), nil}
// AllCliqueProtocolChanges contains every protocol change (EIPs) introduced // AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
// and accepted by the Ethereum core developers into the Clique consensus. // and accepted by the Ethereum core developers into the Clique consensus.
// //
// This configuration is intentionally not using keyed fields to force anyone // This configuration is intentionally not using keyed fields to force anyone
// adding flags to the config to also have to set these fields. // 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, 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, &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, 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, new(EthashConfig), nil}
TestRules = TestChainConfig.Rules(new(big.Int)) TestRules = TestChainConfig.Rules(new(big.Int))
) )
@ -183,7 +177,6 @@ type ChainConfig struct {
EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block
EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 HF block EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 HF block
DisposalBlock *big.Int `json:"disposalBlock,omitempty"` // Bomb disposal HF block DisposalBlock *big.Int `json:"disposalBlock,omitempty"` // Bomb disposal HF block
SocialBlock *big.Int `json:"socialBlock,omitempty"` // Ethereum Social Reward block
ByzantiumBlock *big.Int `json:"byzantiumBlock,omitempty"` // Byzantium switch block (nil = no fork, 0 = already on byzantium) ByzantiumBlock *big.Int `json:"byzantiumBlock,omitempty"` // Byzantium switch block (nil = no fork, 0 = already on byzantium)
ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated) ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated)
@ -229,7 +222,7 @@ func (c *ChainConfig) String() string {
default: default:
engine = "unknown" 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 ECIP1010PauseBlock: %v ECIP1010Length: %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 ECIP1017: %v EIP160: %v ECIP1010PauseBlock: %v ECIP1010Length: %v Constantinople: %v Engine: %v}",
c.ChainID, c.ChainID,
c.HomesteadBlock, c.HomesteadBlock,
c.DAOForkBlock, c.DAOForkBlock,
@ -239,7 +232,6 @@ func (c *ChainConfig) String() string {
c.EIP158Block, c.EIP158Block,
c.ByzantiumBlock, c.ByzantiumBlock,
c.DisposalBlock, c.DisposalBlock,
c.SocialBlock,
c.ECIP1017EraRounds, c.ECIP1017EraRounds,
c.EIP160Block, c.EIP160Block,
c.ECIP1010PauseBlock, c.ECIP1010PauseBlock,
@ -302,10 +294,6 @@ func (c *ChainConfig) IsBombDisposal(num *big.Int) bool {
return isForked(c.DisposalBlock, num) return isForked(c.DisposalBlock, num)
} }
func (c *ChainConfig) IsSocial(num *big.Int) bool {
return isForked(c.SocialBlock, num)
}
func (c *ChainConfig) IsECIP1010(num *big.Int) bool { func (c *ChainConfig) IsECIP1010(num *big.Int) bool {
return isForked(c.ECIP1010PauseBlock, num) return isForked(c.ECIP1010PauseBlock, num)
} }