ipld-eth-server/pkg/geth/blockchain.go

81 lines
2.4 KiB
Go
Raw Normal View History

package geth
2017-10-23 18:58:33 +00:00
import (
"math/big"
2018-01-15 21:27:45 +00:00
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
2018-05-02 16:17:02 +00:00
"golang.org/x/net/context"
2018-08-07 15:51:34 +00:00
"github.com/ethereum/go-ethereum/core/types"
"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
)
type BlockChain struct {
client core.EthClient
blockConverter vulcCommon.BlockConverter
headerConverter vulcCommon.HeaderConverter
node core.Node
}
func NewBlockChain(client core.EthClient, node core.Node, converter vulcCommon.TransactionConverter) *BlockChain {
return &BlockChain{
client: client,
blockConverter: vulcCommon.NewBlockConverter(converter),
headerConverter: vulcCommon.HeaderConverter{},
node: node,
}
}
func (blockChain *BlockChain) GetBlockByNumber(blockNumber int64) (block core.Block, err error) {
gethBlock, err := blockChain.client.BlockByNumber(context.Background(), big.NewInt(blockNumber))
if err != nil {
return block, err
}
return blockChain.blockConverter.ToCoreBlock(gethBlock)
}
func (blockChain *BlockChain) GetHeaderByNumber(blockNumber int64) (header core.Header, err error) {
gethHeader, err := blockChain.client.HeaderByNumber(context.Background(), big.NewInt(blockNumber))
if err != nil {
return header, err
}
return blockChain.headerConverter.Convert(gethHeader)
}
func (blockChain *BlockChain) GetLogs(contract core.Contract, startingBlockNumber, endingBlockNumber *big.Int) ([]core.Log, error) {
if endingBlockNumber == nil {
endingBlockNumber = startingBlockNumber
}
contractAddress := common.HexToAddress(contract.Hash)
fc := ethereum.FilterQuery{
FromBlock: startingBlockNumber,
ToBlock: endingBlockNumber,
Addresses: []common.Address{contractAddress},
}
2018-08-07 15:51:34 +00:00
gethLogs, err := blockChain.GetEthLogsWithCustomQuery(fc)
if err != nil {
return []core.Log{}, err
}
2018-05-02 16:17:02 +00:00
logs := vulcCommon.ToCoreLogs(gethLogs)
return logs, nil
}
2018-08-07 15:51:34 +00:00
func (blockChain *BlockChain) GetEthLogsWithCustomQuery(query ethereum.FilterQuery) ([]types.Log, error) {
gethLogs, err := blockChain.client.FilterLogs(context.Background(), query)
if err != nil {
return []types.Log{}, err
}
return gethLogs, nil
}
func (blockChain *BlockChain) LastBlock() *big.Int {
block, _ := blockChain.client.HeaderByNumber(context.Background(), nil)
return block.Number
}
func (blockChain *BlockChain) Node() core.Node {
return blockChain.node
}