2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2018 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
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
|
|
|
|
}
|