|
|
|
@ -25,33 +25,28 @@ import ( |
|
|
|
|
|
|
|
|
|
// ReadTxLookupEntry retrieves the positional metadata associated with a transaction
|
|
|
|
|
// hash to allow retrieving the transaction or receipt by hash.
|
|
|
|
|
func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, uint64) { |
|
|
|
|
func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) common.Hash { |
|
|
|
|
data, _ := db.Get(txLookupKey(hash)) |
|
|
|
|
if len(data) == 0 { |
|
|
|
|
return common.Hash{}, 0, 0 |
|
|
|
|
return common.Hash{} |
|
|
|
|
} |
|
|
|
|
var entry TxLookupEntry |
|
|
|
|
if len(data) == common.HashLength { |
|
|
|
|
return common.BytesToHash(data) |
|
|
|
|
} |
|
|
|
|
// Probably it's legacy txlookup entry data, try to decode it.
|
|
|
|
|
var entry LegacyTxLookupEntry |
|
|
|
|
if err := rlp.DecodeBytes(data, &entry); err != nil { |
|
|
|
|
log.Error("Invalid transaction lookup entry RLP", "hash", hash, "err", err) |
|
|
|
|
return common.Hash{}, 0, 0 |
|
|
|
|
log.Error("Invalid transaction lookup entry RLP", "hash", hash, "blob", data, "err", err) |
|
|
|
|
return common.Hash{} |
|
|
|
|
} |
|
|
|
|
return entry.BlockHash, entry.BlockIndex, entry.Index |
|
|
|
|
return entry.BlockHash |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// WriteTxLookupEntries stores a positional metadata for every transaction from
|
|
|
|
|
// a block, enabling hash based transaction and receipt lookups.
|
|
|
|
|
func WriteTxLookupEntries(db DatabaseWriter, block *types.Block) { |
|
|
|
|
for i, tx := range block.Transactions() { |
|
|
|
|
entry := TxLookupEntry{ |
|
|
|
|
BlockHash: block.Hash(), |
|
|
|
|
BlockIndex: block.NumberU64(), |
|
|
|
|
Index: uint64(i), |
|
|
|
|
} |
|
|
|
|
data, err := rlp.EncodeToBytes(entry) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Crit("Failed to encode transaction lookup entry", "err", err) |
|
|
|
|
} |
|
|
|
|
if err := db.Put(txLookupKey(tx.Hash()), data); err != nil { |
|
|
|
|
for _, tx := range block.Transactions() { |
|
|
|
|
if err := db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes()); err != nil { |
|
|
|
|
log.Crit("Failed to store transaction lookup entry", "err", err) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -65,31 +60,47 @@ func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) { |
|
|
|
|
// ReadTransaction retrieves a specific transaction from the database, along with
|
|
|
|
|
// its added positional metadata.
|
|
|
|
|
func ReadTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) { |
|
|
|
|
blockHash, blockNumber, txIndex := ReadTxLookupEntry(db, hash) |
|
|
|
|
blockHash := ReadTxLookupEntry(db, hash) |
|
|
|
|
if blockHash == (common.Hash{}) { |
|
|
|
|
return nil, common.Hash{}, 0, 0 |
|
|
|
|
} |
|
|
|
|
body := ReadBody(db, blockHash, blockNumber) |
|
|
|
|
if body == nil || len(body.Transactions) <= int(txIndex) { |
|
|
|
|
log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash, "index", txIndex) |
|
|
|
|
blockNumber := ReadHeaderNumber(db, blockHash) |
|
|
|
|
if blockNumber == nil { |
|
|
|
|
return nil, common.Hash{}, 0, 0 |
|
|
|
|
} |
|
|
|
|
body := ReadBody(db, blockHash, *blockNumber) |
|
|
|
|
if body == nil { |
|
|
|
|
log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash) |
|
|
|
|
return nil, common.Hash{}, 0, 0 |
|
|
|
|
} |
|
|
|
|
return body.Transactions[txIndex], blockHash, blockNumber, txIndex |
|
|
|
|
for txIndex, tx := range body.Transactions { |
|
|
|
|
if tx.Hash() == hash { |
|
|
|
|
return tx, blockHash, *blockNumber, uint64(txIndex) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash) |
|
|
|
|
return nil, common.Hash{}, 0, 0 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ReadReceipt retrieves a specific transaction receipt from the database, along with
|
|
|
|
|
// its added positional metadata.
|
|
|
|
|
func ReadReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) { |
|
|
|
|
blockHash, blockNumber, receiptIndex := ReadTxLookupEntry(db, hash) |
|
|
|
|
blockHash := ReadTxLookupEntry(db, hash) |
|
|
|
|
if blockHash == (common.Hash{}) { |
|
|
|
|
return nil, common.Hash{}, 0, 0 |
|
|
|
|
} |
|
|
|
|
receipts := ReadReceipts(db, blockHash, blockNumber) |
|
|
|
|
if len(receipts) <= int(receiptIndex) { |
|
|
|
|
log.Error("Receipt refereced missing", "number", blockNumber, "hash", blockHash, "index", receiptIndex) |
|
|
|
|
blockNumber := ReadHeaderNumber(db, blockHash) |
|
|
|
|
if blockNumber == nil { |
|
|
|
|
return nil, common.Hash{}, 0, 0 |
|
|
|
|
} |
|
|
|
|
return receipts[receiptIndex], blockHash, blockNumber, receiptIndex |
|
|
|
|
receipts := ReadReceipts(db, blockHash, *blockNumber) |
|
|
|
|
for receiptIndex, receipt := range receipts { |
|
|
|
|
if receipt.TxHash == hash { |
|
|
|
|
return receipt, blockHash, *blockNumber, uint64(receiptIndex) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", hash) |
|
|
|
|
return nil, common.Hash{}, 0, 0 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
|
|
|
|
|