clientdb_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 server
  17. import (
  18. "reflect"
  19. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common/mclock"
  22. "github.com/ethereum/go-ethereum/core/rawdb"
  23. "github.com/ethereum/go-ethereum/les/utils"
  24. "github.com/ethereum/go-ethereum/p2p/enode"
  25. )
  26. func expval(v uint64) utils.ExpiredValue {
  27. return utils.ExpiredValue{Base: v}
  28. }
  29. func TestNodeDB(t *testing.T) {
  30. ndb := newNodeDB(rawdb.NewMemoryDatabase(), mclock.System{})
  31. defer ndb.close()
  32. var cases = []struct {
  33. id enode.ID
  34. ip string
  35. balance utils.ExpiredValue
  36. positive bool
  37. }{
  38. {enode.ID{0x00, 0x01, 0x02}, "", expval(100), true},
  39. {enode.ID{0x00, 0x01, 0x02}, "", expval(200), true},
  40. {enode.ID{}, "127.0.0.1", expval(100), false},
  41. {enode.ID{}, "127.0.0.1", expval(200), false},
  42. }
  43. for _, c := range cases {
  44. if c.positive {
  45. ndb.setBalance(c.id.Bytes(), false, c.balance)
  46. if pb := ndb.getOrNewBalance(c.id.Bytes(), false); !reflect.DeepEqual(pb, c.balance) {
  47. t.Fatalf("Positive balance mismatch, want %v, got %v", c.balance, pb)
  48. }
  49. } else {
  50. ndb.setBalance([]byte(c.ip), true, c.balance)
  51. if nb := ndb.getOrNewBalance([]byte(c.ip), true); !reflect.DeepEqual(nb, c.balance) {
  52. t.Fatalf("Negative balance mismatch, want %v, got %v", c.balance, nb)
  53. }
  54. }
  55. }
  56. for _, c := range cases {
  57. if c.positive {
  58. ndb.delBalance(c.id.Bytes(), false)
  59. if pb := ndb.getOrNewBalance(c.id.Bytes(), false); !reflect.DeepEqual(pb, utils.ExpiredValue{}) {
  60. t.Fatalf("Positive balance mismatch, want %v, got %v", utils.ExpiredValue{}, pb)
  61. }
  62. } else {
  63. ndb.delBalance([]byte(c.ip), true)
  64. if nb := ndb.getOrNewBalance([]byte(c.ip), true); !reflect.DeepEqual(nb, utils.ExpiredValue{}) {
  65. t.Fatalf("Negative balance mismatch, want %v, got %v", utils.ExpiredValue{}, nb)
  66. }
  67. }
  68. }
  69. posExp, negExp := utils.Fixed64(1000), utils.Fixed64(2000)
  70. ndb.setExpiration(posExp, negExp)
  71. if pos, neg := ndb.getExpiration(); pos != posExp || neg != negExp {
  72. t.Fatalf("Expiration mismatch, want %v / %v, got %v / %v", posExp, negExp, pos, neg)
  73. }
  74. /* curBalance := currencyBalance{typ: "ETH", amount: 10000}
  75. ndb.setCurrencyBalance(enode.ID{0x01, 0x02}, curBalance)
  76. if got := ndb.getCurrencyBalance(enode.ID{0x01, 0x02}); !reflect.DeepEqual(got, curBalance) {
  77. t.Fatalf("Currency balance mismatch, want %v, got %v", curBalance, got)
  78. }*/
  79. }
  80. func TestNodeDBExpiration(t *testing.T) {
  81. var (
  82. iterated int
  83. done = make(chan struct{}, 1)
  84. )
  85. callback := func(now mclock.AbsTime, neg bool, b utils.ExpiredValue) bool {
  86. iterated += 1
  87. return true
  88. }
  89. clock := &mclock.Simulated{}
  90. ndb := newNodeDB(rawdb.NewMemoryDatabase(), clock)
  91. defer ndb.close()
  92. ndb.evictCallBack = callback
  93. ndb.cleanupHook = func() { done <- struct{}{} }
  94. var cases = []struct {
  95. id []byte
  96. neg bool
  97. balance utils.ExpiredValue
  98. }{
  99. {[]byte{0x01, 0x02}, false, expval(1)},
  100. {[]byte{0x03, 0x04}, false, expval(1)},
  101. {[]byte{0x05, 0x06}, false, expval(1)},
  102. {[]byte{0x07, 0x08}, false, expval(1)},
  103. {[]byte("127.0.0.1"), true, expval(1)},
  104. {[]byte("127.0.0.2"), true, expval(1)},
  105. {[]byte("127.0.0.3"), true, expval(1)},
  106. {[]byte("127.0.0.4"), true, expval(1)},
  107. }
  108. for _, c := range cases {
  109. ndb.setBalance(c.id, c.neg, c.balance)
  110. }
  111. clock.WaitForTimers(1)
  112. clock.Run(time.Hour + time.Minute)
  113. select {
  114. case <-done:
  115. case <-time.NewTimer(time.Second).C:
  116. t.Fatalf("timeout")
  117. }
  118. if iterated != 8 {
  119. t.Fatalf("Failed to evict useless balances, want %v, got %d", 8, iterated)
  120. }
  121. for _, c := range cases {
  122. ndb.setBalance(c.id, c.neg, c.balance)
  123. }
  124. clock.WaitForTimers(1)
  125. clock.Run(time.Hour + time.Minute)
  126. select {
  127. case <-done:
  128. case <-time.NewTimer(time.Second).C:
  129. t.Fatalf("timeout")
  130. }
  131. if iterated != 16 {
  132. t.Fatalf("Failed to evict useless balances, want %v, got %d", 16, iterated)
  133. }
  134. }