2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
2017-12-07 19:32:16 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-10 15:00:55 +00:00
|
|
|
"fmt"
|
2018-03-07 21:29:21 +00:00
|
|
|
"regexp"
|
2019-10-28 19:40:10 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-03-27 21:06:12 +00:00
|
|
|
|
2018-01-08 20:19:42 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-12-07 19:32:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2019-10-28 19:40:10 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
2017-12-07 19:32:16 +00:00
|
|
|
)
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
type IPropertiesReader interface {
|
2018-03-07 21:29:21 +00:00
|
|
|
NodeInfo() (id string, name string)
|
2019-10-18 16:16:19 +00:00
|
|
|
NetworkID() float64
|
2018-03-07 21:29:21 +00:00
|
|
|
GenesisBlock() string
|
2017-12-07 19:32:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
type PropertiesReader struct {
|
2019-10-18 16:16:19 +00:00
|
|
|
client core.RPCClient
|
2018-03-07 21:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ParityClient struct {
|
2018-07-20 16:37:46 +00:00
|
|
|
PropertiesReader
|
2018-03-07 21:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GethClient struct {
|
2018-07-20 16:37:46 +00:00
|
|
|
PropertiesReader
|
2018-03-07 21:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type InfuraClient struct {
|
2018-07-20 16:37:46 +00:00
|
|
|
PropertiesReader
|
2018-03-07 21:29:21 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 18:53:08 +00:00
|
|
|
type GanacheClient struct {
|
|
|
|
PropertiesReader
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func MakeNode(rpcClient core.RPCClient) core.Node {
|
2018-07-20 16:37:46 +00:00
|
|
|
pr := makePropertiesReader(rpcClient)
|
|
|
|
id, name := pr.NodeInfo()
|
|
|
|
return core.Node{
|
|
|
|
GenesisBlock: pr.GenesisBlock(),
|
2020-02-10 15:00:55 +00:00
|
|
|
NetworkID: fmt.Sprintf("%f", pr.NetworkID()),
|
2018-07-20 16:37:46 +00:00
|
|
|
ID: id,
|
|
|
|
ClientName: name,
|
2018-03-07 21:29:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func makePropertiesReader(client core.RPCClient) IPropertiesReader {
|
2018-07-20 16:37:46 +00:00
|
|
|
switch getNodeType(client) {
|
2018-03-07 21:29:21 +00:00
|
|
|
case core.GETH:
|
2018-07-20 16:37:46 +00:00
|
|
|
return GethClient{PropertiesReader: PropertiesReader{client: client}}
|
2018-03-07 21:29:21 +00:00
|
|
|
case core.PARITY:
|
2018-07-20 16:37:46 +00:00
|
|
|
return ParityClient{PropertiesReader: PropertiesReader{client: client}}
|
2018-03-07 21:29:21 +00:00
|
|
|
case core.INFURA:
|
2018-07-20 16:37:46 +00:00
|
|
|
return InfuraClient{PropertiesReader: PropertiesReader{client: client}}
|
2018-11-15 18:53:08 +00:00
|
|
|
case core.GANACHE:
|
|
|
|
return GanacheClient{PropertiesReader: PropertiesReader{client: client}}
|
2018-03-07 21:29:21 +00:00
|
|
|
default:
|
2018-07-20 16:37:46 +00:00
|
|
|
return PropertiesReader{client: client}
|
2017-12-07 19:32:16 +00:00
|
|
|
}
|
2018-01-10 21:54:36 +00:00
|
|
|
}
|
2017-12-07 19:32:16 +00:00
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func getNodeType(client core.RPCClient) core.NodeType {
|
2020-02-25 22:38:27 +00:00
|
|
|
// TODO: fix this
|
|
|
|
// This heuristics for figuring out the node type are not usefull...
|
|
|
|
// for example we often port forward remote nodes to localhost
|
|
|
|
// and geth does not have to expose the admin api...
|
2018-07-20 16:37:46 +00:00
|
|
|
if strings.Contains(client.IpcPath(), "infura") {
|
|
|
|
return core.INFURA
|
|
|
|
}
|
2018-11-15 18:53:08 +00:00
|
|
|
if strings.Contains(client.IpcPath(), "127.0.0.1") || strings.Contains(client.IpcPath(), "localhost") {
|
|
|
|
return core.GANACHE
|
|
|
|
}
|
2018-07-20 16:37:46 +00:00
|
|
|
modules, _ := client.SupportedModules()
|
|
|
|
if _, ok := modules["admin"]; ok {
|
|
|
|
return core.GETH
|
2018-03-07 21:29:21 +00:00
|
|
|
}
|
2018-07-20 16:37:46 +00:00
|
|
|
return core.PARITY
|
2018-03-07 21:29:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (reader PropertiesReader) NetworkID() float64 {
|
2018-01-08 20:19:42 +00:00
|
|
|
var version string
|
2018-07-20 16:37:46 +00:00
|
|
|
err := reader.client.CallContext(context.Background(), &version, "net_version")
|
2018-03-27 21:06:12 +00:00
|
|
|
if err != nil {
|
2019-10-28 19:40:10 +00:00
|
|
|
log.Error(err)
|
2018-03-27 21:06:12 +00:00
|
|
|
}
|
2019-10-18 16:16:19 +00:00
|
|
|
networkID, _ := strconv.ParseFloat(version, 64)
|
|
|
|
return networkID
|
2018-01-10 21:54:36 +00:00
|
|
|
}
|
2018-01-08 20:19:42 +00:00
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (reader PropertiesReader) GenesisBlock() string {
|
2018-01-08 20:19:42 +00:00
|
|
|
var header *types.Header
|
2018-03-07 21:29:21 +00:00
|
|
|
blockZero := "0x0"
|
|
|
|
includeTransactions := false
|
2019-10-28 19:40:10 +00:00
|
|
|
err := reader.client.CallContext(context.Background(), &header, "eth_getBlockByNumber", blockZero, includeTransactions)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2018-01-10 21:54:36 +00:00
|
|
|
return header.Hash().Hex()
|
2017-12-07 19:32:16 +00:00
|
|
|
}
|
2018-03-07 21:29:21 +00:00
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (reader PropertiesReader) NodeInfo() (string, string) {
|
2018-03-07 21:29:21 +00:00
|
|
|
var info p2p.NodeInfo
|
2019-10-28 19:40:10 +00:00
|
|
|
err := reader.client.CallContext(context.Background(), &info, "admin_nodeInfo")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2018-03-07 21:29:21 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2018-11-15 18:53:08 +00:00
|
|
|
func (client GanacheClient) NodeInfo() (string, string) {
|
|
|
|
return "ganache", "ganache"
|
|
|
|
}
|
|
|
|
|
2018-03-07 21:29:21 +00:00
|
|
|
func (client ParityClient) parityNodeInfo() string {
|
|
|
|
var nodeInfo core.ParityNodeInfo
|
2019-10-28 19:40:10 +00:00
|
|
|
err := client.client.CallContext(context.Background(), &nodeInfo, "parity_versionInfo")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2018-03-07 21:29:21 +00:00
|
|
|
return nodeInfo.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client ParityClient) parityID() string {
|
2019-10-18 16:16:19 +00:00
|
|
|
var enodeID = regexp.MustCompile(`^enode://(.+)@.+$`)
|
2018-03-07 21:29:21 +00:00
|
|
|
var enodeURL string
|
2019-10-28 19:40:10 +00:00
|
|
|
err := client.client.CallContext(context.Background(), &enodeURL, "parity_enode")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2019-10-18 16:16:19 +00:00
|
|
|
enode := enodeID.FindStringSubmatch(enodeURL)
|
2018-03-07 21:29:21 +00:00
|
|
|
if len(enode) < 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return enode[1]
|
|
|
|
}
|