Skip to content

Commit 91726e8

Browse files
authored
les: allow either full enode strings or raw hex ids in the API (#22423)
1 parent 6d9707a commit 91726e8

1 file changed

Lines changed: 45 additions & 6 deletions

File tree

‎les/api.go‎

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ func NewPrivateLightServerAPI(server *LesServer) *PrivateLightServerAPI {
4949
}
5050
}
5151

52+
// parseNode parses either an enode address a raw hex node id
53+
func parseNode(node string) (enode.ID, error) {
54+
if id, err := enode.ParseID(node); err == nil {
55+
return id, nil
56+
}
57+
if node, err := enode.Parse(enode.ValidSchemes, node); err == nil {
58+
return node.ID(), nil
59+
} else {
60+
return enode.ID{}, err
61+
}
62+
}
63+
5264
// ServerInfo returns global server parameters
5365
func (api *PrivateLightServerAPI) ServerInfo() map[string]interface{} {
5466
res := make(map[string]interface{})
@@ -59,7 +71,14 @@ func (api *PrivateLightServerAPI) ServerInfo() map[string]interface{} {
5971
}
6072

6173
// ClientInfo returns information about clients listed in the ids list or matching the given tags
62-
func (api *PrivateLightServerAPI) ClientInfo(ids []enode.ID) map[enode.ID]map[string]interface{} {
74+
func (api *PrivateLightServerAPI) ClientInfo(nodes []string) map[enode.ID]map[string]interface{} {
75+
var ids []enode.ID
76+
for _, node := range nodes {
77+
if id, err := parseNode(node); err == nil {
78+
ids = append(ids, id)
79+
}
80+
}
81+
6382
res := make(map[enode.ID]map[string]interface{})
6483
api.server.clientPool.forClients(ids, func(client *clientInfo) {
6584
res[client.node.ID()] = api.clientInfo(client)
@@ -159,8 +178,18 @@ func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, clien
159178

160179
// SetClientParams sets client parameters for all clients listed in the ids list
161180
// or all connected clients if the list is empty
162-
func (api *PrivateLightServerAPI) SetClientParams(ids []enode.ID, params map[string]interface{}) error {
163-
var err error
181+
func (api *PrivateLightServerAPI) SetClientParams(nodes []string, params map[string]interface{}) error {
182+
var (
183+
ids []enode.ID
184+
err error
185+
)
186+
for _, node := range nodes {
187+
if id, err := parseNode(node); err != nil {
188+
return err
189+
} else {
190+
ids = append(ids, id)
191+
}
192+
}
164193
api.server.clientPool.forClients(ids, func(client *clientInfo) {
165194
if client.connected {
166195
posFactors, negFactors := client.balance.GetPriceFactors()
@@ -201,7 +230,11 @@ func (api *PrivateLightServerAPI) SetConnectedBias(bias time.Duration) error {
201230

202231
// AddBalance adds the given amount to the balance of a client if possible and returns
203232
// the balance before and after the operation
204-
func (api *PrivateLightServerAPI) AddBalance(id enode.ID, amount int64) (balance [2]uint64, err error) {
233+
func (api *PrivateLightServerAPI) AddBalance(node string, amount int64) (balance [2]uint64, err error) {
234+
var id enode.ID
235+
if id, err = parseNode(node); err != nil {
236+
return
237+
}
205238
api.server.clientPool.forClients([]enode.ID{id}, func(c *clientInfo) {
206239
balance[0], balance[1], err = c.balance.AddBalance(amount)
207240
})
@@ -297,8 +330,14 @@ func NewPrivateDebugAPI(server *LesServer) *PrivateDebugAPI {
297330
}
298331

299332
// FreezeClient forces a temporary client freeze which normally happens when the server is overloaded
300-
func (api *PrivateDebugAPI) FreezeClient(id enode.ID) error {
301-
var err error
333+
func (api *PrivateDebugAPI) FreezeClient(node string) error {
334+
var (
335+
id enode.ID
336+
err error
337+
)
338+
if id, err = parseNode(node); err != nil {
339+
return err
340+
}
302341
api.server.clientPool.forClients([]enode.ID{id}, func(c *clientInfo) {
303342
if c.connected {
304343
c.peer.freeze()

0 commit comments

Comments
 (0)