ipld-eth-server/core/geth_blockchain.go
Matt Krump 571bc7f63a Add integration test
* Update Travis to run integration tests
2017-10-24 15:36:50 -05:00

56 lines
1.5 KiB
Go

package core
import (
"fmt"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"golang.org/x/net/context"
"reflect"
)
type GethBlockchain struct {
client *ethclient.Client
observers []BlockchainObserver
subscription ethereum.Subscription
}
func NewGethBlockchain(ipcPath string) *GethBlockchain {
fmt.Printf("Creating Geth Blockchain to: %s\n", ipcPath)
blockchain := GethBlockchain{}
client, _ := ethclient.Dial(ipcPath)
// TODO: handle error gracefully
blockchain.client = client
return &blockchain
}
func (blockchain GethBlockchain) notifyObservers(getBlock *types.Block) {
block := convertBlock(getBlock)
for _, observer := range blockchain.observers {
observer.NotifyBlockAdded(block)
}
}
func convertBlock(gethBlock *types.Block) Block {
return Block{
Number: gethBlock.Number(),
NumberOfTransactions: len(gethBlock.Transactions()),
}
}
func (blockchain *GethBlockchain) RegisterObserver(observer BlockchainObserver) {
fmt.Printf("Registering observer: %v\n", reflect.TypeOf(observer))
blockchain.observers = append(blockchain.observers, observer)
}
func (blockchain *GethBlockchain) SubscribeToEvents() {
headers := make(chan *types.Header, 10)
myContext := context.Background()
sub, _ := blockchain.client.SubscribeNewHead(myContext, headers)
blockchain.subscription = sub
for header := range headers {
gethBlock, _ := blockchain.client.BlockByNumber(myContext, header.Number)
blockchain.notifyObservers(gethBlock)
}
}