2017-12-07 19:32:16 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2018-01-08 20:19:42 +00:00
|
|
|
"strconv"
|
|
|
|
|
2018-01-06 20:31:53 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
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"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
2018-01-10 21:54:36 +00:00
|
|
|
func Info(client *rpc.Client) core.Node {
|
2017-12-07 19:32:16 +00:00
|
|
|
node := core.Node{}
|
2018-01-10 21:54:36 +00:00
|
|
|
node.NetworkId = NetworkId(client)
|
|
|
|
node.GenesisBlock = GenesisBlock(client)
|
|
|
|
node.Id, node.ClientName = IdClientName(client)
|
2017-12-07 19:32:16 +00:00
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2018-01-10 21:54:36 +00:00
|
|
|
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
|
2017-12-07 19:32:16 +00:00
|
|
|
}
|
2018-01-10 21:54:36 +00:00
|
|
|
return "", ""
|
|
|
|
}
|
2017-12-07 19:32:16 +00:00
|
|
|
|
2018-01-10 21:54:36 +00:00
|
|
|
func NetworkId(client *rpc.Client) float64 {
|
2018-01-08 20:19:42 +00:00
|
|
|
var version string
|
|
|
|
client.CallContext(context.Background(), &version, "net_version")
|
2018-01-10 21:54:36 +00:00
|
|
|
networkId, _ := strconv.ParseFloat(version, 64)
|
|
|
|
return networkId
|
|
|
|
}
|
2018-01-08 20:19:42 +00:00
|
|
|
|
2018-01-10 21:54:36 +00:00
|
|
|
func ProtocolVersion(client *rpc.Client) string {
|
2018-01-08 20:19:42 +00:00
|
|
|
var protocolVersion string
|
|
|
|
client.CallContext(context.Background(), &protocolVersion, "eth_protocolVersion")
|
2018-01-10 21:54:36 +00:00
|
|
|
return protocolVersion
|
|
|
|
}
|
2018-01-08 20:19:42 +00:00
|
|
|
|
2018-01-10 21:54:36 +00:00
|
|
|
func GenesisBlock(client *rpc.Client) string {
|
2018-01-08 20:19:42 +00:00
|
|
|
var header *types.Header
|
|
|
|
client.CallContext(context.Background(), &header, "eth_getBlockByNumber", "0x0", false)
|
2018-01-10 21:54:36 +00:00
|
|
|
return header.Hash().Hex()
|
2017-12-07 19:32:16 +00:00
|
|
|
}
|