ipld-eth-server/geth/geth_blockchain.go

50 lines
1.4 KiB
Go
Raw Normal View History

package geth
2017-10-23 18:58:33 +00:00
import (
"fmt"
"github.com/8thlight/vulcanizedb/core"
"github.com/ethereum/go-ethereum"
2017-10-23 18:58:33 +00:00
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"golang.org/x/net/context"
)
2017-10-23 18:58:33 +00:00
type GethBlockchain struct {
client *ethclient.Client
readGethHeaders chan *types.Header
outputBlocks chan core.Block
newHeadSubscription ethereum.Subscription
2017-10-23 18:58:33 +00:00
}
func NewGethBlockchain(ipcPath string) *GethBlockchain {
fmt.Printf("Creating Geth Blockchain to: %s\n", ipcPath)
2017-10-23 18:58:33 +00:00
blockchain := GethBlockchain{}
client, _ := ethclient.Dial(ipcPath)
2017-10-23 18:58:33 +00:00
blockchain.client = client
return &blockchain
}
func (blockchain *GethBlockchain) SubscribeToBlocks(blocks chan core.Block) {
blockchain.outputBlocks = blocks
fmt.Println("SubscribeToBlocks")
inputHeaders := make(chan *types.Header, 10)
myContext := context.Background()
blockchain.readGethHeaders = inputHeaders
subscription, _ := blockchain.client.SubscribeNewHead(myContext, inputHeaders)
blockchain.newHeadSubscription = subscription
2017-10-23 18:58:33 +00:00
}
func (blockchain *GethBlockchain) StartListening() {
2017-10-23 18:58:33 +00:00
myContext := context.Background()
for header := range blockchain.readGethHeaders {
gethBlock, _ := blockchain.client.BlockByNumber(myContext, header.Number)
block := GethBlockToCoreBlock(gethBlock)
blockchain.outputBlocks <- block
2017-10-23 18:58:33 +00:00
}
}
func (blockchain *GethBlockchain) StopListening() {
blockchain.newHeadSubscription.Unsubscribe()
}