2017-11-02 19:37:07 +00:00
|
|
|
package geth
|
2017-10-23 15:56:29 +00:00
|
|
|
|
2017-10-23 18:58:33 +00:00
|
|
|
import (
|
2018-07-17 21:23:07 +00:00
|
|
|
"math/big"
|
2018-01-15 21:27:45 +00:00
|
|
|
|
2017-11-03 13:54:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
2017-12-11 21:08:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-05-02 16:17:02 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2018-09-18 19:46:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2018-08-07 15:51:34 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-09-20 22:15:06 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-02-08 16:12:08 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-05-02 16:17:02 +00:00
|
|
|
vulcCommon "github.com/vulcanize/vulcanizedb/pkg/geth/converters/common"
|
2017-10-23 18:58:33 +00:00
|
|
|
)
|
2017-10-23 15:56:29 +00:00
|
|
|
|
2018-09-20 22:15:06 +00:00
|
|
|
var ErrEmptyHeader = errors.New("empty header returned over RPC")
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
type BlockChain struct {
|
|
|
|
blockConverter vulcCommon.BlockConverter
|
2018-09-18 19:46:16 +00:00
|
|
|
ethClient core.EthClient
|
2018-07-17 21:23:07 +00:00
|
|
|
headerConverter vulcCommon.HeaderConverter
|
|
|
|
node core.Node
|
2018-09-18 19:46:16 +00:00
|
|
|
rpcClient core.RpcClient
|
2017-12-07 19:32:16 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 19:46:16 +00:00
|
|
|
func NewBlockChain(ethClient core.EthClient, rpcClient core.RpcClient, node core.Node, converter vulcCommon.TransactionConverter) *BlockChain {
|
2018-07-17 21:23:07 +00:00
|
|
|
return &BlockChain{
|
2018-07-18 20:59:40 +00:00
|
|
|
blockConverter: vulcCommon.NewBlockConverter(converter),
|
2018-09-18 19:46:16 +00:00
|
|
|
ethClient: ethClient,
|
2018-07-17 21:23:07 +00:00
|
|
|
headerConverter: vulcCommon.HeaderConverter{},
|
2018-07-18 20:59:40 +00:00
|
|
|
node: node,
|
2018-09-18 19:46:16 +00:00
|
|
|
rpcClient: rpcClient,
|
2018-07-17 21:23:07 +00:00
|
|
|
}
|
2018-01-05 17:55:00 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func (blockChain *BlockChain) GetBlockByNumber(blockNumber int64) (block core.Block, err error) {
|
2018-09-18 19:46:16 +00:00
|
|
|
gethBlock, err := blockChain.ethClient.BlockByNumber(context.Background(), big.NewInt(blockNumber))
|
2018-07-18 20:59:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return block, err
|
|
|
|
}
|
|
|
|
return blockChain.blockConverter.ToCoreBlock(gethBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blockChain *BlockChain) GetHeaderByNumber(blockNumber int64) (header core.Header, err error) {
|
2018-09-18 19:46:16 +00:00
|
|
|
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))
|
2018-07-18 20:59:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return header, err
|
|
|
|
}
|
|
|
|
return blockChain.headerConverter.Convert(gethHeader)
|
|
|
|
}
|
|
|
|
|
2018-09-18 19:46:16 +00:00
|
|
|
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
|
|
|
|
}
|
2018-09-20 22:15:06 +00:00
|
|
|
if POAHeader.Number == nil {
|
|
|
|
return header, ErrEmptyHeader
|
|
|
|
}
|
2018-09-18 19:46:16 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
func (blockChain *BlockChain) GetLogs(contract core.Contract, startingBlockNumber, endingBlockNumber *big.Int) ([]core.Log, error) {
|
2017-12-22 17:42:35 +00:00
|
|
|
if endingBlockNumber == nil {
|
|
|
|
endingBlockNumber = startingBlockNumber
|
2017-12-14 16:15:36 +00:00
|
|
|
}
|
2017-12-11 21:08:00 +00:00
|
|
|
contractAddress := common.HexToAddress(contract.Hash)
|
|
|
|
fc := ethereum.FilterQuery{
|
2017-12-22 17:42:35 +00:00
|
|
|
FromBlock: startingBlockNumber,
|
|
|
|
ToBlock: endingBlockNumber,
|
2017-12-11 21:08:00 +00:00
|
|
|
Addresses: []common.Address{contractAddress},
|
2018-07-24 16:38:49 +00:00
|
|
|
Topics: nil,
|
2017-12-11 21:08:00 +00:00
|
|
|
}
|
2018-08-07 15:51:34 +00:00
|
|
|
gethLogs, err := blockChain.GetEthLogsWithCustomQuery(fc)
|
2017-12-11 21:08:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return []core.Log{}, err
|
|
|
|
}
|
2018-05-02 16:17:02 +00:00
|
|
|
logs := vulcCommon.ToCoreLogs(gethLogs)
|
2017-12-11 21:08:00 +00:00
|
|
|
return logs, nil
|
|
|
|
}
|
|
|
|
|
2018-08-07 15:51:34 +00:00
|
|
|
func (blockChain *BlockChain) GetEthLogsWithCustomQuery(query ethereum.FilterQuery) ([]types.Log, error) {
|
2018-09-18 19:46:16 +00:00
|
|
|
gethLogs, err := blockChain.ethClient.FilterLogs(context.Background(), query)
|
2018-08-07 15:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return []types.Log{}, err
|
|
|
|
}
|
|
|
|
return gethLogs, nil
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
func (blockChain *BlockChain) LastBlock() *big.Int {
|
2018-09-18 19:46:16 +00:00
|
|
|
block, _ := blockChain.ethClient.HeaderByNumber(context.Background(), nil)
|
2017-12-11 21:08:00 +00:00
|
|
|
return block.Number
|
|
|
|
}
|
2018-07-18 20:59:40 +00:00
|
|
|
|
|
|
|
func (blockChain *BlockChain) Node() core.Node {
|
|
|
|
return blockChain.node
|
|
|
|
}
|