requests.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2021 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package vflux
  17. import (
  18. "errors"
  19. "math"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/rlp"
  22. )
  23. var ErrNoReply = errors.New("no reply for given request")
  24. const (
  25. MaxRequestLength = 16 // max number of individual requests in a batch
  26. CapacityQueryName = "cq"
  27. CapacityQueryMaxLen = 16
  28. )
  29. type (
  30. // Request describes a single vflux request inside a batch. Service and request
  31. // type are identified by strings, parameters are RLP encoded.
  32. Request struct {
  33. Service, Name string
  34. Params []byte
  35. }
  36. // Requests are a batch of vflux requests
  37. Requests []Request
  38. // Replies are the replies to a batch of requests
  39. Replies [][]byte
  40. // CapacityQueryReq is the encoding format of the capacity query
  41. CapacityQueryReq struct {
  42. Bias uint64 // seconds
  43. AddTokens []IntOrInf
  44. }
  45. // CapacityQueryReq is the encoding format of the response to the capacity query
  46. CapacityQueryReply []uint64
  47. )
  48. // Add encodes and adds a new request to the batch
  49. func (r *Requests) Add(service, name string, val interface{}) (int, error) {
  50. enc, err := rlp.EncodeToBytes(val)
  51. if err != nil {
  52. return -1, err
  53. }
  54. *r = append(*r, Request{
  55. Service: service,
  56. Name: name,
  57. Params: enc,
  58. })
  59. return len(*r) - 1, nil
  60. }
  61. // Get decodes the reply to the i-th request in the batch
  62. func (r Replies) Get(i int, val interface{}) error {
  63. if i < 0 || i >= len(r) {
  64. return ErrNoReply
  65. }
  66. return rlp.DecodeBytes(r[i], val)
  67. }
  68. const (
  69. IntNonNegative = iota
  70. IntNegative
  71. IntPlusInf
  72. IntMinusInf
  73. )
  74. // IntOrInf is the encoding format for arbitrary length signed integers that can also
  75. // hold the values of +Inf or -Inf
  76. type IntOrInf struct {
  77. Type uint8
  78. Value big.Int
  79. }
  80. // BigInt returns the value as a big.Int or panics if the value is infinity
  81. func (i *IntOrInf) BigInt() *big.Int {
  82. switch i.Type {
  83. case IntNonNegative:
  84. return new(big.Int).Set(&i.Value)
  85. case IntNegative:
  86. return new(big.Int).Neg(&i.Value)
  87. case IntPlusInf:
  88. panic(nil) // caller should check Inf() before trying to convert to big.Int
  89. case IntMinusInf:
  90. panic(nil)
  91. }
  92. return &big.Int{} // invalid type decodes to 0 value
  93. }
  94. // Inf returns 1 if the value is +Inf, -1 if it is -Inf, 0 otherwise
  95. func (i *IntOrInf) Inf() int {
  96. switch i.Type {
  97. case IntPlusInf:
  98. return 1
  99. case IntMinusInf:
  100. return -1
  101. }
  102. return 0 // invalid type decodes to 0 value
  103. }
  104. // Int64 limits the value between MinInt64 and MaxInt64 (even if it is +-Inf) and returns an int64 type
  105. func (i *IntOrInf) Int64() int64 {
  106. switch i.Type {
  107. case IntNonNegative:
  108. if i.Value.IsInt64() {
  109. return i.Value.Int64()
  110. } else {
  111. return math.MaxInt64
  112. }
  113. case IntNegative:
  114. if i.Value.IsInt64() {
  115. return -i.Value.Int64()
  116. } else {
  117. return math.MinInt64
  118. }
  119. case IntPlusInf:
  120. return math.MaxInt64
  121. case IntMinusInf:
  122. return math.MinInt64
  123. }
  124. return 0 // invalid type decodes to 0 value
  125. }
  126. // SetBigInt sets the value to the given big.Int
  127. func (i *IntOrInf) SetBigInt(v *big.Int) {
  128. if v.Sign() >= 0 {
  129. i.Type = IntNonNegative
  130. i.Value.Set(v)
  131. } else {
  132. i.Type = IntNegative
  133. i.Value.Neg(v)
  134. }
  135. }
  136. // SetInt64 sets the value to the given int64. Note that MaxInt64 translates to +Inf
  137. // while MinInt64 translates to -Inf.
  138. func (i *IntOrInf) SetInt64(v int64) {
  139. if v >= 0 {
  140. if v == math.MaxInt64 {
  141. i.Type = IntPlusInf
  142. } else {
  143. i.Type = IntNonNegative
  144. i.Value.SetInt64(v)
  145. }
  146. } else {
  147. if v == math.MinInt64 {
  148. i.Type = IntMinusInf
  149. } else {
  150. i.Type = IntNegative
  151. i.Value.SetInt64(-v)
  152. }
  153. }
  154. }
  155. // SetInf sets the value to +Inf or -Inf
  156. func (i *IntOrInf) SetInf(sign int) {
  157. if sign == 1 {
  158. i.Type = IntPlusInf
  159. } else {
  160. i.Type = IntMinusInf
  161. }
  162. }