|
17 | 17 | package les
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "context" |
20 | 21 | "errors"
|
21 | 22 | "fmt"
|
22 |
| - "math" |
23 | 23 | "time"
|
24 | 24 |
|
25 | 25 | "github.com/ethereum/go-ethereum/common/hexutil"
|
26 | 26 | "github.com/ethereum/go-ethereum/common/mclock"
|
| 27 | + "github.com/ethereum/go-ethereum/p2p/discv5" |
27 | 28 | "github.com/ethereum/go-ethereum/p2p/enode"
|
| 29 | + "github.com/ethereum/go-ethereum/rlp" |
28 | 30 | )
|
29 | 31 |
|
30 | 32 | var (
|
|
35 | 37 | errNoPriority = errors.New("priority too low to raise capacity")
|
36 | 38 | )
|
37 | 39 |
|
38 |
| -const maxBalance = math.MaxInt64 |
39 |
| - |
40 | 40 | // PrivateLightServerAPI provides an API to access the LES light server.
|
41 | 41 | type PrivateLightServerAPI struct {
|
42 | 42 | server *LesServer
|
@@ -105,12 +105,12 @@ func (api *PrivateLightServerAPI) clientInfo(c *clientInfo, id enode.ID) map[str
|
105 | 105 | pb, nb := c.balanceTracker.getBalance(now)
|
106 | 106 | info["pricing/balance"], info["pricing/negBalance"] = pb, nb
|
107 | 107 | info["pricing/balanceMeta"] = c.balanceMetaInfo
|
108 |
| - info["priority"] = pb != 0 |
| 108 | + info["priority"] = pb.base != 0 |
109 | 109 | } else {
|
110 | 110 | info["isConnected"] = false
|
111 | 111 | pb := api.server.clientPool.ndb.getOrNewPB(id)
|
112 | 112 | info["pricing/balance"], info["pricing/balanceMeta"] = pb.value, pb.meta
|
113 |
| - info["priority"] = pb.value != 0 |
| 113 | + info["priority"] = pb.value.base != 0 |
114 | 114 | }
|
115 | 115 | return info
|
116 | 116 | }
|
@@ -150,7 +150,7 @@ func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, clien
|
150 | 150 | setFactor(&negFactors.requestFactor)
|
151 | 151 | case !defParams && name == "capacity":
|
152 | 152 | if capacity, ok := value.(float64); ok && uint64(capacity) >= api.server.minCapacity {
|
153 |
| - err = api.server.clientPool.setCapacity(client, uint64(capacity)) |
| 153 | + _, _, err = api.server.clientPool.setCapacity(client.id, client.freeID, uint64(capacity), 0, true) |
154 | 154 | // Don't have to call factor update explicitly. It's already done
|
155 | 155 | // in setCapacity function.
|
156 | 156 | } else {
|
@@ -184,7 +184,7 @@ func (api *PrivateLightServerAPI) SetClientParams(ids []enode.ID, params map[str
|
184 | 184 | if client != nil {
|
185 | 185 | update, err := api.setParams(params, client, nil, nil)
|
186 | 186 | if update {
|
187 |
| - client.updatePriceFactors() |
| 187 | + updatePriceFactors(&client.balanceTracker, client.posFactors, client.negFactors, client.capacity) |
188 | 188 | }
|
189 | 189 | return err
|
190 | 190 | } else {
|
@@ -352,3 +352,213 @@ func (api *PrivateLightAPI) GetCheckpointContractAddress() (string, error) {
|
352 | 352 | }
|
353 | 353 | return api.backend.oracle.config.Address.Hex(), nil
|
354 | 354 | }
|
| 355 | + |
| 356 | +// PrivateLespayAPI provides an API to use the LESpay commands of either the local or a remote server |
| 357 | +type PrivateLespayAPI struct { |
| 358 | + peerSet *peerSet |
| 359 | + clientHandler *clientHandler |
| 360 | + dht *discv5.Network |
| 361 | + tokenSale *tokenSale |
| 362 | +} |
| 363 | + |
| 364 | +// NewPrivateLespayAPI creates a new LESPAY API. |
| 365 | +func NewPrivateLespayAPI(peerSet *peerSet, clientHandler *clientHandler, dht *discv5.Network, tokenSale *tokenSale) *PrivateLespayAPI { |
| 366 | + return &PrivateLespayAPI{ |
| 367 | + peerSet: peerSet, |
| 368 | + clientHandler: clientHandler, |
| 369 | + dht: dht, |
| 370 | + tokenSale: tokenSale, |
| 371 | + } |
| 372 | +} |
| 373 | + |
| 374 | +// makeCall sends an encoded command to either the local or a remote server and returns the encoded reply |
| 375 | +// |
| 376 | +// Note: nodeStr can represent either the node ID of a connected node or the full enode of any remote node. |
| 377 | +// If remote is true then the command is sent to the specified node. It is sent through LES if it was specified |
| 378 | +// with node ID, throush UDP talk otherwise. |
| 379 | +// If remote is false then the command is executed locally, with the specified remote node assumed as sender. |
| 380 | +func (api *PrivateLespayAPI) makeCall(ctx context.Context, remote bool, nodeStr string, cmd []byte) ([]byte, error) { |
| 381 | + var ( |
| 382 | + id enode.ID |
| 383 | + freeID string |
| 384 | + peer *peer |
| 385 | + node *enode.Node |
| 386 | + err error |
| 387 | + ) |
| 388 | + if nodeStr != "" { |
| 389 | + if id, err = enode.ParseID(nodeStr); err == nil { |
| 390 | + if peer = api.peerSet.Peer(peerIdToString(id)); peer == nil { |
| 391 | + return nil, errors.New("peer not connected") |
| 392 | + } |
| 393 | + freeID = peer.freeClientId() |
| 394 | + } else { |
| 395 | + var err error |
| 396 | + if node, err = enode.Parse(enode.ValidSchemes, nodeStr); err == nil { |
| 397 | + id = node.ID() |
| 398 | + freeID = node.IP().String() |
| 399 | + } else { |
| 400 | + return nil, err |
| 401 | + } |
| 402 | + } |
| 403 | + } |
| 404 | + |
| 405 | + if remote { |
| 406 | + var ( |
| 407 | + reply []byte |
| 408 | + cancelFn func() bool |
| 409 | + ) |
| 410 | + delivered := make(chan struct{}) |
| 411 | + if peer != nil { |
| 412 | + // remote call to a connected peer through LES |
| 413 | + if api.clientHandler == nil { |
| 414 | + return nil, errors.New("client handler not available") |
| 415 | + } |
| 416 | + cancelFn = api.clientHandler.makeLespayCall(peer, cmd, func(r []byte, delay uint) bool { |
| 417 | + reply = r |
| 418 | + close(delivered) |
| 419 | + return reply != nil |
| 420 | + }) |
| 421 | + } else { |
| 422 | + // remote call through UDP TALK |
| 423 | + if api.dht == nil { |
| 424 | + return nil, errors.New("UDP DHT not available") |
| 425 | + } |
| 426 | + cancelFn = api.dht.SendTalkRequest(node, "lespay", [][]byte{cmd}, func(payload interface{}, delay uint) bool { |
| 427 | + if replies, ok := payload.([]interface{}); ok && len(replies) == 1 { |
| 428 | + reply, _ = replies[0].([]byte) |
| 429 | + } |
| 430 | + close(delivered) |
| 431 | + return reply != nil |
| 432 | + }) |
| 433 | + } |
| 434 | + select { |
| 435 | + case <-time.After(time.Second * 5): |
| 436 | + cancelFn() |
| 437 | + return nil, errors.New("timeout") |
| 438 | + case <-ctx.Done(): |
| 439 | + cancelFn() |
| 440 | + return nil, ctx.Err() |
| 441 | + case <-delivered: |
| 442 | + if len(reply) == 0 { |
| 443 | + return nil, errors.New("unknown command") |
| 444 | + } |
| 445 | + return reply, nil |
| 446 | + } |
| 447 | + } else { |
| 448 | + if api.tokenSale == nil { |
| 449 | + return nil, errors.New("token sale module not available") |
| 450 | + } |
| 451 | + // execute call locally |
| 452 | + return api.tokenSale.runCommand(cmd, id, freeID), nil |
| 453 | + } |
| 454 | + |
| 455 | +} |
| 456 | + |
| 457 | +// Connection checks whether it is possible with the current balance levels to establish |
| 458 | +// requested connection or capacity change and then stay connected for the given amount |
| 459 | +// of time. If it is possible and setCap is also true then the client is activated of the |
| 460 | +// capacity change is performed. If not then returns how many tokens are missing and how |
| 461 | +// much that would currently cost using the specified payment module(s). |
| 462 | +func (api *PrivateLespayAPI) Connection(ctx context.Context, remote bool, node string, requestedCapacity, stayConnected uint64, paymentModule []string, setCap bool) (results tsConnectionResults, err error) { |
| 463 | + params := tsConnectionParams{requestedCapacity, stayConnected, paymentModule, setCap} |
| 464 | + enc, _ := rlp.EncodeToBytes(¶ms) |
| 465 | + var resEnc []byte |
| 466 | + resEnc, err = api.makeCall(ctx, remote, node, append([]byte{tsConnection}, enc...)) |
| 467 | + if err != nil { |
| 468 | + return |
| 469 | + } |
| 470 | + err = rlp.DecodeBytes(resEnc, &results) |
| 471 | + return |
| 472 | +} |
| 473 | + |
| 474 | +// Deposit credits a payment on the sender's account using the specified payment module |
| 475 | +func (api *PrivateLespayAPI) Deposit(ctx context.Context, remote bool, node, paymentModule, proofOfPayment string) (results tsDepositResults, err error) { |
| 476 | + var proof []byte |
| 477 | + if proof, err = hexutil.Decode(proofOfPayment); err != nil { |
| 478 | + return |
| 479 | + } |
| 480 | + params := tsDepositParams{paymentModule, proof} |
| 481 | + enc, _ := rlp.EncodeToBytes(¶ms) |
| 482 | + var resEnc []byte |
| 483 | + resEnc, err = api.makeCall(ctx, remote, node, append([]byte{tsDeposit}, enc...)) |
| 484 | + if err != nil { |
| 485 | + return |
| 486 | + } |
| 487 | + err = rlp.DecodeBytes(resEnc, &results) |
| 488 | + return |
| 489 | +} |
| 490 | + |
| 491 | +// BuyTokens tries to convert the permanent balance (nominated in the server's preferred |
| 492 | +// currency, PC) to service tokens. If spendAll is true then it sells the maxSpend amount |
| 493 | +// of PC coins if the received service token amount is at least minReceive. If spendAll is |
| 494 | +// false then is buys minReceive amount of tokens if it does not cost more than maxSpend |
| 495 | +// amount of PC coins. |
| 496 | +// if relative is true then maxSpend and minReceive are specified relative to their current |
| 497 | +// balances. In this case maxSpend represents the amount under which the PC balance should |
| 498 | +// not go and minReceive represents the amount the service token balance should reach. |
| 499 | +// This mode is useful when actual conversion is intended to happen and the sender has to |
| 500 | +// retry the command after not receiving a reply previously. In this case the sender cannot |
| 501 | +// be sure whether the conversion has already happened or not. If relative is true then it |
| 502 | +// is impossible to do a conversion twice. In exchange the sender needs to know its current |
| 503 | +// balances (which it probably does if it has made a previous call to just ask the current price). |
| 504 | +func (api *PrivateLespayAPI) BuyTokens(ctx context.Context, remote bool, node string, maxSpend, minReceive uint64, relative, spendAll bool) (results tsBuyTokensResults, err error) { |
| 505 | + params := tsBuyTokensParams{maxSpend, minReceive, relative, spendAll} |
| 506 | + enc, _ := rlp.EncodeToBytes(¶ms) |
| 507 | + var resEnc []byte |
| 508 | + resEnc, err = api.makeCall(ctx, remote, node, append([]byte{tsBuyTokens}, enc...)) |
| 509 | + if err != nil { |
| 510 | + return |
| 511 | + } |
| 512 | + err = rlp.DecodeBytes(resEnc, &results) |
| 513 | + return |
| 514 | +} |
| 515 | + |
| 516 | +// SellTokens tries to convert service tokens to permanent balance (nominated in the server's |
| 517 | +// preferred currency, PC). Parameters work similarly to BuyTokens. |
| 518 | +func (api *PrivateLespayAPI) SellTokens(ctx context.Context, remote bool, node string, maxSell, minRefund uint64, relative, sellAll bool) (results tsSellTokensResults, err error) { |
| 519 | + params := tsSellTokensParams{maxSell, minRefund, relative, sellAll} |
| 520 | + enc, _ := rlp.EncodeToBytes(¶ms) |
| 521 | + var resEnc []byte |
| 522 | + resEnc, err = api.makeCall(ctx, remote, node, append([]byte{tsSellTokens}, enc...)) |
| 523 | + if err != nil { |
| 524 | + return |
| 525 | + } |
| 526 | + err = rlp.DecodeBytes(resEnc, &results) |
| 527 | + return |
| 528 | +} |
| 529 | + |
| 530 | +// GetBalance returns the current PC balance and service token balance |
| 531 | +func (api *PrivateLespayAPI) GetBalance(ctx context.Context, remote bool, node string) (results tsGetBalanceResults, err error) { |
| 532 | + var resEnc []byte |
| 533 | + resEnc, err = api.makeCall(ctx, remote, node, []byte{tsGetBalance}) |
| 534 | + if err != nil { |
| 535 | + return |
| 536 | + } |
| 537 | + err = rlp.DecodeBytes(resEnc, &results) |
| 538 | + return |
| 539 | +} |
| 540 | + |
| 541 | +// Info returns general information about the server, including version info of the |
| 542 | +// lespay command set, supported payment modules and token expiration time constant |
| 543 | +func (api *PrivateLespayAPI) Info(ctx context.Context, remote bool, node string) (results tsInfoApiResults, err error) { |
| 544 | + var resEnc []byte |
| 545 | + resEnc, err = api.makeCall(ctx, remote, node, []byte{tsInfo}) |
| 546 | + if err != nil { |
| 547 | + return |
| 548 | + } |
| 549 | + err = rlp.DecodeBytes(resEnc, &results) |
| 550 | + return |
| 551 | +} |
| 552 | + |
| 553 | +// ReceiverInfo returns information about the specified payment receiver(s) if supported |
| 554 | +func (api *PrivateLespayAPI) ReceiverInfo(ctx context.Context, remote bool, node string, receiverIDs []string) (results tsReceiverInfoApiResults, err error) { |
| 555 | + params := tsReceiverInfoParams(receiverIDs) |
| 556 | + enc, _ := rlp.EncodeToBytes(¶ms) |
| 557 | + var resEnc []byte |
| 558 | + resEnc, err = api.makeCall(ctx, remote, node, append([]byte{tsReceiverInfo}, enc...)) |
| 559 | + if err != nil { |
| 560 | + return |
| 561 | + } |
| 562 | + err = rlp.DecodeBytes(resEnc, &results) |
| 563 | + return |
| 564 | +} |
0 commit comments