going ahead and indexing the entire uncle blocks (one of the issues open on public); finish tests
This commit is contained in:
@@ -17,12 +17,13 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
"strings"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/libraries/shared/utilities"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
)
|
||||
|
||||
@@ -56,8 +57,41 @@ func (bc BlockConverter) ToCoreBlock(gethBlock *types.Block) (core.Block, error)
|
||||
UncleHash: gethBlock.UncleHash().Hex(),
|
||||
}
|
||||
coreBlock.Reward = CalcBlockReward(coreBlock, gethBlock.Uncles()).String()
|
||||
uncleRewards, mappedUncleRewards := CalcUnclesReward(coreBlock, gethBlock.Uncles())
|
||||
coreBlock.UnclesReward = utilities.NullToZero(uncleRewards.String())
|
||||
coreBlock.MappedUncleRewards = mappedUncleRewards
|
||||
totalUncleReward, uncles := bc.ToCoreUncle(coreBlock, gethBlock.Uncles())
|
||||
|
||||
coreBlock.UnclesReward = totalUncleReward.String()
|
||||
coreBlock.Uncles = uncles
|
||||
return coreBlock, nil
|
||||
}
|
||||
|
||||
// Rewards for the miners of uncles is calculated as (U_n + 8 - B_n) * R / 8
|
||||
// Where U_n is the uncle block number, B_n is the parent block number and R is the static block reward at B_n
|
||||
// https://github.com/ethereum/go-ethereum/issues/1591
|
||||
// https://ethereum.stackexchange.com/questions/27172/different-uncles-reward
|
||||
// https://github.com/ethereum/homestead-guide/issues/399
|
||||
// Returns the total uncle reward and the individual processed uncles
|
||||
func (bc BlockConverter) ToCoreUncle(block core.Block, uncles []*types.Header) (*big.Int, []core.Uncle) {
|
||||
totalUncleRewards := new(big.Int)
|
||||
coreUncles := make([]core.Uncle, 0, len(uncles))
|
||||
for _, uncle := range uncles {
|
||||
staticBlockReward := staticRewardByBlockNumber(block.Number)
|
||||
rewardDiv8 := staticBlockReward.Div(staticBlockReward, big.NewInt(8))
|
||||
mainBlock := big.NewInt(block.Number)
|
||||
uncleBlock := big.NewInt(uncle.Number.Int64())
|
||||
uncleBlockPlus8 := uncleBlock.Add(uncleBlock, big.NewInt(8))
|
||||
uncleBlockPlus8MinusMainBlock := uncleBlockPlus8.Sub(uncleBlockPlus8, mainBlock)
|
||||
thisUncleReward := rewardDiv8.Mul(rewardDiv8, uncleBlockPlus8MinusMainBlock)
|
||||
raw, _ := json.Marshal(uncle)
|
||||
coreUncle := core.Uncle{
|
||||
Miner: uncle.Coinbase.Hex(),
|
||||
BlockHash: block.Hash,
|
||||
Hash: uncle.Hash().Hex(),
|
||||
Raw: raw,
|
||||
Reward: thisUncleReward.String(),
|
||||
Timestamp: uncle.Time.String(),
|
||||
}
|
||||
coreUncles = append(coreUncles, coreUncle)
|
||||
totalUncleRewards.Add(totalUncleRewards, thisUncleReward)
|
||||
}
|
||||
return totalUncleRewards, coreUncles
|
||||
}
|
||||
|
||||
@@ -159,15 +159,16 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
|
||||
expectedTotalReward := new(big.Int)
|
||||
expectedTotalReward.SetString("6875000000000000000", 10)
|
||||
totalReward, mappedRewards := vulcCommon.CalcUnclesReward(coreBlock, block.Uncles())
|
||||
totalReward, coreUncles := blockConverter.ToCoreUncle(coreBlock, block.Uncles())
|
||||
Expect(totalReward.String()).To(Equal(expectedTotalReward.String()))
|
||||
|
||||
Expect(len(mappedRewards)).To(Equal(1))
|
||||
Expect(len(mappedRewards["0x0000000000000000000000000000000000000000"])).To(Equal(2))
|
||||
Expect(mappedRewards["0x0000000000000000000000000000000000000000"]["0xb629de4014b6e30cf9555ee833f1806fa0d8b8516fde194405f9c98c2deb8772"].String()).
|
||||
To(Equal(big.NewInt(3125000000000000000).String()))
|
||||
Expect(mappedRewards["0x0000000000000000000000000000000000000000"]["0x673f5231e4888a951e0bc8a25b5774b982e6e9e258362c21affaff6e02dd5a2b"].String()).
|
||||
To(Equal(big.NewInt(3750000000000000000).String()))
|
||||
Expect(len(coreUncles)).To(Equal(2))
|
||||
Expect(coreUncles[0].Reward).To(Equal("3125000000000000000"))
|
||||
Expect(coreUncles[0].Miner).To(Equal("0x0000000000000000000000000000000000000000"))
|
||||
Expect(coreUncles[0].Hash).To(Equal("0xb629de4014b6e30cf9555ee833f1806fa0d8b8516fde194405f9c98c2deb8772"))
|
||||
Expect(coreUncles[1].Reward).To(Equal("3750000000000000000"))
|
||||
Expect(coreUncles[1].Miner).To(Equal("0x0000000000000000000000000000000000000000"))
|
||||
Expect(coreUncles[1].Hash).To(Equal("0x673f5231e4888a951e0bc8a25b5774b982e6e9e258362c21affaff6e02dd5a2b"))
|
||||
})
|
||||
|
||||
It("decreases the static block reward from 5 to 3 for blocks after block 4,269,999", func() {
|
||||
|
||||
@@ -23,34 +23,6 @@ import (
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
)
|
||||
|
||||
// (U_n + 8 - B_n) * R / 8
|
||||
// https://github.com/ethereum/go-ethereum/issues/1591
|
||||
// https://ethereum.stackexchange.com/questions/27172/different-uncles-reward
|
||||
// https://github.com/ethereum/homestead-guide/issues/399
|
||||
// Returns a map of miner addresses to a map of the uncles they mined (hashes) to the rewards received for that uncle
|
||||
func CalcUnclesReward(block core.Block, uncles []*types.Header) (*big.Int, map[string]map[string]*big.Int) {
|
||||
uncleRewards := new(big.Int)
|
||||
mappedUncleRewards := make(map[string]map[string]*big.Int)
|
||||
for _, uncle := range uncles {
|
||||
staticBlockReward := staticRewardByBlockNumber(block.Number)
|
||||
rewardDiv8 := staticBlockReward.Div(staticBlockReward, big.NewInt(8))
|
||||
uncleBlock := big.NewInt(uncle.Number.Int64())
|
||||
uncleBlockPlus8 := uncleBlock.Add(uncleBlock, big.NewInt(8))
|
||||
mainBlock := big.NewInt(block.Number)
|
||||
uncleBlockPlus8MinusMainBlock := uncleBlockPlus8.Sub(uncleBlockPlus8, mainBlock)
|
||||
thisUncleReward := rewardDiv8.Mul(rewardDiv8, uncleBlockPlus8MinusMainBlock)
|
||||
uncleRewards = uncleRewards.Add(uncleRewards, thisUncleReward)
|
||||
if mappedUncleRewards[uncle.Coinbase.Hex()] == nil {
|
||||
mappedUncleRewards[uncle.Coinbase.Hex()] = make(map[string]*big.Int)
|
||||
}
|
||||
if mappedUncleRewards[uncle.Coinbase.Hex()][uncle.Hash().Hex()] == nil {
|
||||
mappedUncleRewards[uncle.Coinbase.Hex()][uncle.Hash().Hex()] = new(big.Int)
|
||||
}
|
||||
mappedUncleRewards[uncle.Coinbase.Hex()][uncle.Hash().Hex()].Add(mappedUncleRewards[uncle.Coinbase.Hex()][uncle.Hash().Hex()], thisUncleReward)
|
||||
}
|
||||
return uncleRewards, mappedUncleRewards
|
||||
}
|
||||
|
||||
func CalcBlockReward(block core.Block, uncles []*types.Header) *big.Int {
|
||||
staticBlockReward := staticRewardByBlockNumber(block.Number)
|
||||
transactionFees := calcTransactionFees(block)
|
||||
|
||||
Reference in New Issue
Block a user