2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// 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 (
|
2019-03-21 19:52:24 +00:00
|
|
|
"math/big"
|
|
|
|
|
2018-01-05 17:55:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-02-08 16:12:08 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-01-05 17:55:00 +00:00
|
|
|
)
|
|
|
|
|
2019-03-21 19:52:24 +00:00
|
|
|
func CalcBlockReward(block core.Block, uncles []*types.Header) *big.Int {
|
|
|
|
staticBlockReward := staticRewardByBlockNumber(block.Number)
|
2018-01-05 17:55:00 +00:00
|
|
|
transactionFees := calcTransactionFees(block)
|
|
|
|
uncleInclusionRewards := calcUncleInclusionRewards(block, uncles)
|
2019-03-21 19:52:24 +00:00
|
|
|
tmp := transactionFees.Add(transactionFees, uncleInclusionRewards)
|
|
|
|
return tmp.Add(tmp, staticBlockReward)
|
2018-01-05 17:55:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 19:52:24 +00:00
|
|
|
func calcTransactionFees(block core.Block) *big.Int {
|
|
|
|
transactionFees := new(big.Int)
|
2018-01-05 17:55:00 +00:00
|
|
|
for _, transaction := range block.Transactions {
|
|
|
|
receipt := transaction.Receipt
|
2019-03-21 19:52:24 +00:00
|
|
|
gasPrice := big.NewInt(transaction.GasPrice)
|
|
|
|
gasUsed := big.NewInt(int64(receipt.GasUsed))
|
|
|
|
transactionFee := gasPrice.Mul(gasPrice, gasUsed)
|
|
|
|
transactionFees = transactionFees.Add(transactionFees, transactionFee)
|
2018-01-05 17:55:00 +00:00
|
|
|
}
|
2019-03-21 19:52:24 +00:00
|
|
|
return transactionFees
|
2018-01-05 17:55:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 19:52:24 +00:00
|
|
|
func calcUncleInclusionRewards(block core.Block, uncles []*types.Header) *big.Int {
|
|
|
|
uncleInclusionRewards := new(big.Int)
|
2018-01-05 17:55:00 +00:00
|
|
|
for range uncles {
|
2019-03-21 19:52:24 +00:00
|
|
|
staticBlockReward := staticRewardByBlockNumber(block.Number)
|
|
|
|
staticBlockReward.Div(staticBlockReward, big.NewInt(32))
|
|
|
|
uncleInclusionRewards.Add(uncleInclusionRewards, staticBlockReward)
|
2018-01-05 17:55:00 +00:00
|
|
|
}
|
|
|
|
return uncleInclusionRewards
|
|
|
|
}
|
|
|
|
|
2019-03-21 19:52:24 +00:00
|
|
|
func staticRewardByBlockNumber(blockNumber int64) *big.Int {
|
|
|
|
staticBlockReward := new(big.Int)
|
2018-01-05 17:55:00 +00:00
|
|
|
//https://blog.ethereum.org/2017/10/12/byzantium-hf-announcement/
|
|
|
|
if blockNumber >= 4370000 {
|
2019-03-21 19:52:24 +00:00
|
|
|
staticBlockReward.SetString("3000000000000000000", 10)
|
2018-01-05 17:55:00 +00:00
|
|
|
} else {
|
2019-03-21 19:52:24 +00:00
|
|
|
staticBlockReward.SetString("5000000000000000000", 10)
|
2018-01-05 17:55:00 +00:00
|
|
|
}
|
|
|
|
return staticBlockReward
|
|
|
|
}
|