ipld-eth-server/core/geth_blockchain.go
Eric Meyer 646e0fa057 Refactor to use listener
* This removes some duplication between the fake blockchain and
   geth blockchain.
 * This pulls the observers into the blockchain listener
2017-11-02 12:51:46 -05:00

42 lines
1.1 KiB
Go

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