Handle headers from POA chain

- Fetching headers from Kovan requires custom type without mixHash/Nonce
This commit is contained in:
Rob Mulholand 2018-09-18 14:46:16 -05:00
parent 778517f33f
commit 042f1142c0
15 changed files with 162 additions and 63 deletions

View File

@ -128,5 +128,5 @@ func getBlockChain() *geth.BlockChain {
vdbEthClient := client.NewEthClient(ethClient)
vdbNode := node.MakeNode(rpcClient)
transactionConverter := vRpc.NewRpcTransactionConverter(ethClient)
return geth.NewBlockChain(vdbEthClient, vdbNode, transactionConverter)
return geth.NewBlockChain(vdbEthClient, rpcClient, vdbNode, transactionConverter)
}

View File

@ -57,7 +57,7 @@ var _ = Describe("ERC20 Fetcher", func() {
blockChainClient := client.NewEthClient(ethClient)
node := node.MakeNode(rpcClient)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
blockChain := geth.NewBlockChain(blockChainClient, rpcClient, node, transactionConverter)
realFetcher := every_block.NewFetcher(blockChain)
result, err := realFetcher.FetchSupplyOf(constants.DaiAbiString, constants.DaiContractAddress, blockNumber)

View File

@ -23,7 +23,7 @@ var _ = Describe("Rewards calculations", func() {
blockChainClient := client.NewEthClient(ethClient)
node := node.MakeNode(rpcClient)
transactionConverter := vRpc.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
blockChain := geth.NewBlockChain(blockChainClient, rpcClient, node, transactionConverter)
block, err := blockChain.GetBlockByNumber(1071819)
Expect(err).ToNot(HaveOccurred())
Expect(block.Reward).To(Equal(5.31355))
@ -37,7 +37,7 @@ var _ = Describe("Rewards calculations", func() {
blockChainClient := client.NewEthClient(ethClient)
node := node.MakeNode(rpcClient)
transactionConverter := vRpc.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
blockChain := geth.NewBlockChain(blockChainClient, rpcClient, node, transactionConverter)
block, err := blockChain.GetBlockByNumber(1071819)
Expect(err).ToNot(HaveOccurred())
Expect(block.UnclesReward).To(Equal(6.875))

View File

@ -39,7 +39,7 @@ var _ = Describe("Reading contracts", func() {
blockChainClient := client.NewEthClient(ethClient)
node := node.MakeNode(rpcClient)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
blockChain := geth.NewBlockChain(blockChainClient, rpcClient, node, transactionConverter)
contract := testing.SampleContract()
logs, err := blockChain.GetLogs(contract, big.NewInt(4703824), nil)
@ -57,7 +57,7 @@ var _ = Describe("Reading contracts", func() {
blockChainClient := client.NewEthClient(ethClient)
node := node.MakeNode(rpcClient)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
blockChain := geth.NewBlockChain(blockChainClient, rpcClient, node, transactionConverter)
logs, err := blockChain.GetLogs(core.Contract{Hash: "0x123"}, big.NewInt(4703824), nil)
@ -75,7 +75,7 @@ var _ = Describe("Reading contracts", func() {
blockChainClient := client.NewEthClient(ethClient)
node := node.MakeNode(rpcClient)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
blockChain := geth.NewBlockChain(blockChainClient, rpcClient, node, transactionConverter)
contract := testing.SampleContract()
var balance = new(big.Int)

View File

@ -26,7 +26,7 @@ var _ = Describe("Reading from the Geth blockchain", func() {
blockChainClient := client.NewEthClient(ethClient)
node := node.MakeNode(rpcClient)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain = geth.NewBlockChain(blockChainClient, node, transactionConverter)
blockChain = geth.NewBlockChain(blockChainClient, rpcClient, node, transactionConverter)
})
It("reads two blocks", func(done Done) {

View File

@ -1,8 +1,31 @@
package core
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
)
type Header struct {
Id int64
BlockNumber int64 `db:"block_number"`
Hash string
Raw []byte
}
type POAHeader struct {
ParentHash common.Hash `json:"parentHash" gencodec:"required"`
UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
Coinbase common.Address `json:"miner" gencodec:"required"`
Root common.Hash `json:"stateRoot" gencodec:"required"`
TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"`
Number *hexutil.Big `json:"number" gencodec:"required"`
GasLimit hexutil.Uint64 `json:"gasLimit" gencodec:"required"`
GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
Time *hexutil.Big `json:"timestamp" gencodec:"required"`
Extra hexutil.Bytes `json:"extraData" gencodec:"required"`
Hash common.Hash `json:"hash"`
}

View File

@ -13,6 +13,10 @@ const (
GANACHE
)
const (
KOVAN_NETWORK_ID = 42
)
type Node struct {
GenesisBlock string
NetworkID float64

View File

@ -2,14 +2,23 @@ package fakes
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/p2p"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
)
type MockRpcClient struct {
callContextErr error
ipcPath string
nodeType core.NodeType
passedContext context.Context
passedMethod string
passedResult interface{}
supportedModules map[string]string
}
@ -21,7 +30,10 @@ func (client *MockRpcClient) SetIpcPath(ipcPath string) {
client.ipcPath = ipcPath
}
func (*MockRpcClient) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
func (client *MockRpcClient) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
client.passedContext = ctx
client.passedResult = result
client.passedMethod = method
switch method {
case "admin_nodeInfo":
if p, ok := result.(*p2p.NodeInfo); ok {
@ -30,9 +42,15 @@ func (*MockRpcClient) CallContext(ctx context.Context, result interface{}, metho
}
case "eth_getBlockByNumber":
if p, ok := result.(*types.Header); ok {
*p = types.Header{}
*p = types.Header{Number: big.NewInt(123)}
}
if p, ok := result.(*core.POAHeader); ok {
n := hexutil.Big(*big.NewInt(123))
*p = core.POAHeader{Number: &n}
}
if client.callContextErr != nil {
return client.callContextErr
}
case "parity_versionInfo":
if p, ok := result.(*core.ParityNodeInfo); ok {
*p = core.ParityNodeInfo{
@ -68,3 +86,13 @@ func (client *MockRpcClient) SupportedModules() (map[string]string, error) {
func (client *MockRpcClient) SetSupporedModules(supportedModules map[string]string) {
client.supportedModules = supportedModules
}
func (client *MockRpcClient) SetCallContextErr(err error) {
client.callContextErr = err
}
func (client *MockRpcClient) AssertCallContextCalledWith(ctx context.Context, result interface{}, method string) {
Expect(client.passedContext).To(Equal(ctx))
Expect(client.passedResult).To(BeAssignableToTypeOf(result))
Expect(client.passedMethod).To(Equal(method))
}

View File

@ -7,29 +7,32 @@ import (
"github.com/ethereum/go-ethereum/common"
"golang.org/x/net/context"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/core"
vulcCommon "github.com/vulcanize/vulcanizedb/pkg/geth/converters/common"
)
type BlockChain struct {
client core.EthClient
blockConverter vulcCommon.BlockConverter
ethClient core.EthClient
headerConverter vulcCommon.HeaderConverter
node core.Node
rpcClient core.RpcClient
}
func NewBlockChain(client core.EthClient, node core.Node, converter vulcCommon.TransactionConverter) *BlockChain {
func NewBlockChain(ethClient core.EthClient, rpcClient core.RpcClient, node core.Node, converter vulcCommon.TransactionConverter) *BlockChain {
return &BlockChain{
client: client,
blockConverter: vulcCommon.NewBlockConverter(converter),
ethClient: ethClient,
headerConverter: vulcCommon.HeaderConverter{},
node: node,
rpcClient: rpcClient,
}
}
func (blockChain *BlockChain) GetBlockByNumber(blockNumber int64) (block core.Block, err error) {
gethBlock, err := blockChain.client.BlockByNumber(context.Background(), big.NewInt(blockNumber))
gethBlock, err := blockChain.ethClient.BlockByNumber(context.Background(), big.NewInt(blockNumber))
if err != nil {
return block, err
}
@ -37,13 +40,45 @@ func (blockChain *BlockChain) GetBlockByNumber(blockNumber int64) (block core.Bl
}
func (blockChain *BlockChain) GetHeaderByNumber(blockNumber int64) (header core.Header, err error) {
gethHeader, err := blockChain.client.HeaderByNumber(context.Background(), big.NewInt(blockNumber))
if blockChain.node.NetworkID == core.KOVAN_NETWORK_ID {
return blockChain.getPOAHeader(blockNumber)
}
return blockChain.getPOWHeader(blockNumber)
}
func (blockChain *BlockChain) getPOWHeader(blockNumber int64) (header core.Header, err error) {
gethHeader, err := blockChain.ethClient.HeaderByNumber(context.Background(), big.NewInt(blockNumber))
if err != nil {
return header, err
}
return blockChain.headerConverter.Convert(gethHeader)
}
func (blockChain *BlockChain) getPOAHeader(blockNumber int64) (header core.Header, err error) {
var POAHeader core.POAHeader
blockNumberArg := hexutil.EncodeBig(big.NewInt(blockNumber))
includeTransactions := false
err = blockChain.rpcClient.CallContext(context.Background(), &POAHeader, "eth_getBlockByNumber", blockNumberArg, includeTransactions)
if err != nil {
return header, err
}
return blockChain.headerConverter.Convert(&types.Header{
ParentHash: POAHeader.ParentHash,
UncleHash: POAHeader.UncleHash,
Coinbase: POAHeader.Coinbase,
Root: POAHeader.Root,
TxHash: POAHeader.TxHash,
ReceiptHash: POAHeader.ReceiptHash,
Bloom: POAHeader.Bloom,
Difficulty: POAHeader.Difficulty.ToInt(),
Number: POAHeader.Number.ToInt(),
GasLimit: uint64(POAHeader.GasLimit),
GasUsed: uint64(POAHeader.GasUsed),
Time: POAHeader.Time.ToInt(),
Extra: POAHeader.Extra,
})
}
func (blockChain *BlockChain) GetLogs(contract core.Contract, startingBlockNumber, endingBlockNumber *big.Int) ([]core.Log, error) {
if endingBlockNumber == nil {
endingBlockNumber = startingBlockNumber
@ -64,7 +99,7 @@ func (blockChain *BlockChain) GetLogs(contract core.Contract, startingBlockNumbe
}
func (blockChain *BlockChain) GetEthLogsWithCustomQuery(query ethereum.FilterQuery) ([]types.Log, error) {
gethLogs, err := blockChain.client.FilterLogs(context.Background(), query)
gethLogs, err := blockChain.ethClient.FilterLogs(context.Background(), query)
if err != nil {
return []types.Log{}, err
}
@ -72,7 +107,7 @@ func (blockChain *BlockChain) GetEthLogsWithCustomQuery(query ethereum.FilterQue
}
func (blockChain *BlockChain) LastBlock() *big.Int {
block, _ := blockChain.client.HeaderByNumber(context.Background(), nil)
block, _ := blockChain.ethClient.HeaderByNumber(context.Background(), nil)
return block.Number
}

View File

@ -18,16 +18,19 @@ import (
var _ = Describe("Geth blockchain", func() {
var mockClient *fakes.MockEthClient
var mockRpcClient *fakes.MockRpcClient
var node vulcCore.Node
var blockChain *geth.BlockChain
BeforeEach(func() {
mockClient = fakes.NewMockEthClient()
node := vulcCore.Node{}
blockChain = geth.NewBlockChain(mockClient, node, cold_db.NewColdDbTransactionConverter())
mockRpcClient = fakes.NewMockRpcClient()
node = vulcCore.Node{}
blockChain = geth.NewBlockChain(mockClient, mockRpcClient, node, cold_db.NewColdDbTransactionConverter())
})
Describe("getting a block", func() {
It("fetches block from client", func() {
It("fetches block from ethClient", func() {
mockClient.SetBlockByNumberReturnBlock(types.NewBlockWithHeader(&types.Header{}))
blockNumber := int64(100)
@ -37,7 +40,7 @@ var _ = Describe("Geth blockchain", func() {
mockClient.AssertBlockByNumberCalledWith(context.Background(), big.NewInt(blockNumber))
})
It("returns err if client returns err", func() {
It("returns err if ethClient returns err", func() {
mockClient.SetBlockByNumberErr(fakes.FakeError)
_, err := blockChain.GetBlockByNumber(100)
@ -48,28 +51,53 @@ var _ = Describe("Geth blockchain", func() {
})
Describe("getting a header", func() {
It("fetches header from client", func() {
blockNumber := int64(100)
mockClient.SetHeaderByNumberReturnHeader(&types.Header{Number: big.NewInt(blockNumber)})
Describe("default/mainnet", func() {
It("fetches header from ethClient", func() {
blockNumber := int64(100)
mockClient.SetHeaderByNumberReturnHeader(&types.Header{Number: big.NewInt(blockNumber)})
_, err := blockChain.GetHeaderByNumber(blockNumber)
_, err := blockChain.GetHeaderByNumber(blockNumber)
Expect(err).NotTo(HaveOccurred())
mockClient.AssertHeaderByNumberCalledWith(context.Background(), big.NewInt(blockNumber))
Expect(err).NotTo(HaveOccurred())
mockClient.AssertHeaderByNumberCalledWith(context.Background(), big.NewInt(blockNumber))
})
It("returns err if ethClient returns err", func() {
mockClient.SetHeaderByNumberErr(fakes.FakeError)
_, err := blockChain.GetHeaderByNumber(100)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
})
It("returns err if client returns err", func() {
mockClient.SetHeaderByNumberErr(fakes.FakeError)
Describe("POA/Kovan", func() {
It("fetches header from rpcClient", func() {
node.NetworkID = vulcCore.KOVAN_NETWORK_ID
blockChain = geth.NewBlockChain(mockClient, mockRpcClient, node, cold_db.NewColdDbTransactionConverter())
_, err := blockChain.GetHeaderByNumber(100)
_, err := blockChain.GetHeaderByNumber(100)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
Expect(err).NotTo(HaveOccurred())
mockRpcClient.AssertCallContextCalledWith(context.Background(), &vulcCore.POAHeader{}, "eth_getBlockByNumber")
})
It("returns err if rpcClient returns err", func() {
node.NetworkID = vulcCore.KOVAN_NETWORK_ID
mockRpcClient.SetCallContextErr(fakes.FakeError)
blockChain = geth.NewBlockChain(mockClient, mockRpcClient, node, cold_db.NewColdDbTransactionConverter())
_, err := blockChain.GetHeaderByNumber(100)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
})
})
Describe("getting logs with default FilterQuery", func() {
It("fetches logs from client", func() {
It("fetches logs from ethClient", func() {
mockClient.SetFilterLogsReturnLogs([]types.Log{{}})
contract := vulcCore.Contract{Hash: common.BytesToHash([]byte{1, 2, 3, 4, 5}).Hex()}
startingBlockNumber := big.NewInt(1)
@ -86,7 +114,7 @@ var _ = Describe("Geth blockchain", func() {
mockClient.AssertFilterLogsCalledWith(context.Background(), expectedQuery)
})
It("returns err if client returns err", func() {
It("returns err if ethClient returns err", func() {
mockClient.SetFilterLogsErr(fakes.FakeError)
contract := vulcCore.Contract{Hash: common.BytesToHash([]byte{1, 2, 3, 4, 5}).Hex()}
startingBlockNumber := big.NewInt(1)
@ -100,7 +128,7 @@ var _ = Describe("Geth blockchain", func() {
})
Describe("getting logs with a custom FilterQuery", func() {
It("fetches logs from client", func() {
It("fetches logs from ethClient", func() {
mockClient.SetFilterLogsReturnLogs([]types.Log{{}})
address := common.HexToAddress("0x")
startingBlockNumber := big.NewInt(1)
@ -119,7 +147,7 @@ var _ = Describe("Geth blockchain", func() {
mockClient.AssertFilterLogsCalledWith(context.Background(), query)
})
It("returns err if client returns err", func() {
It("returns err if ethClient returns err", func() {
mockClient.SetFilterLogsErr(fakes.FakeError)
contract := vulcCore.Contract{Hash: common.BytesToHash([]byte{1, 2, 3, 4, 5}).Hex()}
startingBlockNumber := big.NewInt(1)
@ -139,7 +167,7 @@ var _ = Describe("Geth blockchain", func() {
})
Describe("getting the most recent block number", func() {
It("fetches latest header from client", func() {
It("fetches latest header from ethClient", func() {
blockNumber := int64(100)
mockClient.SetHeaderByNumberReturnHeader(&types.Header{Number: big.NewInt(blockNumber)})

View File

@ -38,5 +38,5 @@ func (blockChain *BlockChain) FetchContractData(abiJSON string, address string,
func (blockChain *BlockChain) callContract(contractHash string, input []byte, blockNumber *big.Int) ([]byte, error) {
to := common.HexToAddress(contractHash)
msg := ethereum.CallMsg{To: &to, Data: input}
return blockChain.client.CallContract(context.Background(), msg, blockNumber)
return blockChain.ethClient.CallContract(context.Background(), msg, blockNumber)
}

View File

@ -46,7 +46,7 @@ var _ = Describe("Integration tests", func() {
blockChainClient := client.NewEthClient(ethClient)
realNode := node.MakeNode(rpcClient)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
realBlockChain := geth.NewBlockChain(blockChainClient, realNode, transactionConverter)
realBlockChain := geth.NewBlockChain(blockChainClient, rpcClient, realNode, transactionConverter)
realFetcher := shared.NewFetcher(realBlockChain)
topic0 := common.HexToHash(shared.BiteSignature)
topics := [][]common.Hash{{topic0}}

View File

@ -44,7 +44,7 @@ var _ = Describe("Integration tests", func() {
blockChainClient := client.NewEthClient(ethClient)
realNode := node.MakeNode(rpcClient)
transactionConverter := vRpc.NewRpcTransactionConverter(ethClient)
realBlockChain := geth.NewBlockChain(blockChainClient, realNode, transactionConverter)
realBlockChain := geth.NewBlockChain(blockChainClient, rpcClient, realNode, transactionConverter)
realFetcher := shared.NewFetcher(realBlockChain)
topic0 := common.HexToHash(shared.FrobSignature)
topics := [][]common.Hash{{topic0}}

View File

@ -2,9 +2,7 @@ package price_feeds_test
import (
"context"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
. "github.com/onsi/ginkgo"
@ -97,23 +95,6 @@ var _ = Describe("Price feeds transformer", func() {
})
})
type POAHeader struct {
ParentHash common.Hash `json:"parentHash" gencodec:"required"`
UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
Coinbase common.Address `json:"miner" gencodec:"required"`
Root common.Hash `json:"stateRoot" gencodec:"required"`
TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"`
Number *hexutil.Big `json:"number" gencodec:"required"`
GasLimit hexutil.Uint64 `json:"gasLimit" gencodec:"required"`
GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
Time *hexutil.Big `json:"timestamp" gencodec:"required"`
Extra hexutil.Bytes `json:"extraData" gencodec:"required"`
Hash common.Hash `json:"hash"`
}
func getClients(ipc string) (client.RpcClient, *ethclient.Client, error) {
raw, err := rpc.Dial(ipc)
if err != nil {
@ -126,12 +107,12 @@ func getBlockChain(rpcClient client.RpcClient, ethClient *ethclient.Client) (cor
client := client.NewEthClient(ethClient)
node := node.MakeNode(rpcClient)
transactionConverter := rpc2.NewRpcTransactionConverter(client)
blockChain := geth.NewBlockChain(client, node, transactionConverter)
blockChain := geth.NewBlockChain(client, rpcClient, node, transactionConverter)
return blockChain, nil
}
func persistHeader(rpcClient client.RpcClient, db *postgres.DB, blockNumber int64) error {
var poaHeader POAHeader
var poaHeader core.POAHeader
blockNumberArg := hexutil.EncodeBig(big.NewInt(int64(blockNumber)))
err := rpcClient.CallContext(context.Background(), &poaHeader, "eth_getBlockByNumber", blockNumberArg, false)
if err != nil {

View File

@ -43,7 +43,7 @@ var _ = Describe("Integration tests", func() {
blockChainClient := client.NewEthClient(ethClient)
realNode := node.MakeNode(rpcClient)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
realBlockChain := geth.NewBlockChain(blockChainClient, realNode, transactionConverter)
realBlockChain := geth.NewBlockChain(blockChainClient, rpcClient, realNode, transactionConverter)
realFetcher := shared.NewFetcher(realBlockChain)
topic0 := common.HexToHash(shared.TendFunctionSignature)
topics := [][]common.Hash{{topic0}}