Add block rewards to db

This commit is contained in:
Matt Krump
2017-12-28 11:58:26 -06:00
parent 3ca4370221
commit 8b024bade9
10 changed files with 149 additions and 121 deletions
+15 -11
View File
@@ -7,24 +7,27 @@ import (
"github.com/ethereum/go-ethereum/params"
)
func UncleReward(gethBlock *types.Block, client GethClient) float64 {
var uncleReward float64
func CalcUnclesReward(gethBlock *types.Block) float64 {
var unclesReward float64
for _, uncle := range gethBlock.Uncles() {
staticBlockReward := float64(blockNumberStaticReward(gethBlock)) / float64(8)
uncleReward += float64(uncle.Number.Int64()-gethBlock.Number().Int64()+int64(8)) * staticBlockReward
blockNumber := gethBlock.Number().Int64()
staticBlockReward := float64(staticRewardByBlockNumber(blockNumber))
unclesReward += (1.0 + float64(uncle.Number.Int64()-gethBlock.Number().Int64())/8.0) * staticBlockReward
}
return uncleReward
return unclesReward
}
func BlockReward(gethBlock *types.Block, client GethClient) float64 {
staticBlockReward := blockNumberStaticReward(gethBlock)
func CalcBlockReward(gethBlock *types.Block, client GethClient) float64 {
blockNumber := gethBlock.Number().Int64()
staticBlockReward := staticRewardByBlockNumber(blockNumber)
transactionFees := calcTransactionFees(gethBlock, client)
uncleInclusionRewards := uncleInclusionRewards(gethBlock, staticBlockReward)
uncleInclusionRewards := calcUncleInclusionRewards(gethBlock)
return transactionFees + uncleInclusionRewards + staticBlockReward
}
func uncleInclusionRewards(gethBlock *types.Block, staticBlockReward float64) float64 {
func calcUncleInclusionRewards(gethBlock *types.Block) float64 {
var uncleInclusionRewards float64
staticBlockReward := staticRewardByBlockNumber(gethBlock.Number().Int64())
for range gethBlock.Uncles() {
uncleInclusionRewards += staticBlockReward * 1 / 32
}
@@ -43,9 +46,10 @@ func calcTransactionFees(gethBlock *types.Block, client GethClient) float64 {
return transactionFees / params.Ether
}
func blockNumberStaticReward(gethBlock *types.Block) float64 {
func staticRewardByBlockNumber(blockNumber int64) float64 {
var staticBlockReward float64
if gethBlock.Number().Int64() > 4269999 {
//https://blog.ethereum.org/2017/10/12/byzantium-hf-announcement/
if blockNumber >= 4370000 {
staticBlockReward = 3
} else {
staticBlockReward = 5
+6 -6
View File
@@ -22,24 +22,24 @@ func GethBlockToCoreBlock(gethBlock *types.Block, client GethClient) core.Block
transaction := gethTransToCoreTrans(gethTransaction, &from)
transactions = append(transactions, transaction)
}
blockReward := BlockReward(gethBlock, client)
uncleReward := UncleReward(gethBlock, client)
blockReward := CalcBlockReward(gethBlock, client)
uncleReward := CalcUnclesReward(gethBlock)
return core.Block{
Difficulty: gethBlock.Difficulty().Int64(),
ExtraData: hexutil.Encode(gethBlock.Extra()),
GasLimit: gethBlock.GasLimit().Int64(),
GasUsed: gethBlock.GasUsed().Int64(),
Hash: gethBlock.Hash().Hex(),
ExtraData: hexutil.Encode(gethBlock.Extra()),
Miner: gethBlock.Coinbase().Hex(),
Nonce: hexutil.Encode(gethBlock.Header().Nonce[:]),
Number: gethBlock.Number().Int64(),
Miner: gethBlock.Coinbase().Hex(),
ParentHash: gethBlock.ParentHash().Hex(),
Reward: blockReward,
Size: gethBlock.Size().Int64(),
Time: gethBlock.Time().Int64(),
Transactions: transactions,
BlockReward: blockReward,
UncleHash: gethBlock.UncleHash().Hex(),
UncleReward: uncleReward,
UnclesReward: uncleReward,
}
}
+70 -64
View File
@@ -83,104 +83,110 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
Expect(gethBlock.IsFinal).To(BeFalse())
})
Describe("the block and uncle rewards calculations", func() {
It("Calculates block rewards for a block", func() {
number := int64(1071819)
uncles := []*types.Header{{Number: big.NewInt(1071817)}, {Number: big.NewInt(1071818)}}
nonce := uint64(226823)
to := common.HexToAddress("0x108fedb097c1dcfed441480170144d8e19bb217f")
amount := big.NewInt(1080900090000000000)
gasLimit := big.NewInt(90000)
gasPrice := big.NewInt(50000000000)
var payload []byte
transaction := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, payload)
Describe("The block and uncle rewards calculations", func() {
It("calculates block rewards for a block", func() {
transaction := types.NewTransaction(
uint64(226823),
common.HexToAddress("0x108fedb097c1dcfed441480170144d8e19bb217f"),
big.NewInt(1080900090000000000),
big.NewInt(90000),
big.NewInt(50000000000),
[]byte{},
)
transactions := []*types.Transaction{transaction}
txHash := transaction.Hash()
receipt := types.Receipt{TxHash: txHash, GasUsed: big.NewInt(21000)}
receipts := []*types.Receipt{&receipt}
number := int64(1071819)
header := types.Header{
Number: big.NewInt(number),
}
uncles := []*types.Header{{Number: big.NewInt(1071817)}, {Number: big.NewInt(1071818)}}
block := types.NewBlock(&header, transactions, uncles, []*types.Receipt{})
client := NewFakeClient()
client.AddReceipts(receipts)
Expect(geth.CalcBlockReward(block, client)).To(Equal(5.31355))
})
It("calculates the uncles reward for a block", func() {
transaction := types.NewTransaction(
uint64(226823),
common.HexToAddress("0x108fedb097c1dcfed441480170144d8e19bb217f"),
big.NewInt(1080900090000000000),
big.NewInt(90000),
big.NewInt(50000000000),
[]byte{})
transactions := []*types.Transaction{transaction}
receipt := types.Receipt{
TxHash: transaction.Hash(),
GasUsed: big.NewInt(21000),
}
receipts := []*types.Receipt{&receipt}
header := types.Header{
Number: big.NewInt(int64(1071819)),
}
uncles := []*types.Header{
{Number: big.NewInt(1071816)},
{Number: big.NewInt(1071817)},
}
block := types.NewBlock(&header, transactions, uncles, []*types.Receipt{})
client := NewFakeClient()
client.AddReceipts(receipts)
Expect(geth.BlockReward(block, client)).To(Equal(5.31355))
Expect(geth.CalcUnclesReward(block)).To(Equal(6.875))
})
It("decreases the static block reward from 5 to 3 for blocks after block 4,269,999", func() {
number := int64(4370055)
var uncles []*types.Header
transactionOne := types.NewTransaction(
uint64(8072),
common.HexToAddress("0xebd17720aeb7ac5186c5dfa7bafeb0bb14c02551 "),
big.NewInt(0),
big.NewInt(500000),
big.NewInt(42000000000),
[]byte{},
)
nonce := uint64(8072)
to := common.HexToAddress("0xebd17720aeb7ac5186c5dfa7bafeb0bb14c02551 ")
amount := big.NewInt(0)
gasLimit := big.NewInt(500000)
gasPrice := big.NewInt(42000000000)
var payload []byte
transactionOne := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, payload)
nonce = uint64(8071)
to = common.HexToAddress("0x3cdab63d764c8c5048ed5e8f0a4e95534ba7e1ea")
amount = big.NewInt(0)
gasLimit = big.NewInt(500000)
gasPrice = big.NewInt(42000000000)
transactionTwo := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, payload)
transactionTwo := types.NewTransaction(uint64(8071),
common.HexToAddress("0x3cdab63d764c8c5048ed5e8f0a4e95534ba7e1ea"),
big.NewInt(0),
big.NewInt(500000),
big.NewInt(42000000000),
[]byte{})
transactions := []*types.Transaction{transactionOne, transactionTwo}
txHashOne := transactionOne.Hash()
receiptOne := types.Receipt{TxHash: txHashOne, GasUsed: big.NewInt(297508)}
txHashTwo := transactionTwo.Hash()
receiptTwo := types.Receipt{TxHash: txHashTwo, GasUsed: big.NewInt(297508)}
receiptOne := types.Receipt{
TxHash: transactionOne.Hash(),
GasUsed: big.NewInt(297508),
}
receiptTwo := types.Receipt{
TxHash: transactionTwo.Hash(),
GasUsed: big.NewInt(297508),
}
receipts := []*types.Receipt{&receiptOne, &receiptTwo}
number := int64(4370055)
header := types.Header{
Number: big.NewInt(number),
}
var uncles []*types.Header
block := types.NewBlock(&header, transactions, uncles, receipts)
client := NewFakeClient()
client.AddReceipts(receipts)
Expect(geth.BlockReward(block, client)).To(Equal(3.024990672))
Expect(geth.CalcBlockReward(block, client)).To(Equal(3.024990672))
})
It("Calculates uncle rewards for a block", func() {
number := int64(1071819)
uncles := []*types.Header{{Number: big.NewInt(1071816)}, {Number: big.NewInt(1071817)}}
nonce := uint64(226823)
to := common.HexToAddress("0x108fedb097c1dcfed441480170144d8e19bb217f")
amount := big.NewInt(1080900090000000000)
gasLimit := big.NewInt(90000)
gasPrice := big.NewInt(50000000000)
var payload []byte
transaction := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, payload)
transactions := []*types.Transaction{transaction}
txHash := transaction.Hash()
receipt := types.Receipt{TxHash: txHash, GasUsed: big.NewInt(21000)}
receipts := []*types.Receipt{&receipt}
header := types.Header{
Number: big.NewInt(number),
}
block := types.NewBlock(&header, transactions, uncles, []*types.Receipt{})
client := NewFakeClient()
client.AddReceipts(receipts)
Expect(geth.UncleReward(block, client)).To(Equal(6.875))
})
})
Describe("the converted transations", func() {
Describe("the converted transactions", func() {
It("is empty", func() {
header := types.Header{}
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{})