From b7f00d5a070c27f0cefedabce4b2621d4645d383 Mon Sep 17 00:00:00 2001 From: Thomas Nguy <81727899+thomas-nguy@users.noreply.github.com> Date: Thu, 24 Jun 2021 16:57:51 +0900 Subject: [PATCH] rpc: fix nil pointer exception when parameters are nil (#180) * fix nil pointer exception when parameters are nil * Update ethereum/rpc/types/types.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> --- ethereum/rpc/namespaces/eth/api.go | 4 +-- ethereum/rpc/types/types.go | 58 ++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/ethereum/rpc/namespaces/eth/api.go b/ethereum/rpc/namespaces/eth/api.go index af45189b..c663f326 100644 --- a/ethereum/rpc/namespaces/eth/api.go +++ b/ethereum/rpc/namespaces/eth/api.go @@ -347,7 +347,7 @@ func (e *PublicAPI) Sign(address common.Address, data hexutil.Bytes) (hexutil.By // SendTransaction sends an Ethereum transaction. func (e *PublicAPI) SendTransaction(args rpctypes.SendTxArgs) (common.Hash, error) { - e.logger.Debugln("eth_sendTransaction", "args", args) + e.logger.Debugln("eth_sendTransaction", "args", args.String()) // Look up the wallet containing the requested signer _, err := e.clientCtx.Keyring.KeyByAddress(sdk.AccAddress(args.From.Bytes())) @@ -493,7 +493,7 @@ func (e *PublicAPI) SendRawTransaction(data hexutil.Bytes) (common.Hash, error) // Call performs a raw contract call. func (e *PublicAPI) Call(args rpctypes.CallArgs, blockNr rpctypes.BlockNumber, _ *rpctypes.StateOverride) (hexutil.Bytes, error) { - e.logger.Debugln("eth_call", "args", args, "block number", blockNr) + e.logger.Debugln("eth_call", "args", args.String(), "block number", blockNr) simRes, err := e.doCall(args, blockNr, big.NewInt(ethermint.DefaultRPCGasLimit)) if err != nil { return []byte{}, err diff --git a/ethereum/rpc/types/types.go b/ethereum/rpc/types/types.go index e3ba37b3..737efd34 100644 --- a/ethereum/rpc/types/types.go +++ b/ethereum/rpc/types/types.go @@ -1,6 +1,8 @@ package types import ( + "fmt" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" ethtypes "github.com/ethereum/go-ethereum/core/types" @@ -68,6 +70,20 @@ type SendTxArgs struct { ChainID *hexutil.Big `json:"chainId,omitempty"` } +// String return the struct in a string format +func (args *SendTxArgs) String() string { + // Todo: There is currently a bug with hexutil.Big when the value its nil, printing would trigger an exception + return fmt.Sprintf("SendTxArgs{From:%v, To:%v, Gas:%v,"+ + " Nonce:%v, Data:%v, Input:%v, AccessList:%v}", + args.From, + args.To, + args.Gas, + args.Nonce, + args.Data, + args.Input, + args.AccessList) +} + // ToTransaction converts the arguments to an ethereum transaction. // This assumes that setTxDefaults has been called. func (args *SendTxArgs) ToTransaction() *evmtypes.MsgEthereumTx { @@ -79,16 +95,34 @@ func (args *SendTxArgs) ToTransaction() *evmtypes.MsgEthereumTx { } data := &evmtypes.TxData{ - To: args.To.Hex(), - ChainID: args.ChainID.ToInt().Bytes(), - Nonce: uint64(*args.Nonce), - GasLimit: uint64(*args.Gas), - GasPrice: args.GasPrice.ToInt().Bytes(), - Amount: args.Value.ToInt().Bytes(), Input: input, Accesses: evmtypes.NewAccessList(args.AccessList), } + if args.ChainID != nil { + data.ChainID = args.ChainID.ToInt().Bytes() + } + + if args.Nonce != nil { + data.Nonce = uint64(*args.Nonce) + } + + if args.Gas != nil { + data.GasLimit = uint64(*args.Gas) + } + + if args.GasPrice != nil { + data.GasPrice = args.GasPrice.ToInt().Bytes() + } + + if args.Value != nil { + data.Amount = args.Value.ToInt().Bytes() + } + + if args.To != nil { + data.To = args.To.Hex() + } + return &evmtypes.MsgEthereumTx{ Data: data, From: args.From.String(), @@ -106,6 +140,18 @@ type CallArgs struct { AccessList *ethtypes.AccessList `json:"accessList"` } +// String return the struct in a string format +func (args *CallArgs) String() string { + // Todo: There is currently a bug with hexutil.Big when the value its nil, printing would trigger an exception + return fmt.Sprintf("SendTxArgs{From:%v, To:%v, Gas:%v,"+ + " Data:%v, AccessList:%v}", + args.From, + args.To, + args.Gas, + args.Data, + args.AccessList) +} + // StateOverride is the collection of overridden accounts. type StateOverride map[common.Address]OverrideAccount