Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ETH compatible error handling #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rpc/ethrpc/eth_block_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (e *EthRPCService) BlockNumber(ctx context.Context) (result string, err err
blockNumber, err := common.GetCurrentHeight()

if err != nil {
return "", err
return "", nil
}

result = hexutil.EncodeUint64(uint64(blockNumber))
Expand Down
6 changes: 3 additions & 3 deletions rpc/ethrpc/eth_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ func (e *EthRPCService) Call(ctx context.Context, argObj common.EthSmartContract
sctxBytes, err := common.GetSctxBytes(argObj)
if err != nil {
logger.Errorf("eth_call: Failed to get smart contract bytes: %+v\n", argObj)
return result, err
return result, nil
}

parse := func(jsonBytes []byte) (interface{}, error) {
trpcResult := trpc.CallSmartContractResult{}
json.Unmarshal(jsonBytes, &trpcResult)
logger.Infof("eth_call Theta RPC result: %+v\n", trpcResult)
if len(trpcResult.VmError) > 0 {
return trpcResult.GasUsed, fmt.Errorf(trpcResult.VmError)
return trpcResult.GasUsed, nil
}
return trpcResult.VmReturn, nil
}
Expand All @@ -54,7 +54,7 @@ func (e *EthRPCService) Call(ctx context.Context, argObj common.EthSmartContract
if err != nil {
if i == maxRetry-1 {
logger.Infof("eth_call error: %v", err)
return "", err
return "", nil
}
time.Sleep(blockInterval) // one block duration
} else {
Expand Down
5 changes: 2 additions & 3 deletions rpc/ethrpc/eth_chain_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ethrpc
import (
"context"
"encoding/json"
"fmt"

"github.com/thetatoken/theta-eth-rpc-adaptor/common"
hexutil "github.com/thetatoken/theta/common/hexutil"
Expand Down Expand Up @@ -36,11 +35,11 @@ func (e *EthRPCService) ChainId(ctx context.Context) (result string, err error)

resultIntf, err := common.HandleThetaRPCResponse(rpcRes, rpcErr, parse)
if err != nil {
return "", err
return "", nil
}
thetaChainIDResult, ok := resultIntf.(chainIDResultWrapper)
if !ok {
return "", fmt.Errorf("failed to convert chainIDResultWrapper")
return "", nil
}

thetaChainID := thetaChainIDResult.chainID
Expand Down
7 changes: 3 additions & 4 deletions rpc/ethrpc/eth_estimate_gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/hex"
"encoding/json"
"fmt"

"github.com/spf13/viper"
"github.com/thetatoken/theta-eth-rpc-adaptor/common"
Expand All @@ -23,7 +22,7 @@ func (e *EthRPCService) EstimateGas(ctx context.Context, argObj common.EthSmartC
sctxBytes, err := common.GetSctxBytes(argObj)
if err != nil {
logger.Errorf("eth_estimateGas: Failed to get smart contract bytes: %+v\n", argObj)
return result, err
return result, nil
}

client := rpcc.NewRPCClient(common.GetThetaRPCEndpoint())
Expand All @@ -35,14 +34,14 @@ func (e *EthRPCService) EstimateGas(ctx context.Context, argObj common.EthSmartC
json.Unmarshal(jsonBytes, &trpcResult)
if len(trpcResult.VmError) > 0 {
logger.Warnf("eth_estimateGas: EVM execution failed: %v\n", trpcResult.VmError)
return trpcResult.GasUsed, fmt.Errorf(trpcResult.VmError)
return trpcResult.GasUsed, nil
}
return trpcResult.GasUsed, nil
}

resultIntf, err := common.HandleThetaRPCResponse(rpcRes, rpcErr, parse)
if err != nil {
return "", err
return "", nil
}

blockGasLimit := viper.GetUint64(common.CfgThetaBlockGasLimit)
Expand Down
10 changes: 5 additions & 5 deletions rpc/ethrpc/eth_gas_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (e *EthRPCService) GasPrice(ctx context.Context) (result string, err error)
currentHeight, err := common.GetCurrentHeight()

if err != nil {
return "", err
return "", nil
}

// fmt.Printf("currentHeight: %v\n", currentHeight)
Expand Down Expand Up @@ -66,11 +66,11 @@ func (e *EthRPCService) GasPrice(ctx context.Context) (result string, err error)

resultIntf, err := common.HandleThetaRPCResponse(rpcRes, rpcErr, parse)
if err != nil {
return "", err
return "", nil
}
thetaGetBlockResult, ok := resultIntf.(common.ThetaGetBlockResult)
if !ok {
return "", fmt.Errorf("failed to convert GetBlockResult")
return "", nil
}
totalGasPrice := big.NewInt(0)
count := 0
Expand Down Expand Up @@ -120,11 +120,11 @@ func getEthChainID(client *rpcc.RPCClient) (uint64, error) {

resultIntf, err := common.HandleThetaRPCResponse(rpcRes, rpcErr, parse)
if err != nil {
return 0, err
return 0, nil
}
thetaChainIDResult, ok := resultIntf.(chainIDResultWrapper)
if !ok {
return 0, fmt.Errorf("failed to convert chainIDResultWrapper")
return 0, nil
}

thetaChainID := thetaChainIDResult.chainID
Expand Down
5 changes: 2 additions & 3 deletions rpc/ethrpc/eth_get_block_by_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"math/big"
"strings"

Expand Down Expand Up @@ -45,7 +44,7 @@ func GetBlockFromTRPCResult(chainID *big.Int, rpcRes *rpcc.RPCResponse, rpcErr e
trpcResult := common.ThetaGetBlockResult{}
json.Unmarshal(jsonBytes, &trpcResult)
if trpcResult.ThetaGetBlockResultInner == nil {
return result, errors.New("empty block")
return result, nil
}
result.Transactions = make([]interface{}, 0)
if txDetails {
Expand Down Expand Up @@ -93,7 +92,7 @@ func GetBlockFromTRPCResult(chainID *big.Int, rpcRes *rpcc.RPCResponse, rpcErr e
}
resultIntf, err := common.HandleThetaRPCResponse(rpcRes, rpcErr, parse)
if err != nil {
return result, err
return result, nil
}
theta_GetBlockResult := resultIntf.(common.ThetaGetBlockResult)
result.Height = hexutil.Uint64(theta_GetBlockResult.Height)
Expand Down
6 changes: 3 additions & 3 deletions rpc/ethrpc/eth_get_block_by_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (e *EthRPCService) GetBlockByNumber(ctx context.Context, numberStr string,
if height == math.MaxUint64 {
height, err = common.GetCurrentHeight()
if err != nil {
return result, err
return result, nil
}
}

Expand All @@ -42,11 +42,11 @@ func (e *EthRPCService) GetBlockByNumber(ctx context.Context, numberStr string,

result, err = GetBlockFromTRPCResult(chainID, rpcRes, rpcErr, txDetails)
if err == nil {
return result, err
return result, nil
}

time.Sleep(blockInterval) // one block duration
}

return result, err
return result, nil
}
4 changes: 2 additions & 2 deletions rpc/ethrpc/eth_get_block_transaction_count_by_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (e *EthRPCService) GetBlockTransactionCountByNumber(ctx context.Context, nu
if height == math.MaxUint64 {
height, err = common.GetCurrentHeight()
if err != nil {
return result, err
return result, nil
}
}

Expand All @@ -35,5 +35,5 @@ func (e *EthRPCService) GetBlockTransactionCountByNumber(ctx context.Context, nu
rpcRes, rpcErr := client.Call("theta.GetBlockByHeight", trpc.GetBlockByHeightArgs{
Height: height})
block, err := GetBlockFromTRPCResult(chainID, rpcRes, rpcErr, false)
return hexutil.Uint64(len(block.Transactions)), err
return hexutil.Uint64(len(block.Transactions)), nil
}
2 changes: 1 addition & 1 deletion rpc/ethrpc/eth_get_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (e *EthRPCService) GetCode(ctx context.Context, address string, tag string)

resultIntf, err := common.HandleThetaRPCResponse(rpcRes, rpcErr, parse)
if err != nil {
return result, err
return result, nil
}

result = resultIntf.(string)
Expand Down
8 changes: 3 additions & 5 deletions rpc/ethrpc/eth_get_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ type EthGetLogsResult struct {

// ------------------------------- eth_getLogs -----------------------------------

//
// Reference: https://docs.alchemy.com/alchemy/guides/eth_getlogs
//
func (e *EthRPCService) GetLogs(ctx context.Context, args EthGetLogsArgs) (result []EthGetLogsResult, err error) {
logger.Infof("eth_getLogs called, fromBlock: %v, toBlock: %v, address: %v, blockHash: %v, topics: %v\n",
args.FromBlock, args.ToBlock, args.Address, args.Blockhash.Hex(), args.Topics)
Expand All @@ -62,12 +60,12 @@ func (e *EthRPCService) GetLogs(ctx context.Context, args EthGetLogsArgs) (resul

addresses, err := parseAddresses(args.Address)
if err != nil {
return result, err
return result, nil
}

topicsFilter, err := parseTopicsFilter(args.Topics)
if err != nil {
return result, err
return result, nil
}

maxRetry := 5
Expand All @@ -78,7 +76,7 @@ func (e *EthRPCService) GetLogs(ctx context.Context, args EthGetLogsArgs) (resul
err = retrieveBlocksByRange(args.FromBlock, args.ToBlock, &blocks, maxRetry)
}
if err != nil {
return result, err
return result, nil
}

queryBlocksTime := time.Since(start)
Expand Down
2 changes: 1 addition & 1 deletion rpc/ethrpc/eth_get_storage_at.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (e *EthRPCService) GetStorageAt(ctx context.Context, address string, storag

resultIntf, err := common.HandleThetaRPCResponse(rpcRes, rpcErr, parse)
if err != nil {
return "", err
return "", nil
}

result = resultIntf.(string)
Expand Down
5 changes: 2 additions & 3 deletions rpc/ethrpc/eth_get_transaction_by_block_hash_and_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ethrpc
import (
"context"
"encoding/json"
"fmt"

"github.com/thetatoken/theta-eth-rpc-adaptor/common"
tcommon "github.com/thetatoken/theta/common"
Expand All @@ -29,7 +28,7 @@ func GetIndexedTransactionFromBlock(rpcRes *rpcc.RPCResponse, rpcErr error, txIn
trpcResult := common.ThetaGetBlockResult{}
json.Unmarshal(jsonBytes, &trpcResult)
if txIndex >= tcommon.JSONUint64(len(trpcResult.Txs)) {
return result, fmt.Errorf("transaction index out of range")
return result, nil
}
result.TransactionIndex = hexutil.Uint64(txIndex)
var objmap map[string]json.RawMessage
Expand Down Expand Up @@ -80,7 +79,7 @@ func GetIndexedTransactionFromBlock(rpcRes *rpcc.RPCResponse, rpcErr error, txIn
}
_, err = common.HandleThetaRPCResponse(rpcRes, rpcErr, parse)
if err != nil {
return result, err
return result, nil
}
return result, nil
}
4 changes: 2 additions & 2 deletions rpc/ethrpc/eth_get_transaction_by_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (e *EthRPCService) GetTransactionByHash(ctx context.Context, hashStr string
resultIntf, err = common.HandleThetaRPCResponse(rpcRes, rpcErr, parse)
if err != nil {
logger.Warnf("eth_getTransactionByHash failed, err: %v", err)
return result, err
return result, nil
}

thetaGetTransactionResult = resultIntf.(trpc.GetTransactionResult)
Expand Down Expand Up @@ -119,7 +119,7 @@ func (e *EthRPCService) GetTransactionByHash(ctx context.Context, hashStr string
}
result.TransactionIndex, err = GetTransactionIndex(result.BlockHash, nativeTxHash, client)
if err != nil {
return result, err
return result, nil
}

//resultJsonBytes, _ := json.MarshalIndent(result, "", " ")
Expand Down
4 changes: 2 additions & 2 deletions rpc/ethrpc/eth_get_transaction_receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (e *EthRPCService) GetTransactionReceipt(ctx context.Context, hashStr strin
resultMsg = resultIntf.(string)
}
logger.Errorf("eth_getTransactionReceipt, err: %v, result: %v", err, resultMsg)
return result, err
return result, nil
}

thetaGetTransactionResult = resultIntf.(trpc.GetTransactionResult)
Expand Down Expand Up @@ -98,7 +98,7 @@ func (e *EthRPCService) GetTransactionReceipt(ctx context.Context, hashStr strin
result.TransactionIndex, result.CumulativeGasUsed, err = GetTransactionIndexAndCumulativeGasUsed(result.BlockHash, result.TxHash, result.Logs, client)
if err != nil {
logger.Errorf("eth_getTransactionReceipt, err: %v, result: %v", err, result)
return nil, err
return nil, nil
}
if thetaGetTransactionResult.Receipt.EvmErr == "" {
result.Status = 1
Expand Down
2 changes: 1 addition & 1 deletion rpc/ethrpc/eth_protocol_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (e *EthRPCService) ProtocolVersion(ctx context.Context) (result string, err

resultIntf, err := common.HandleThetaRPCResponse(rpcRes, rpcErr, parse)
if err != nil {
return "", err
return "", nil
}
result = resultIntf.(string)

Expand Down
2 changes: 1 addition & 1 deletion rpc/ethrpc/eth_send_raw_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (e *EthRPCService) SendRawTransaction(ctx context.Context, txBytes string)
resultIntf, err := common.HandleThetaRPCResponse(rpcRes, rpcErr, parse)
if err != nil {
logger.Errorf("eth_sendRawTransaction, err: %v", err)
return "", err
return "", nil
}
result = resultIntf.(string)

Expand Down
5 changes: 2 additions & 3 deletions rpc/ethrpc/eth_syncing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ethrpc
import (
"context"
"encoding/json"
"fmt"

"github.com/thetatoken/theta-eth-rpc-adaptor/common"

Expand Down Expand Up @@ -39,11 +38,11 @@ func (e *EthRPCService) Syncing(ctx context.Context) (result interface{}, err er

resultIntf, err := common.HandleThetaRPCResponse(rpcRes, rpcErr, parse)
if err != nil {
return "", err
return "", nil
}
thetaSyncingResult, ok := resultIntf.(syncingResultWrapper)
if !ok {
return nil, fmt.Errorf("failed to convert syncingResultWrapper")
return nil, nil
}
if !thetaSyncingResult.syncing {
result = false
Expand Down
5 changes: 2 additions & 3 deletions rpc/netrpc/net_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package netrpc
import (
"context"
"encoding/json"
"fmt"

"github.com/thetatoken/theta-eth-rpc-adaptor/common"
hexutil "github.com/thetatoken/theta/common/hexutil"
Expand Down Expand Up @@ -36,11 +35,11 @@ func (e *NetRPCService) Version(ctx context.Context) (result string, err error)

resultIntf, err := common.HandleThetaRPCResponse(rpcRes, rpcErr, parse)
if err != nil {
return "", err
return "", nil
}
thetaChainIDResult, ok := resultIntf.(chainIDResultWrapper)
if !ok {
return "", fmt.Errorf("failed to convert chainIDResultWrapper")
return "", nil
}

thetaChainID := thetaChainIDResult.chainID
Expand Down