les: simplify and fix setParams

This commit is contained in:
Zsolt Felfoldi 2019-11-08 14:46:17 +01:00
parent f85833b456
commit 57bb045a03
2 changed files with 32 additions and 44 deletions

View file

@ -34,7 +34,6 @@ var (
errNoCheckpoint = errors.New("no local checkpoint provided") errNoCheckpoint = errors.New("no local checkpoint provided")
errNotActivated = errors.New("checkpoint registrar is not activated") errNotActivated = errors.New("checkpoint registrar is not activated")
errUnknownBenchmarkType = errors.New("unknown benchmark type") errUnknownBenchmarkType = errors.New("unknown benchmark type")
errClientNotConnected = errors.New("client is not connected")
errBalanceOverflow = errors.New("balance overflow") errBalanceOverflow = errors.New("balance overflow")
errNoPriority = errors.New("not enough priority") errNoPriority = errors.New("not enough priority")
) )
@ -152,29 +151,25 @@ func (api *PrivateLightServerAPI) sendEvent(clientEvent string, client *clientIn
} }
} }
// setParams either sets the given parameters for a single client (if ID is specified) // setParams either sets the given parameters for a single connected client (if specified)
// or the default parameters applicable to clients connected in the future // or the default parameters applicable to clients connected in the future
func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, client *clientInfo, id enode.ID, posFactors, negFactors *priceFactors) (updateFactors bool, err error) { func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, client *clientInfo, posFactors, negFactors *priceFactors) (updateFactors bool, err error) {
if client != nil { defParams := client == nil
if !defParams {
posFactors, negFactors = &client.posFactors, &client.negFactors posFactors, negFactors = &client.posFactors, &client.negFactors
} }
defParams := id == enode.ID{}
loop: loop:
for name, value := range params { for name, value := range params {
errValue := func() error { errValue := func() error {
return fmt.Errorf("invalid value for parameter '%s'", name) return fmt.Errorf("invalid value for parameter '%s'", name)
} }
setFactor := func(v *float64) { setFactor := func(v *float64) {
if posFactors != nil {
if val, ok := value.(float64); ok && val >= 0 { if val, ok := value.(float64); ok && val >= 0 {
*v = val / float64(time.Second) *v = val / float64(time.Second)
updateFactors = true updateFactors = true
} else { } else {
err = errValue() err = errValue()
} }
} else {
err = errClientNotConnected
}
} }
processed := true processed := true
@ -203,36 +198,24 @@ loop:
} }
switch name { switch name {
case "capacity": case "capacity":
if client != nil { if capacity, ok := value.(float64); ok && uint64(capacity) >= api.server.minCapacity {
if capacity, ok := value.(float64); ok && (capacity == 0 || uint64(capacity) >= api.server.minCapacity) {
err = api.server.clientPool.setCapacity(client, uint64(capacity)) err = api.server.clientPool.setCapacity(client, uint64(capacity))
updateFactors = true updateFactors = true
} else { } else {
err = errValue() err = errValue()
} }
} else {
err = errClientNotConnected
}
case "pricing/alert": case "pricing/alert":
if client != nil {
if val, ok := value.(float64); ok && val >= 0 { if val, ok := value.(float64); ok && val >= 0 {
api.setBalanceUpdate(client, uint64(val), false) api.setBalanceUpdate(client, uint64(val), false)
} else { } else {
err = errValue() err = errValue()
} }
} else {
err = errClientNotConnected
}
case "pricing/periodicUpdate": case "pricing/periodicUpdate":
if client != nil {
if val, ok := value.(float64); ok && val >= 0 { if val, ok := value.(float64); ok && val >= 0 {
api.setBalanceUpdate(client, uint64(val), true) api.setBalanceUpdate(client, uint64(val), true)
} else { } else {
err = errValue() err = errValue()
} }
} else {
err = errClientNotConnected
}
default: default:
err = fmt.Errorf("invalid client parameter '%s'", name) err = fmt.Errorf("invalid client parameter '%s'", name)
} }
@ -251,20 +234,24 @@ func (api *PrivateLightServerAPI) UpdateBalance(id enode.ID, value int64, meta s
func (api *PrivateLightServerAPI) SetClientParams(ids []enode.ID, params map[string]interface{}) error { func (api *PrivateLightServerAPI) SetClientParams(ids []enode.ID, params map[string]interface{}) error {
var finalErr error var finalErr error
api.server.clientPool.forClients(ids, func(client *clientInfo, id enode.ID) { api.server.clientPool.forClients(ids, func(client *clientInfo, id enode.ID) {
update, err := api.setParams(params, client, id, nil, nil) if client != nil {
update, err := api.setParams(params, client, nil, nil)
if err != nil { if err != nil {
finalErr = err finalErr = err
} }
if update { if update {
client.updatePriceFactors() client.updatePriceFactors()
} }
} else {
finalErr = fmt.Errorf("client %064x is not connected", id[:])
}
}) })
return finalErr return finalErr
} }
// SetDefaultParams sets the default parameters applicable to clients connected in the future // SetDefaultParams sets the default parameters applicable to clients connected in the future
func (api *PrivateLightServerAPI) SetDefaultParams(params map[string]interface{}) error { func (api *PrivateLightServerAPI) SetDefaultParams(params map[string]interface{}) error {
update, err := api.setParams(params, nil, enode.ID{}, &api.defaultPosFactors, &api.defaultNegFactors) update, err := api.setParams(params, nil, &api.defaultPosFactors, &api.defaultNegFactors)
if update { if update {
api.server.clientPool.setDefaultFactors(api.defaultPosFactors, api.defaultNegFactors) api.server.clientPool.setDefaultFactors(api.defaultPosFactors, api.defaultNegFactors)
} }
@ -410,7 +397,7 @@ func NewPrivateDebugAPI(server *LesServer) *PrivateDebugAPI {
// FreezeClient forces a temporary client freeze which normally happens when the server is overloaded // FreezeClient forces a temporary client freeze which normally happens when the server is overloaded
func (api *PrivateDebugAPI) FreezeClient(id enode.ID) error { func (api *PrivateDebugAPI) FreezeClient(id enode.ID) error {
err := errClientNotConnected err := fmt.Errorf("client %064x is not connected", id[:])
api.server.clientPool.forClients([]enode.ID{id}, func(c *clientInfo, id enode.ID) { api.server.clientPool.forClients([]enode.ID{id}, func(c *clientInfo, id enode.ID) {
c.peer.freezeClient() c.peer.freezeClient()
err = nil err = nil

View file

@ -19,6 +19,7 @@ package les
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt"
"io" "io"
"math" "math"
"sync" "sync"
@ -483,7 +484,7 @@ func (f *clientPool) setLimits(totalConn int, totalCap uint64) {
// setCapacity sets the assigned capacity of a connected client // setCapacity sets the assigned capacity of a connected client
func (f *clientPool) setCapacity(c *clientInfo, capacity uint64) error { func (f *clientPool) setCapacity(c *clientInfo, capacity uint64) error {
if f.connectedMap[c.id] != c { if f.connectedMap[c.id] != c {
return errClientNotConnected return fmt.Errorf("client %064x is not connected", c.id[:])
} }
if c.capacity == capacity { if c.capacity == capacity {
return nil return nil