2018-05-07 11:35:06 +00:00
|
|
|
// Copyright 2018 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package rawdb
|
|
|
|
|
|
|
|
import (
|
2019-04-25 14:24:56 +00:00
|
|
|
"math/big"
|
|
|
|
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
2021-07-29 10:23:23 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2018-05-07 11:35:06 +00:00
|
|
|
)
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
// TxLookupEntry is a positional metadata to help looking up the data content of
|
|
|
|
// a transaction or receipt given only its hash.
|
|
|
|
type TxLookupEntry struct {
|
|
|
|
BlockHash common.Hash
|
|
|
|
BlockIndex uint64
|
|
|
|
Index uint64
|
|
|
|
}
|
|
|
|
|
2018-05-07 11:35:06 +00:00
|
|
|
// ReadTxLookupEntry retrieves the positional metadata associated with a transaction
|
|
|
|
// hash to allow retrieving the transaction or receipt by hash.
|
2022-01-06 11:22:59 +00:00
|
|
|
func ReadTxLookupEntry(db kv.Getter, txnHash common.Hash) (*uint64, error) {
|
2021-07-28 02:47:38 +00:00
|
|
|
data, err := db.GetOne(kv.TxLookup, txnHash.Bytes())
|
2021-07-11 05:25:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-01 07:42:23 +00:00
|
|
|
if len(data) == 0 {
|
2021-07-11 05:25:21 +00:00
|
|
|
return nil, nil
|
2021-05-01 07:42:23 +00:00
|
|
|
}
|
|
|
|
number := new(big.Int).SetBytes(data).Uint64()
|
2021-07-11 05:25:21 +00:00
|
|
|
return &number, nil
|
2021-05-01 07:42:23 +00:00
|
|
|
}
|
2018-05-07 11:35:06 +00:00
|
|
|
|
|
|
|
// WriteTxLookupEntries stores a positional metadata for every transaction from
|
|
|
|
// a block, enabling hash based transaction and receipt lookups.
|
2021-07-28 02:47:38 +00:00
|
|
|
func WriteTxLookupEntries(db kv.Putter, block *types.Block) {
|
2019-02-21 13:14:35 +00:00
|
|
|
for _, tx := range block.Transactions() {
|
2019-05-27 13:51:49 +00:00
|
|
|
data := block.Number().Bytes()
|
2021-07-28 02:47:38 +00:00
|
|
|
if err := db.Put(kv.TxLookup, tx.Hash().Bytes(), data); err != nil {
|
2018-05-07 11:35:06 +00:00
|
|
|
log.Crit("Failed to store transaction lookup entry", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTxLookupEntry removes all transaction data associated with a hash.
|
2021-07-28 02:47:38 +00:00
|
|
|
func DeleteTxLookupEntry(db kv.Deleter, hash common.Hash) error {
|
|
|
|
return db.Delete(kv.TxLookup, hash.Bytes(), nil)
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 11:22:59 +00:00
|
|
|
// ReadTransactionByHash retrieves a specific transaction from the database, along with
|
2018-05-07 11:35:06 +00:00
|
|
|
// its added positional metadata.
|
2022-01-06 11:22:59 +00:00
|
|
|
func ReadTransactionByHash(db kv.Tx, hash common.Hash) (types.Transaction, common.Hash, uint64, uint64, error) {
|
2021-07-11 05:25:21 +00:00
|
|
|
blockNumber, err := ReadTxLookupEntry(db, hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, common.Hash{}, 0, 0, err
|
|
|
|
}
|
2019-04-25 14:24:56 +00:00
|
|
|
if blockNumber == nil {
|
2021-07-11 05:25:21 +00:00
|
|
|
return nil, common.Hash{}, 0, 0, nil
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
2020-10-10 06:05:56 +00:00
|
|
|
blockHash, err := ReadCanonicalHash(db, *blockNumber)
|
|
|
|
if err != nil {
|
2021-07-11 05:25:21 +00:00
|
|
|
return nil, common.Hash{}, 0, 0, err
|
2020-10-10 06:05:56 +00:00
|
|
|
}
|
2019-04-25 14:24:56 +00:00
|
|
|
if blockHash == (common.Hash{}) {
|
2021-07-11 05:25:21 +00:00
|
|
|
return nil, common.Hash{}, 0, 0, nil
|
2019-02-21 13:14:35 +00:00
|
|
|
}
|
2022-01-07 13:52:38 +00:00
|
|
|
body := ReadCanonicalBodyWithTransactions(db, blockHash, *blockNumber)
|
2019-02-21 13:14:35 +00:00
|
|
|
if body == nil {
|
|
|
|
log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash)
|
2021-07-11 05:25:21 +00:00
|
|
|
return nil, common.Hash{}, 0, 0, nil
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
2021-05-03 19:49:55 +00:00
|
|
|
senders, err1 := ReadSenders(db, blockHash, *blockNumber)
|
|
|
|
if err1 != nil {
|
2021-07-11 05:25:21 +00:00
|
|
|
return nil, common.Hash{}, 0, 0, err1
|
2021-05-03 19:49:55 +00:00
|
|
|
}
|
|
|
|
body.SendersToTxs(senders)
|
2019-02-21 13:14:35 +00:00
|
|
|
for txIndex, tx := range body.Transactions {
|
|
|
|
if tx.Hash() == hash {
|
2021-07-11 05:25:21 +00:00
|
|
|
return tx, blockHash, *blockNumber, uint64(txIndex), nil
|
2019-02-21 13:14:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
|
2021-07-11 05:25:21 +00:00
|
|
|
return nil, common.Hash{}, 0, 0, nil
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 11:22:59 +00:00
|
|
|
// ReadTransaction retrieves a specific transaction from the database, along with
|
|
|
|
// its added positional metadata.
|
|
|
|
func ReadTransaction(db kv.Tx, hash common.Hash, blockNumber uint64) (types.Transaction, common.Hash, uint64, uint64, error) {
|
|
|
|
blockHash, err := ReadCanonicalHash(db, blockNumber)
|
|
|
|
if err != nil {
|
|
|
|
return nil, common.Hash{}, 0, 0, err
|
|
|
|
}
|
|
|
|
if blockHash == (common.Hash{}) {
|
|
|
|
return nil, common.Hash{}, 0, 0, nil
|
|
|
|
}
|
2022-01-07 13:52:38 +00:00
|
|
|
body := ReadCanonicalBodyWithTransactions(db, blockHash, blockNumber)
|
2022-01-06 11:22:59 +00:00
|
|
|
if body == nil {
|
|
|
|
log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash)
|
|
|
|
return nil, common.Hash{}, 0, 0, nil
|
|
|
|
}
|
|
|
|
senders, err1 := ReadSenders(db, blockHash, blockNumber)
|
|
|
|
if err1 != nil {
|
|
|
|
return nil, common.Hash{}, 0, 0, err1
|
|
|
|
}
|
|
|
|
body.SendersToTxs(senders)
|
|
|
|
for txIndex, tx := range body.Transactions {
|
|
|
|
if tx.Hash() == hash {
|
|
|
|
return tx, blockHash, blockNumber, uint64(txIndex), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
|
|
|
|
return nil, common.Hash{}, 0, 0, nil
|
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
func ReadReceipt(db kv.Tx, txHash common.Hash) (*types.Receipt, common.Hash, uint64, uint64, error) {
|
2021-06-05 15:17:04 +00:00
|
|
|
// Retrieve the context of the receipt based on the transaction hash
|
2021-07-11 05:25:21 +00:00
|
|
|
blockNumber, err := ReadTxLookupEntry(db, txHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, common.Hash{}, 0, 0, err
|
|
|
|
}
|
2021-05-01 07:42:23 +00:00
|
|
|
if blockNumber == nil {
|
2021-07-11 05:25:21 +00:00
|
|
|
return nil, common.Hash{}, 0, 0, nil
|
2021-05-01 07:42:23 +00:00
|
|
|
}
|
|
|
|
blockHash, err := ReadCanonicalHash(db, *blockNumber)
|
|
|
|
if err != nil {
|
2021-07-11 05:25:21 +00:00
|
|
|
return nil, common.Hash{}, 0, 0, err
|
2021-05-01 07:42:23 +00:00
|
|
|
}
|
|
|
|
if blockHash == (common.Hash{}) {
|
2021-07-11 05:25:21 +00:00
|
|
|
return nil, common.Hash{}, 0, 0, nil
|
2021-05-01 07:42:23 +00:00
|
|
|
}
|
2021-06-05 15:17:04 +00:00
|
|
|
b, senders, err := ReadBlockWithSenders(db, blockHash, *blockNumber)
|
2020-10-10 06:05:56 +00:00
|
|
|
if err != nil {
|
2021-07-11 05:25:21 +00:00
|
|
|
return nil, common.Hash{}, 0, 0, err
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
2019-04-15 09:36:27 +00:00
|
|
|
// Read all the receipts from the block and return the one with the matching hash
|
2021-06-05 15:17:04 +00:00
|
|
|
receipts := ReadReceipts(db, b, senders)
|
2019-02-21 13:14:35 +00:00
|
|
|
for receiptIndex, receipt := range receipts {
|
2021-06-04 12:28:18 +00:00
|
|
|
if receipt.TxHash == txHash {
|
2021-07-11 05:25:21 +00:00
|
|
|
return receipt, blockHash, *blockNumber, uint64(receiptIndex), nil
|
2019-02-21 13:14:35 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-04 12:28:18 +00:00
|
|
|
log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", txHash)
|
2021-07-11 05:25:21 +00:00
|
|
|
return nil, common.Hash{}, 0, 0, nil
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|