github.com/theQRL/go-zond@v0.2.1/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 "math/big" 22 "testing" 23 24 "github.com/theQRL/go-zond/common" 25 "github.com/theQRL/go-zond/core/types" 26 "github.com/theQRL/go-zond/internal/blocktest" 27 "github.com/theQRL/go-zond/params" 28 "github.com/theQRL/go-zond/zonddb" 29 ) 30 31 var newTestHasher = blocktest.NewHasher 32 33 // Tests that positional lookup metadata can be stored and retrieved. 34 func TestLookupStorage(t *testing.T) { 35 tests := []struct { 36 name string 37 writeTxLookupEntriesByBlock func(zonddb.Writer, *types.Block) 38 }{ 39 { 40 "DatabaseV6", 41 func(db zonddb.Writer, block *types.Block) { 42 WriteTxLookupEntriesByBlock(db, block) 43 }, 44 }, 45 } 46 47 for _, tc := range tests { 48 t.Run(tc.name, func(t *testing.T) { 49 db := NewMemoryDatabase() 50 51 to1 := common.BytesToAddress([]byte{0x11}) 52 tx1 := types.NewTx(&types.DynamicFeeTx{Nonce: 1, To: &to1, Value: big.NewInt(111), Gas: 1111, GasFeeCap: big.NewInt(11111), Data: []byte{0x11, 0x11, 0x11}}) 53 to2 := common.BytesToAddress([]byte{0x22}) 54 tx2 := types.NewTx(&types.DynamicFeeTx{Nonce: 2, To: &to2, Value: big.NewInt(222), Gas: 2222, GasFeeCap: big.NewInt(22222), Data: []byte{0x22, 0x22, 0x22}}) 55 to3 := common.BytesToAddress([]byte{0x33}) 56 tx3 := types.NewTx(&types.DynamicFeeTx{Nonce: 3, To: &to3, Value: big.NewInt(333), Gas: 3333, GasFeeCap: big.NewInt(33333), Data: []byte{0x33, 0x33, 0x33}}) 57 txs := []*types.Transaction{tx1, tx2, tx3} 58 59 block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, &types.Body{Transactions: txs}, nil, newTestHasher()) 60 61 // Check that no transactions entries are in a pristine database 62 for i, tx := range txs { 63 if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil { 64 t.Fatalf("tx #%d [%x]: non existent transaction returned: %v", i, tx.Hash(), txn) 65 } 66 } 67 // Insert all the transactions into the database, and verify contents 68 WriteCanonicalHash(db, block.Hash(), block.NumberU64()) 69 WriteBlock(db, block) 70 tc.writeTxLookupEntriesByBlock(db, block) 71 72 for i, tx := range txs { 73 if txn, hash, number, index := ReadTransaction(db, tx.Hash()); txn == nil { 74 t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash()) 75 } else { 76 if hash != block.Hash() || number != block.NumberU64() || index != uint64(i) { 77 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) 78 } 79 if tx.Hash() != txn.Hash() { 80 t.Fatalf("tx #%d [%x]: transaction mismatch: have %v, want %v", i, tx.Hash(), txn, tx) 81 } 82 } 83 } 84 // Delete the transactions and check purge 85 for i, tx := range txs { 86 DeleteTxLookupEntry(db, tx.Hash()) 87 if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil { 88 t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn) 89 } 90 } 91 }) 92 } 93 } 94 95 func TestDeleteBloomBits(t *testing.T) { 96 // Prepare testing data 97 db := NewMemoryDatabase() 98 for i := uint(0); i < 2; i++ { 99 for s := uint64(0); s < 2; s++ { 100 WriteBloomBits(db, i, s, params.MainnetGenesisHash, []byte{0x01, 0x02}) 101 } 102 } 103 check := func(bit uint, section uint64, head common.Hash, exist bool) { 104 bits, _ := ReadBloomBits(db, bit, section, head) 105 if exist && !bytes.Equal(bits, []byte{0x01, 0x02}) { 106 t.Fatalf("Bloombits mismatch") 107 } 108 if !exist && len(bits) > 0 { 109 t.Fatalf("Bloombits should be removed") 110 } 111 } 112 // Check the existence of written data. 113 check(0, 0, params.MainnetGenesisHash, true) 114 115 // Check the existence of deleted data. 116 DeleteBloombits(db, 0, 0, 1) 117 check(0, 0, params.MainnetGenesisHash, false) 118 check(0, 1, params.MainnetGenesisHash, true) 119 120 // Check the existence of deleted data. 121 DeleteBloombits(db, 0, 0, 2) 122 check(0, 0, params.MainnetGenesisHash, false) 123 check(0, 1, params.MainnetGenesisHash, false) 124 125 // Bit1 shouldn't be affect. 126 check(1, 0, params.MainnetGenesisHash, true) 127 check(1, 1, params.MainnetGenesisHash, true) 128 }