Update BlockChain to record NodeInfo (#95)

This commit is contained in:
Matt K
2017-12-07 13:32:16 -06:00
committed by GitHub
parent 18163f970e
commit 921bde1089
21 changed files with 276 additions and 45 deletions
+10 -1
View File
@@ -6,9 +6,11 @@ import (
"math/big"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/geth/node"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"golang.org/x/net/context"
)
@@ -17,6 +19,11 @@ type GethBlockchain struct {
readGethHeaders chan *types.Header
outputBlocks chan core.Block
newHeadSubscription ethereum.Subscription
node core.Node
}
func (blockchain *GethBlockchain) Node() core.Node {
return blockchain.node
}
func (blockchain *GethBlockchain) GetBlockByNumber(blockNumber int64) core.Block {
@@ -26,7 +33,9 @@ func (blockchain *GethBlockchain) GetBlockByNumber(blockNumber int64) core.Block
func NewGethBlockchain(ipcPath string) *GethBlockchain {
blockchain := GethBlockchain{}
client, _ := ethclient.Dial(ipcPath)
rpcClient, _ := rpc.Dial(ipcPath)
client := ethclient.NewClient(rpcClient)
blockchain.node = node.Retrieve(rpcClient)
blockchain.client = client
return &blockchain
}
+32
View File
@@ -0,0 +1,32 @@
package node
import (
"context"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
)
func Retrieve(client *rpc.Client) core.Node {
var info p2p.NodeInfo
node := core.Node{}
client.CallContext(context.Background(), &info, "admin_nodeInfo")
for protocolName, protocol := range info.Protocols {
if protocolName == "eth" {
protocolMap, _ := protocol.(map[string]interface{})
node.GenesisBlock = getAttribute(protocolMap, "genesis").(string)
node.NetworkId = getAttribute(protocolMap, "network").(float64)
}
}
return node
}
func getAttribute(protocolMap map[string]interface{}, protocol string) interface{} {
for key, val := range protocolMap {
if key == protocol {
return val
}
}
return nil
}