limiter_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 utils
  17. import (
  18. "math/rand"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/p2p/enode"
  21. )
  22. const (
  23. ltTolerance = 0.03
  24. ltRounds = 7
  25. )
  26. type (
  27. ltNode struct {
  28. addr, id int
  29. value, exp float64
  30. cost uint
  31. reqRate float64
  32. reqMax, runCount int
  33. lastTotalCost uint
  34. served, dropped int
  35. }
  36. ltResult struct {
  37. node *ltNode
  38. ch chan struct{}
  39. }
  40. limTest struct {
  41. limiter *Limiter
  42. results chan ltResult
  43. runCount int
  44. expCost, totalCost uint
  45. }
  46. )
  47. func (lt *limTest) request(n *ltNode) {
  48. var (
  49. address string
  50. id enode.ID
  51. )
  52. if n.addr >= 0 {
  53. address = string([]byte{byte(n.addr)})
  54. } else {
  55. var b [32]byte
  56. rand.Read(b[:])
  57. address = string(b[:])
  58. }
  59. if n.id >= 0 {
  60. id = enode.ID{byte(n.id)}
  61. } else {
  62. rand.Read(id[:])
  63. }
  64. lt.runCount++
  65. n.runCount++
  66. cch := lt.limiter.Add(id, address, n.value, n.cost)
  67. go func() {
  68. lt.results <- ltResult{n, <-cch}
  69. }()
  70. }
  71. func (lt *limTest) moreRequests(n *ltNode) {
  72. maxStart := int(float64(lt.totalCost-n.lastTotalCost) * n.reqRate)
  73. if maxStart != 0 {
  74. n.lastTotalCost = lt.totalCost
  75. }
  76. for n.reqMax > n.runCount && maxStart > 0 {
  77. lt.request(n)
  78. maxStart--
  79. }
  80. }
  81. func (lt *limTest) process() {
  82. res := <-lt.results
  83. lt.runCount--
  84. res.node.runCount--
  85. if res.ch != nil {
  86. res.node.served++
  87. if res.node.exp != 0 {
  88. lt.expCost += res.node.cost
  89. }
  90. lt.totalCost += res.node.cost
  91. close(res.ch)
  92. } else {
  93. res.node.dropped++
  94. }
  95. }
  96. func TestLimiter(t *testing.T) {
  97. limTests := [][]*ltNode{
  98. { // one id from an individual address and two ids from a shared address
  99. {addr: 0, id: 0, value: 0, cost: 1, reqRate: 1, reqMax: 1, exp: 0.5},
  100. {addr: 1, id: 1, value: 0, cost: 1, reqRate: 1, reqMax: 1, exp: 0.25},
  101. {addr: 1, id: 2, value: 0, cost: 1, reqRate: 1, reqMax: 1, exp: 0.25},
  102. },
  103. { // varying request costs
  104. {addr: 0, id: 0, value: 0, cost: 10, reqRate: 0.2, reqMax: 1, exp: 0.5},
  105. {addr: 1, id: 1, value: 0, cost: 3, reqRate: 0.5, reqMax: 1, exp: 0.25},
  106. {addr: 1, id: 2, value: 0, cost: 1, reqRate: 1, reqMax: 1, exp: 0.25},
  107. },
  108. { // different request rate
  109. {addr: 0, id: 0, value: 0, cost: 1, reqRate: 2, reqMax: 2, exp: 0.5},
  110. {addr: 1, id: 1, value: 0, cost: 1, reqRate: 10, reqMax: 10, exp: 0.25},
  111. {addr: 1, id: 2, value: 0, cost: 1, reqRate: 1, reqMax: 1, exp: 0.25},
  112. },
  113. { // adding value
  114. {addr: 0, id: 0, value: 3, cost: 1, reqRate: 1, reqMax: 1, exp: (0.5 + 0.3) / 2},
  115. {addr: 1, id: 1, value: 0, cost: 1, reqRate: 1, reqMax: 1, exp: 0.25 / 2},
  116. {addr: 1, id: 2, value: 7, cost: 1, reqRate: 1, reqMax: 1, exp: (0.25 + 0.7) / 2},
  117. },
  118. { // DoS attack from a single address with a single id
  119. {addr: 0, id: 0, value: 1, cost: 1, reqRate: 1, reqMax: 1, exp: 0.3333},
  120. {addr: 1, id: 1, value: 1, cost: 1, reqRate: 1, reqMax: 1, exp: 0.3333},
  121. {addr: 2, id: 2, value: 1, cost: 1, reqRate: 1, reqMax: 1, exp: 0.3333},
  122. {addr: 3, id: 3, value: 0, cost: 1, reqRate: 10, reqMax: 1000000000, exp: 0},
  123. },
  124. { // DoS attack from a single address with different ids
  125. {addr: 0, id: 0, value: 1, cost: 1, reqRate: 1, reqMax: 1, exp: 0.3333},
  126. {addr: 1, id: 1, value: 1, cost: 1, reqRate: 1, reqMax: 1, exp: 0.3333},
  127. {addr: 2, id: 2, value: 1, cost: 1, reqRate: 1, reqMax: 1, exp: 0.3333},
  128. {addr: 3, id: -1, value: 0, cost: 1, reqRate: 1, reqMax: 1000000000, exp: 0},
  129. },
  130. { // DDoS attack from different addresses with a single id
  131. {addr: 0, id: 0, value: 1, cost: 1, reqRate: 1, reqMax: 1, exp: 0.3333},
  132. {addr: 1, id: 1, value: 1, cost: 1, reqRate: 1, reqMax: 1, exp: 0.3333},
  133. {addr: 2, id: 2, value: 1, cost: 1, reqRate: 1, reqMax: 1, exp: 0.3333},
  134. {addr: -1, id: 3, value: 0, cost: 1, reqRate: 1, reqMax: 1000000000, exp: 0},
  135. },
  136. { // DDoS attack from different addresses with different ids
  137. {addr: 0, id: 0, value: 1, cost: 1, reqRate: 1, reqMax: 1, exp: 0.3333},
  138. {addr: 1, id: 1, value: 1, cost: 1, reqRate: 1, reqMax: 1, exp: 0.3333},
  139. {addr: 2, id: 2, value: 1, cost: 1, reqRate: 1, reqMax: 1, exp: 0.3333},
  140. {addr: -1, id: -1, value: 0, cost: 1, reqRate: 1, reqMax: 1000000000, exp: 0},
  141. },
  142. }
  143. lt := &limTest{
  144. limiter: NewLimiter(100),
  145. results: make(chan ltResult),
  146. }
  147. for _, test := range limTests {
  148. lt.expCost, lt.totalCost = 0, 0
  149. iterCount := 10000
  150. for j := 0; j < ltRounds; j++ {
  151. // try to reach expected target range in multiple rounds with increasing iteration counts
  152. last := j == ltRounds-1
  153. for _, n := range test {
  154. lt.request(n)
  155. }
  156. for i := 0; i < iterCount; i++ {
  157. lt.process()
  158. for _, n := range test {
  159. lt.moreRequests(n)
  160. }
  161. }
  162. for lt.runCount > 0 {
  163. lt.process()
  164. }
  165. if spamRatio := 1 - float64(lt.expCost)/float64(lt.totalCost); spamRatio > 0.5*(1+ltTolerance) {
  166. t.Errorf("Spam ratio too high (%f)", spamRatio)
  167. }
  168. fail, success := false, true
  169. for _, n := range test {
  170. if n.exp != 0 {
  171. if n.dropped > 0 {
  172. t.Errorf("Dropped %d requests of non-spam node", n.dropped)
  173. fail = true
  174. }
  175. r := float64(n.served) * float64(n.cost) / float64(lt.expCost)
  176. if r < n.exp*(1-ltTolerance) || r > n.exp*(1+ltTolerance) {
  177. if last {
  178. // print error only if the target is still not reached in the last round
  179. t.Errorf("Request ratio (%f) does not match expected value (%f)", r, n.exp)
  180. }
  181. success = false
  182. }
  183. }
  184. }
  185. if fail || success {
  186. break
  187. }
  188. // neither failed nor succeeded; try more iterations to reach probability targets
  189. iterCount *= 2
  190. }
  191. }
  192. lt.limiter.Stop()
  193. }