github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/rawdb/accessors_indexes_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package rawdb 19 20 import ( 21 "math/big" 22 "testing" 23 24 "github.com/AigarNetwork/aigar/common" 25 "github.com/AigarNetwork/aigar/core/types" 26 "github.com/AigarNetwork/aigar/ethdb" 27 "github.com/AigarNetwork/aigar/rlp" 28 ) 29 30 // Tests that positional lookup metadata can be stored and retrieved. 31 func TestLookupStorage(t *testing.T) { 32 tests := []struct { 33 name string 34 writeTxLookupEntries func(ethdb.Writer, *types.Block) 35 }{ 36 { 37 "DatabaseV6", 38 func(db ethdb.Writer, block *types.Block) { 39 WriteTxLookupEntries(db, block) 40 }, 41 }, 42 { 43 "DatabaseV4-V5", 44 func(db ethdb.Writer, block *types.Block) { 45 for _, tx := range block.Transactions() { 46 db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes()) 47 } 48 }, 49 }, 50 { 51 "DatabaseV3", 52 func(db ethdb.Writer, block *types.Block) { 53 for index, tx := range block.Transactions() { 54 entry := LegacyTxLookupEntry{ 55 BlockHash: block.Hash(), 56 BlockIndex: block.NumberU64(), 57 Index: uint64(index), 58 } 59 data, _ := rlp.EncodeToBytes(entry) 60 db.Put(txLookupKey(tx.Hash()), data) 61 } 62 }, 63 }, 64 } 65 66 for _, tc := range tests { 67 t.Run(tc.name, func(t *testing.T) { 68 db := NewMemoryDatabase() 69 70 tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11}) 71 tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22}) 72 tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33}) 73 txs := []*types.Transaction{tx1, tx2, tx3} 74 75 block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil) 76 77 // Check that no transactions entries are in a pristine database 78 for i, tx := range txs { 79 if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil { 80 t.Fatalf("tx #%d [%x]: non existent transaction returned: %v", i, tx.Hash(), txn) 81 } 82 } 83 // Insert all the transactions into the database, and verify contents 84 WriteCanonicalHash(db, block.Hash(), block.NumberU64()) 85 WriteBlock(db, block) 86 tc.writeTxLookupEntries(db, block) 87 88 for i, tx := range txs { 89 if txn, hash, number, index := ReadTransaction(db, tx.Hash()); txn == nil { 90 t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash()) 91 } else { 92 if hash != block.Hash() || number != block.NumberU64() || index != uint64(i) { 93 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) 94 } 95 if tx.Hash() != txn.Hash() { 96 t.Fatalf("tx #%d [%x]: transaction mismatch: have %v, want %v", i, tx.Hash(), txn, tx) 97 } 98 } 99 } 100 // Delete the transactions and check purge 101 for i, tx := range txs { 102 DeleteTxLookupEntry(db, tx.Hash()) 103 if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil { 104 t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn) 105 } 106 } 107 }) 108 } 109 }