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"
|
|
|
|
|
2018-01-08 20:19:42 +00:00
|
|
|
"strconv"
|
|
|
|
|
2018-03-07 21:29:21 +00:00
|
|
|
"regexp"
|
|
|
|
|
2018-03-27 21:06:12 +00:00
|
|
|
"log"
|
|
|
|
|
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"
|
2018-02-08 16:12:08 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-07-20 16:37:46 +00:00
|
|
|
"strings"
|
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)
|
|
|
|
NetworkId() float64
|
|
|
|
GenesisBlock() string
|
2017-12-07 19:32:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
type PropertiesReader struct {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
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,
|
2018-03-07 21:29:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func makePropertiesReader(client core.RpcClient) IPropertiesReader {
|
|
|
|
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
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func getNodeType(client core.RpcClient) core.NodeType {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +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 {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2018-01-10 21:54:36 +00:00
|
|
|
networkId, _ := strconv.ParseFloat(version, 64)
|
|
|
|
return networkId
|
|
|
|
}
|
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
|
2018-07-20 16:37:46 +00:00
|
|
|
reader.client.CallContext(context.Background(), &header, "eth_getBlockByNumber", blockZero, includeTransactions)
|
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
|
2018-07-20 16:37:46 +00:00
|
|
|
reader.client.CallContext(context.Background(), &info, "admin_nodeInfo")
|
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
|
2018-07-20 16:37:46 +00:00
|
|
|
client.client.CallContext(context.Background(), &nodeInfo, "parity_versionInfo")
|
2018-03-07 21:29:21 +00:00
|
|
|
return nodeInfo.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client ParityClient) parityID() string {
|
|
|
|
var enodeId = regexp.MustCompile(`^enode://(.+)@.+$`)
|
|
|
|
var enodeURL string
|
2018-07-20 16:37:46 +00:00
|
|
|
client.client.CallContext(context.Background(), &enodeURL, "parity_enode")
|
2018-03-07 21:29:21 +00:00
|
|
|
enode := enodeId.FindStringSubmatch(enodeURL)
|
|
|
|
if len(enode) < 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return enode[1]
|
|
|
|
}
|