From e7fb74675096ce457c05ce0205e91c39b558aa1b Mon Sep 17 00:00:00 2001 From: Akira Takizawa Date: Mon, 9 Apr 2018 12:27:11 +0900 Subject: [PATCH] Use new logic for Ethereum Social block reward --- consensus/ethash/consensus.go | 48 ++++++++++++++++++++++++++++++++--- params/config.go | 20 +++------------ 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index f09c949c1f..1f4e97025a 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -657,9 +657,16 @@ func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Head 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 // header to conform to the ethash protocol. The changes are done inline. 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) if parent == nil { return consensus.ErrUnknownAncestor @@ -688,15 +695,50 @@ var ( // AccumulateRewards 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 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 blockReward := FrontierBlockReward if config.IsByzantium(header.Number) { blockReward = ByzantiumBlockReward } - if config.IsSocial(header.Number) { - blockReward = SocialBlockReward + 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) // $$ + } + } 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() { // Ensure value 'era' is configured. eraLen := config.ECIP1017EraRounds diff --git a/params/config.go b/params/config.go index ba796f41a5..f109cd9ba6 100644 --- a/params/config.go +++ b/params/config.go @@ -44,7 +44,6 @@ var ( EIP158Block: big.NewInt(2675000), ByzantiumBlock: big.NewInt(4370000), DisposalBlock: nil, - SocialBlock: nil, ConstantinopleBlock: nil, Ethash: new(EthashConfig), } @@ -61,7 +60,6 @@ var ( EIP158Block: nil, ByzantiumBlock: nil, DisposalBlock: big.NewInt(0), - SocialBlock: nil, ConstantinopleBlock: nil, ECIP1017EraRounds: big.NewInt(10000000), EIP160Block: big.NewInt(0), @@ -80,7 +78,6 @@ var ( EIP158Block: nil, ByzantiumBlock: nil, DisposalBlock: big.NewInt(5900000), - SocialBlock: nil, ConstantinopleBlock: nil, ECIP1017EraRounds: big.NewInt(5000000), EIP160Block: big.NewInt(3000000), @@ -101,7 +98,6 @@ var ( EIP158Block: nil, ByzantiumBlock: nil, DisposalBlock: big.NewInt(0), - SocialBlock: big.NewInt(0), ConstantinopleBlock: nil, ECIP1017EraRounds: big.NewInt(5000000), EIP160Block: big.NewInt(0), @@ -120,7 +116,6 @@ var ( EIP158Block: big.NewInt(10), ByzantiumBlock: big.NewInt(1700000), DisposalBlock: nil, - SocialBlock: nil, ConstantinopleBlock: nil, Ethash: new(EthashConfig), } @@ -137,7 +132,6 @@ var ( EIP158Block: big.NewInt(3), ByzantiumBlock: big.NewInt(1035301), DisposalBlock: nil, - SocialBlock: nil, ConstantinopleBlock: nil, Clique: &CliqueConfig{ Period: 15, @@ -150,16 +144,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, 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 // 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, 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)) ) @@ -183,7 +177,6 @@ type ChainConfig struct { EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 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) 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: 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.HomesteadBlock, c.DAOForkBlock, @@ -239,7 +232,6 @@ func (c *ChainConfig) String() string { c.EIP158Block, c.ByzantiumBlock, c.DisposalBlock, - c.SocialBlock, c.ECIP1017EraRounds, c.EIP160Block, c.ECIP1010PauseBlock, @@ -302,10 +294,6 @@ func (c *ChainConfig) IsBombDisposal(num *big.Int) bool { 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 { return isForked(c.ECIP1010PauseBlock, num) }