2017-10-23 15:56:29 +00:00
|
|
|
package core
|
|
|
|
|
2017-10-23 18:58:33 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2017-10-25 22:24:38 +00:00
|
|
|
|
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 15:56:29 +00:00
|
|
|
|
2017-10-23 18:58:33 +00:00
|
|
|
type GethBlockchain struct {
|
2017-11-02 11:41:24 +00:00
|
|
|
client *ethclient.Client
|
|
|
|
readGethHeaders chan *types.Header
|
|
|
|
outputBlocks chan Block
|
2017-10-23 18:58:33 +00:00
|
|
|
}
|
2017-10-23 15:56:29 +00:00
|
|
|
|
2017-10-23 19:33:08 +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{}
|
2017-10-23 19:33:08 +00:00
|
|
|
client, _ := ethclient.Dial(ipcPath)
|
2017-10-23 18:58:33 +00:00
|
|
|
blockchain.client = client
|
|
|
|
return &blockchain
|
|
|
|
}
|
2017-10-24 14:24:07 +00:00
|
|
|
|
2017-11-02 11:41:24 +00:00
|
|
|
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)
|
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() {
|
2017-10-23 18:58:33 +00:00
|
|
|
myContext := context.Background()
|
2017-11-02 11:41:24 +00:00
|
|
|
for header := range blockchain.readGethHeaders {
|
2017-10-23 20:02:56 +00:00
|
|
|
gethBlock, _ := blockchain.client.BlockByNumber(myContext, header.Number)
|
2017-11-02 11:41:24 +00:00
|
|
|
block := GethBlockToCoreBlock(gethBlock)
|
|
|
|
blockchain.outputBlocks <- block
|
2017-10-23 18:58:33 +00:00
|
|
|
}
|
2017-10-23 15:56:29 +00:00
|
|
|
}
|