accessors_indexes_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2018 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 rawdb
  17. import (
  18. "bytes"
  19. "hash"
  20. "math/big"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/ethdb"
  25. "github.com/ethereum/go-ethereum/params"
  26. "github.com/ethereum/go-ethereum/rlp"
  27. "golang.org/x/crypto/sha3"
  28. )
  29. // testHasher is the helper tool for transaction/receipt list hashing.
  30. // The original hasher is trie, in order to get rid of import cycle,
  31. // use the testing hasher instead.
  32. type testHasher struct {
  33. hasher hash.Hash
  34. }
  35. func newHasher() *testHasher {
  36. return &testHasher{hasher: sha3.NewLegacyKeccak256()}
  37. }
  38. func (h *testHasher) Reset() {
  39. h.hasher.Reset()
  40. }
  41. func (h *testHasher) Update(key, val []byte) {
  42. h.hasher.Write(key)
  43. h.hasher.Write(val)
  44. }
  45. func (h *testHasher) Hash() common.Hash {
  46. return common.BytesToHash(h.hasher.Sum(nil))
  47. }
  48. // Tests that positional lookup metadata can be stored and retrieved.
  49. func TestLookupStorage(t *testing.T) {
  50. tests := []struct {
  51. name string
  52. writeTxLookupEntriesByBlock func(ethdb.Writer, *types.Block)
  53. }{
  54. {
  55. "DatabaseV6",
  56. func(db ethdb.Writer, block *types.Block) {
  57. WriteTxLookupEntriesByBlock(db, block)
  58. },
  59. },
  60. {
  61. "DatabaseV4-V5",
  62. func(db ethdb.Writer, block *types.Block) {
  63. for _, tx := range block.Transactions() {
  64. db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes())
  65. }
  66. },
  67. },
  68. {
  69. "DatabaseV3",
  70. func(db ethdb.Writer, block *types.Block) {
  71. for index, tx := range block.Transactions() {
  72. entry := LegacyTxLookupEntry{
  73. BlockHash: block.Hash(),
  74. BlockIndex: block.NumberU64(),
  75. Index: uint64(index),
  76. }
  77. data, _ := rlp.EncodeToBytes(entry)
  78. db.Put(txLookupKey(tx.Hash()), data)
  79. }
  80. },
  81. },
  82. }
  83. for _, tc := range tests {
  84. t.Run(tc.name, func(t *testing.T) {
  85. db := NewMemoryDatabase()
  86. tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
  87. tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22})
  88. tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
  89. txs := []*types.Transaction{tx1, tx2, tx3}
  90. block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil, newHasher())
  91. // Check that no transactions entries are in a pristine database
  92. for i, tx := range txs {
  93. if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil {
  94. t.Fatalf("tx #%d [%x]: non existent transaction returned: %v", i, tx.Hash(), txn)
  95. }
  96. }
  97. // Insert all the transactions into the database, and verify contents
  98. WriteCanonicalHash(db, block.Hash(), block.NumberU64())
  99. WriteBlock(db, block)
  100. tc.writeTxLookupEntriesByBlock(db, block)
  101. for i, tx := range txs {
  102. if txn, hash, number, index := ReadTransaction(db, tx.Hash()); txn == nil {
  103. t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash())
  104. } else {
  105. if hash != block.Hash() || number != block.NumberU64() || index != uint64(i) {
  106. t.Fatalf("tx #%d [%x]: positional metadata mismatch: have %x/%d/%d, want %x/%v/%v", i, tx.Hash(), hash, number, index, block.Hash(), block.NumberU64(), i)
  107. }
  108. if tx.Hash() != txn.Hash() {
  109. t.Fatalf("tx #%d [%x]: transaction mismatch: have %v, want %v", i, tx.Hash(), txn, tx)
  110. }
  111. }
  112. }
  113. // Delete the transactions and check purge
  114. for i, tx := range txs {
  115. DeleteTxLookupEntry(db, tx.Hash())
  116. if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil {
  117. t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn)
  118. }
  119. }
  120. })
  121. }
  122. }
  123. func TestDeleteBloomBits(t *testing.T) {
  124. // Prepare testing data
  125. db := NewMemoryDatabase()
  126. for i := uint(0); i < 2; i++ {
  127. for s := uint64(0); s < 2; s++ {
  128. WriteBloomBits(db, i, s, params.MainnetGenesisHash, []byte{0x01, 0x02})
  129. WriteBloomBits(db, i, s, params.RinkebyGenesisHash, []byte{0x01, 0x02})
  130. }
  131. }
  132. check := func(bit uint, section uint64, head common.Hash, exist bool) {
  133. bits, _ := ReadBloomBits(db, bit, section, head)
  134. if exist && !bytes.Equal(bits, []byte{0x01, 0x02}) {
  135. t.Fatalf("Bloombits mismatch")
  136. }
  137. if !exist && len(bits) > 0 {
  138. t.Fatalf("Bloombits should be removed")
  139. }
  140. }
  141. // Check the existence of written data.
  142. check(0, 0, params.MainnetGenesisHash, true)
  143. check(0, 0, params.RinkebyGenesisHash, true)
  144. // Check the existence of deleted data.
  145. DeleteBloombits(db, 0, 0, 1)
  146. check(0, 0, params.MainnetGenesisHash, false)
  147. check(0, 0, params.RinkebyGenesisHash, false)
  148. check(0, 1, params.MainnetGenesisHash, true)
  149. check(0, 1, params.RinkebyGenesisHash, true)
  150. // Check the existence of deleted data.
  151. DeleteBloombits(db, 0, 0, 2)
  152. check(0, 0, params.MainnetGenesisHash, false)
  153. check(0, 0, params.RinkebyGenesisHash, false)
  154. check(0, 1, params.MainnetGenesisHash, false)
  155. check(0, 1, params.RinkebyGenesisHash, false)
  156. // Bit1 shouldn't be affect.
  157. check(1, 0, params.MainnetGenesisHash, true)
  158. check(1, 0, params.RinkebyGenesisHash, true)
  159. check(1, 1, params.MainnetGenesisHash, true)
  160. check(1, 1, params.RinkebyGenesisHash, true)
  161. }