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