erigon-pulse/core/rawdb/accessors_indexes.go

133 lines
4.9 KiB
Go
Raw Normal View History

// 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 (
"math/big"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/log"
)
// 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
}
// ReadTxLookupEntry retrieves the positional metadata associated with a transaction
// hash to allow retrieving the transaction or receipt by hash.
func ReadTxLookupEntry(db ethdb.DatabaseReader, hash common.Hash) *uint64 {
data, _ := db.Get(dbutils.TxLookupPrefix, hash.Bytes())
if len(data) == 0 {
return nil
}
number := new(big.Int).SetBytes(data).Uint64()
return &number
}
// WriteTxLookupEntries stores a positional metadata for every transaction from
// a block, enabling hash based transaction and receipt lookups.
func WriteTxLookupEntries(db ethdb.Putter, block *types.Block) {
for _, tx := range block.Transactions() {
data := block.Number().Bytes()
if err := db.Put(dbutils.TxLookupPrefix, tx.Hash().Bytes(), data); err != nil {
log.Crit("Failed to store transaction lookup entry", "err", err)
}
}
}
// DeleteTxLookupEntry removes all transaction data associated with a hash.
func DeleteTxLookupEntry(db ethdb.Deleter, hash common.Hash) error {
return db.Delete(dbutils.TxLookupPrefix, hash.Bytes(), nil)
}
// ReadTransaction retrieves a specific transaction from the database, along with
// its added positional metadata.
Aleut support (Eip1559) (#1704) * Where I am at * Refactoring of transaction types * More refactoring * Use Homested signer in rpc daemon * Unified signer * Continue unified signer * A bit more * Fixes and down the rabbit hole... * More tx pool fixes * More refactoring fixes * More fixes' * more fixes * More fixes * More compile fixes * More RLP hand-writing * Finish RLP encoding/decoding of transactions * Fixes to header encoding, start on protocol packets * Transaction decoding * Use DecodeTransaction function * Decoding BlockBodyPacket * Encode and decode for pool txs * Start fixing tests * Introduce SigningHash * Fixes to SignHash * RLP encoding fixes * Fixes for encoding/decoding * More test fixes * Fix more tests * More test fixes * More test fixes * Fix core tests * More fixes for signer * Fix for tx * Fixes to string encoding/size * Fix eip2930 test * Fix rest of ./tests * More fixes * Fix compilation * More test fixes * More test fixes * Test fixes * More fixes * Reuse EncodingSize in EncodeRLP for accessList * Rearrange things in dynamic fee tx * Add MarshalBinary * More fixes * Make V,R,S non-pointers * More NPE fixes * More fixes * Receipt fixes * Fix core/types * Fix ./eth * More compile fixes for tests * More test fixes * More test fixes * Try to see lint errors better * Try to see lint errors better * Fix lint * Debugging eip1559 test * Fix TestEIP1559Transition test * Fix NewBlockPacket encoding/decoding * Fix calculation of TxHash * Fix perf problem with senders * Update aleut config values * Try adding static peers * Add staticpeers to defaul flags * Change aleut networkID * Fix test Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local> Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
2021-04-22 17:11:37 +00:00
func ReadTransaction(db ethdb.Getter, hash common.Hash) (types.Transaction, common.Hash, uint64, uint64) {
blockNumber := ReadTxLookupEntry(db, hash)
if blockNumber == nil {
return nil, common.Hash{}, 0, 0
}
blockHash, err := ReadCanonicalHash(db, *blockNumber)
if err != nil {
log.Error("ReadCanonicalHash failed", "err", err)
return nil, common.Hash{}, 0, 0
}
if blockHash == (common.Hash{}) {
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
}
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.
2021-03-30 09:53:54 +00:00
func ReadReceipt(db ethdb.Getter, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) {
// Retrieve the context of the receipt based on the transaction hash
blockNumber := ReadTxLookupEntry(db, hash)
if blockNumber == nil {
return nil, common.Hash{}, 0, 0
}
blockHash, err := ReadCanonicalHash(db, *blockNumber)
if err != nil {
log.Error("ReadCanonicalHash failed", "err", err)
return nil, common.Hash{}, 0, 0
}
if blockHash == (common.Hash{}) {
return nil, common.Hash{}, 0, 0
}
// Read all the receipts from the block and return the one with the matching hash
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
// section and bit index from the.
func ReadBloomBits(db ethdb.DatabaseReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
return db.Get(dbutils.BloomBitsPrefix, dbutils.BloomBitsKey(bit, section, head))
}
// WriteBloomBits stores the compressed bloom bits vector belonging to the given
// section and bit index.
func WriteBloomBits(db ethdb.Putter, bit uint, section uint64, head common.Hash, bits []byte) {
if err := db.Put(dbutils.BloomBitsPrefix, dbutils.BloomBitsKey(bit, section, head), bits); err != nil {
log.Crit("Failed to store bloom bits", "err", err)
}
}