From 8be83495a85e1bc7f1e2e65009414a738e17e4ad Mon Sep 17 00:00:00 2001 From: aaronbuchwald Date: Mon, 27 Jun 2022 14:44:26 -0400 Subject: [PATCH] Replace ctx deadline hack with err check (#828) * Replace ctx deadline hack with err check * Remove deadcode --- eth/api_backend.go | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/eth/api_backend.go b/eth/api_backend.go index bcf088412d..6b8e0fbb16 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -49,10 +49,7 @@ import ( "github.com/ethereum/go-ethereum/event" ) -var ( - ErrUnfinalizedData = errors.New("cannot query unfinalized data") - errExpired = errors.New("request expired") -) +var ErrUnfinalizedData = errors.New("cannot query unfinalized data") // EthAPIBackend implements ethapi.Backend for full nodes type EthAPIBackend struct { @@ -80,8 +77,8 @@ func (b *EthAPIBackend) LastAcceptedBlock() *types.Block { } func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { - if deadline, exists := ctx.Deadline(); exists && time.Until(deadline) < 0 { - return nil, errExpired + if err := ctx.Err(); err != nil { + return nil, err } // Treat requests for the pending, latest, or accepted block // identically. @@ -117,15 +114,15 @@ func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash } func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { - if deadline, exists := ctx.Deadline(); exists && time.Until(deadline) < 0 { - return nil, errExpired + if err := ctx.Err(); err != nil { + return nil, err } return b.eth.blockchain.GetHeaderByHash(hash), nil } func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { - if deadline, exists := ctx.Deadline(); exists && time.Until(deadline) < 0 { - return nil, errExpired + if err := ctx.Err(); err != nil { + return nil, err } // Treat requests for the pending, latest, or accepted block // identically. @@ -144,8 +141,8 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe } func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { - if deadline, exists := ctx.Deadline(); exists && time.Until(deadline) < 0 { - return nil, errExpired + if err := ctx.Err(); err != nil { + return nil, err } return b.eth.blockchain.GetBlockByHash(hash), nil } @@ -154,8 +151,8 @@ func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash r if blockNr, ok := blockNrOrHash.Number(); ok { return b.BlockByNumber(ctx, blockNr) } - if deadline, exists := ctx.Deadline(); exists && time.Until(deadline) < 0 { - return nil, errExpired + if err := ctx.Err(); err != nil { + return nil, err } if hash, ok := blockNrOrHash.Hash(); ok { header := b.eth.blockchain.GetHeaderByHash(hash) @@ -199,8 +196,8 @@ func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockN if blockNr, ok := blockNrOrHash.Number(); ok { return b.StateAndHeaderByNumber(ctx, blockNr) } - if deadline, exists := ctx.Deadline(); exists && time.Until(deadline) < 0 { - return nil, nil, errExpired + if err := ctx.Err(); err != nil { + return nil, nil, err } if hash, ok := blockNrOrHash.Hash(); ok { header, err := b.HeaderByHash(ctx, hash) @@ -220,15 +217,15 @@ func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockN } func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { - if deadline, exists := ctx.Deadline(); exists && time.Until(deadline) < 0 { - return nil, errExpired + if err := ctx.Err(); err != nil { + return nil, err } return b.eth.blockchain.GetReceiptsByHash(hash), nil } func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { - if deadline, exists := ctx.Deadline(); exists && time.Until(deadline) < 0 { - return nil, errExpired + if err := ctx.Err(); err != nil { + return nil, err } db := b.eth.ChainDb() number := rawdb.ReadHeaderNumber(db, hash) @@ -289,8 +286,8 @@ func (b *EthAPIBackend) SubscribeAcceptedTransactionEvent(ch chan<- core.NewTxsE } func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { - if deadline, exists := ctx.Deadline(); exists && time.Until(deadline) < 0 { - return errExpired + if err := ctx.Err(); err != nil { + return err } return b.eth.txPool.AddLocal(signedTx) }