github.com/xfond/eth-implementation@v1.8.9-0.20180514135602-f6bc65fc6811/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 "encoding/binary" 21 22 "github.com/ethereum/go-ethereum/common" 23 "github.com/ethereum/go-ethereum/core/types" 24 "github.com/ethereum/go-ethereum/log" 25 "github.com/ethereum/go-ethereum/rlp" 26 ) 27 28 // ReadTxLookupEntry retrieves the positional metadata associated with a transaction 29 // hash to allow retrieving the transaction or receipt by hash. 30 func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, uint64) { 31 data, _ := db.Get(append(txLookupPrefix, hash.Bytes()...)) 32 if len(data) == 0 { 33 return common.Hash{}, 0, 0 34 } 35 var entry TxLookupEntry 36 if err := rlp.DecodeBytes(data, &entry); err != nil { 37 log.Error("Invalid transaction lookup entry RLP", "hash", hash, "err", err) 38 return common.Hash{}, 0, 0 39 } 40 return entry.BlockHash, entry.BlockIndex, entry.Index 41 } 42 43 // WriteTxLookupEntries stores a positional metadata for every transaction from 44 // a block, enabling hash based transaction and receipt lookups. 45 func WriteTxLookupEntries(db DatabaseWriter, block *types.Block) { 46 for i, tx := range block.Transactions() { 47 entry := TxLookupEntry{ 48 BlockHash: block.Hash(), 49 BlockIndex: block.NumberU64(), 50 Index: uint64(i), 51 } 52 data, err := rlp.EncodeToBytes(entry) 53 if err != nil { 54 log.Crit("Failed to encode transaction lookup entry", "err", err) 55 } 56 if err := db.Put(append(txLookupPrefix, tx.Hash().Bytes()...), data); err != nil { 57 log.Crit("Failed to store transaction lookup entry", "err", err) 58 } 59 } 60 } 61 62 // DeleteTxLookupEntry removes all transaction data associated with a hash. 63 func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) { 64 db.Delete(append(txLookupPrefix, hash.Bytes()...)) 65 } 66 67 // ReadTransaction retrieves a specific transaction from the database, along with 68 // its added positional metadata. 69 func ReadTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) { 70 blockHash, blockNumber, txIndex := ReadTxLookupEntry(db, hash) 71 if blockHash == (common.Hash{}) { 72 return nil, common.Hash{}, 0, 0 73 } 74 body := ReadBody(db, blockHash, blockNumber) 75 if body == nil || len(body.Transactions) <= int(txIndex) { 76 log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash, "index", txIndex) 77 return nil, common.Hash{}, 0, 0 78 } 79 return body.Transactions[txIndex], blockHash, blockNumber, txIndex 80 } 81 82 // ReadReceipt retrieves a specific transaction receipt from the database, along with 83 // its added positional metadata. 84 func ReadReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) { 85 blockHash, blockNumber, receiptIndex := ReadTxLookupEntry(db, hash) 86 if blockHash == (common.Hash{}) { 87 return nil, common.Hash{}, 0, 0 88 } 89 receipts := ReadReceipts(db, blockHash, blockNumber) 90 if len(receipts) <= int(receiptIndex) { 91 log.Error("Receipt refereced missing", "number", blockNumber, "hash", blockHash, "index", receiptIndex) 92 return nil, common.Hash{}, 0, 0 93 } 94 return receipts[receiptIndex], blockHash, blockNumber, receiptIndex 95 } 96 97 // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given 98 // section and bit index from the. 99 func ReadBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash) ([]byte, error) { 100 key := append(append(bloomBitsPrefix, make([]byte, 10)...), head.Bytes()...) 101 102 binary.BigEndian.PutUint16(key[1:], uint16(bit)) 103 binary.BigEndian.PutUint64(key[3:], section) 104 105 return db.Get(key) 106 } 107 108 // WriteBloomBits stores the compressed bloom bits vector belonging to the given 109 // section and bit index. 110 func WriteBloomBits(db DatabaseWriter, bit uint, section uint64, head common.Hash, bits []byte) { 111 key := append(append(bloomBitsPrefix, make([]byte, 10)...), head.Bytes()...) 112 113 binary.BigEndian.PutUint16(key[1:], uint16(bit)) 114 binary.BigEndian.PutUint64(key[3:], section) 115 116 if err := db.Put(key, bits); err != nil { 117 log.Crit("Failed to store bloom bits", "err", err) 118 } 119 }