erigon-pulse/core/rawdb/accessors_indexes.go

66 lines
2.2 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"
libcommon "github.com/ledgerwatch/erigon-lib/common"
2021-07-29 11:53:13 +00:00
"github.com/ledgerwatch/erigon-lib/kv"
2021-07-29 10:23:23 +00:00
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/erigon/core/types"
)
// 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 libcommon.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 kv.Getter, txnHash libcommon.Hash) (*uint64, error) {
data, err := db.GetOne(kv.TxLookup, txnHash.Bytes())
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, nil
}
number := new(big.Int).SetBytes(data).Uint64()
return &number, nil
}
// WriteTxLookupEntries stores a positional metadata for every transaction from
// a block, enabling hash based transaction and receipt lookups.
func WriteTxLookupEntries(db kv.Putter, block *types.Block) {
for _, tx := range block.Transactions() {
data := block.Number().Bytes()
if err := db.Put(kv.TxLookup, 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 kv.Deleter, hash libcommon.Hash) error {
return db.Delete(kv.TxLookup, hash.Bytes())
}