diff --git a/jsonrpc/backend/block.go b/jsonrpc/backend/block.go index 46bc3b7..5c4e474 100644 --- a/jsonrpc/backend/block.go +++ b/jsonrpc/backend/block.go @@ -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) { @@ -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 @@ -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) diff --git a/jsonrpc/backend/cosmos.go b/jsonrpc/backend/cosmos.go index fa4fe9a..5598d99 100644 --- a/jsonrpc/backend/cosmos.go +++ b/jsonrpc/backend/cosmos.go @@ -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 @@ -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 diff --git a/jsonrpc/backend/filters.go b/jsonrpc/backend/filters.go index cd3b178..31f5d2e 100644 --- a/jsonrpc/backend/filters.go +++ b/jsonrpc/backend/filters.go @@ -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{} diff --git a/jsonrpc/backend/tx.go b/jsonrpc/backend/tx.go index 43d8e59..5902466 100644 --- a/jsonrpc/backend/tx.go +++ b/jsonrpc/backend/tx.go @@ -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 } @@ -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) @@ -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 { @@ -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() } @@ -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)) @@ -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) @@ -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) @@ -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 @@ -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 diff --git a/jsonrpc/config/config.go b/jsonrpc/config/config.go index 400d1af..ed9f254 100644 --- a/jsonrpc/config/config.go +++ b/jsonrpc/config/config.go @@ -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. @@ -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" @@ -114,7 +111,7 @@ func DefaultJSONRPCConfig() JSONRPCConfig { return JSONRPCConfig{ Enable: DefaultEnable, Address: DefaultAddress, - EnableWS: DefaultEnable, + EnableWS: DefaultEnableWS, AddressWS: DefaultAddressWS, EnableUnsafeCORS: DefaultEnableUnsafeCORS,