github.com/theQRL/go-zond@v0.1.1/core/rawdb/accessors_indexes.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 23 "github.com/theQRL/go-zond/common" 24 "github.com/theQRL/go-zond/core/types" 25 "github.com/theQRL/go-zond/zonddb" 26 "github.com/theQRL/go-zond/log" 27 "github.com/theQRL/go-zond/params" 28 "github.com/theQRL/go-zond/rlp" 29 ) 30 31 // ReadTxLookupEntry retrieves the positional metadata associated with a transaction 32 // hash to allow retrieving the transaction or receipt by hash. 33 func ReadTxLookupEntry(db zonddb.Reader, hash common.Hash) *uint64 { 34 data, _ := db.Get(txLookupKey(hash)) 35 if len(data) == 0 { 36 return nil 37 } 38 // Database v6 tx lookup just stores the block number 39 if len(data) < common.HashLength { 40 number := new(big.Int).SetBytes(data).Uint64() 41 return &number 42 } 43 // Database v4-v5 tx lookup format just stores the hash 44 if len(data) == common.HashLength { 45 return ReadHeaderNumber(db, common.BytesToHash(data)) 46 } 47 // Finally try database v3 tx lookup format 48 var entry LegacyTxLookupEntry 49 if err := rlp.DecodeBytes(data, &entry); err != nil { 50 log.Error("Invalid transaction lookup entry RLP", "hash", hash, "blob", data, "err", err) 51 return nil 52 } 53 return &entry.BlockIndex 54 } 55 56 // writeTxLookupEntry stores a positional metadata for a transaction, 57 // enabling hash based transaction and receipt lookups. 58 func writeTxLookupEntry(db zonddb.KeyValueWriter, hash common.Hash, numberBytes []byte) { 59 if err := db.Put(txLookupKey(hash), numberBytes); err != nil { 60 log.Crit("Failed to store transaction lookup entry", "err", err) 61 } 62 } 63 64 // WriteTxLookupEntries is identical to WriteTxLookupEntry, but it works on 65 // a list of hashes 66 func WriteTxLookupEntries(db zonddb.KeyValueWriter, number uint64, hashes []common.Hash) { 67 numberBytes := new(big.Int).SetUint64(number).Bytes() 68 for _, hash := range hashes { 69 writeTxLookupEntry(db, hash, numberBytes) 70 } 71 } 72 73 // WriteTxLookupEntriesByBlock stores a positional metadata for every transaction from 74 // a block, enabling hash based transaction and receipt lookups. 75 func WriteTxLookupEntriesByBlock(db zonddb.KeyValueWriter, block *types.Block) { 76 numberBytes := block.Number().Bytes() 77 for _, tx := range block.Transactions() { 78 writeTxLookupEntry(db, tx.Hash(), numberBytes) 79 } 80 } 81 82 // DeleteTxLookupEntry removes all transaction data associated with a hash. 83 func DeleteTxLookupEntry(db zonddb.KeyValueWriter, hash common.Hash) { 84 if err := db.Delete(txLookupKey(hash)); err != nil { 85 log.Crit("Failed to delete transaction lookup entry", "err", err) 86 } 87 } 88 89 // DeleteTxLookupEntries removes all transaction lookups for a given block. 90 func DeleteTxLookupEntries(db zonddb.KeyValueWriter, hashes []common.Hash) { 91 for _, hash := range hashes { 92 DeleteTxLookupEntry(db, hash) 93 } 94 } 95 96 // ReadTransaction retrieves a specific transaction from the database, along with 97 // its added positional metadata. 98 func ReadTransaction(db zonddb.Reader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) { 99 blockNumber := ReadTxLookupEntry(db, hash) 100 if blockNumber == nil { 101 return nil, common.Hash{}, 0, 0 102 } 103 blockHash := ReadCanonicalHash(db, *blockNumber) 104 if blockHash == (common.Hash{}) { 105 return nil, common.Hash{}, 0, 0 106 } 107 body := ReadBody(db, blockHash, *blockNumber) 108 if body == nil { 109 log.Error("Transaction referenced missing", "number", *blockNumber, "hash", blockHash) 110 return nil, common.Hash{}, 0, 0 111 } 112 for txIndex, tx := range body.Transactions { 113 if tx.Hash() == hash { 114 return tx, blockHash, *blockNumber, uint64(txIndex) 115 } 116 } 117 log.Error("Transaction not found", "number", *blockNumber, "hash", blockHash, "txhash", hash) 118 return nil, common.Hash{}, 0, 0 119 } 120 121 // ReadReceipt retrieves a specific transaction receipt from the database, along with 122 // its added positional metadata. 123 func ReadReceipt(db zonddb.Reader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) { 124 // Retrieve the context of the receipt based on the transaction hash 125 blockNumber := ReadTxLookupEntry(db, hash) 126 if blockNumber == nil { 127 return nil, common.Hash{}, 0, 0 128 } 129 blockHash := ReadCanonicalHash(db, *blockNumber) 130 if blockHash == (common.Hash{}) { 131 return nil, common.Hash{}, 0, 0 132 } 133 blockHeader := ReadHeader(db, blockHash, *blockNumber) 134 if blockHeader == nil { 135 return nil, common.Hash{}, 0, 0 136 } 137 // Read all the receipts from the block and return the one with the matching hash 138 receipts := ReadReceipts(db, blockHash, *blockNumber, blockHeader.Time, config) 139 for receiptIndex, receipt := range receipts { 140 if receipt.TxHash == hash { 141 return receipt, blockHash, *blockNumber, uint64(receiptIndex) 142 } 143 } 144 log.Error("Receipt not found", "number", *blockNumber, "hash", blockHash, "txhash", hash) 145 return nil, common.Hash{}, 0, 0 146 } 147 148 // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given 149 // section and bit index from the. 150 func ReadBloomBits(db zonddb.KeyValueReader, bit uint, section uint64, head common.Hash) ([]byte, error) { 151 return db.Get(bloomBitsKey(bit, section, head)) 152 } 153 154 // WriteBloomBits stores the compressed bloom bits vector belonging to the given 155 // section and bit index. 156 func WriteBloomBits(db zonddb.KeyValueWriter, bit uint, section uint64, head common.Hash, bits []byte) { 157 if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil { 158 log.Crit("Failed to store bloom bits", "err", err) 159 } 160 } 161 162 // DeleteBloombits removes all compressed bloom bits vector belonging to the 163 // given section range and bit index. 164 func DeleteBloombits(db zonddb.Database, bit uint, from uint64, to uint64) { 165 start, end := bloomBitsKey(bit, from, common.Hash{}), bloomBitsKey(bit, to, common.Hash{}) 166 it := db.NewIterator(nil, start) 167 defer it.Release() 168 169 for it.Next() { 170 if bytes.Compare(it.Key(), end) >= 0 { 171 break 172 } 173 if len(it.Key()) != len(bloomBitsPrefix)+2+8+32 { 174 continue 175 } 176 db.Delete(it.Key()) 177 } 178 if it.Error() != nil { 179 log.Crit("Failed to delete bloom bits", "err", it.Error()) 180 } 181 }