filter_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2015 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 filters
  17. import (
  18. "context"
  19. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/consensus/ethash"
  23. "github.com/ethereum/go-ethereum/core"
  24. "github.com/ethereum/go-ethereum/core/rawdb"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/ethereum/go-ethereum/params"
  28. )
  29. func makeReceipt(addr common.Address) *types.Receipt {
  30. receipt := types.NewReceipt(nil, false, 0)
  31. receipt.Logs = []*types.Log{
  32. {Address: addr},
  33. }
  34. receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
  35. return receipt
  36. }
  37. func BenchmarkFilters(b *testing.B) {
  38. dir := b.TempDir()
  39. var (
  40. db, _ = rawdb.NewLevelDBDatabase(dir, 0, 0, "", false)
  41. _, sys = newTestFilterSystem(b, db, Config{})
  42. key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  43. addr1 = crypto.PubkeyToAddress(key1.PublicKey)
  44. addr2 = common.BytesToAddress([]byte("jeff"))
  45. addr3 = common.BytesToAddress([]byte("ethereum"))
  46. addr4 = common.BytesToAddress([]byte("random addresses please"))
  47. gspec = core.Genesis{
  48. Alloc: core.GenesisAlloc{addr1: {Balance: big.NewInt(1000000)}},
  49. BaseFee: big.NewInt(params.InitialBaseFee),
  50. }
  51. genesis = gspec.ToBlock()
  52. )
  53. defer db.Close()
  54. gspec.MustCommit(db)
  55. chain, receipts := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 100010, func(i int, gen *core.BlockGen) {
  56. switch i {
  57. case 2403:
  58. receipt := makeReceipt(addr1)
  59. gen.AddUncheckedReceipt(receipt)
  60. gen.AddUncheckedTx(types.NewTransaction(999, common.HexToAddress("0x999"), big.NewInt(999), 999, gen.BaseFee(), nil))
  61. case 1034:
  62. receipt := makeReceipt(addr2)
  63. gen.AddUncheckedReceipt(receipt)
  64. gen.AddUncheckedTx(types.NewTransaction(999, common.HexToAddress("0x999"), big.NewInt(999), 999, gen.BaseFee(), nil))
  65. case 34:
  66. receipt := makeReceipt(addr3)
  67. gen.AddUncheckedReceipt(receipt)
  68. gen.AddUncheckedTx(types.NewTransaction(999, common.HexToAddress("0x999"), big.NewInt(999), 999, gen.BaseFee(), nil))
  69. case 99999:
  70. receipt := makeReceipt(addr4)
  71. gen.AddUncheckedReceipt(receipt)
  72. gen.AddUncheckedTx(types.NewTransaction(999, common.HexToAddress("0x999"), big.NewInt(999), 999, gen.BaseFee(), nil))
  73. }
  74. })
  75. for i, block := range chain {
  76. rawdb.WriteBlock(db, block)
  77. rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64())
  78. rawdb.WriteHeadBlockHash(db, block.Hash())
  79. rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts[i])
  80. }
  81. b.ResetTimer()
  82. filter := sys.NewRangeFilter(0, -1, []common.Address{addr1, addr2, addr3, addr4}, nil)
  83. for i := 0; i < b.N; i++ {
  84. logs, _ := filter.Logs(context.Background())
  85. if len(logs) != 4 {
  86. b.Fatal("expected 4 logs, got", len(logs))
  87. }
  88. }
  89. }
  90. func TestFilters(t *testing.T) {
  91. dir := t.TempDir()
  92. var (
  93. db, _ = rawdb.NewLevelDBDatabase(dir, 0, 0, "", false)
  94. _, sys = newTestFilterSystem(t, db, Config{})
  95. key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  96. addr = crypto.PubkeyToAddress(key1.PublicKey)
  97. hash1 = common.BytesToHash([]byte("topic1"))
  98. hash2 = common.BytesToHash([]byte("topic2"))
  99. hash3 = common.BytesToHash([]byte("topic3"))
  100. hash4 = common.BytesToHash([]byte("topic4"))
  101. gspec = core.Genesis{
  102. Alloc: core.GenesisAlloc{addr: {Balance: big.NewInt(1000000)}},
  103. BaseFee: big.NewInt(params.InitialBaseFee),
  104. }
  105. genesis = gspec.ToBlock()
  106. )
  107. defer db.Close()
  108. gspec.MustCommit(db)
  109. chain, receipts := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 1000, func(i int, gen *core.BlockGen) {
  110. switch i {
  111. case 1:
  112. receipt := types.NewReceipt(nil, false, 0)
  113. receipt.Logs = []*types.Log{
  114. {
  115. Address: addr,
  116. Topics: []common.Hash{hash1},
  117. },
  118. }
  119. gen.AddUncheckedReceipt(receipt)
  120. gen.AddUncheckedTx(types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, gen.BaseFee(), nil))
  121. case 2:
  122. receipt := types.NewReceipt(nil, false, 0)
  123. receipt.Logs = []*types.Log{
  124. {
  125. Address: addr,
  126. Topics: []common.Hash{hash2},
  127. },
  128. }
  129. gen.AddUncheckedReceipt(receipt)
  130. gen.AddUncheckedTx(types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, gen.BaseFee(), nil))
  131. case 998:
  132. receipt := types.NewReceipt(nil, false, 0)
  133. receipt.Logs = []*types.Log{
  134. {
  135. Address: addr,
  136. Topics: []common.Hash{hash3},
  137. },
  138. }
  139. gen.AddUncheckedReceipt(receipt)
  140. gen.AddUncheckedTx(types.NewTransaction(998, common.HexToAddress("0x998"), big.NewInt(998), 998, gen.BaseFee(), nil))
  141. case 999:
  142. receipt := types.NewReceipt(nil, false, 0)
  143. receipt.Logs = []*types.Log{
  144. {
  145. Address: addr,
  146. Topics: []common.Hash{hash4},
  147. },
  148. }
  149. gen.AddUncheckedReceipt(receipt)
  150. gen.AddUncheckedTx(types.NewTransaction(999, common.HexToAddress("0x999"), big.NewInt(999), 999, gen.BaseFee(), nil))
  151. }
  152. })
  153. for i, block := range chain {
  154. rawdb.WriteBlock(db, block)
  155. rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64())
  156. rawdb.WriteHeadBlockHash(db, block.Hash())
  157. rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts[i])
  158. }
  159. filter := sys.NewRangeFilter(0, -1, []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}})
  160. logs, _ := filter.Logs(context.Background())
  161. if len(logs) != 4 {
  162. t.Error("expected 4 log, got", len(logs))
  163. }
  164. filter = sys.NewRangeFilter(900, 999, []common.Address{addr}, [][]common.Hash{{hash3}})
  165. logs, _ = filter.Logs(context.Background())
  166. if len(logs) != 1 {
  167. t.Error("expected 1 log, got", len(logs))
  168. }
  169. if len(logs) > 0 && logs[0].Topics[0] != hash3 {
  170. t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0])
  171. }
  172. filter = sys.NewRangeFilter(990, -1, []common.Address{addr}, [][]common.Hash{{hash3}})
  173. logs, _ = filter.Logs(context.Background())
  174. if len(logs) != 1 {
  175. t.Error("expected 1 log, got", len(logs))
  176. }
  177. if len(logs) > 0 && logs[0].Topics[0] != hash3 {
  178. t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0])
  179. }
  180. filter = sys.NewRangeFilter(1, 10, nil, [][]common.Hash{{hash1, hash2}})
  181. logs, _ = filter.Logs(context.Background())
  182. if len(logs) != 2 {
  183. t.Error("expected 2 log, got", len(logs))
  184. }
  185. failHash := common.BytesToHash([]byte("fail"))
  186. filter = sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{failHash}})
  187. logs, _ = filter.Logs(context.Background())
  188. if len(logs) != 0 {
  189. t.Error("expected 0 log, got", len(logs))
  190. }
  191. failAddr := common.BytesToAddress([]byte("failmenow"))
  192. filter = sys.NewRangeFilter(0, -1, []common.Address{failAddr}, nil)
  193. logs, _ = filter.Logs(context.Background())
  194. if len(logs) != 0 {
  195. t.Error("expected 0 log, got", len(logs))
  196. }
  197. filter = sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{failHash}, {hash1}})
  198. logs, _ = filter.Logs(context.Background())
  199. if len(logs) != 0 {
  200. t.Error("expected 0 log, got", len(logs))
  201. }
  202. }