Skip to content

Commit

Permalink
fix: cleanup error handling (#89)
Browse files Browse the repository at this point in the history
* cleanup error handling

* use app latest block height instead of status
  • Loading branch information
beer-1 authored Oct 28, 2024
1 parent f8a1a44 commit 5a41c08
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
12 changes: 4 additions & 8 deletions jsonrpc/backend/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ import (
)

func (b *JSONRPCBackend) BlockNumber() (hexutil.Uint64, error) {
res, err := b.clientCtx.Client.Status(b.ctx)
if err != nil {
return 0, err
}

return hexutil.Uint64(res.SyncInfo.LatestBlockHeight), nil
return hexutil.Uint64(b.app.LastBlockHeight()), nil
}

func (b *JSONRPCBackend) resolveBlockNrOrHash(blockNrOrHash rpc.BlockNumberOrHash) (uint64, error) {
Expand Down Expand Up @@ -72,7 +67,7 @@ func (b *JSONRPCBackend) GetHeaderByNumber(ethBlockNum rpc.BlockNumber) (*corety
return nil, nil
} else if err != nil {
b.logger.Error("failed to get block header by number", "err", err)
return nil, err
return nil, NewInternalError("failed to get block header by number")
}

// cache the header
Expand Down Expand Up @@ -138,7 +133,8 @@ func (b *JSONRPCBackend) blockNumberByHash(hash common.Hash) (uint64, error) {

number, err := b.app.EVMIndexer().BlockHashToNumber(queryCtx, hash)
if err != nil {
return 0, err
b.logger.Error("failed to get block number by hash", "err", err)
return 0, NewInternalError("failed to get block number by hash")
}

_ = b.blockHashCache.Add(hash, number)
Expand Down
6 changes: 6 additions & 0 deletions jsonrpc/backend/cosmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func (b *JSONRPCBackend) CosmosTxHashByTxHash(hash common.Hash) ([]byte, error)
cosmosTxHash, err := b.app.EVMIndexer().CosmosTxHashByTxHash(queryCtx, hash)
if err != nil && errors.Is(err, collections.ErrNotFound) {
return nil, nil
} else if err != nil {
b.logger.Error("failed to get cosmos tx hash by tx hash", "err", err)
return nil, NewInternalError("failed to get cosmos tx hash by tx hash")
}

return cosmosTxHash, nil
Expand All @@ -32,6 +35,9 @@ func (b *JSONRPCBackend) TxHashByCosmosTxHash(hash []byte) (common.Hash, error)
txHash, err := b.app.EVMIndexer().TxHashByCosmosTxHash(queryCtx, hash)
if err != nil && errors.Is(err, collections.ErrNotFound) {
return common.Hash{}, nil
} else if err != nil {
b.logger.Debug("failed to get tx hash by cosmos tx hash", "err", err)
return common.Hash{}, NewInternalError("failed to get tx hash by cosmos tx hash")
}

return txHash, nil
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/backend/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (b *JSONRPCBackend) GetLogsByHeight(height uint64) ([]*coretypes.Log, error
return nil, err
}
if len(txs) != len(receipts) {
return nil, NewInternalError("number of transactions and receipts do not match")
return nil, NewInternalError("mismatched number of transactions and receipts")
}

blockLogs := []*coretypes.Log{}
Expand Down
30 changes: 13 additions & 17 deletions jsonrpc/backend/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ func (b *JSONRPCBackend) GetTransactionByBlockNumberAndIndex(blockNum rpc.BlockN
return nil, err
}
if txs, ok := b.blockTxsCache.Get(blockNumber); ok {
if int(idx) >= len(txs) {
return nil, nil
}

return txs[idx], nil
}

Expand All @@ -231,7 +235,7 @@ func (b *JSONRPCBackend) GetTransactionByBlockNumberAndIndex(blockNum rpc.BlockN
return nil, nil
} else if err != nil {
b.logger.Error("failed to get transaction by block and index", "err", err)
return nil, err
return nil, NewInternalError("failed to get transaction by block and index")
}

return b.getTransaction(txhash)
Expand Down Expand Up @@ -265,16 +269,7 @@ func (b *JSONRPCBackend) GetBlockTransactionCountByNumber(blockNum rpc.BlockNumb

// GetRawTransactionByHash returns the bytes of the transaction for the given hash.
func (b *JSONRPCBackend) GetRawTransactionByHash(hash common.Hash) (hexutil.Bytes, error) {
if tx, ok := b.txLookupCache.Get(hash); ok {
return tx.ToTransaction().MarshalBinary()
}

queryCtx, err := b.getQueryCtx()
if err != nil {
return nil, err
}

rpcTx, err := b.app.EVMIndexer().TxByHash(queryCtx, hash)
rpcTx, err := b.getTransaction(hash)
if err != nil && errors.Is(err, collections.ErrNotFound) {
return nil, nil
} else if err != nil {
Expand All @@ -284,7 +279,6 @@ func (b *JSONRPCBackend) GetRawTransactionByHash(hash common.Hash) (hexutil.Byte
return nil, nil
}

_ = b.txLookupCache.Add(hash, rpcTx)
return rpcTx.ToTransaction().MarshalBinary()
}

Expand Down Expand Up @@ -364,7 +358,7 @@ func (b *JSONRPCBackend) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc
}

if len(txs) != len(receipts) {
return nil, fmt.Errorf("receipts length mismatch: %d vs %d", len(txs), len(receipts))
return nil, NewInternalError("mismatched number of transactions and receipts")
}

result := make([]map[string]interface{}, len(receipts))
Expand Down Expand Up @@ -399,7 +393,7 @@ func (b *JSONRPCBackend) getTransaction(hash common.Hash) (*rpctypes.RPCTransact
return nil, nil
} else if err != nil {
b.logger.Error("failed to get transaction by hash", "err", err)
return nil, NewTxIndexingError()
return nil, NewInternalError("failed to get transaction by hash")
}

_ = b.txLookupCache.Add(hash, tx)
Expand All @@ -421,7 +415,7 @@ func (b *JSONRPCBackend) getReceipt(hash common.Hash) (*coretypes.Receipt, error
return nil, nil
} else if err != nil {
b.logger.Error("failed to get transaction receipt by hash", "err", err)
return nil, NewTxIndexingError()
return nil, NewInternalError("failed to get transaction receipt by hash")
}

_ = b.receiptCache.Add(hash, receipt)
Expand All @@ -444,7 +438,8 @@ func (b *JSONRPCBackend) getBlockTransactions(blockNumber uint64) ([]*rpctypes.R
return false, nil
})
if err != nil {
return nil, err
b.logger.Error("failed to get block transactions", "err", err)
return nil, NewInternalError("failed to get block transactions")
}

// cache the transactions
Expand All @@ -468,7 +463,8 @@ func (b *JSONRPCBackend) getBlockReceipts(blockNumber uint64) ([]*coretypes.Rece
return false, nil
})
if err != nil {
return nil, err
b.logger.Error("failed to get block receipts", "err", err)
return nil, NewInternalError("failed to get block receipts")
}

// cache the receipts
Expand Down
5 changes: 1 addition & 4 deletions jsonrpc/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ const (
// DefaultMaxOpenConnections is the default maximum number of simultaneous connections
// for the server listener.
DefaultMaxOpenConnections = 100
// DefaultLogsCap is the default max number of results can be returned from single `eth_getLogs` query.
DefaultLogsCap int32 = 100
// DefaultAddress defines the default HTTP server to listen on.
DefaultAddress = "127.0.0.1:8545"
// DefaultAddressWS defines the default WebSocket server address to bind to.
Expand Down Expand Up @@ -58,7 +56,6 @@ const (
flagJSONRPCAddressWS = "json-rpc.address-ws"
flagJSONRPCEnableUnsafeCORS = "json-rpc.enable-unsafe-cors"
flagJSONRPCAPIs = "json-rpc.apis"
flagJSONRPCLogsCap = "json-rpc.logs-cap"
flagJSONRPCHTTPTimeout = "json-rpc.http-timeout"
flagJSONRPCHTTPIdleTimeout = "json-rpc.http-idle-timeout"
flagJSONRPCMaxOpenConnections = "json-rpc.max-open-connections"
Expand Down Expand Up @@ -114,7 +111,7 @@ func DefaultJSONRPCConfig() JSONRPCConfig {
return JSONRPCConfig{
Enable: DefaultEnable,
Address: DefaultAddress,
EnableWS: DefaultEnable,
EnableWS: DefaultEnableWS,
AddressWS: DefaultAddressWS,
EnableUnsafeCORS: DefaultEnableUnsafeCORS,

Expand Down

0 comments on commit 5a41c08

Please sign in to comment.