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