forked from cerc-io/ipld-eth-server
adjust block/uncle reward tests and add methods to pkg/geth blockchain and ethclient for direct fetching of eth balances
This commit is contained in:
@@ -265,3 +265,7 @@ func (blockChain *BlockChain) getPOWHeaders(blockNumbers []int64) (headers []cor
|
||||
|
||||
return headers, err
|
||||
}
|
||||
|
||||
func (blockChain *BlockChain) GetAccountBalance(address common.Address, blockNumber *big.Int) (*big.Int, error) {
|
||||
return blockChain.ethClient.BalanceAt(context.Background(), address, blockNumber)
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package geth_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum"
|
||||
@@ -244,4 +245,28 @@ var _ = Describe("Geth blockchain", func() {
|
||||
Expect(result).To(Equal(big.NewInt(blockNumber)))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("getting an account balance", func() {
|
||||
It("fetches the balance for a given account address at a given block height", func() {
|
||||
balance := big.NewInt(100000)
|
||||
mockClient.SetBalanceAt(balance)
|
||||
|
||||
result, err := blockChain.GetAccountBalance(common.HexToAddress("0x40"), big.NewInt(100))
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
mockClient.AssertBalanceAtCalled(context.Background(), common.HexToAddress("0x40"), big.NewInt(100))
|
||||
Expect(result).To(Equal(balance))
|
||||
})
|
||||
|
||||
It("fails if the client returns an error", func() {
|
||||
balance := big.NewInt(100000)
|
||||
mockClient.SetBalanceAt(balance)
|
||||
setErr := errors.New("testError")
|
||||
mockClient.SetBalanceAtErr(setErr)
|
||||
|
||||
_, err := blockChain.GetAccountBalance(common.HexToAddress("0x40"), big.NewInt(100))
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err).To(Equal(setErr))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -56,3 +56,7 @@ func (client EthClient) TransactionSender(ctx context.Context, tx *types.Transac
|
||||
func (client EthClient) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
|
||||
return client.client.TransactionReceipt(ctx, txHash)
|
||||
}
|
||||
|
||||
func (client EthClient) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) {
|
||||
return client.client.BalanceAt(ctx, account, blockNumber)
|
||||
}
|
||||
|
||||
@@ -17,10 +17,13 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type BlockConverter struct {
|
||||
@@ -54,7 +57,7 @@ func (bc BlockConverter) ToCoreBlock(gethBlock *types.Block) (core.Block, error)
|
||||
}
|
||||
coreBlock.Reward = CalcBlockReward(coreBlock, gethBlock.Uncles()).String()
|
||||
uncleRewards, mappedUncleRewards := CalcUnclesReward(coreBlock, gethBlock.Uncles())
|
||||
coreBlock.UnclesReward = uncleRewards.String()
|
||||
coreBlock.UnclesReward = utilities.NullToZero(uncleRewards.String())
|
||||
coreBlock.MappedUncleRewards = mappedUncleRewards
|
||||
return coreBlock, nil
|
||||
}
|
||||
|
||||
@@ -114,9 +114,13 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
blockConverter := vulcCommon.NewBlockConverter(transactionConverter)
|
||||
|
||||
coreBlock, err := blockConverter.ToCoreBlock(block)
|
||||
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(vulcCommon.CalcBlockReward(coreBlock, block.Uncles())).To(Equal(5.31355))
|
||||
|
||||
expectedBlockReward := new(big.Int)
|
||||
expectedBlockReward.SetString("5313550000000000000", 10)
|
||||
|
||||
blockReward := vulcCommon.CalcBlockReward(coreBlock, block.Uncles())
|
||||
Expect(blockReward.String()).To(Equal(expectedBlockReward.String()))
|
||||
})
|
||||
|
||||
It("calculates the uncles reward for a block", func() {
|
||||
@@ -151,9 +155,19 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
blockConverter := vulcCommon.NewBlockConverter(transactionConverter)
|
||||
|
||||
coreBlock, err := blockConverter.ToCoreBlock(block)
|
||||
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(vulcCommon.CalcUnclesReward(coreBlock, block.Uncles())).To(Equal(6.875))
|
||||
|
||||
expectedTotalReward := new(big.Int)
|
||||
expectedTotalReward.SetString("6875000000000000000", 10)
|
||||
totalReward, mappedRewards := vulcCommon.CalcUnclesReward(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()))
|
||||
})
|
||||
|
||||
It("decreases the static block reward from 5 to 3 for blocks after block 4,269,999", func() {
|
||||
@@ -200,9 +214,12 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
blockConverter := vulcCommon.NewBlockConverter(transactionConverter)
|
||||
|
||||
coreBlock, err := blockConverter.ToCoreBlock(block)
|
||||
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(vulcCommon.CalcBlockReward(coreBlock, block.Uncles())).To(Equal(3.024990672))
|
||||
|
||||
expectedRewards := new(big.Int)
|
||||
expectedRewards.SetString("3024990672000000000", 10)
|
||||
rewards := vulcCommon.CalcBlockReward(coreBlock, block.Uncles())
|
||||
Expect(rewards.String()).To(Equal(expectedRewards.String()))
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -37,6 +37,12 @@ func CalcUnclesReward(block core.Block, uncles []*types.Header) (*big.Int, map[s
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user