diff --git a/cmd/rpcdaemon/commands/debug_api.go b/cmd/rpcdaemon/commands/debug_api.go index a6ece366f..796980932 100644 --- a/cmd/rpcdaemon/commands/debug_api.go +++ b/cmd/rpcdaemon/commands/debug_api.go @@ -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 } diff --git a/cmd/rpcdaemon/commands/erigon_receipts.go b/cmd/rpcdaemon/commands/erigon_receipts.go index b5649ad53..4e6b059f1 100644 --- a/cmd/rpcdaemon/commands/erigon_receipts.go +++ b/cmd/rpcdaemon/commands/erigon_receipts.go @@ -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 } diff --git a/cmd/rpcdaemon/commands/eth_api.go b/cmd/rpcdaemon/commands/eth_api.go index 69dd668ee..b6dd8952a 100644 --- a/cmd/rpcdaemon/commands/eth_api.go +++ b/cmd/rpcdaemon/commands/eth_api.go @@ -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") } diff --git a/cmd/rpcdaemon/commands/eth_block.go b/cmd/rpcdaemon/commands/eth_block.go index 49b38049b..e396fb87b 100644 --- a/cmd/rpcdaemon/commands/eth_block.go +++ b/cmd/rpcdaemon/commands/eth_block.go @@ -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) } diff --git a/cmd/rpcdaemon/commands/eth_call.go b/cmd/rpcdaemon/commands/eth_call.go index 3be898f37..b8ba3b9c1 100644 --- a/cmd/rpcdaemon/commands/eth_call.go +++ b/cmd/rpcdaemon/commands/eth_call.go @@ -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 } diff --git a/cmd/rpcdaemon/commands/eth_callMany.go b/cmd/rpcdaemon/commands/eth_callMany.go index b605a0bd3..77e524cf9 100644 --- a/cmd/rpcdaemon/commands/eth_callMany.go +++ b/cmd/rpcdaemon/commands/eth_callMany.go @@ -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 } diff --git a/cmd/rpcdaemon/commands/eth_receipts.go b/cmd/rpcdaemon/commands/eth_receipts.go index 6da3a4412..b100f205d 100644 --- a/cmd/rpcdaemon/commands/eth_receipts.go +++ b/cmd/rpcdaemon/commands/eth_receipts.go @@ -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 } diff --git a/cmd/rpcdaemon/commands/eth_system.go b/cmd/rpcdaemon/commands/eth_system.go index 4077a6f03..cf813730b 100644 --- a/cmd/rpcdaemon/commands/eth_system.go +++ b/cmd/rpcdaemon/commands/eth_system.go @@ -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 diff --git a/cmd/rpcdaemon/commands/eth_txs.go b/cmd/rpcdaemon/commands/eth_txs.go index 4831e4d92..c5f31a904 100644 --- a/cmd/rpcdaemon/commands/eth_txs.go +++ b/cmd/rpcdaemon/commands/eth_txs.go @@ -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 } diff --git a/cmd/rpcdaemon/commands/eth_uncles.go b/cmd/rpcdaemon/commands/eth_uncles.go index 31ae196a3..a08356869 100644 --- a/cmd/rpcdaemon/commands/eth_uncles.go +++ b/cmd/rpcdaemon/commands/eth_uncles.go @@ -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 } diff --git a/cmd/rpcdaemon/commands/otterscan_api.go b/cmd/rpcdaemon/commands/otterscan_api.go index b04e7e1a6..b7ab128bb 100644 --- a/cmd/rpcdaemon/commands/otterscan_api.go +++ b/cmd/rpcdaemon/commands/otterscan_api.go @@ -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 } diff --git a/cmd/rpcdaemon/commands/otterscan_generic_tracer.go b/cmd/rpcdaemon/commands/otterscan_generic_tracer.go index 3996699bc..f2ffd3b44 100644 --- a/cmd/rpcdaemon/commands/otterscan_generic_tracer.go +++ b/cmd/rpcdaemon/commands/otterscan_generic_tracer.go @@ -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 } diff --git a/cmd/rpcdaemon/commands/trace_adhoc.go b/cmd/rpcdaemon/commands/trace_adhoc.go index 1a3586861..ba42c3008 100644 --- a/cmd/rpcdaemon/commands/trace_adhoc.go +++ b/cmd/rpcdaemon/commands/trace_adhoc.go @@ -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 } diff --git a/cmd/rpcdaemon/commands/trace_filtering.go b/cmd/rpcdaemon/commands/trace_filtering.go index 944941795..ba70a6a30 100644 --- a/cmd/rpcdaemon/commands/trace_filtering.go +++ b/cmd/rpcdaemon/commands/trace_filtering.go @@ -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 } diff --git a/cmd/rpcdaemon/commands/tracing.go b/cmd/rpcdaemon/commands/tracing.go index 00761018e..4dbd5ffc1 100644 --- a/cmd/rpcdaemon/commands/tracing.go +++ b/cmd/rpcdaemon/commands/tracing.go @@ -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 diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index ae960dff1..61e58496b 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -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)) diff --git a/core/types/receipt.go b/core/types/receipt.go index d556547c8..cdebd3b6c 100644 --- a/core/types/receipt.go +++ b/core/types/receipt.go @@ -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 diff --git a/eth/gasprice/feehistory.go b/eth/gasprice/feehistory.go index 246f00a04..2e9923a23 100644 --- a/eth/gasprice/feehistory.go +++ b/eth/gasprice/feehistory.go @@ -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)) diff --git a/eth/gasprice/gasprice.go b/eth/gasprice/gasprice.go index bb3298563..2d4036423 100644 --- a/eth/gasprice/gasprice.go +++ b/eth/gasprice/gasprice.go @@ -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) } diff --git a/eth/gasprice/gasprice_test.go b/eth/gasprice/gasprice_test.go index 23e1f53bb..9ebe5e8cf 100644 --- a/eth/gasprice/gasprice_test.go +++ b/eth/gasprice/gasprice_test.go @@ -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 } diff --git a/eth/protocols/eth/handler_test.go b/eth/protocols/eth/handler_test.go index 32a27bc2d..c1b103b23 100644 --- a/eth/protocols/eth/handler_test.go +++ b/eth/protocols/eth/handler_test.go @@ -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) diff --git a/eth/protocols/eth/handlers.go b/eth/protocols/eth/handlers.go index 9be97f2b2..17f305a5f 100644 --- a/eth/protocols/eth/handlers.go +++ b/eth/protocols/eth/handlers.go @@ -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 { diff --git a/eth/stagedsync/stage_senders_test.go b/eth/stagedsync/stage_senders_test.go index 43083639f..7bb2ab2f6 100644 --- a/eth/stagedsync/stage_senders_test.go +++ b/eth/stagedsync/stage_senders_test.go @@ -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)