pruner_test.go 5.8 KB

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