-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathparity.go
More file actions
167 lines (146 loc) · 4.78 KB
/
Copy pathparity.go
File metadata and controls
167 lines (146 loc) · 4.78 KB
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
package ethnode
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
)
var _ EthNode = &parityNode{}
type parityClient struct {
CanHandleLargeRequests bool `json:"can_handle_large_requests"`
Compiler string `json:"compiler"`
Identity string `json:"identity"`
Name string `json:"name"`
OS string `json:"os"`
Semver string `json:"semver"`
}
func (c parityClient) String() string {
return fmt.Sprintf("Parity/%s/%s/%s", c.Semver, c.OS, c.Compiler)
}
// parityPeerInfo is a clone of the PeerInfo type but with polymorphic parsing for Name
// to support both string and parityClient types.
type parityPeerInfo struct {
ID string `json:"id"`
Name json.RawMessage `json:"name"`
Caps []string `json:"caps"`
Protocols map[string]json.RawMessage `json:"protocols"`
Network struct {
LocalAddress string `json:"localAddress"`
RemoteAddress string `json:"remoteAddress"`
} `json:"network"`
}
// PeerInfo converts parityPeerInfo into PeerInfo
func (pi parityPeerInfo) PeerInfo() (PeerInfo, error) {
r := PeerInfo{
ID: pi.ID,
Caps: pi.Caps,
Protocols: pi.Protocols,
Network: pi.Network,
}
if bytes.HasPrefix(pi.Name, []byte(`"`)) {
// Name is a string
if err := json.Unmarshal(pi.Name, &r.Name); err != nil {
return r, err
}
} else {
// Name is a ParityClient thing
var name struct {
ParityClient parityClient `json:"ParityClient"`
}
if err := json.Unmarshal(pi.Name, &name); err != nil {
return r, err
}
r.Name = name.ParityClient.String()
}
return r, nil
}
type parityPeers struct {
Peers []parityPeerInfo `json:"peers"`
}
type parityNode struct {
baseNode
}
func (n *parityNode) Kind() NodeKind {
return Parity
}
func (n *parityNode) ConnectPeer(ctx context.Context, nodeURI string) error {
// Parity doesn't have a way to just add peers, so we overload
// addReservedPeer for this.
return n.AddTrustedPeer(ctx, parityNodeID(nodeURI))
}
func (n *parityNode) DisconnectPeer(ctx context.Context, nodeID string) error {
// Parity doesn't have a way to drop a specific peer, so we overload
// removeReservedPeer for this.
return n.RemoveTrustedPeer(ctx, parityNodeID(nodeID))
}
func (n *parityNode) AddTrustedPeer(ctx context.Context, nodeID string) error {
var result interface{}
return n.client.CallContext(ctx, &result, "parity_addReservedPeer", parityNodeID(nodeID))
}
func (n *parityNode) RemoveTrustedPeer(ctx context.Context, nodeID string) error {
var result interface{}
return n.client.CallContext(ctx, &result, "parity_removeReservedPeer", parityNodeID(nodeID))
}
func (n *parityNode) Peers(ctx context.Context) ([]PeerInfo, error) {
var result parityPeers
err := n.client.CallContext(ctx, &result, "parity_netPeers")
if err != nil {
return nil, err
}
return filterActivePeers(result.Peers)
}
func (n *parityNode) Enode(ctx context.Context) (string, error) {
var result string
if err := n.client.CallContext(ctx, &result, "parity_enode"); err != nil {
return "", err
}
return result, nil
}
func (n *parityNode) CheckCompatible(ctx context.Context) error {
var result interface{}
if err := n.client.CallContext(ctx, &result, "parity_enode"); err != nil {
return parityWrapModuleError(err, "parity_enode", "parity")
}
if err := n.client.CallContext(ctx, &result, "parity_addReservedPeer", ""); err != nil {
return parityWrapModuleError(err, "parity_addReservedPeer", "parity_set")
}
return nil
}
// filterActivePeers filters out any peers that have not completed the
// handshake yet. In Parity, these are peers without any specified Protocols.
func filterActivePeers(peers []parityPeerInfo) ([]PeerInfo, error) {
activePeers := make([]PeerInfo, 0, len(peers))
for _, peer := range peers {
if len(peer.Protocols) == 0 {
continue
}
peerInfo, err := peer.PeerInfo()
if err != nil {
return nil, err
}
activePeers = append(activePeers, peerInfo)
}
return activePeers, nil
}
// parityNodeID fixes nodeIDs with an enode:// scheme and an empty host.
// FIXME: We should supply a fully qualified enode:// string instead of nodeIDs
// in the future and not need this.
func parityNodeID(nodeID string) string {
if strings.HasPrefix(nodeID, "enode://") {
return nodeID
}
return "enode://" + nodeID + "@[::]:30303"
}
func parityWrapModuleError(err error, method string, module string) error {
if err == nil {
return nil
}
if jsonErr, ok := err.(interface{ ErrorCode() int }); ok && jsonErr.ErrorCode() == errCodeMethodNotFound {
return CompatibilityError{
Err: err,
Explanation: fmt.Sprintf(`Parity RPC method %q is not available, add %q to allowed RPC modules. For example, run parity with the flag --ipc-apis="safe,parity_set"`, method, module),
}
}
return err
}