github.com/ethereum-optimism/optimism/l2geth@v0.0.0-20230612200230-50b04ade19e3/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 "math/big" 21 "testing" 22 23 "github.com/ethereum-optimism/optimism/l2geth/common" 24 "github.com/ethereum-optimism/optimism/l2geth/core/types" 25 "github.com/ethereum-optimism/optimism/l2geth/ethdb" 26 "github.com/ethereum-optimism/optimism/l2geth/rlp" 27 ) 28 29 // Tests that positional lookup metadata can be stored and retrieved. 30 func TestLookupStorage(t *testing.T) { 31 tests := []struct { 32 name string 33 writeTxLookupEntries func(ethdb.Writer, *types.Block) 34 }{ 35 { 36 "DatabaseV6", 37 func(db ethdb.Writer, block *types.Block) { 38 WriteTxLookupEntries(db, block) 39 }, 40 }, 41 { 42 "DatabaseV4-V5", 43 func(db ethdb.Writer, block *types.Block) { 44 for _, tx := range block.Transactions() { 45 db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes()) 46 } 47 }, 48 }, 49 { 50 "DatabaseV3", 51 func(db ethdb.Writer, block *types.Block) { 52 for index, tx := range block.Transactions() { 53 entry := LegacyTxLookupEntry{ 54 BlockHash: block.Hash(), 55 BlockIndex: block.NumberU64(), 56 Index: uint64(index), 57 } 58 data, _ := rlp.EncodeToBytes(entry) 59 db.Put(txLookupKey(tx.Hash()), data) 60 } 61 }, 62 }, 63 } 64 65 for _, tc := range tests { 66 t.Run(tc.name, func(t *testing.T) { 67 db := NewMemoryDatabase() 68 69 sender1 := common.BytesToAddress([]byte{0x44}) 70 sender2 := common.BytesToAddress([]byte{0x55}) 71 72 l1BlockNumber1 := big.NewInt(1) 73 l1BlockNumber2 := big.NewInt(2) 74 75 tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11}) 76 tx1Meta := types.NewTransactionMeta(l1BlockNumber1, 0, &sender1, types.QueueOriginSequencer, nil, nil, nil) 77 tx1.SetTransactionMeta(tx1Meta) 78 79 tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22}) 80 tx2Meta := types.NewTransactionMeta(l1BlockNumber2, 0, &sender2, types.QueueOriginSequencer, nil, nil, nil) 81 tx2.SetTransactionMeta(tx2Meta) 82 83 tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33}) 84 tx3Meta := types.NewTransactionMeta(l1BlockNumber1, 0, nil, types.QueueOriginSequencer, nil, nil, nil) 85 tx3.SetTransactionMeta(tx3Meta) 86 87 txs := []*types.Transaction{tx1, tx2, tx3} 88 89 block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil) 90 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.writeTxLookupEntries(db, block) 101 102 for i, tx := range txs { 103 if txn, hash, number, index := ReadTransaction(db, tx.Hash()); txn == nil { 104 t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash()) 105 } else { 106 if hash != block.Hash() || number != block.NumberU64() || index != uint64(i) { 107 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) 108 } 109 if tx.Hash() != txn.Hash() { 110 t.Fatalf("tx #%d [%x]: transaction mismatch: have %v, want %v", i, tx.Hash(), txn, tx) 111 } 112 } 113 } 114 // Delete the transactions and check purge 115 for i, tx := range txs { 116 DeleteTxLookupEntry(db, tx.Hash()) 117 if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil { 118 t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn) 119 } 120 } 121 }) 122 } 123 }