mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
les: simplify and fix setParams
This commit is contained in:
parent
f85833b456
commit
57bb045a03
2 changed files with 32 additions and 44 deletions
73
les/api.go
73
les/api.go
|
|
@ -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,28 +151,24 @@ 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 {
|
|
||||||
err = errValue()
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
err = errClientNotConnected
|
err = errValue()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -203,35 +198,23 @@ 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 {
|
|
||||||
err = errValue()
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
err = errClientNotConnected
|
err = errValue()
|
||||||
}
|
}
|
||||||
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 {
|
|
||||||
err = errValue()
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
err = errClientNotConnected
|
err = errValue()
|
||||||
}
|
}
|
||||||
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 {
|
|
||||||
err = errValue()
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
err = errClientNotConnected
|
err = errValue()
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("invalid client parameter '%s'", name)
|
err = fmt.Errorf("invalid client parameter '%s'", name)
|
||||||
|
|
@ -251,12 +234,16 @@ 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 {
|
||||||
if err != nil {
|
update, err := api.setParams(params, client, nil, nil)
|
||||||
finalErr = err
|
if err != nil {
|
||||||
}
|
finalErr = err
|
||||||
if update {
|
}
|
||||||
client.updatePriceFactors()
|
if update {
|
||||||
|
client.updatePriceFactors()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
finalErr = fmt.Errorf("client %064x is not connected", id[:])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return finalErr
|
return finalErr
|
||||||
|
|
@ -264,7 +251,7 @@ func (api *PrivateLightServerAPI) SetClientParams(ids []enode.ID, params map[str
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue