Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
fix nil pointer exception when parameters are nil
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-nguy committed Jun 24, 2021
1 parent 1c06553 commit f7b3ff1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
4 changes: 2 additions & 2 deletions ethereum/rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down Expand Up @@ -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
Expand Down
57 changes: 51 additions & 6 deletions ethereum/rpc/types/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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"
Expand Down Expand Up @@ -68,6 +69,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 {
Expand All @@ -79,16 +94,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(),
Expand All @@ -106,6 +139,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

Expand Down

0 comments on commit f7b3ff1

Please sign in to comment.