/
rpc.go
184 lines (164 loc) · 5.03 KB
/
rpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package ethnode
import (
"context"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/rpc"
)
// NodeKind represents the different kinds of node implementations we know about.
type NodeKind int
const (
Unknown NodeKind = iota // We'll treat unknown as Geth, just in case.
Geth
Parity
)
type NetworkID int
const (
Mainnet NetworkID = 1
Morden NetworkID = 2
Ropsten NetworkID = 3
Rinkeby NetworkID = 4
Kovan NetworkID = 42
)
func (id NetworkID) String() string {
switch id {
case Mainnet:
return "mainnet"
case Morden:
return "morden"
case Ropsten:
return "ropsten"
case Rinkeby:
return "rinkeby"
case Kovan:
return "kovan"
}
return "unknown"
}
// Is compares the ID to a network name.
func (id NetworkID) Is(network string) bool {
return id.String() == strings.ToLower(network)
}
func (n NodeKind) String() string {
switch n {
case Geth:
return "geth"
case Parity:
return "parity"
default:
return "unknown"
}
}
// UserAgent is the metadata about node client.
type UserAgent struct {
Version string // Result of web3_clientVersion
EthProtocol string // Result of eth_protocolVersion
// Parsed/derived values
Kind NodeKind // Node implementation
Network NetworkID // Network ID
IsFullNode bool // Is this a full node? (or a light client?)
}
// ParseUserAgent takes string values as output from the web3 RPC for
// web3_clientVersion, eth_protocolVersion, and net_version. It returns a
// parsed user agent metadata.
func ParseUserAgent(clientVersion, protocolVersion, netVersion string) (*UserAgent, error) {
networkID, err := strconv.Atoi(netVersion)
if err != nil {
return nil, err
}
agent := &UserAgent{
Version: clientVersion,
EthProtocol: protocolVersion,
Network: NetworkID(networkID),
IsFullNode: true,
}
if strings.HasPrefix(agent.Version, "Geth/") {
agent.Kind = Geth
} else if strings.HasPrefix(agent.Version, "Parity-Ethereum/") || strings.HasPrefix(agent.Version, "Parity/") {
agent.Kind = Parity
}
protocol, err := strconv.ParseInt(protocolVersion, 0, 32)
if err != nil {
return nil, err
}
// FIXME: Can't find any docs on how this protocol value is supposed to be
// parsed, so just using anecdotal values for now.
if agent.Kind == Parity && protocol == 1 {
agent.IsFullNode = false
} else if agent.Kind == Geth && protocol == 10002 {
agent.IsFullNode = false
}
return agent, nil
}
// Dial is a wrapper around go-ethereum/rpc.Dial with client detection.
func Dial(ctx context.Context, uri string) (EthNode, error) {
client, err := rpc.DialContext(ctx, uri)
if err != nil {
return nil, err
}
return RemoteNode(client)
}
// DetectClient queries the RPC API to determine which kind of node is running.
func DetectClient(client *rpc.Client) (*UserAgent, error) {
var clientVersion string
if err := client.Call(&clientVersion, "web3_clientVersion"); err != nil {
return nil, err
}
var protocolVersion string
if err := client.Call(&protocolVersion, "eth_protocolVersion"); err != nil {
return nil, err
}
var netVersion string
if err := client.Call(&netVersion, "net_version"); err != nil {
return nil, err
}
return ParseUserAgent(clientVersion, protocolVersion, netVersion)
}
// PeerInfo stores the node ID and client metadata about a peer.
type PeerInfo struct {
ID string `json:"id"` // Unique node identifier (also the encryption pubkey)
Name string `json:"name"` // Name of the node, including client type, version, OS, custom data
}
// EthNode is the normalized interface between different kinds of nodes.
type EthNode interface {
ContractBackend() bind.ContractBackend
// Kind returns the kind of node this is.
Kind() NodeKind
// Enode returns this node's enode://...
Enode(ctx context.Context) (string, error)
// AddTrustedPeer adds a nodeID to a set of nodes that can always connect, even
// if the maximum number of connections is reached.
AddTrustedPeer(ctx context.Context, nodeID string) error
// RemoveTrustedPeer removes a nodeID from the trusted node set.
RemoveTrustedPeer(ctx context.Context, nodeID string) error
// ConnectPeer prompts a connection to the given nodeURI.
ConnectPeer(ctx context.Context, nodeURI string) error
// DisconnectPeer disconnects from the given nodeID, if connected.
DisconnectPeer(ctx context.Context, nodeID string) error
// Peers returns the list of connected peers
Peers(ctx context.Context) ([]PeerInfo, error)
// BlockNumber returns the current sync'd block number.
BlockNumber(ctx context.Context) (uint64, error)
}
// RemoteNode autodetects the node kind and returns the appropriate EthNode
// implementation.
func RemoteNode(client *rpc.Client) (EthNode, error) {
version, err := DetectClient(client)
if err != nil {
return nil, err
}
switch version.Kind {
case Parity:
return &parityNode{client: client}, nil
default:
// Treat everything else as Geth
// FIXME: Is this a bad idea?
node := &gethNode{client: client}
ctx := context.TODO()
if err := node.CheckCompatible(ctx); err != nil {
return nil, err
}
return node, nil
}
}