From 91726e8aadeebc4bda8f136aebf4b5c7da8b9d74 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Felf=C3=B6ldi=20Zsolt?= <zsfelfoldi@gmail.com>
Date: Tue, 16 Mar 2021 12:55:43 +0100
Subject: [PATCH] les: allow either full enode strings or raw hex ids in the
 API (#22423)

---
 les/api.go | 51 +++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 45 insertions(+), 6 deletions(-)

diff --git a/les/api.go b/les/api.go
index 6491c4dcc..a93052451 100644
--- a/les/api.go
+++ b/les/api.go
@@ -49,6 +49,18 @@ func NewPrivateLightServerAPI(server *LesServer) *PrivateLightServerAPI {
 	}
 }
 
+// parseNode parses either an enode address a raw hex node id
+func parseNode(node string) (enode.ID, error) {
+	if id, err := enode.ParseID(node); err == nil {
+		return id, nil
+	}
+	if node, err := enode.Parse(enode.ValidSchemes, node); err == nil {
+		return node.ID(), nil
+	} else {
+		return enode.ID{}, err
+	}
+}
+
 // ServerInfo returns global server parameters
 func (api *PrivateLightServerAPI) ServerInfo() map[string]interface{} {
 	res := make(map[string]interface{})
@@ -59,7 +71,14 @@ func (api *PrivateLightServerAPI) ServerInfo() map[string]interface{} {
 }
 
 // ClientInfo returns information about clients listed in the ids list or matching the given tags
-func (api *PrivateLightServerAPI) ClientInfo(ids []enode.ID) map[enode.ID]map[string]interface{} {
+func (api *PrivateLightServerAPI) ClientInfo(nodes []string) map[enode.ID]map[string]interface{} {
+	var ids []enode.ID
+	for _, node := range nodes {
+		if id, err := parseNode(node); err == nil {
+			ids = append(ids, id)
+		}
+	}
+
 	res := make(map[enode.ID]map[string]interface{})
 	api.server.clientPool.forClients(ids, func(client *clientInfo) {
 		res[client.node.ID()] = api.clientInfo(client)
@@ -159,8 +178,18 @@ func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, clien
 
 // SetClientParams sets client parameters for all clients listed in the ids list
 // or all connected clients if the list is empty
-func (api *PrivateLightServerAPI) SetClientParams(ids []enode.ID, params map[string]interface{}) error {
-	var err error
+func (api *PrivateLightServerAPI) SetClientParams(nodes []string, params map[string]interface{}) error {
+	var (
+		ids []enode.ID
+		err error
+	)
+	for _, node := range nodes {
+		if id, err := parseNode(node); err != nil {
+			return err
+		} else {
+			ids = append(ids, id)
+		}
+	}
 	api.server.clientPool.forClients(ids, func(client *clientInfo) {
 		if client.connected {
 			posFactors, negFactors := client.balance.GetPriceFactors()
@@ -201,7 +230,11 @@ func (api *PrivateLightServerAPI) SetConnectedBias(bias time.Duration) error {
 
 // AddBalance adds the given amount to the balance of a client if possible and returns
 // the balance before and after the operation
-func (api *PrivateLightServerAPI) AddBalance(id enode.ID, amount int64) (balance [2]uint64, err error) {
+func (api *PrivateLightServerAPI) AddBalance(node string, amount int64) (balance [2]uint64, err error) {
+	var id enode.ID
+	if id, err = parseNode(node); err != nil {
+		return
+	}
 	api.server.clientPool.forClients([]enode.ID{id}, func(c *clientInfo) {
 		balance[0], balance[1], err = c.balance.AddBalance(amount)
 	})
@@ -297,8 +330,14 @@ func NewPrivateDebugAPI(server *LesServer) *PrivateDebugAPI {
 }
 
 // FreezeClient forces a temporary client freeze which normally happens when the server is overloaded
-func (api *PrivateDebugAPI) FreezeClient(id enode.ID) error {
-	var err error
+func (api *PrivateDebugAPI) FreezeClient(node string) error {
+	var (
+		id  enode.ID
+		err error
+	)
+	if id, err = parseNode(node); err != nil {
+		return err
+	}
 	api.server.clientPool.forClients([]enode.ID{id}, func(c *clientInfo) {
 		if c.connected {
 			c.peer.freeze()
-- 
GitLab