Allow Parity as ingest node (#36)
* Upgrade go-ethereum to v1.8 * Add Node Info for parity nodes * Upgrade start_private_blockchain to use v1.8
This commit is contained in:
+100
-23
@@ -5,45 +5,122 @@ import (
|
||||
|
||||
"strconv"
|
||||
|
||||
"regexp"
|
||||
|
||||
"strings"
|
||||
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
)
|
||||
|
||||
func Info(client *rpc.Client) core.Node {
|
||||
node := core.Node{}
|
||||
node.NetworkID = NetworkID(client)
|
||||
node.GenesisBlock = GenesisBlock(client)
|
||||
node.ID, node.ClientName = IDClientName(client)
|
||||
return node
|
||||
type PropertiesReader interface {
|
||||
NodeInfo() (id string, name string)
|
||||
NetworkId() float64
|
||||
GenesisBlock() string
|
||||
}
|
||||
|
||||
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
|
||||
type ClientWrapper struct {
|
||||
ContextCaller
|
||||
IPCPath string
|
||||
}
|
||||
|
||||
type ContextCaller interface {
|
||||
CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error
|
||||
SupportedModules() (map[string]string, error)
|
||||
}
|
||||
|
||||
type ParityClient struct {
|
||||
ClientWrapper
|
||||
}
|
||||
|
||||
type GethClient struct {
|
||||
ClientWrapper
|
||||
}
|
||||
|
||||
type InfuraClient struct {
|
||||
ClientWrapper
|
||||
}
|
||||
|
||||
func (clientWrapper ClientWrapper) NodeType() core.NodeType {
|
||||
if strings.Contains(clientWrapper.IPCPath, "infura") {
|
||||
return core.INFURA
|
||||
}
|
||||
return "", ""
|
||||
modules, _ := clientWrapper.SupportedModules()
|
||||
if _, ok := modules["admin"]; ok {
|
||||
return core.GETH
|
||||
}
|
||||
return core.PARITY
|
||||
}
|
||||
|
||||
func NetworkID(client *rpc.Client) float64 {
|
||||
func makePropertiesReader(wrapper ClientWrapper) PropertiesReader {
|
||||
switch wrapper.NodeType() {
|
||||
case core.GETH:
|
||||
return GethClient{ClientWrapper: wrapper}
|
||||
case core.PARITY:
|
||||
return ParityClient{ClientWrapper: wrapper}
|
||||
case core.INFURA:
|
||||
return InfuraClient{ClientWrapper: wrapper}
|
||||
default:
|
||||
return wrapper
|
||||
}
|
||||
}
|
||||
|
||||
func MakeNode(wrapper ClientWrapper) core.Node {
|
||||
pr := makePropertiesReader(wrapper)
|
||||
id, name := pr.NodeInfo()
|
||||
return core.Node{
|
||||
GenesisBlock: pr.GenesisBlock(),
|
||||
NetworkID: pr.NetworkId(),
|
||||
ID: id,
|
||||
ClientName: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (client ClientWrapper) NetworkId() 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 {
|
||||
func (client ClientWrapper) GenesisBlock() string {
|
||||
var header *types.Header
|
||||
client.CallContext(context.Background(), &header, "eth_getBlockByNumber", "0x0", false)
|
||||
blockZero := "0x0"
|
||||
includeTransactions := false
|
||||
client.CallContext(context.Background(), &header, "eth_getBlockByNumber", blockZero, includeTransactions)
|
||||
return header.Hash().Hex()
|
||||
}
|
||||
|
||||
func (client ClientWrapper) NodeInfo() (string, string) {
|
||||
var info p2p.NodeInfo
|
||||
client.CallContext(context.Background(), &info, "admin_nodeInfo")
|
||||
return info.ID, info.Name
|
||||
}
|
||||
|
||||
func (client ParityClient) NodeInfo() (string, string) {
|
||||
nodeInfo := client.parityNodeInfo()
|
||||
id := client.parityID()
|
||||
return id, nodeInfo
|
||||
}
|
||||
|
||||
func (client InfuraClient) NodeInfo() (string, string) {
|
||||
return "infura", "infura"
|
||||
}
|
||||
|
||||
func (client ParityClient) parityNodeInfo() string {
|
||||
var nodeInfo core.ParityNodeInfo
|
||||
client.CallContext(context.Background(), &nodeInfo, "parity_versionInfo")
|
||||
return nodeInfo.String()
|
||||
}
|
||||
|
||||
func (client ParityClient) parityID() string {
|
||||
var enodeId = regexp.MustCompile(`^enode://(.+)@.+$`)
|
||||
var enodeURL string
|
||||
client.CallContext(context.Background(), &enodeURL, "parity_enode")
|
||||
enode := enodeId.FindStringSubmatch(enodeURL)
|
||||
if len(enode) < 2 {
|
||||
return ""
|
||||
}
|
||||
return enode[1]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package node_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestNode(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Node Suite")
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package node_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"context"
|
||||
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
|
||||
)
|
||||
|
||||
type MockContextCaller struct {
|
||||
nodeType core.NodeType
|
||||
}
|
||||
|
||||
var EmpytHeaderHash = "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
|
||||
|
||||
func (MockContextCaller) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
|
||||
switch method {
|
||||
case "admin_nodeInfo":
|
||||
if p, ok := result.(*p2p.NodeInfo); ok {
|
||||
p.ID = "enode://GethNode@172.17.0.1:30303"
|
||||
p.Name = "Geth/v1.7"
|
||||
}
|
||||
case "eth_getBlockByNumber":
|
||||
if p, ok := result.(*types.Header); ok {
|
||||
*p = types.Header{}
|
||||
}
|
||||
|
||||
case "parity_versionInfo":
|
||||
if p, ok := result.(*core.ParityNodeInfo); ok {
|
||||
*p = core.ParityNodeInfo{
|
||||
Track: "",
|
||||
ParityVersion: core.ParityVersion{
|
||||
Major: 1,
|
||||
Minor: 2,
|
||||
Patch: 3,
|
||||
},
|
||||
Hash: "",
|
||||
}
|
||||
}
|
||||
case "parity_enode":
|
||||
if p, ok := result.(*string); ok {
|
||||
*p = "enode://ParityNode@172.17.0.1:30303"
|
||||
}
|
||||
case "net_version":
|
||||
if p, ok := result.(*string); ok {
|
||||
*p = "1234"
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mcc MockContextCaller) SupportedModules() (map[string]string, error) {
|
||||
result := make(map[string]string)
|
||||
if mcc.nodeType == core.GETH {
|
||||
result["admin"] = "ok"
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
var _ = Describe("Parity Node Info", func() {
|
||||
|
||||
It("verifies parity_versionInfo can be unmarshalled into ParityNodeInfo", func() {
|
||||
var parityNodeInfo core.ParityNodeInfo
|
||||
nodeInfoJSON := []byte(
|
||||
`{
|
||||
"hash": "0x2ae8b4ca278dd7b896090366615fef81cbbbc0e0",
|
||||
"track": "null",
|
||||
"version": {
|
||||
"major": 1,
|
||||
"minor": 6,
|
||||
"patch": 0
|
||||
}
|
||||
}`)
|
||||
json.Unmarshal(nodeInfoJSON, &parityNodeInfo)
|
||||
Expect(parityNodeInfo.Hash).To(Equal("0x2ae8b4ca278dd7b896090366615fef81cbbbc0e0"))
|
||||
Expect(parityNodeInfo.Track).To(Equal("null"))
|
||||
Expect(parityNodeInfo.Major).To(Equal(1))
|
||||
Expect(parityNodeInfo.Minor).To(Equal(6))
|
||||
Expect(parityNodeInfo.Patch).To(Equal(0))
|
||||
})
|
||||
|
||||
It("Creates client string", func() {
|
||||
parityNodeInfo := core.ParityNodeInfo{
|
||||
Track: "null",
|
||||
ParityVersion: core.ParityVersion{
|
||||
Major: 1,
|
||||
Minor: 6,
|
||||
Patch: 0,
|
||||
},
|
||||
Hash: "0x1232144j",
|
||||
}
|
||||
Expect(parityNodeInfo.String()).To(Equal("Parity/v1.6.0/"))
|
||||
})
|
||||
|
||||
It("returns the genesis block for any client", func() {
|
||||
mcc := MockContextCaller{}
|
||||
cw := node.ClientWrapper{ContextCaller: mcc}
|
||||
n := node.MakeNode(cw)
|
||||
Expect(n.GenesisBlock).To(Equal(EmpytHeaderHash))
|
||||
})
|
||||
|
||||
It("returns the network id for any client", func() {
|
||||
mcc := MockContextCaller{}
|
||||
cw := node.ClientWrapper{ContextCaller: mcc}
|
||||
n := node.MakeNode(cw)
|
||||
Expect(n.NetworkID).To(Equal(float64(1234)))
|
||||
})
|
||||
|
||||
It("returns parity ID and client name for parity node", func() {
|
||||
mcc := MockContextCaller{core.PARITY}
|
||||
cw := node.ClientWrapper{ContextCaller: mcc}
|
||||
n := node.MakeNode(cw)
|
||||
Expect(n.ID).To(Equal("ParityNode"))
|
||||
Expect(n.ClientName).To(Equal("Parity/v1.2.3/"))
|
||||
})
|
||||
|
||||
It("returns geth ID and client name for geth node", func() {
|
||||
mcc := MockContextCaller{core.GETH}
|
||||
cw := node.ClientWrapper{ContextCaller: mcc}
|
||||
n := node.MakeNode(cw)
|
||||
Expect(n.ID).To(Equal("enode://GethNode@172.17.0.1:30303"))
|
||||
Expect(n.ClientName).To(Equal("Geth/v1.7"))
|
||||
})
|
||||
|
||||
It("returns infura ID and client name for infura node", func() {
|
||||
mcc := MockContextCaller{core.INFURA}
|
||||
cw := node.ClientWrapper{mcc, "https://mainnet.infura.io/123"}
|
||||
n := node.MakeNode(cw)
|
||||
Expect(n.ID).To(Equal("infura"))
|
||||
Expect(n.ClientName).To(Equal("infura"))
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user