Rename geth to eth, signifying client independence (#161)

This commit is contained in:
Edvard Hübinette
2019-10-28 12:30:24 +01:00
committed by GitHub
parent f7c4a6736d
commit 3fff2896aa
52 changed files with 111 additions and 111 deletions
+153
View File
@@ -0,0 +1,153 @@
// VulcanizeDB
// Copyright © 2019 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package node
import (
"context"
"strconv"
"regexp"
"log"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/p2p"
"github.com/vulcanize/vulcanizedb/pkg/core"
"strings"
)
type IPropertiesReader interface {
NodeInfo() (id string, name string)
NetworkId() float64
GenesisBlock() string
}
type PropertiesReader struct {
client core.RpcClient
}
type ParityClient struct {
PropertiesReader
}
type GethClient struct {
PropertiesReader
}
type InfuraClient struct {
PropertiesReader
}
type GanacheClient struct {
PropertiesReader
}
func MakeNode(rpcClient core.RpcClient) core.Node {
pr := makePropertiesReader(rpcClient)
id, name := pr.NodeInfo()
return core.Node{
GenesisBlock: pr.GenesisBlock(),
NetworkID: pr.NetworkId(),
ID: id,
ClientName: name,
}
}
func makePropertiesReader(client core.RpcClient) IPropertiesReader {
switch getNodeType(client) {
case core.GETH:
return GethClient{PropertiesReader: PropertiesReader{client: client}}
case core.PARITY:
return ParityClient{PropertiesReader: PropertiesReader{client: client}}
case core.INFURA:
return InfuraClient{PropertiesReader: PropertiesReader{client: client}}
case core.GANACHE:
return GanacheClient{PropertiesReader: PropertiesReader{client: client}}
default:
return PropertiesReader{client: client}
}
}
func getNodeType(client core.RpcClient) core.NodeType {
if strings.Contains(client.IpcPath(), "infura") {
return core.INFURA
}
if strings.Contains(client.IpcPath(), "127.0.0.1") || strings.Contains(client.IpcPath(), "localhost") {
return core.GANACHE
}
modules, _ := client.SupportedModules()
if _, ok := modules["admin"]; ok {
return core.GETH
}
return core.PARITY
}
func (reader PropertiesReader) NetworkId() float64 {
var version string
err := reader.client.CallContext(context.Background(), &version, "net_version")
if err != nil {
log.Println(err)
}
networkId, _ := strconv.ParseFloat(version, 64)
return networkId
}
func (reader PropertiesReader) GenesisBlock() string {
var header *types.Header
blockZero := "0x0"
includeTransactions := false
reader.client.CallContext(context.Background(), &header, "eth_getBlockByNumber", blockZero, includeTransactions)
return header.Hash().Hex()
}
func (reader PropertiesReader) NodeInfo() (string, string) {
var info p2p.NodeInfo
reader.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 GanacheClient) NodeInfo() (string, string) {
return "ganache", "ganache"
}
func (client ParityClient) parityNodeInfo() string {
var nodeInfo core.ParityNodeInfo
client.client.CallContext(context.Background(), &nodeInfo, "parity_versionInfo")
return nodeInfo.String()
}
func (client ParityClient) parityID() string {
var enodeId = regexp.MustCompile(`^enode://(.+)@.+$`)
var enodeURL string
client.client.CallContext(context.Background(), &enodeURL, "parity_enode")
enode := enodeId.FindStringSubmatch(enodeURL)
if len(enode) < 2 {
return ""
}
return enode[1]
}
+29
View File
@@ -0,0 +1,29 @@
// VulcanizeDB
// Copyright © 2019 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package node_test
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestNode(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Node Suite")
}
+119
View File
@@ -0,0 +1,119 @@
// VulcanizeDB
// Copyright © 2019 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package node_test
import (
"encoding/json"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/eth/node"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
)
var EmpytHeaderHash = "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
var _ = Describe("Node Info", func() {
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 parity ID and client name for parity node", func() {
client := fakes.NewMockRpcClient()
n := node.MakeNode(client)
Expect(n.ID).To(Equal("ParityNode"))
Expect(n.ClientName).To(Equal("Parity/v1.2.3/"))
})
})
It("returns the genesis block for any client", func() {
client := fakes.NewMockRpcClient()
n := node.MakeNode(client)
Expect(n.GenesisBlock).To(Equal(EmpytHeaderHash))
})
It("returns the network id for any client", func() {
client := fakes.NewMockRpcClient()
n := node.MakeNode(client)
Expect(n.NetworkID).To(Equal(float64(1234)))
})
It("returns geth ID and client name for geth node", func() {
client := fakes.NewMockRpcClient()
supportedModules := make(map[string]string)
supportedModules["admin"] = "ok"
client.SetSupporedModules(supportedModules)
n := node.MakeNode(client)
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() {
client := fakes.NewMockRpcClient()
client.SetIpcPath("infura/path")
n := node.MakeNode(client)
Expect(n.ID).To(Equal("infura"))
Expect(n.ClientName).To(Equal("infura"))
})
It("returns local id and client name for Local node", func() {
client := fakes.NewMockRpcClient()
client.SetIpcPath("127.0.0.1")
n := node.MakeNode(client)
Expect(n.ID).To(Equal("ganache"))
Expect(n.ClientName).To(Equal("ganache"))
client.SetIpcPath("localhost")
n = node.MakeNode(client)
Expect(n.ID).To(Equal("ganache"))
Expect(n.ClientName).To(Equal("ganache"))
})
})