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 (
|
2017-11-06 20:36:12 +00:00
|
|
|
"math/big"
|
|
|
|
|
2018-01-15 21:27:45 +00:00
|
|
|
"log"
|
|
|
|
|
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"
|
2017-10-23 18:58:33 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
2017-12-07 19:32:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
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"
|
|
|
|
vulcRpc "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
|
2018-02-08 16:12:08 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
|
2017-10-23 18:58:33 +00:00
|
|
|
)
|
2017-10-23 15:56:29 +00:00
|
|
|
|
2018-01-05 17:55:00 +00:00
|
|
|
type Blockchain struct {
|
2017-11-03 13:54:32 +00:00
|
|
|
client *ethclient.Client
|
2018-05-02 16:17:02 +00:00
|
|
|
blockConverter vulcCommon.BlockConverter
|
2017-11-03 13:54:32 +00:00
|
|
|
readGethHeaders chan *types.Header
|
|
|
|
outputBlocks chan core.Block
|
|
|
|
newHeadSubscription ethereum.Subscription
|
2017-12-07 19:32:16 +00:00
|
|
|
node core.Node
|
|
|
|
}
|
|
|
|
|
2018-01-05 17:55:00 +00:00
|
|
|
func NewBlockchain(ipcPath string) *Blockchain {
|
|
|
|
blockchain := Blockchain{}
|
2018-01-15 21:27:45 +00:00
|
|
|
rpcClient, err := rpc.Dial(ipcPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-01-05 17:55:00 +00:00
|
|
|
client := ethclient.NewClient(rpcClient)
|
2018-03-07 21:29:21 +00:00
|
|
|
clientWrapper := node.ClientWrapper{ContextCaller: rpcClient, IPCPath: ipcPath}
|
|
|
|
blockchain.node = node.MakeNode(clientWrapper)
|
2018-01-05 17:55:00 +00:00
|
|
|
blockchain.client = client
|
2018-05-02 16:17:02 +00:00
|
|
|
transactionConverter := vulcRpc.NewRpcTransactionConverter(client)
|
|
|
|
blockchain.blockConverter = vulcCommon.NewBlockConverter(transactionConverter)
|
2018-01-05 17:55:00 +00:00
|
|
|
return &blockchain
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blockchain *Blockchain) GetLogs(contract core.Contract, startingBlockNumber *big.Int, 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},
|
|
|
|
}
|
|
|
|
gethLogs, err := blockchain.client.FilterLogs(context.Background(), fc)
|
|
|
|
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-01-05 17:55:00 +00:00
|
|
|
func (blockchain *Blockchain) Node() core.Node {
|
2017-12-07 19:32:16 +00:00
|
|
|
return blockchain.node
|
2017-10-23 18:58:33 +00:00
|
|
|
}
|
2017-10-23 15:56:29 +00:00
|
|
|
|
2018-03-27 21:06:12 +00:00
|
|
|
func (blockchain *Blockchain) GetBlockByNumber(blockNumber int64) (core.Block, error) {
|
|
|
|
gethBlock, err := blockchain.client.BlockByNumber(context.Background(), big.NewInt(blockNumber))
|
|
|
|
if err != nil {
|
|
|
|
return core.Block{}, err
|
|
|
|
}
|
2018-05-02 16:17:02 +00:00
|
|
|
block, err := blockchain.blockConverter.ToCoreBlock(gethBlock)
|
2018-03-27 21:06:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return core.Block{}, err
|
|
|
|
}
|
|
|
|
return block, nil
|
2017-11-03 13:54:32 +00:00
|
|
|
}
|
2017-12-11 21:08:00 +00:00
|
|
|
|
2018-01-05 17:55:00 +00:00
|
|
|
func (blockchain *Blockchain) LastBlock() *big.Int {
|
2017-12-11 21:08:00 +00:00
|
|
|
block, _ := blockchain.client.HeaderByNumber(context.Background(), nil)
|
|
|
|
return block.Number
|
|
|
|
}
|