Skip to content

Commit

Permalink
fix jsonrpc & remove tx indexing error (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Nov 4, 2024
1 parent 561dc1e commit f023dba
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 42 deletions.
21 changes: 0 additions & 21 deletions jsonrpc/backend/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,6 @@ package backend

import "errors"

// TxIndexingError is an API error that indicates the transaction indexing is not
// fully finished yet with JSON error code and a binary data blob.
type TxIndexingError struct{}

// NewTxIndexingError creates a TxIndexingError instance.
func NewTxIndexingError() *TxIndexingError { return &TxIndexingError{} }

// Error implement error interface, returning the error message.
func (e *TxIndexingError) Error() string {
return "transaction indexing is in progress"
}

// ErrorCode returns the JSON error code for a revert.
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal
func (e *TxIndexingError) ErrorCode() int {
return -32000 // to be decided
}

// ErrorData returns the hex encoded revert reason.
func (e *TxIndexingError) ErrorData() interface{} { return "transaction indexing is in progress" }

type InternalError struct {
msg string
}
Expand Down
27 changes: 6 additions & 21 deletions jsonrpc/backend/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,7 @@ func (b *JSONRPCBackend) getQueryCtxWithHeight(height uint64) (context.Context,

// GetTransactionByHash returns the transaction with the given hash.
func (b *JSONRPCBackend) GetTransactionByHash(hash common.Hash) (*rpctypes.RPCTransaction, error) {
rpcTx, err := b.getTransaction(hash)
if rpcTx == nil {
return nil, err
}

return rpcTx, nil
return b.getTransaction(hash)
}

// GetTransactionCount returns the number of transactions at the given block number.
Expand Down Expand Up @@ -187,12 +182,10 @@ func (b *JSONRPCBackend) GetTransactionCount(address common.Address, blockNrOrHa
// GetTransactionReceipt returns the transaction receipt for the given transaction hash.
func (b *JSONRPCBackend) GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) {
rpcTx, err := b.getTransaction(hash)
if rpcTx == nil && err == nil {
if err != nil {
return nil, err
} else if rpcTx == nil {
return nil, nil // tx is not found
} else if rpcTx != nil && err != nil {
return nil, NewTxIndexingError() // tx is not fully indexed
} else if err != nil {
return nil, err // just error case
}

receipt, err := b.getReceipt(hash)
Expand Down Expand Up @@ -368,14 +361,6 @@ func (b *JSONRPCBackend) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc

// getTransaction retrieves the lookup along with the transaction itself associate
// with the given transaction hash.
//
// An error will be returned if the transaction is not found, and background
// indexing for transactions is still in progress. The error is used to indicate the
// scenario explicitly that the transaction might be reachable shortly.
//
// A null will be returned in the transaction is not found and background transaction
// indexing is already finished. The transaction is not existent from the perspective
// of node.
func (b *JSONRPCBackend) getTransaction(hash common.Hash) (*rpctypes.RPCTransaction, error) {
if tx, ok := b.txLookupCache.Get(hash); ok {
return tx, nil
Expand All @@ -385,13 +370,13 @@ func (b *JSONRPCBackend) getTransaction(hash common.Hash) (*rpctypes.RPCTransact
if cacheKey, ok := b.queuedTxHashes.Load(hash); ok {
if cacheItem, ok := b.queuedTxs.Get(cacheKey.(string)); ok {
rpcTx := rpctypes.NewRPCTransaction(cacheItem.body, common.Hash{}, 0, 0, cacheItem.body.ChainId())
return rpcTx, NewTxIndexingError()
return rpcTx, nil
}
}

// check if the transaction is in the pending txs
if tx := b.app.EVMIndexer().TxInMempool(hash); tx != nil {
return tx, NewTxIndexingError()
return tx, nil
}

queryCtx, err := b.getQueryCtx()
Expand Down

0 comments on commit f023dba

Please sign in to comment.