mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-27 08:56:18 +00:00
177 lines
6.4 KiB
Go
177 lines
6.4 KiB
Go
// 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 (
|
|
"github.com/XinFinOrg/XDPoSChain/common"
|
|
"github.com/XinFinOrg/XDPoSChain/core/types"
|
|
"github.com/XinFinOrg/XDPoSChain/ethdb"
|
|
"github.com/XinFinOrg/XDPoSChain/log"
|
|
"github.com/XinFinOrg/XDPoSChain/params"
|
|
"github.com/XinFinOrg/XDPoSChain/rlp"
|
|
)
|
|
|
|
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.Reader, hash common.Hash) (common.Hash, uint64, uint64) {
|
|
// Load the positional metadata from disk and bail if it fails
|
|
data, _ := db.Get(txLookupKey(hash))
|
|
if len(data) == 0 {
|
|
return common.Hash{}, 0, 0
|
|
}
|
|
// Parse and return the contents of the lookup entry
|
|
var entry TxLookupEntry
|
|
if err := rlp.DecodeBytes(data, &entry); err != nil {
|
|
log.Error("Invalid lookup entry RLP", "hash", hash, "err", err)
|
|
return common.Hash{}, 0, 0
|
|
}
|
|
return entry.BlockHash, entry.BlockIndex, entry.Index
|
|
}
|
|
|
|
// WriteTxLookupEntriesByBlock stores a positional metadata for every transaction from
|
|
// a block, enabling hash based transaction and receipt lookups.
|
|
func WriteTxLookupEntriesByBlock(db ethdb.KeyValueWriter, block *types.Block) {
|
|
// Iterate over each transaction and encode its metadata
|
|
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 RLP encode TxLookupEntry", "err", err)
|
|
}
|
|
if err := db.Put(txLookupKey(tx.Hash()), data); err != nil {
|
|
log.Crit("Failed to store tx lookup entry", "err", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// DeleteTxLookupEntry removes all transaction data associated with a hash.
|
|
func DeleteTxLookupEntry(db ethdb.KeyValueWriter, hash common.Hash) {
|
|
db.Delete(txLookupKey(hash))
|
|
}
|
|
|
|
// ReadTransaction retrieves a specific transaction from the database, along with
|
|
// its added positional metadata.
|
|
func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
|
|
// Retrieve the lookup metadata and resolve the transaction from the body
|
|
blockHash, blockNumber, txIndex := ReadTxLookupEntry(db, hash)
|
|
|
|
if blockHash != (common.Hash{}) {
|
|
body := ReadBody(db, blockHash, blockNumber)
|
|
if body == nil || len(body.Transactions) <= int(txIndex) {
|
|
log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash, "index", txIndex)
|
|
return nil, common.Hash{}, 0, 0
|
|
}
|
|
return body.Transactions[txIndex], blockHash, blockNumber, txIndex
|
|
}
|
|
// Old transaction representation, load the transaction and it's metadata separately
|
|
data, _ := db.Get(hash.Bytes())
|
|
if len(data) == 0 {
|
|
return nil, common.Hash{}, 0, 0
|
|
}
|
|
var tx types.Transaction
|
|
if err := rlp.DecodeBytes(data, &tx); err != nil {
|
|
return nil, common.Hash{}, 0, 0
|
|
}
|
|
// Retrieve the blockchain positional metadata
|
|
data, _ = db.Get(append(hash.Bytes(), oldTxMetaSuffix...))
|
|
if len(data) == 0 {
|
|
return nil, common.Hash{}, 0, 0
|
|
}
|
|
var entry TxLookupEntry
|
|
if err := rlp.DecodeBytes(data, &entry); err != nil {
|
|
return nil, common.Hash{}, 0, 0
|
|
}
|
|
return &tx, entry.BlockHash, entry.BlockIndex, entry.Index
|
|
}
|
|
|
|
// ReadReceipt retrieves a specific transaction receipt from the database, along with
|
|
// its added positional metadata.
|
|
func ReadReceipt(db ethdb.Reader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) {
|
|
// Retrieve the lookup metadata and resolve the receipt from the receipts
|
|
blockHash, blockNumber, receiptIndex := ReadTxLookupEntry(db, hash)
|
|
|
|
if blockHash != (common.Hash{}) {
|
|
receipts := ReadReceipts(db, blockHash, blockNumber, config)
|
|
if len(receipts) <= int(receiptIndex) {
|
|
log.Error("Receipt refereced missing", "number", blockNumber, "hash", blockHash, "index", receiptIndex)
|
|
return nil, common.Hash{}, 0, 0
|
|
}
|
|
return receipts[receiptIndex], blockHash, blockNumber, receiptIndex
|
|
}
|
|
// Old receipt representation, load the receipt and set an unknown metadata
|
|
data, _ := db.Get(append(oldReceiptsPrefix, hash[:]...))
|
|
if len(data) == 0 {
|
|
return nil, common.Hash{}, 0, 0
|
|
}
|
|
var receipt types.ReceiptForStorage
|
|
err := rlp.DecodeBytes(data, &receipt)
|
|
if err != nil {
|
|
log.Error("Invalid receipt RLP", "hash", hash, "err", err)
|
|
}
|
|
return (*types.Receipt)(&receipt), 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.KeyValueReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
|
|
return db.Get(bloomBitsKey(bit, section, head))
|
|
}
|
|
|
|
// WriteBloomBits writes the compressed bloom bits vector belonging to the given
|
|
// section and bit index.
|
|
func WriteBloomBits(db ethdb.KeyValueWriter, bit uint, section uint64, head common.Hash, bits []byte) {
|
|
if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil {
|
|
log.Crit("Failed to store bloom bits", "err", err)
|
|
}
|
|
}
|
|
|
|
// FindCommonAncestor returns the last common ancestor of two block headers
|
|
func FindCommonAncestor(db ethdb.Reader, a, b *types.Header) *types.Header {
|
|
for bn := b.Number.Uint64(); a.Number.Uint64() > bn; {
|
|
a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1)
|
|
if a == nil {
|
|
return nil
|
|
}
|
|
}
|
|
for an := a.Number.Uint64(); an < b.Number.Uint64(); {
|
|
b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1)
|
|
if b == nil {
|
|
return nil
|
|
}
|
|
}
|
|
for a.Hash() != b.Hash() {
|
|
a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1)
|
|
if a == nil {
|
|
return nil
|
|
}
|
|
b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1)
|
|
if b == nil {
|
|
return nil
|
|
}
|
|
}
|
|
return a
|
|
}
|