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- errNoPriority = errors .New ("priority too low to raise capacity" )
3534)
3635
3736// PrivateLightServerAPI provides an API to access the LES light server.
@@ -44,8 +43,8 @@ type PrivateLightServerAPI struct {
4443func NewPrivateLightServerAPI (server * LesServer ) * PrivateLightServerAPI {
4544 return & PrivateLightServerAPI {
4645 server : server ,
47- defaultPosFactors : server . clientPool . defaultPosFactors ,
48- defaultNegFactors : server . clientPool . defaultNegFactors ,
46+ defaultPosFactors : defaultPosFactors ,
47+ defaultNegFactors : defaultNegFactors ,
4948 }
5049}
5150
@@ -66,7 +65,9 @@ func (api *PrivateLightServerAPI) ServerInfo() map[string]interface{} {
6665 res := make (map [string ]interface {})
6766 res ["minimumCapacity" ] = api .server .minCapacity
6867 res ["maximumCapacity" ] = api .server .maxCapacity
69- res ["totalCapacity" ], res ["totalConnectedCapacity" ], res ["priorityConnectedCapacity" ] = api .server .clientPool .capacityInfo ()
68+ _ , res ["totalCapacity" ] = api .server .clientPool .Limits ()
69+ _ , res ["totalConnectedCapacity" ] = api .server .clientPool .Active ()
70+ res ["priorityConnectedCapacity" ] = 0 //TODO connect when token sale module is added
7071 return res
7172}
7273
@@ -80,9 +81,18 @@ func (api *PrivateLightServerAPI) ClientInfo(nodes []string) map[enode.ID]map[st
8081 }
8182
8283 res := make (map [enode.ID ]map [string ]interface {})
83- api .server .clientPool .forClients (ids , func (client * clientInfo ) {
84- res [client .node .ID ()] = api .clientInfo (client )
85- })
84+ if len (ids ) == 0 {
85+ ids = api .server .peers .ids ()
86+ }
87+ for _ , id := range ids {
88+ if peer := api .server .peers .peer (id ); peer != nil {
89+ res [id ] = api .clientInfo (peer , peer .balance )
90+ } else {
91+ api .server .clientPool .BalanceOperation (id , "" , func (balance vfs.AtomicBalanceOperator ) {
92+ res [id ] = api .clientInfo (nil , balance )
93+ })
94+ }
95+ }
8696 return res
8797}
8898
@@ -94,39 +104,43 @@ func (api *PrivateLightServerAPI) ClientInfo(nodes []string) map[enode.ID]map[st
94104// assigned to it.
95105func (api * PrivateLightServerAPI ) PriorityClientInfo (start , stop enode.ID , maxCount int ) map [enode.ID ]map [string ]interface {} {
96106 res := make (map [enode.ID ]map [string ]interface {})
97- ids := api .server .clientPool .bt . GetPosBalanceIDs (start , stop , maxCount + 1 )
107+ ids := api .server .clientPool .GetPosBalanceIDs (start , stop , maxCount + 1 )
98108 if len (ids ) > maxCount {
99109 res [ids [maxCount ]] = make (map [string ]interface {})
100110 ids = ids [:maxCount ]
101111 }
102- if len (ids ) != 0 {
103- api .server .clientPool .forClients (ids , func (client * clientInfo ) {
104- res [client .node .ID ()] = api .clientInfo (client )
105- })
112+ for _ , id := range ids {
113+ if peer := api .server .peers .peer (id ); peer != nil {
114+ res [id ] = api .clientInfo (peer , peer .balance )
115+ } else {
116+ api .server .clientPool .BalanceOperation (id , "" , func (balance vfs.AtomicBalanceOperator ) {
117+ res [id ] = api .clientInfo (nil , balance )
118+ })
119+ }
106120 }
107121 return res
108122}
109123
110124// clientInfo creates a client info data structure
111- func (api * PrivateLightServerAPI ) clientInfo (c * clientInfo ) map [string ]interface {} {
125+ func (api * PrivateLightServerAPI ) clientInfo (peer * clientPeer , balance vfs. ReadOnlyBalance ) map [string ]interface {} {
112126 info := make (map [string ]interface {})
113- pb , nb := c . balance .GetBalance ()
114- info ["isConnected" ] = c . connected
127+ pb , nb := balance .GetBalance ()
128+ info ["isConnected" ] = peer != nil
115129 info ["pricing/balance" ] = pb
116130 info ["priority" ] = pb != 0
117131 // cb := api.server.clientPool.ndb.getCurrencyBalance(id)
118132 // info["pricing/currency"] = cb.amount
119- if c . connected {
120- info ["connectionTime" ] = float64 (mclock .Now ()- c .connectedAt ) / float64 (time .Second )
121- info ["capacity" ], _ = api . server . clientPool . ns . GetField ( c . node , priorityPoolSetup . CapacityField ).( uint64 )
133+ if peer != nil {
134+ info ["connectionTime" ] = float64 (mclock .Now ()- peer .connectedAt ) / float64 (time .Second )
135+ info ["capacity" ] = peer . getCapacity ( )
122136 info ["pricing/negBalance" ] = nb
123137 }
124138 return info
125139}
126140
127141// setParams either sets the given parameters for a single connected client (if specified)
128142// or the default parameters applicable to clients connected in the future
129- func (api * PrivateLightServerAPI ) setParams (params map [string ]interface {}, client * clientInfo , posFactors , negFactors * vfs.PriceFactors ) (updateFactors bool , err error ) {
143+ func (api * PrivateLightServerAPI ) setParams (params map [string ]interface {}, client * clientPeer , posFactors , negFactors * vfs.PriceFactors ) (updateFactors bool , err error ) {
130144 defParams := client == nil
131145 for name , value := range params {
132146 errValue := func () error {
@@ -156,9 +170,8 @@ func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, clien
156170 setFactor (& negFactors .RequestFactor )
157171 case ! defParams && name == "capacity" :
158172 if capacity , ok := value .(float64 ); ok && uint64 (capacity ) >= api .server .minCapacity {
159- _ , err = api .server .clientPool .setCapacity (client .node , client .address , uint64 (capacity ), 0 , true )
160- // Don't have to call factor update explicitly. It's already done
161- // in setCapacity function.
173+ _ , err = api .server .clientPool .SetCapacity (client .Node (), uint64 (capacity ), 0 , false )
174+ // time factor recalculation is performed automatically by the balance tracker
162175 } else {
163176 err = errValue ()
164177 }
@@ -179,39 +192,33 @@ func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, clien
179192// SetClientParams sets client parameters for all clients listed in the ids list
180193// or all connected clients if the list is empty
181194func (api * PrivateLightServerAPI ) SetClientParams (nodes []string , params map [string ]interface {}) error {
182- var (
183- ids []enode.ID
184- err error
185- )
195+ var err error
186196 for _ , node := range nodes {
187- if id , err := parseNode (node ); err != nil {
197+ var id enode.ID
198+ if id , err = parseNode (node ); err != nil {
188199 return err
189- } else {
190- ids = append (ids , id )
191200 }
192- }
193- api .server .clientPool .forClients (ids , func (client * clientInfo ) {
194- if client .connected {
195- posFactors , negFactors := client .balance .GetPriceFactors ()
196- update , e := api .setParams (params , client , & posFactors , & negFactors )
201+ if peer := api .server .peers .peer (id ); peer != nil {
202+ posFactors , negFactors := peer .balance .GetPriceFactors ()
203+ update , e := api .setParams (params , peer , & posFactors , & negFactors )
197204 if update {
198- client .balance .SetPriceFactors (posFactors , negFactors )
205+ peer .balance .SetPriceFactors (posFactors , negFactors )
199206 }
200207 if e != nil {
201208 err = e
202209 }
203210 } else {
204- err = fmt .Errorf ("client %064x is not connected" , client . node . ID () )
211+ err = fmt .Errorf ("client %064x is not connected" , id )
205212 }
206- })
213+ }
207214 return err
208215}
209216
210217// SetDefaultParams sets the default parameters applicable to clients connected in the future
211218func (api * PrivateLightServerAPI ) SetDefaultParams (params map [string ]interface {}) error {
212219 update , err := api .setParams (params , nil , & api .defaultPosFactors , & api .defaultNegFactors )
213220 if update {
214- api .server .clientPool .setDefaultFactors (api .defaultPosFactors , api .defaultNegFactors )
221+ api .server .clientPool .SetDefaultFactors (api .defaultPosFactors , api .defaultNegFactors )
215222 }
216223 return err
217224}
@@ -224,7 +231,7 @@ func (api *PrivateLightServerAPI) SetConnectedBias(bias time.Duration) error {
224231 if bias < time .Duration (0 ) {
225232 return fmt .Errorf ("bias illegal: %v less than 0" , bias )
226233 }
227- api .server .clientPool .setConnectedBias (bias )
234+ api .server .clientPool .SetConnectedBias (bias )
228235 return nil
229236}
230237
@@ -235,8 +242,8 @@ func (api *PrivateLightServerAPI) AddBalance(node string, amount int64) (balance
235242 if id , err = parseNode (node ); err != nil {
236243 return
237244 }
238- api .server .clientPool .forClients ([]enode. ID { id }, func (c * clientInfo ) {
239- balance [0 ], balance [1 ], err = c . balance .AddBalance (amount )
245+ api .server .clientPool .BalanceOperation ( id , "" , func (nb vfs. AtomicBalanceOperator ) {
246+ balance [0 ], balance [1 ], err = nb .AddBalance (amount )
240247 })
241248 return
242249}
@@ -338,14 +345,12 @@ func (api *PrivateDebugAPI) FreezeClient(node string) error {
338345 if id , err = parseNode (node ); err != nil {
339346 return err
340347 }
341- api .server .clientPool .forClients ([]enode.ID {id }, func (c * clientInfo ) {
342- if c .connected {
343- c .peer .freeze ()
344- } else {
345- err = fmt .Errorf ("client %064x is not connected" , id [:])
346- }
347- })
348- return err
348+ if peer := api .server .peers .peer (id ); peer != nil {
349+ peer .freeze ()
350+ return nil
351+ } else {
352+ return fmt .Errorf ("client %064x is not connected" , id [:])
353+ }
349354}
350355
351356// PrivateLightAPI provides an API to access the LES light server or light client.
0 commit comments