ipld-eth-server/pkg/geth/node/node.go

50 lines
1.3 KiB
Go
Raw Normal View History

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"
"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 {
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)
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
}
2018-01-10 21:54:36 +00:00
return "", ""
}
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()
}