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-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-07-17 21:23:07 +00:00
|
|
|
type BlockChain struct {
|
2018-07-18 20:59:40 +00:00
|
|
|
client core.Client
|
2018-07-17 21:23:07 +00:00
|
|
|
blockConverter vulcCommon.BlockConverter
|
|
|
|
headerConverter vulcCommon.HeaderConverter
|
|
|
|
node core.Node
|
2017-12-07 19:32:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func NewBlockChain(client core.Client, node core.Node, converter vulcCommon.TransactionConverter) *BlockChain {
|
2018-07-17 21:23:07 +00:00
|
|
|
return &BlockChain{
|
|
|
|
client: client,
|
2018-07-18 20:59:40 +00:00
|
|
|
blockConverter: vulcCommon.NewBlockConverter(converter),
|
2018-07-17 21:23:07 +00:00
|
|
|
headerConverter: vulcCommon.HeaderConverter{},
|
2018-07-18 20:59:40 +00:00
|
|
|
node: node,
|
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) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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-17 21:23:07 +00:00
|
|
|
gethLogs, err := blockChain.client.FilterLogs(context.Background(), 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-07-17 21:23:07 +00:00
|
|
|
func (blockChain *BlockChain) LastBlock() *big.Int {
|
|
|
|
block, _ := blockChain.client.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
|
|
|
|
}
|