31
31
errNoCheckpoint = errors .New ("no local checkpoint provided" )
32
32
errNotActivated = errors .New ("checkpoint registrar is not activated" )
33
33
errUnknownBenchmarkType = errors .New ("unknown benchmark type" )
34
- errNoPriority = errors .New ("priority too low to raise capacity" )
35
34
)
36
35
37
36
// PrivateLightServerAPI provides an API to access the LES light server.
@@ -44,8 +43,8 @@ type PrivateLightServerAPI struct {
44
43
func NewPrivateLightServerAPI (server * LesServer ) * PrivateLightServerAPI {
45
44
return & PrivateLightServerAPI {
46
45
server : server ,
47
- defaultPosFactors : server . clientPool . defaultPosFactors ,
48
- defaultNegFactors : server . clientPool . defaultNegFactors ,
46
+ defaultPosFactors : defaultPosFactors ,
47
+ defaultNegFactors : defaultNegFactors ,
49
48
}
50
49
}
51
50
@@ -66,7 +65,9 @@ func (api *PrivateLightServerAPI) ServerInfo() map[string]interface{} {
66
65
res := make (map [string ]interface {})
67
66
res ["minimumCapacity" ] = api .server .minCapacity
68
67
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
70
71
return res
71
72
}
72
73
@@ -80,9 +81,18 @@ func (api *PrivateLightServerAPI) ClientInfo(nodes []string) map[enode.ID]map[st
80
81
}
81
82
82
83
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
+ }
86
96
return res
87
97
}
88
98
@@ -94,39 +104,43 @@ func (api *PrivateLightServerAPI) ClientInfo(nodes []string) map[enode.ID]map[st
94
104
// assigned to it.
95
105
func (api * PrivateLightServerAPI ) PriorityClientInfo (start , stop enode.ID , maxCount int ) map [enode.ID ]map [string ]interface {} {
96
106
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 )
98
108
if len (ids ) > maxCount {
99
109
res [ids [maxCount ]] = make (map [string ]interface {})
100
110
ids = ids [:maxCount ]
101
111
}
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
+ }
106
120
}
107
121
return res
108
122
}
109
123
110
124
// 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 {} {
112
126
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
115
129
info ["pricing/balance" ] = pb
116
130
info ["priority" ] = pb != 0
117
131
// cb := api.server.clientPool.ndb.getCurrencyBalance(id)
118
132
// 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 ( )
122
136
info ["pricing/negBalance" ] = nb
123
137
}
124
138
return info
125
139
}
126
140
127
141
// setParams either sets the given parameters for a single connected client (if specified)
128
142
// 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 ) {
130
144
defParams := client == nil
131
145
for name , value := range params {
132
146
errValue := func () error {
@@ -156,9 +170,8 @@ func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, clien
156
170
setFactor (& negFactors .RequestFactor )
157
171
case ! defParams && name == "capacity" :
158
172
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
162
175
} else {
163
176
err = errValue ()
164
177
}
@@ -179,39 +192,33 @@ func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, clien
179
192
// SetClientParams sets client parameters for all clients listed in the ids list
180
193
// or all connected clients if the list is empty
181
194
func (api * PrivateLightServerAPI ) SetClientParams (nodes []string , params map [string ]interface {}) error {
182
- var (
183
- ids []enode.ID
184
- err error
185
- )
195
+ var err error
186
196
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 {
188
199
return err
189
- } else {
190
- ids = append (ids , id )
191
200
}
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 )
197
204
if update {
198
- client .balance .SetPriceFactors (posFactors , negFactors )
205
+ peer .balance .SetPriceFactors (posFactors , negFactors )
199
206
}
200
207
if e != nil {
201
208
err = e
202
209
}
203
210
} else {
204
- err = fmt .Errorf ("client %064x is not connected" , client . node . ID () )
211
+ err = fmt .Errorf ("client %064x is not connected" , id )
205
212
}
206
- })
213
+ }
207
214
return err
208
215
}
209
216
210
217
// SetDefaultParams sets the default parameters applicable to clients connected in the future
211
218
func (api * PrivateLightServerAPI ) SetDefaultParams (params map [string ]interface {}) error {
212
219
update , err := api .setParams (params , nil , & api .defaultPosFactors , & api .defaultNegFactors )
213
220
if update {
214
- api .server .clientPool .setDefaultFactors (api .defaultPosFactors , api .defaultNegFactors )
221
+ api .server .clientPool .SetDefaultFactors (api .defaultPosFactors , api .defaultNegFactors )
215
222
}
216
223
return err
217
224
}
@@ -224,7 +231,7 @@ func (api *PrivateLightServerAPI) SetConnectedBias(bias time.Duration) error {
224
231
if bias < time .Duration (0 ) {
225
232
return fmt .Errorf ("bias illegal: %v less than 0" , bias )
226
233
}
227
- api .server .clientPool .setConnectedBias (bias )
234
+ api .server .clientPool .SetConnectedBias (bias )
228
235
return nil
229
236
}
230
237
@@ -235,8 +242,8 @@ func (api *PrivateLightServerAPI) AddBalance(node string, amount int64) (balance
235
242
if id , err = parseNode (node ); err != nil {
236
243
return
237
244
}
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 )
240
247
})
241
248
return
242
249
}
@@ -338,14 +345,12 @@ func (api *PrivateDebugAPI) FreezeClient(node string) error {
338
345
if id , err = parseNode (node ); err != nil {
339
346
return err
340
347
}
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
+ }
349
354
}
350
355
351
356
// PrivateLightAPI provides an API to access the LES light server or light client.
0 commit comments