valuetracker_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2020 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 client
  17. import (
  18. "math"
  19. "math/rand"
  20. "strconv"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common/mclock"
  24. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  25. "github.com/ethereum/go-ethereum/p2p/enode"
  26. "github.com/ethereum/go-ethereum/les/utils"
  27. )
  28. const (
  29. testReqTypes = 3
  30. testNodeCount = 5
  31. testReqCount = 10000
  32. testRounds = 10
  33. )
  34. func TestValueTracker(t *testing.T) {
  35. db := memorydb.New()
  36. clock := &mclock.Simulated{}
  37. requestList := make([]RequestInfo, testReqTypes)
  38. relPrices := make([]float64, testReqTypes)
  39. totalAmount := make([]uint64, testReqTypes)
  40. for i := range requestList {
  41. requestList[i] = RequestInfo{Name: "testreq" + strconv.Itoa(i), InitAmount: 1, InitValue: 1}
  42. totalAmount[i] = 1
  43. relPrices[i] = rand.Float64() + 0.1
  44. }
  45. nodes := make([]*NodeValueTracker, testNodeCount)
  46. for round := 0; round < testRounds; round++ {
  47. makeRequests := round < testRounds-2
  48. useExpiration := round == testRounds-1
  49. var expRate float64
  50. if useExpiration {
  51. expRate = math.Log(2) / float64(time.Hour*100)
  52. }
  53. vt := NewValueTracker(db, clock, requestList, time.Minute, 1/float64(time.Hour), expRate, expRate)
  54. updateCosts := func(i int) {
  55. costList := make([]uint64, testReqTypes)
  56. baseCost := rand.Float64()*10000000 + 100000
  57. for j := range costList {
  58. costList[j] = uint64(baseCost * relPrices[j])
  59. }
  60. nodes[i].UpdateCosts(costList)
  61. }
  62. for i := range nodes {
  63. nodes[i] = vt.Register(enode.ID{byte(i)})
  64. updateCosts(i)
  65. }
  66. if makeRequests {
  67. for i := 0; i < testReqCount; i++ {
  68. reqType := rand.Intn(testReqTypes)
  69. reqAmount := rand.Intn(10) + 1
  70. node := rand.Intn(testNodeCount)
  71. respTime := time.Duration((rand.Float64() + 1) * float64(time.Second) * float64(node+1) / testNodeCount)
  72. totalAmount[reqType] += uint64(reqAmount)
  73. nodes[node].Served([]ServedRequest{{uint32(reqType), uint32(reqAmount)}}, respTime)
  74. clock.Run(time.Second)
  75. }
  76. } else {
  77. clock.Run(time.Hour * 100)
  78. if useExpiration {
  79. for i, a := range totalAmount {
  80. totalAmount[i] = a / 2
  81. }
  82. }
  83. }
  84. vt.Stop()
  85. var sumrp, sumrv float64
  86. for i, rp := range relPrices {
  87. sumrp += rp
  88. sumrv += vt.refBasket.reqValues[i]
  89. }
  90. for i, rp := range relPrices {
  91. ratio := vt.refBasket.reqValues[i] * sumrp / (rp * sumrv)
  92. if ratio < 0.99 || ratio > 1.01 {
  93. t.Errorf("reqValues (%v) does not match relPrices (%v)", vt.refBasket.reqValues, relPrices)
  94. break
  95. }
  96. }
  97. exp := utils.ExpFactor(vt.StatsExpirer().LogOffset(clock.Now()))
  98. basketAmount := make([]uint64, testReqTypes)
  99. for i, bi := range vt.refBasket.basket.items {
  100. basketAmount[i] += uint64(exp.Value(float64(bi.amount), vt.refBasket.basket.exp))
  101. }
  102. if makeRequests {
  103. // if we did not make requests in this round then we expect all amounts to be
  104. // in the reference basket
  105. for _, node := range nodes {
  106. for i, bi := range node.basket.basket.items {
  107. basketAmount[i] += uint64(exp.Value(float64(bi.amount), node.basket.basket.exp))
  108. }
  109. }
  110. }
  111. for i, a := range basketAmount {
  112. amount := a / basketFactor
  113. if amount+10 < totalAmount[i] || amount > totalAmount[i]+10 {
  114. t.Errorf("totalAmount[%d] mismatch in round %d (expected %d, got %d)", i, round, totalAmount[i], amount)
  115. }
  116. }
  117. var sumValue float64
  118. for _, node := range nodes {
  119. s := node.RtStats()
  120. sumValue += s.Value(maxResponseWeights, exp)
  121. }
  122. s := vt.RtStats()
  123. mainValue := s.Value(maxResponseWeights, exp)
  124. if sumValue < mainValue-10 || sumValue > mainValue+10 {
  125. t.Errorf("Main rtStats value does not match sum of node rtStats values in round %d (main %v, sum %v)", round, mainValue, sumValue)
  126. }
  127. }
  128. }