Skip to content

Commit

Permalink
Replace ctx deadline hack with err check (#828)
Browse files Browse the repository at this point in the history
* Replace ctx deadline hack with err check

* Remove deadcode
  • Loading branch information
aaronbuchwald authored Jun 27, 2022
1 parent 51fda40 commit 8be8349
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 8be8349

Please sign in to comment.