receipts: less allocs in DeriveFields, use blockReader, remove ReadReceiptsByHash, gasPriceOracle don't read block twice and use blockLru (#7592)

preparation for adding BlockID
This commit is contained in:
Alex Sharov 2023-05-27 16:39:14 +07:00 committed by GitHub
parent 14653bbda1
commit 3ec7d9b010
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 79 additions and 112 deletions

View File

@ -83,7 +83,7 @@ func (api *PrivateDebugAPIImpl) StorageRangeAt(ctx context.Context, blockHash co
return storageRangeAtV3(tx.(kv.TemporalTx), contractAddress, keyStart, minTxNum+txIndex, maxResult)
}
block, err := api.blockByHashWithSenders(tx, blockHash)
block, err := api.blockByHashWithSenders(ctx, tx, blockHash)
if err != nil {
return StorageRangeResult{}, err
}
@ -124,7 +124,7 @@ func (api *PrivateDebugAPIImpl) AccountRange(ctx context.Context, blockNrOrHash
}
} else if hash, ok := blockNrOrHash.Hash(); ok {
block, err1 := api.blockByHashWithSenders(tx, hash)
block, err1 := api.blockByHashWithSenders(ctx, tx, hash)
if err1 != nil {
return state.IteratorDump{}, err1
}
@ -247,7 +247,7 @@ func (api *PrivateDebugAPIImpl) GetModifiedAccountsByHash(ctx context.Context, s
}
defer tx.Rollback()
startBlock, err := api.blockByHashWithSenders(tx, startHash)
startBlock, err := api.blockByHashWithSenders(ctx, tx, startHash)
if err != nil {
return nil, err
}
@ -258,7 +258,7 @@ func (api *PrivateDebugAPIImpl) GetModifiedAccountsByHash(ctx context.Context, s
endNum := startNum + 1 // allows for single parameter calls
if endHash != nil {
endBlock, err := api.blockByHashWithSenders(tx, *endHash)
endBlock, err := api.blockByHashWithSenders(ctx, tx, *endHash)
if err != nil {
return nil, err
}
@ -341,7 +341,7 @@ func (api *PrivateDebugAPIImpl) AccountAt(ctx context.Context, blockHash common.
}
engine := api.engine()
block, err := api.blockByHashWithSenders(tx, blockHash)
block, err := api.blockByHashWithSenders(ctx, tx, blockHash)
if err != nil {
return nil, err
}
@ -397,7 +397,7 @@ func (api *PrivateDebugAPIImpl) GetRawBlock(ctx context.Context, blockNrOrHash r
if err != nil {
return nil, err
}
block, err := api.blockWithSenders(tx, h, n)
block, err := api.blockWithSenders(ctx, tx, h, n)
if err != nil {
return nil, err
}

View File

@ -34,7 +34,7 @@ func (api *ErigonImpl) GetLogsByHash(ctx context.Context, hash common.Hash) ([][
return nil, err
}
block, err := api.blockByHashWithSenders(tx, hash)
block, err := api.blockByHashWithSenders(ctx, tx, hash)
if err != nil {
return nil, err
}
@ -377,7 +377,7 @@ func (api *ErigonImpl) GetBlockReceiptsByBlockHash(ctx context.Context, cannonic
if err != nil {
return nil, err
}
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, err
}

View File

@ -157,15 +157,15 @@ func (api *BaseAPI) txnLookup(ctx context.Context, tx kv.Tx, txnHash common.Hash
return api._txnReader.TxnLookup(ctx, tx, txnHash)
}
func (api *BaseAPI) blockByNumberWithSenders(tx kv.Tx, number uint64) (*types.Block, error) {
func (api *BaseAPI) blockByNumberWithSenders(ctx context.Context, tx kv.Tx, number uint64) (*types.Block, error) {
hash, hashErr := rawdb.ReadCanonicalHash(tx, number)
if hashErr != nil {
return nil, hashErr
}
return api.blockWithSenders(tx, hash, number)
return api.blockWithSenders(ctx, tx, hash, number)
}
func (api *BaseAPI) blockByHashWithSenders(tx kv.Tx, hash common.Hash) (*types.Block, error) {
func (api *BaseAPI) blockByHashWithSenders(ctx context.Context, tx kv.Tx, hash common.Hash) (*types.Block, error) {
if api.blocksLRU != nil {
if it, ok := api.blocksLRU.Get(hash); ok && it != nil {
return it, nil
@ -176,16 +176,16 @@ func (api *BaseAPI) blockByHashWithSenders(tx kv.Tx, hash common.Hash) (*types.B
return nil, nil
}
return api.blockWithSenders(tx, hash, *number)
return api.blockWithSenders(ctx, tx, hash, *number)
}
func (api *BaseAPI) blockWithSenders(tx kv.Tx, hash common.Hash, number uint64) (*types.Block, error) {
func (api *BaseAPI) blockWithSenders(ctx context.Context, tx kv.Tx, hash common.Hash, number uint64) (*types.Block, error) {
if api.blocksLRU != nil {
if it, ok := api.blocksLRU.Get(hash); ok && it != nil {
return it, nil
}
}
block, _, err := api._blockReader.BlockWithSenders(context.Background(), tx, hash, number)
block, _, err := api._blockReader.BlockWithSenders(ctx, tx, hash, number)
if err != nil {
return nil, err
}
@ -247,13 +247,13 @@ func (api *BaseAPI) pendingBlock() *types.Block {
return api.filters.LastPendingBlock()
}
func (api *BaseAPI) blockByRPCNumber(number rpc.BlockNumber, tx kv.Tx) (*types.Block, error) {
func (api *BaseAPI) blockByRPCNumber(ctx context.Context, number rpc.BlockNumber, tx kv.Tx) (*types.Block, error) {
n, _, _, err := rpchelper.GetBlockNumber(rpc.BlockNumberOrHashWithNumber(number), tx, api.filters)
if err != nil {
return nil, err
}
block, err := api.blockByNumberWithSenders(tx, n)
block, err := api.blockByNumberWithSenders(ctx, tx, n)
return block, err
}
@ -279,14 +279,14 @@ func (api *BaseAPI) checkPruneHistory(tx kv.Tx, block uint64) error {
return nil
}
if p.History.Enabled() {
latest, err := api.blockByRPCNumber(rpc.LatestBlockNumber, tx)
latest, _, _, err := rpchelper.GetBlockNumber(rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber), tx, api.filters)
if err != nil {
return err
}
if latest == nil {
if latest <= 1 {
return nil
}
prunedTo := p.History.PruneTo(latest.Number().Uint64())
prunedTo := p.History.PruneTo(latest)
if block < prunedTo {
return fmt.Errorf("history has been pruned for this block")
}

View File

@ -53,7 +53,7 @@ func (api *APIImpl) CallBundle(ctx context.Context, txHashes []common.Hash, stat
if !ok {
return nil, nil
}
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, err
}
@ -261,7 +261,7 @@ func (api *APIImpl) GetBlockByHash(ctx context.Context, numberOrHash rpc.BlockNu
additionalFields := make(map[string]interface{})
block, err := api.blockByHashWithSenders(tx, hash)
block, err := api.blockByHashWithSenders(ctx, tx, hash)
if err != nil {
return nil, err
}
@ -313,7 +313,7 @@ func (api *APIImpl) GetBlockTransactionCountByNumber(ctx context.Context, blockN
defer tx.Rollback()
if blockNr == rpc.PendingBlockNumber {
b, err := api.blockByRPCNumber(blockNr, tx)
b, err := api.blockByRPCNumber(ctx, blockNr, tx)
if err != nil {
return nil, err
}
@ -372,7 +372,7 @@ func (api *APIImpl) GetBlockTransactionCountByHash(ctx context.Context, blockHas
func (api *APIImpl) blockByNumber(ctx context.Context, number rpc.BlockNumber, tx kv.Tx) (*types.Block, error) {
if number != rpc.PendingBlockNumber {
return api.blockByRPCNumber(number, tx)
return api.blockByRPCNumber(ctx, number, tx)
}
if block := api.pendingBlock(); block != nil {
@ -387,5 +387,5 @@ func (api *APIImpl) blockByNumber(ctx context.Context, number rpc.BlockNumber, t
return block, nil
}
return api.blockByRPCNumber(number, tx)
return api.blockByRPCNumber(ctx, number, tx)
}

View File

@ -59,7 +59,7 @@ func (api *APIImpl) Call(ctx context.Context, args ethapi2.CallArgs, blockNrOrHa
if err != nil {
return nil, err
}
block, err := api.blockWithSenders(tx, hash, blockNumber)
block, err := api.blockWithSenders(ctx, tx, hash, blockNumber)
if err != nil {
return nil, err
}
@ -234,7 +234,7 @@ func (api *APIImpl) EstimateGas(ctx context.Context, argsOrNil *ethapi2.CallArgs
// try and get the block from the lru cache first then try DB before failing
block := api.tryBlockFromLru(latestCanHash)
if block == nil {
block, err = api.blockWithSenders(dbtx, latestCanHash, latestCanBlockNumber)
block, err = api.blockWithSenders(ctx, dbtx, latestCanHash, latestCanBlockNumber)
if err != nil {
return 0, err
}
@ -449,7 +449,7 @@ func (api *APIImpl) CreateAccessList(ctx context.Context, args ethapi2.CallArgs,
if err != nil {
return nil, err
}
block, err := api.blockWithSenders(tx, hash, blockNumber)
block, err := api.blockWithSenders(ctx, tx, hash, blockNumber)
if err != nil {
return nil, err
}

View File

@ -112,7 +112,7 @@ func (api *APIImpl) CallMany(ctx context.Context, bundles []Bundle, simulateCont
return nil, err
}
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, err
}

View File

@ -617,7 +617,7 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha
blockNum = *blockNumPtr
}
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, err
}
@ -688,7 +688,7 @@ func (api *APIImpl) GetBlockReceipts(ctx context.Context, number rpc.BlockNumber
if err != nil {
return nil, err
}
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, err
}

View File

@ -5,7 +5,6 @@ import (
"math/big"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common/hexutil"
@ -216,13 +215,13 @@ func (b *GasPriceOracleBackend) HeaderByNumber(ctx context.Context, number rpc.B
return header, nil
}
func (b *GasPriceOracleBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
return b.baseApi.blockByRPCNumber(number, b.tx)
return b.baseApi.blockByRPCNumber(ctx, number, b.tx)
}
func (b *GasPriceOracleBackend) ChainConfig() *chain.Config {
return b.cc
}
func (b *GasPriceOracleBackend) GetReceipts(ctx context.Context, hash libcommon.Hash) (types.Receipts, error) {
return rawdb.ReadReceiptsByHash(b.tx, hash)
func (b *GasPriceOracleBackend) GetReceipts(ctx context.Context, block *types.Block) (types.Receipts, error) {
return rawdb.ReadReceipts(b.tx, block, nil), nil
}
func (b *GasPriceOracleBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
return nil, nil

View File

@ -48,7 +48,7 @@ func (api *APIImpl) GetTransactionByHash(ctx context.Context, txnHash common.Has
}
}
if ok {
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, err
}
@ -131,7 +131,7 @@ func (api *APIImpl) GetRawTransactionByHash(ctx context.Context, hash common.Has
if !ok {
return nil, nil
}
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, err
}
@ -176,7 +176,7 @@ func (api *APIImpl) GetTransactionByBlockHashAndIndex(ctx context.Context, block
}
// https://infura.io/docs/ethereum/json-rpc/eth-getTransactionByBlockHashAndIndex
block, err := api.blockByHashWithSenders(tx, blockHash)
block, err := api.blockByHashWithSenders(ctx, tx, blockHash)
if err != nil {
return nil, err
}
@ -211,7 +211,7 @@ func (api *APIImpl) GetRawTransactionByBlockHashAndIndex(ctx context.Context, bl
defer tx.Rollback()
// https://infura.io/docs/ethereum/json-rpc/eth-getRawTransactionByBlockHashAndIndex
block, err := api.blockByHashWithSenders(tx, blockHash)
block, err := api.blockByHashWithSenders(ctx, tx, blockHash)
if err != nil {
return nil, err
}
@ -240,7 +240,7 @@ func (api *APIImpl) GetTransactionByBlockNumberAndIndex(ctx context.Context, blo
return nil, err
}
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, err
}
@ -275,7 +275,7 @@ func (api *APIImpl) GetRawTransactionByBlockNumberAndIndex(ctx context.Context,
defer tx.Rollback()
// https://infura.io/docs/ethereum/json-rpc/eth-getRawTransactionByBlockNumberAndIndex
block, err := api.blockByRPCNumber(blockNr, tx)
block, err := api.blockByRPCNumber(ctx, blockNr, tx)
if err != nil {
return nil, err
}

View File

@ -26,7 +26,7 @@ func (api *APIImpl) GetUncleByBlockNumberAndIndex(ctx context.Context, number rp
if err != nil {
return nil, err
}
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, err
}
@ -57,7 +57,7 @@ func (api *APIImpl) GetUncleByBlockHashAndIndex(ctx context.Context, hash common
}
defer tx.Rollback()
block, err := api.blockByHashWithSenders(tx, hash)
block, err := api.blockByHashWithSenders(ctx, tx, hash)
if err != nil {
return nil, err
}
@ -97,7 +97,7 @@ func (api *APIImpl) GetUncleCountByBlockNumber(ctx context.Context, number rpc.B
return &n, err
}
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, err
}
@ -122,7 +122,7 @@ func (api *APIImpl) GetUncleCountByBlockHash(ctx context.Context, hash common.Ha
return nil, nil // not error, see https://github.com/ledgerwatch/erigon/issues/1645
}
block, err := api.blockWithSenders(tx, hash, *number)
block, err := api.blockWithSenders(ctx, tx, hash, *number)
if err != nil {
return nil, err
}

View File

@ -83,7 +83,7 @@ func (api *OtterscanAPIImpl) getTransactionByHash(ctx context.Context, tx kv.Tx,
return nil, nil, common.Hash{}, 0, 0, nil
}
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, nil, common.Hash{}, 0, 0, err
}

View File

@ -73,7 +73,7 @@ func (api *OtterscanAPIImpl) genericTracer(dbtx kv.Tx, ctx context.Context, bloc
return h
}
engine := api.engine()
block, err := api.blockByNumberWithSenders(dbtx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, dbtx, blockNum)
if err != nil {
return err
}

View File

@ -739,7 +739,7 @@ func (api *TraceAPIImpl) ReplayTransaction(ctx context.Context, txHash libcommon
}
blockNum = *blockNumPtr
}
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, err
}
@ -815,7 +815,7 @@ func (api *TraceAPIImpl) ReplayBlockTransactions(ctx context.Context, blockNrOrH
}
// Extract transactions from block
block, bErr := api.blockByNumberWithSenders(tx, blockNumber)
block, bErr := api.blockByNumberWithSenders(ctx, tx, blockNumber)
if bErr != nil {
return nil, bErr
}
@ -896,7 +896,7 @@ func (api *TraceAPIImpl) Call(ctx context.Context, args TraceCallParam, traceTyp
ibs := state.New(stateReader)
block, err := api.blockWithSenders(tx, hash, blockNumber)
block, err := api.blockWithSenders(ctx, tx, hash, blockNumber)
if err != nil {
return nil, err
}
@ -1058,7 +1058,7 @@ func (api *TraceAPIImpl) CallMany(ctx context.Context, calls json.RawMessage, pa
}
// TODO: can read here only parent header
parentBlock, err := api.blockWithSenders(dbtx, hash, blockNumber)
parentBlock, err := api.blockWithSenders(ctx, dbtx, hash, blockNumber)
if err != nil {
return nil, err
}
@ -1112,7 +1112,7 @@ func (api *TraceAPIImpl) doCallMany(ctx context.Context, dbtx kv.Tx, msgs []type
ibs := state.New(cachedReader)
// TODO: can read here only parent header
parentBlock, err := api.blockWithSenders(dbtx, hash, blockNumber)
parentBlock, err := api.blockWithSenders(ctx, dbtx, hash, blockNumber)
if err != nil {
return nil, nil, err
}

View File

@ -67,7 +67,7 @@ func (api *TraceAPIImpl) Transaction(ctx context.Context, txHash common.Hash, ga
}
blockNumber = *blockNumPtr
}
block, err := api.blockByNumberWithSenders(tx, blockNumber)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNumber)
if err != nil {
return nil, err
}
@ -76,7 +76,7 @@ func (api *TraceAPIImpl) Transaction(ctx context.Context, txHash common.Hash, ga
}
// Extract transactions from block
block, bErr := api.blockByNumberWithSenders(tx, blockNumber)
block, bErr := api.blockByNumberWithSenders(ctx, tx, blockNumber)
if bErr != nil {
return nil, bErr
}
@ -176,7 +176,7 @@ func (api *TraceAPIImpl) Block(ctx context.Context, blockNr rpc.BlockNumber, gas
bn := hexutil.Uint64(blockNum)
// Extract transactions from block
block, bErr := api.blockByNumberWithSenders(tx, blockNum)
block, bErr := api.blockByNumberWithSenders(ctx, tx, blockNum)
if bErr != nil {
return nil, bErr
}
@ -408,7 +408,7 @@ func (api *TraceAPIImpl) Filter(ctx context.Context, req TraceFilterRequest, gas
continue
}
block, bErr := api.blockWithSenders(dbtx, hash, b)
block, bErr := api.blockWithSenders(ctx, dbtx, hash, b)
if bErr != nil {
if first {
first = false
@ -905,7 +905,7 @@ func (api *TraceAPIImpl) callManyTransactions(
parentNo := rpc.BlockNumber(pNo)
rules := cfg.Rules(blockNumber, block.Time())
header := block.Header()
parentBlock, err := api.blockByRPCNumber(parentNo, dbtx)
parentBlock, err := api.blockByRPCNumber(ctx, parentNo, dbtx)
if err != nil {
return nil, nil, err
}

View File

@ -52,9 +52,9 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp
hashOk bool
)
if number, numberOk = blockNrOrHash.Number(); numberOk {
block, err = api.blockByRPCNumber(number, tx)
block, err = api.blockByRPCNumber(ctx, number, tx)
} else if hash, hashOk = blockNrOrHash.Hash(); hashOk {
block, err = api.blockByHashWithSenders(tx, hash)
block, err = api.blockByHashWithSenders(ctx, tx, hash)
} else {
return fmt.Errorf("invalid arguments; neither block nor hash specified")
}
@ -88,7 +88,7 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp
}
var excessDataGas *big.Int
parentBlock, err := api.blockByHashWithSenders(tx, block.ParentHash())
parentBlock, err := api.blockByHashWithSenders(ctx, tx, block.ParentHash())
if err != nil {
stream.WriteNil()
return err
@ -220,7 +220,7 @@ func (api *PrivateDebugAPIImpl) TraceTransaction(ctx context.Context, hash commo
}
blockNum = *blockNumPtr
}
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
stream.WriteNil()
return err
@ -380,7 +380,7 @@ func (api *PrivateDebugAPIImpl) TraceCallMany(ctx context.Context, bundles []Bun
return err
}
block, err := api.blockByNumberWithSenders(tx, blockNum)
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
stream.WriteNil()
return err

View File

@ -1053,29 +1053,6 @@ func ReadReceipts(db kv.Tx, block *types.Block, senders []libcommon.Address) typ
return receipts
}
func ReadReceiptsByHash(db kv.Tx, hash libcommon.Hash) (types.Receipts, error) {
number := ReadHeaderNumber(db, hash)
if number == nil {
return nil, nil
}
canonicalHash, err := ReadCanonicalHash(db, *number)
if err != nil {
return nil, fmt.Errorf("requested non-canonical hash %x. canonical=%x", hash, canonicalHash)
}
b, s, err := ReadBlockWithSenders(db, hash, *number)
if err != nil {
return nil, err
}
if b == nil {
return nil, nil
}
receipts := ReadReceipts(db, b, s)
if receipts == nil {
return nil, nil
}
return receipts, nil
}
// WriteReceipts stores all the transaction receipts belonging to a block.
func WriteReceipts(tx kv.Putter, number uint64, receipts types.Receipts) error {
buf := bytes.NewBuffer(make([]byte, 0, 1024))

View File

@ -463,6 +463,8 @@ func (r Receipts) DeriveFields(hash libcommon.Hash, number uint64, txs Transacti
if len(senders) != len(txs) {
return fmt.Errorf("transaction and senders count mismatch, tx count = %d, senders count = %d", len(txs), len(senders))
}
blockNumber := new(big.Int).SetUint64(number)
for i := 0; i < len(r); i++ {
// The transaction type and hash can be retrieved from the transaction itself
r[i].Type = txs[i].Type()
@ -470,7 +472,7 @@ func (r Receipts) DeriveFields(hash libcommon.Hash, number uint64, txs Transacti
// block location fields
r[i].BlockHash = hash
r[i].BlockNumber = new(big.Int).SetUint64(number)
r[i].BlockNumber = blockNumber
r[i].TransactionIndex = uint(i)
// The contract address can be derived from the transaction itself

View File

@ -258,7 +258,7 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks int, unresolvedLast
if len(rewardPercentiles) != 0 {
fees.block, fees.err = oracle.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNumber))
if fees.block != nil && fees.err == nil {
fees.receipts, fees.err = oracle.backend.GetReceipts(ctx, fees.block.Hash())
fees.receipts, fees.err = oracle.backend.GetReceipts(ctx, fees.block)
}
} else {
fees.header, fees.err = oracle.backend.HeaderByNumber(ctx, rpc.BlockNumber(blockNumber))

View File

@ -40,7 +40,7 @@ type OracleBackend interface {
BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
ChainConfig() *chain.Config
GetReceipts(ctx context.Context, hash libcommon.Hash) (types.Receipts, error)
GetReceipts(ctx context.Context, block *types.Block) (types.Receipts, error)
PendingBlockAndReceipts() (*types.Block, types.Receipts)
}

View File

@ -47,30 +47,14 @@ type testBackend struct {
blockReader services.FullBlockReader
}
func (b *testBackend) GetReceipts(ctx context.Context, hash libcommon.Hash) (types.Receipts, error) {
func (b *testBackend) GetReceipts(ctx context.Context, block *types.Block) (types.Receipts, error) {
tx, err := b.db.BeginRo(context.Background())
if err != nil {
return nil, err
}
defer tx.Rollback()
number := rawdb.ReadHeaderNumber(tx, hash)
if number == nil {
return nil, nil
}
canonicalHash, err := rawdb.ReadCanonicalHash(tx, *number)
if err != nil {
return nil, fmt.Errorf("requested non-canonical hash %x. canonical=%x", hash, canonicalHash)
}
block, s, err := b.blockReader.BlockWithSenders(ctx, tx, hash, *number)
if err != nil {
return nil, err
}
if block == nil {
return nil, nil
}
receipts := rawdb.ReadReceipts(tx, block, s)
receipts := rawdb.ReadReceipts(tx, block, nil)
return receipts, nil
}

View File

@ -93,14 +93,14 @@ func TestGetBlockReceipts(t *testing.T) {
err := m.DB.View(m.Ctx, func(tx kv.Tx) error {
for i := uint64(0); i <= rawdb.ReadCurrentHeader(tx).Number.Uint64(); i++ {
block := rawdb.ReadHeaderByNumber(tx, i)
hash, err := rawdb.ReadCanonicalHash(tx, i)
require.NoError(t, err)
block, senders, err := rawdb.ReadBlockWithSenders(tx, hash, i)
require.NoError(t, err)
hashes = append(hashes, block.Hash())
// If known, encode and queue for response packet
r, err := rawdb.ReadReceiptsByHash(tx, block.Hash())
if err != nil {
return err
}
r := rawdb.ReadReceipts(tx, block, senders)
encoded, err := rlp.EncodeToBytes(r)
require.NoError(t, err)
receipts = append(receipts, encoded)

View File

@ -178,10 +178,15 @@ func AnswerGetReceiptsQuery(db kv.Tx, query GetReceiptsPacket) ([]rlp.RawValue,
break
}
// Retrieve the requested block's receipts
results, err := rawdb.ReadReceiptsByHash(db, hash)
number := rawdb.ReadHeaderNumber(db, hash)
if number == nil {
return nil, nil
}
block, senders, err := rawdb.ReadBlockWithSenders(db, hash, *number)
if err != nil {
return nil, err
}
results := rawdb.ReadReceipts(db, block, senders)
if results == nil {
header, err := rawdb.ReadHeaderByHash(db, hash)
if err != nil {

View File

@ -130,7 +130,7 @@ func TestSenders(t *testing.T) {
require.NoError(stages.SaveStageProgress(tx, stages.Bodies, 3))
blockRetire := snapshotsync.NewBlockRetire(1, "", nil, db, nil, nil, logger)
blockRetire := snapshotsync.NewBlockRetire(1, "", br, db, nil, nil, logger)
cfg := stagedsync.StageSendersCfg(db, params.TestChainConfig, false, "", prune.Mode{}, blockRetire, bw, br, nil)
err = stagedsync.SpawnRecoverSendersStage(cfg, &stagedsync.StageState{ID: stages.Senders}, nil, tx, 3, m.Ctx, log.New())
require.NoError(err)