Skip to content

Commit

Permalink
Merge pull request #124 from getamis/edwin/update-reward-calculating
Browse files Browse the repository at this point in the history
common: update the miner reward calculating
  • Loading branch information
bailantaotao authored Jan 4, 2019
2 parents f30bdfa + 334631d commit 3ddf731
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
3 changes: 3 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ func AccumulateRewards(header *types.Header, uncles []*types.Header) (minerBaseR
if params.MainnetChainConfig.ByzantiumBlock.Cmp(header.Number) <= 0 {
minerBaseReward = ethash.ByzantiumBlockReward
}
if params.MainnetChainConfig.ConstantinopleBlock.Cmp(header.Number) <= 0 {
minerBaseReward = ethash.ConstantinopleBlockReward
}

// Accumulate the rewards for the miner and any included uncles
r := new(big.Int)
Expand Down
56 changes: 40 additions & 16 deletions common/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,73 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/assert"
)

func TestAccumulateRewards(t *testing.T) {
blockNum := big.NewInt(5862127)
minerBaseReward := ethash.FrontierBlockReward
if params.MainnetChainConfig.ByzantiumBlock.Cmp(blockNum) <= 0 {
minerBaseReward = ethash.ByzantiumBlockReward
}
header := &types.Header{Number: blockNum}

byzantiumBlock := big.NewInt(5862127)
constantinopleBlock := big.NewInt(7162127)
tests := []struct {
description string
uncleHeaders []*types.Header
blockNum *big.Int
uncleInclusionReward *big.Int
minerBaseReward *big.Int
unclesReward []*big.Int
}{
{
description: "no uncles",
description: "no uncles on byzantium",
uncleHeaders: []*types.Header{},
blockNum: byzantiumBlock,
uncleInclusionReward: big.NewInt(0),
minerBaseReward: minerBaseReward,
minerBaseReward: ethash.ByzantiumBlockReward,
unclesReward: []*big.Int{},
},
{
description: "two uncles in same block number",
uncleHeaders: []*types.Header{{Number: big.NewInt(blockNum.Int64() - 1), Coinbase: common.HexToAddress("uncle1")}, {Number: big.NewInt(blockNum.Int64() - 1), Coinbase: common.HexToAddress("uncle2")}},
description: "two uncles in same block number on byzantium",
uncleHeaders: []*types.Header{{Number: big.NewInt(byzantiumBlock.Int64() - 1), Coinbase: common.HexToAddress("uncle1")}, {Number: big.NewInt(byzantiumBlock.Int64() - 1), Coinbase: common.HexToAddress("uncle2")}},
blockNum: byzantiumBlock,
uncleInclusionReward: big.NewInt(187500000000000000),
minerBaseReward: minerBaseReward,
minerBaseReward: ethash.ByzantiumBlockReward,
unclesReward: []*big.Int{big.NewInt(2625000000000000000), big.NewInt(2625000000000000000)},
},
{
description: "two uncles in different block number",
uncleHeaders: []*types.Header{{Number: big.NewInt(blockNum.Int64() - 1), Coinbase: common.HexToAddress("uncle1")}, {Number: big.NewInt(blockNum.Int64() - 2), Coinbase: common.HexToAddress("uncle2")}},
description: "two uncles in different block number on byzantium",
uncleHeaders: []*types.Header{{Number: big.NewInt(byzantiumBlock.Int64() - 1), Coinbase: common.HexToAddress("uncle1")}, {Number: big.NewInt(byzantiumBlock.Int64() - 2), Coinbase: common.HexToAddress("uncle2")}},
blockNum: byzantiumBlock,
uncleInclusionReward: big.NewInt(187500000000000000),
minerBaseReward: minerBaseReward,
minerBaseReward: ethash.ByzantiumBlockReward,
unclesReward: []*big.Int{big.NewInt(2625000000000000000), big.NewInt(2250000000000000000)},
},

{
description: "no uncles on constantinople",
uncleHeaders: []*types.Header{},
blockNum: constantinopleBlock,
uncleInclusionReward: big.NewInt(0),
minerBaseReward: ethash.ConstantinopleBlockReward,
unclesReward: []*big.Int{},
},
{
description: "two uncles in same block number on constantinople",
uncleHeaders: []*types.Header{{Number: big.NewInt(constantinopleBlock.Int64() - 1), Coinbase: common.HexToAddress("uncle1")}, {Number: big.NewInt(constantinopleBlock.Int64() - 1), Coinbase: common.HexToAddress("uncle2")}},
blockNum: constantinopleBlock,
uncleInclusionReward: big.NewInt(125000000000000000),
minerBaseReward: ethash.ConstantinopleBlockReward,
unclesReward: []*big.Int{big.NewInt(1750000000000000000), big.NewInt(1750000000000000000)},
},
{
description: "two uncles in different block number on constantinople",
uncleHeaders: []*types.Header{{Number: big.NewInt(constantinopleBlock.Int64() - 1), Coinbase: common.HexToAddress("uncle1")}, {Number: big.NewInt(constantinopleBlock.Int64() - 2), Coinbase: common.HexToAddress("uncle2")}},
blockNum: constantinopleBlock,
uncleInclusionReward: big.NewInt(125000000000000000),
minerBaseReward: ethash.ConstantinopleBlockReward,
unclesReward: []*big.Int{big.NewInt(1750000000000000000), big.NewInt(1500000000000000000)},
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
header := &types.Header{Number: tt.blockNum}
minerBaseReward, uncleInclusionReward, unclesCoinbase, unclesReward, _ := AccumulateRewards(header, tt.uncleHeaders)

assert.Equal(t, tt.minerBaseReward, minerBaseReward)
Expand Down

0 comments on commit 3ddf731

Please sign in to comment.