2018-05-02 16:17:02 +00:00
|
|
|
package common
|
2018-01-05 17:55:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2018-02-08 16:12:08 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-01-05 17:55:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func CalcUnclesReward(block core.Block, uncles []*types.Header) float64 {
|
|
|
|
var unclesReward float64
|
|
|
|
for _, uncle := range uncles {
|
|
|
|
blockNumber := block.Number
|
|
|
|
staticBlockReward := float64(staticRewardByBlockNumber(blockNumber))
|
|
|
|
unclesReward += (1.0 + float64(uncle.Number.Int64()-block.Number)/8.0) * staticBlockReward
|
|
|
|
}
|
|
|
|
return unclesReward
|
|
|
|
}
|
|
|
|
|
|
|
|
func CalcBlockReward(block core.Block, uncles []*types.Header) float64 {
|
|
|
|
blockNumber := block.Number
|
|
|
|
staticBlockReward := staticRewardByBlockNumber(blockNumber)
|
|
|
|
transactionFees := calcTransactionFees(block)
|
|
|
|
uncleInclusionRewards := calcUncleInclusionRewards(block, uncles)
|
|
|
|
return transactionFees + uncleInclusionRewards + staticBlockReward
|
|
|
|
}
|
|
|
|
|
|
|
|
func calcTransactionFees(block core.Block) float64 {
|
|
|
|
var transactionFees float64
|
|
|
|
for _, transaction := range block.Transactions {
|
|
|
|
receipt := transaction.Receipt
|
2018-03-07 21:29:21 +00:00
|
|
|
transactionFees += float64(uint64(transaction.GasPrice) * receipt.GasUsed)
|
2018-01-05 17:55:00 +00:00
|
|
|
}
|
|
|
|
return transactionFees / params.Ether
|
|
|
|
}
|
|
|
|
|
|
|
|
func calcUncleInclusionRewards(block core.Block, uncles []*types.Header) float64 {
|
|
|
|
var uncleInclusionRewards float64
|
|
|
|
staticBlockReward := staticRewardByBlockNumber(block.Number)
|
|
|
|
for range uncles {
|
|
|
|
uncleInclusionRewards += staticBlockReward * 1 / 32
|
|
|
|
}
|
|
|
|
return uncleInclusionRewards
|
|
|
|
}
|
|
|
|
|
|
|
|
func staticRewardByBlockNumber(blockNumber int64) float64 {
|
|
|
|
var staticBlockReward float64
|
|
|
|
//https://blog.ethereum.org/2017/10/12/byzantium-hf-announcement/
|
|
|
|
if blockNumber >= 4370000 {
|
|
|
|
staticBlockReward = 3
|
|
|
|
} else {
|
|
|
|
staticBlockReward = 5
|
|
|
|
}
|
|
|
|
return staticBlockReward
|
|
|
|
}
|