Allow for multiple Geth nodes (#128)

This commit is contained in:
Matt K
2018-01-10 15:54:36 -06:00
committed by GitHub
parent f41bf49b0e
commit a9bea4f492
12 changed files with 98 additions and 26 deletions
+14 -1
View File
@@ -3,6 +3,8 @@ package geth
import (
"math/big"
"strings"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/geth/node"
"github.com/ethereum/go-ethereum"
@@ -25,11 +27,22 @@ func NewBlockchain(ipcPath string) *Blockchain {
blockchain := Blockchain{}
rpcClient, _ := rpc.Dial(ipcPath)
client := ethclient.NewClient(rpcClient)
blockchain.node = node.Retrieve(rpcClient)
blockchain.node = node.Info(rpcClient)
if infura := isInfuraNode(ipcPath); infura {
blockchain.node.Id = "infura"
blockchain.node.ClientName = "infura"
}
blockchain.client = client
return &blockchain
}
func isInfuraNode(ipcPath string) bool {
if strings.Contains(ipcPath, "infura") {
return true
}
return false
}
func (blockchain *Blockchain) GetLogs(contract core.Contract, startingBlockNumber *big.Int, endingBlockNumber *big.Int) ([]core.Log, error) {
if endingBlockNumber == nil {
endingBlockNumber = startingBlockNumber
+34 -13
View File
@@ -7,22 +7,43 @@ import (
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
)
func Retrieve(client *rpc.Client) core.Node {
func Info(client *rpc.Client) core.Node {
node := core.Node{}
var version string
client.CallContext(context.Background(), &version, "net_version")
node.NetworkId, _ = strconv.ParseFloat(version, 64)
var protocolVersion string
client.CallContext(context.Background(), &protocolVersion, "eth_protocolVersion")
var header *types.Header
client.CallContext(context.Background(), &header, "eth_getBlockByNumber", "0x0", false)
node.GenesisBlock = header.Hash().Hex()
node.NetworkId = NetworkId(client)
node.GenesisBlock = GenesisBlock(client)
node.Id, node.ClientName = IdClientName(client)
return node
}
func IdClientName(client *rpc.Client) (string, string) {
var info p2p.NodeInfo
modules, _ := client.SupportedModules()
if _, ok := modules["admin"]; ok {
client.CallContext(context.Background(), &info, "admin_nodeInfo")
return info.ID, info.Name
}
return "", ""
}
func NetworkId(client *rpc.Client) float64 {
var version string
client.CallContext(context.Background(), &version, "net_version")
networkId, _ := strconv.ParseFloat(version, 64)
return networkId
}
func ProtocolVersion(client *rpc.Client) string {
var protocolVersion string
client.CallContext(context.Background(), &protocolVersion, "eth_protocolVersion")
return protocolVersion
}
func GenesisBlock(client *rpc.Client) string {
var header *types.Header
client.CallContext(context.Background(), &header, "eth_getBlockByNumber", "0x0", false)
return header.Hash().Hex()
}