pruner_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2019 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 les
  17. import (
  18. "bytes"
  19. "context"
  20. "encoding/binary"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/core"
  24. "github.com/ethereum/go-ethereum/light"
  25. )
  26. func TestLightPruner(t *testing.T) {
  27. var (
  28. waitIndexers = func(cIndexer, bIndexer, btIndexer *core.ChainIndexer) {
  29. for {
  30. cs, _, _ := cIndexer.Sections()
  31. bts, _, _ := btIndexer.Sections()
  32. if cs >= 3 && bts >= 3 {
  33. break
  34. }
  35. time.Sleep(10 * time.Millisecond)
  36. }
  37. }
  38. config = light.TestClientIndexerConfig
  39. netconfig = testnetConfig{
  40. blocks: int(3*config.ChtSize + config.ChtConfirms),
  41. protocol: 3,
  42. indexFn: waitIndexers,
  43. connect: true,
  44. }
  45. )
  46. server, client, tearDown := newClientServerEnv(t, netconfig)
  47. defer tearDown()
  48. // checkDB iterates the chain with given prefix, resolves the block number
  49. // with given callback and ensures this entry should exist or not.
  50. checkDB := func(from, to uint64, prefix []byte, resolve func(key, value []byte) *uint64, exist bool) bool {
  51. it := client.db.NewIterator(prefix, nil)
  52. defer it.Release()
  53. var next = from
  54. for it.Next() {
  55. number := resolve(it.Key(), it.Value())
  56. if number == nil || *number < from {
  57. continue
  58. } else if *number > to {
  59. return true
  60. }
  61. if exist {
  62. if *number != next {
  63. return false
  64. }
  65. next++
  66. } else {
  67. return false
  68. }
  69. }
  70. return true
  71. }
  72. // checkPruned checks and ensures the stale chain data has been pruned.
  73. checkPruned := func(from, to uint64) {
  74. // Iterate canonical hash
  75. if !checkDB(from, to, []byte("h"), func(key, value []byte) *uint64 {
  76. if len(key) == 1+8+1 && bytes.Equal(key[9:10], []byte("n")) {
  77. n := binary.BigEndian.Uint64(key[1:9])
  78. return &n
  79. }
  80. return nil
  81. }, false) {
  82. t.Fatalf("canonical hash mappings are not properly pruned")
  83. }
  84. // Iterate header
  85. if !checkDB(from, to, []byte("h"), func(key, value []byte) *uint64 {
  86. if len(key) == 1+8+32 {
  87. n := binary.BigEndian.Uint64(key[1:9])
  88. return &n
  89. }
  90. return nil
  91. }, false) {
  92. t.Fatalf("headers are not properly pruned")
  93. }
  94. // Iterate body
  95. if !checkDB(from, to, []byte("b"), func(key, value []byte) *uint64 {
  96. if len(key) == 1+8+32 {
  97. n := binary.BigEndian.Uint64(key[1:9])
  98. return &n
  99. }
  100. return nil
  101. }, false) {
  102. t.Fatalf("block bodies are not properly pruned")
  103. }
  104. // Iterate receipts
  105. if !checkDB(from, to, []byte("r"), func(key, value []byte) *uint64 {
  106. if len(key) == 1+8+32 {
  107. n := binary.BigEndian.Uint64(key[1:9])
  108. return &n
  109. }
  110. return nil
  111. }, false) {
  112. t.Fatalf("receipts are not properly pruned")
  113. }
  114. // Iterate td
  115. if !checkDB(from, to, []byte("h"), func(key, value []byte) *uint64 {
  116. if len(key) == 1+8+32+1 && bytes.Equal(key[41:42], []byte("t")) {
  117. n := binary.BigEndian.Uint64(key[1:9])
  118. return &n
  119. }
  120. return nil
  121. }, false) {
  122. t.Fatalf("tds are not properly pruned")
  123. }
  124. }
  125. // Start light pruner.
  126. time.Sleep(1500 * time.Millisecond) // Ensure light client has finished the syncing and indexing
  127. newPruner(client.db, client.chtIndexer, client.bloomTrieIndexer)
  128. time.Sleep(1500 * time.Millisecond) // Ensure pruner have enough time to prune data.
  129. checkPruned(1, config.ChtSize-1)
  130. // Ensure all APIs still work after pruning.
  131. var cases = []struct {
  132. from, to uint64
  133. methodName string
  134. method func(uint64) bool
  135. }{
  136. {
  137. 1, 10, "GetHeaderByNumber",
  138. func(n uint64) bool {
  139. _, err := light.GetHeaderByNumber(context.Background(), client.handler.backend.odr, n)
  140. return err == nil
  141. },
  142. },
  143. {
  144. 11, 20, "GetCanonicalHash",
  145. func(n uint64) bool {
  146. _, err := light.GetCanonicalHash(context.Background(), client.handler.backend.odr, n)
  147. return err == nil
  148. },
  149. },
  150. {
  151. 21, 30, "GetTd",
  152. func(n uint64) bool {
  153. _, err := light.GetTd(context.Background(), client.handler.backend.odr, server.handler.blockchain.GetHeaderByNumber(n).Hash(), n)
  154. return err == nil
  155. },
  156. },
  157. {
  158. 31, 40, "GetBodyRLP",
  159. func(n uint64) bool {
  160. _, err := light.GetBodyRLP(context.Background(), client.handler.backend.odr, server.handler.blockchain.GetHeaderByNumber(n).Hash(), n)
  161. return err == nil
  162. },
  163. },
  164. {
  165. 41, 50, "GetBlock",
  166. func(n uint64) bool {
  167. _, err := light.GetBlock(context.Background(), client.handler.backend.odr, server.handler.blockchain.GetHeaderByNumber(n).Hash(), n)
  168. return err == nil
  169. },
  170. },
  171. {
  172. 51, 60, "GetBlockReceipts",
  173. func(n uint64) bool {
  174. _, err := light.GetBlockReceipts(context.Background(), client.handler.backend.odr, server.handler.blockchain.GetHeaderByNumber(n).Hash(), n)
  175. return err == nil
  176. },
  177. },
  178. }
  179. for _, c := range cases {
  180. for i := c.from; i <= c.to; i++ {
  181. if !c.method(i) {
  182. t.Fatalf("rpc method %s failed, number %d", c.methodName, i)
  183. }
  184. }
  185. }
  186. // Check GetBloombits
  187. _, err := light.GetBloomBits(context.Background(), client.handler.backend.odr, 0, []uint64{0})
  188. if err != nil {
  189. t.Fatalf("Failed to retrieve bloombits of pruned section: %v", err)
  190. }
  191. // Ensure the ODR cached data can be cleaned by pruner.
  192. newPruner(client.db, client.chtIndexer, client.bloomTrieIndexer)
  193. time.Sleep(50 * time.Millisecond) // Ensure pruner have enough time to prune data.
  194. checkPruned(1, config.ChtSize-1) // Ensure all cached data(by odr) is cleaned.
  195. }