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"
|
|
|
|
|
2017-12-19 20:14:41 +00:00
|
|
|
"log"
|
|
|
|
|
2017-11-06 18:53:43 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/pkg/core"
|
2017-12-07 19:32:16 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/pkg/geth/node"
|
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"
|
2017-10-23 18:58:33 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
2017-10-23 15:56:29 +00:00
|
|
|
|
2017-10-23 18:58:33 +00:00
|
|
|
type GethBlockchain struct {
|
2017-11-03 13:54:32 +00:00
|
|
|
client *ethclient.Client
|
|
|
|
readGethHeaders chan *types.Header
|
|
|
|
outputBlocks chan core.Block
|
|
|
|
newHeadSubscription ethereum.Subscription
|
2017-12-07 19:32:16 +00:00
|
|
|
node core.Node
|
|
|
|
}
|
|
|
|
|
2017-12-22 17:42:35 +00:00
|
|
|
func (blockchain *GethBlockchain) GetLogs(contract core.Contract, startingBlockNumber *big.Int, endingBlockNumber *big.Int) ([]core.Log, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
logs := GethLogsToCoreLogs(gethLogs)
|
|
|
|
return logs, nil
|
|
|
|
}
|
|
|
|
|
2017-12-07 19:32:16 +00:00
|
|
|
func (blockchain *GethBlockchain) Node() core.Node {
|
|
|
|
return blockchain.node
|
2017-10-23 18:58:33 +00:00
|
|
|
}
|
2017-10-23 15:56:29 +00:00
|
|
|
|
2017-11-06 20:36:12 +00:00
|
|
|
func (blockchain *GethBlockchain) GetBlockByNumber(blockNumber int64) core.Block {
|
|
|
|
gethBlock, _ := blockchain.client.BlockByNumber(context.Background(), big.NewInt(blockNumber))
|
2017-11-08 20:55:35 +00:00
|
|
|
return GethBlockToCoreBlock(gethBlock, blockchain.client)
|
2017-11-06 20:36:12 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 19:33:08 +00:00
|
|
|
func NewGethBlockchain(ipcPath string) *GethBlockchain {
|
2017-10-23 18:58:33 +00:00
|
|
|
blockchain := GethBlockchain{}
|
2017-12-07 19:32:16 +00:00
|
|
|
rpcClient, _ := rpc.Dial(ipcPath)
|
|
|
|
client := ethclient.NewClient(rpcClient)
|
|
|
|
blockchain.node = node.Retrieve(rpcClient)
|
2017-10-23 18:58:33 +00:00
|
|
|
blockchain.client = client
|
|
|
|
return &blockchain
|
|
|
|
}
|
2017-10-24 14:24:07 +00:00
|
|
|
|
2017-11-02 19:37:07 +00:00
|
|
|
func (blockchain *GethBlockchain) SubscribeToBlocks(blocks chan core.Block) {
|
2017-11-02 11:41:24 +00:00
|
|
|
blockchain.outputBlocks = blocks
|
2017-12-19 20:14:41 +00:00
|
|
|
log.Println("SubscribeToBlocks")
|
2017-11-02 11:41:24 +00:00
|
|
|
inputHeaders := make(chan *types.Header, 10)
|
|
|
|
myContext := context.Background()
|
|
|
|
blockchain.readGethHeaders = inputHeaders
|
2017-11-03 13:54:32 +00:00
|
|
|
subscription, _ := blockchain.client.SubscribeNewHead(myContext, inputHeaders)
|
|
|
|
blockchain.newHeadSubscription = subscription
|
2017-10-23 18:58:33 +00:00
|
|
|
}
|
2017-10-23 15:56:29 +00:00
|
|
|
|
2017-11-02 11:41:24 +00:00
|
|
|
func (blockchain *GethBlockchain) StartListening() {
|
|
|
|
for header := range blockchain.readGethHeaders {
|
2017-11-06 20:36:12 +00:00
|
|
|
block := blockchain.GetBlockByNumber(header.Number.Int64())
|
2017-11-02 11:41:24 +00:00
|
|
|
blockchain.outputBlocks <- block
|
2017-10-23 18:58:33 +00:00
|
|
|
}
|
2017-10-23 15:56:29 +00:00
|
|
|
}
|
2017-11-03 13:54:32 +00:00
|
|
|
|
|
|
|
func (blockchain *GethBlockchain) StopListening() {
|
|
|
|
blockchain.newHeadSubscription.Unsubscribe()
|
|
|
|
}
|
2017-12-11 21:08:00 +00:00
|
|
|
|
2017-12-20 20:06:22 +00:00
|
|
|
func (blockchain *GethBlockchain) LastBlock() *big.Int {
|
2017-12-11 21:08:00 +00:00
|
|
|
block, _ := blockchain.client.HeaderByNumber(context.Background(), nil)
|
|
|
|
return block.Number
|
|
|
|
}
|