@@ -19,28 +19,25 @@ package les
1919import (
2020 "errors"
2121 "fmt"
22- "math"
2322 "time"
2423
2524 "github.com/ethereum/go-ethereum/common/hexutil"
2625 "github.com/ethereum/go-ethereum/common/mclock"
26+ lps "github.com/ethereum/go-ethereum/les/lespay/server"
2727 "github.com/ethereum/go-ethereum/p2p/enode"
2828)
2929
3030var (
3131 errNoCheckpoint = errors .New ("no local checkpoint provided" )
3232 errNotActivated = errors .New ("checkpoint registrar is not activated" )
3333 errUnknownBenchmarkType = errors .New ("unknown benchmark type" )
34- errBalanceOverflow = errors .New ("balance overflow" )
3534 errNoPriority = errors .New ("priority too low to raise capacity" )
3635)
3736
38- const maxBalance = math .MaxInt64
39-
4037// PrivateLightServerAPI provides an API to access the LES light server.
4138type PrivateLightServerAPI struct {
4239 server * LesServer
43- defaultPosFactors , defaultNegFactors priceFactors
40+ defaultPosFactors , defaultNegFactors lps. PriceFactors
4441}
4542
4643// NewPrivateLightServerAPI creates a new LES light server API.
@@ -57,17 +54,15 @@ func (api *PrivateLightServerAPI) ServerInfo() map[string]interface{} {
5754 res := make (map [string ]interface {})
5855 res ["minimumCapacity" ] = api .server .minCapacity
5956 res ["maximumCapacity" ] = api .server .maxCapacity
60- res ["freeClientCapacity" ] = api .server .freeCapacity
6157 res ["totalCapacity" ], res ["totalConnectedCapacity" ], res ["priorityConnectedCapacity" ] = api .server .clientPool .capacityInfo ()
6258 return res
6359}
6460
6561// ClientInfo returns information about clients listed in the ids list or matching the given tags
6662func (api * PrivateLightServerAPI ) ClientInfo (ids []enode.ID ) map [enode.ID ]map [string ]interface {} {
6763 res := make (map [enode.ID ]map [string ]interface {})
68- api .server .clientPool .forClients (ids , func (client * clientInfo , id enode.ID ) error {
69- res [id ] = api .clientInfo (client , id )
70- return nil
64+ api .server .clientPool .forClients (ids , func (client * clientInfo ) {
65+ res [client .node .ID ()] = api .clientInfo (client )
7166 })
7267 return res
7368}
@@ -80,48 +75,40 @@ func (api *PrivateLightServerAPI) ClientInfo(ids []enode.ID) map[enode.ID]map[st
8075// assigned to it.
8176func (api * PrivateLightServerAPI ) PriorityClientInfo (start , stop enode.ID , maxCount int ) map [enode.ID ]map [string ]interface {} {
8277 res := make (map [enode.ID ]map [string ]interface {})
83- ids := api .server .clientPool .ndb . getPosBalanceIDs (start , stop , maxCount + 1 )
78+ ids := api .server .clientPool .bt . GetPosBalanceIDs (start , stop , maxCount + 1 )
8479 if len (ids ) > maxCount {
8580 res [ids [maxCount ]] = make (map [string ]interface {})
8681 ids = ids [:maxCount ]
8782 }
8883 if len (ids ) != 0 {
89- api .server .clientPool .forClients (ids , func (client * clientInfo , id enode.ID ) error {
90- res [id ] = api .clientInfo (client , id )
91- return nil
84+ api .server .clientPool .forClients (ids , func (client * clientInfo ) {
85+ res [client .node .ID ()] = api .clientInfo (client )
9286 })
9387 }
9488 return res
9589}
9690
9791// clientInfo creates a client info data structure
98- func (api * PrivateLightServerAPI ) clientInfo (c * clientInfo , id enode. ID ) map [string ]interface {} {
92+ func (api * PrivateLightServerAPI ) clientInfo (c * clientInfo ) map [string ]interface {} {
9993 info := make (map [string ]interface {})
100- if c != nil {
101- now := mclock .Now ()
102- info ["isConnected" ] = true
103- info ["connectionTime" ] = float64 (now - c .connectedAt ) / float64 (time .Second )
104- info ["capacity" ] = c .capacity
105- pb , nb := c .balanceTracker .getBalance (now )
106- info ["pricing/balance" ], info ["pricing/negBalance" ] = pb , nb
107- info ["pricing/balanceMeta" ] = c .balanceMetaInfo
108- info ["priority" ] = pb != 0
109- } else {
110- info ["isConnected" ] = false
111- pb := api .server .clientPool .ndb .getOrNewPB (id )
112- info ["pricing/balance" ], info ["pricing/balanceMeta" ] = pb .value , pb .meta
113- info ["priority" ] = pb .value != 0
94+ pb , nb := c .balance .GetBalance ()
95+ info ["isConnected" ] = c .connected
96+ info ["pricing/balance" ] = pb
97+ info ["priority" ] = pb != 0
98+ // cb := api.server.clientPool.ndb.getCurrencyBalance(id)
99+ // info["pricing/currency"] = cb.amount
100+ if c .connected {
101+ info ["connectionTime" ] = float64 (mclock .Now ()- c .connectedAt ) / float64 (time .Second )
102+ info ["capacity" ], _ = api .server .clientPool .ns .GetField (c .node , priorityPoolSetup .CapacityField ).(uint64 )
103+ info ["pricing/negBalance" ] = nb
114104 }
115105 return info
116106}
117107
118108// setParams either sets the given parameters for a single connected client (if specified)
119109// or the default parameters applicable to clients connected in the future
120- func (api * PrivateLightServerAPI ) setParams (params map [string ]interface {}, client * clientInfo , posFactors , negFactors * priceFactors ) (updateFactors bool , err error ) {
110+ func (api * PrivateLightServerAPI ) setParams (params map [string ]interface {}, client * clientInfo , posFactors , negFactors * lps. PriceFactors ) (updateFactors bool , err error ) {
121111 defParams := client == nil
122- if ! defParams {
123- posFactors , negFactors = & client .posFactors , & client .negFactors
124- }
125112 for name , value := range params {
126113 errValue := func () error {
127114 return fmt .Errorf ("invalid value for parameter '%s'" , name )
@@ -137,20 +124,20 @@ func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, clien
137124
138125 switch {
139126 case name == "pricing/timeFactor" :
140- setFactor (& posFactors .timeFactor )
127+ setFactor (& posFactors .TimeFactor )
141128 case name == "pricing/capacityFactor" :
142- setFactor (& posFactors .capacityFactor )
129+ setFactor (& posFactors .CapacityFactor )
143130 case name == "pricing/requestCostFactor" :
144- setFactor (& posFactors .requestFactor )
131+ setFactor (& posFactors .RequestFactor )
145132 case name == "pricing/negative/timeFactor" :
146- setFactor (& negFactors .timeFactor )
133+ setFactor (& negFactors .TimeFactor )
147134 case name == "pricing/negative/capacityFactor" :
148- setFactor (& negFactors .capacityFactor )
135+ setFactor (& negFactors .CapacityFactor )
149136 case name == "pricing/negative/requestCostFactor" :
150- setFactor (& negFactors .requestFactor )
137+ setFactor (& negFactors .RequestFactor )
151138 case ! defParams && name == "capacity" :
152139 if capacity , ok := value .(float64 ); ok && uint64 (capacity ) >= api .server .minCapacity {
153- err = api .server .clientPool .setCapacity (client , uint64 (capacity ))
140+ _ , err = api .server .clientPool .setCapacity (client . node , client . address , uint64 (capacity ), 0 , true )
154141 // Don't have to call factor update explicitly. It's already done
155142 // in setCapacity function.
156143 } else {
@@ -170,27 +157,25 @@ func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, clien
170157 return
171158}
172159
173- // AddBalance updates the balance of a client (either overwrites it or adds to it).
174- // It also updates the balance meta info string.
175- func (api * PrivateLightServerAPI ) AddBalance (id enode.ID , value int64 , meta string ) ([2 ]uint64 , error ) {
176- oldBalance , newBalance , err := api .server .clientPool .addBalance (id , value , meta )
177- return [2 ]uint64 {oldBalance , newBalance }, err
178- }
179-
180160// SetClientParams sets client parameters for all clients listed in the ids list
181161// or all connected clients if the list is empty
182162func (api * PrivateLightServerAPI ) SetClientParams (ids []enode.ID , params map [string ]interface {}) error {
183- return api .server .clientPool .forClients (ids , func (client * clientInfo , id enode.ID ) error {
184- if client != nil {
185- update , err := api .setParams (params , client , nil , nil )
163+ var err error
164+ api .server .clientPool .forClients (ids , func (client * clientInfo ) {
165+ if client .connected {
166+ posFactors , negFactors := client .balance .GetPriceFactors ()
167+ update , e := api .setParams (params , client , & posFactors , & negFactors )
186168 if update {
187- client .updatePriceFactors ()
169+ client .balance .SetPriceFactors (posFactors , negFactors )
170+ }
171+ if e != nil {
172+ err = e
188173 }
189- return err
190174 } else {
191- return fmt .Errorf ("client %064x is not connected" , id [:] )
175+ err = fmt .Errorf ("client %064x is not connected" , client . node . ID () )
192176 }
193177 })
178+ return err
194179}
195180
196181// SetDefaultParams sets the default parameters applicable to clients connected in the future
@@ -214,6 +199,15 @@ func (api *PrivateLightServerAPI) SetConnectedBias(bias time.Duration) error {
214199 return nil
215200}
216201
202+ // AddBalance adds the given amount to the balance of a client if possible and returns
203+ // the balance before and after the operation
204+ func (api * PrivateLightServerAPI ) AddBalance (id enode.ID , amount int64 ) (balance [2 ]uint64 , err error ) {
205+ api .server .clientPool .forClients ([]enode.ID {id }, func (c * clientInfo ) {
206+ balance [0 ], balance [1 ], err = c .balance .AddBalance (amount )
207+ })
208+ return
209+ }
210+
217211// Benchmark runs a request performance benchmark with a given set of measurement setups
218212// in multiple passes specified by passCount. The measurement time for each setup in each
219213// pass is specified in milliseconds by length.
@@ -304,13 +298,15 @@ func NewPrivateDebugAPI(server *LesServer) *PrivateDebugAPI {
304298
305299// FreezeClient forces a temporary client freeze which normally happens when the server is overloaded
306300func (api * PrivateDebugAPI ) FreezeClient (id enode.ID ) error {
307- return api .server .clientPool .forClients ([]enode.ID {id }, func (c * clientInfo , id enode.ID ) error {
308- if c == nil {
309- return fmt .Errorf ("client %064x is not connected" , id [:])
301+ var err error
302+ api .server .clientPool .forClients ([]enode.ID {id }, func (c * clientInfo ) {
303+ if c .connected {
304+ c .peer .freeze ()
305+ } else {
306+ err = fmt .Errorf ("client %064x is not connected" , id [:])
310307 }
311- c .peer .freezeClient ()
312- return nil
313308 })
309+ return err
314310}
315311
316312// PrivateLightAPI provides an API to access the LES light server or light client.
0 commit comments