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>
This commit is contained in:
Thomas Nguy 2021-06-24 16:57:51 +09:00 committed by GitHub
parent 1c06553746
commit b7f00d5a07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 8 deletions

View File

@ -347,7 +347,7 @@ func (e *PublicAPI) Sign(address common.Address, data hexutil.Bytes) (hexutil.By
// SendTransaction sends an Ethereum transaction. // SendTransaction sends an Ethereum transaction.
func (e *PublicAPI) SendTransaction(args rpctypes.SendTxArgs) (common.Hash, error) { 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 // Look up the wallet containing the requested signer
_, err := e.clientCtx.Keyring.KeyByAddress(sdk.AccAddress(args.From.Bytes())) _, 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. // Call performs a raw contract call.
func (e *PublicAPI) Call(args rpctypes.CallArgs, blockNr rpctypes.BlockNumber, _ *rpctypes.StateOverride) (hexutil.Bytes, error) { 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)) simRes, err := e.doCall(args, blockNr, big.NewInt(ethermint.DefaultRPCGasLimit))
if err != nil { if err != nil {
return []byte{}, err return []byte{}, err

View File

@ -1,6 +1,8 @@
package types package types
import ( import (
"fmt"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types" ethtypes "github.com/ethereum/go-ethereum/core/types"
@ -68,6 +70,20 @@ type SendTxArgs struct {
ChainID *hexutil.Big `json:"chainId,omitempty"` 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. // ToTransaction converts the arguments to an ethereum transaction.
// This assumes that setTxDefaults has been called. // This assumes that setTxDefaults has been called.
func (args *SendTxArgs) ToTransaction() *evmtypes.MsgEthereumTx { func (args *SendTxArgs) ToTransaction() *evmtypes.MsgEthereumTx {
@ -79,16 +95,34 @@ func (args *SendTxArgs) ToTransaction() *evmtypes.MsgEthereumTx {
} }
data := &evmtypes.TxData{ 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, Input: input,
Accesses: evmtypes.NewAccessList(args.AccessList), 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{ return &evmtypes.MsgEthereumTx{
Data: data, Data: data,
From: args.From.String(), From: args.From.String(),
@ -106,6 +140,18 @@ type CallArgs struct {
AccessList *ethtypes.AccessList `json:"accessList"` 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. // StateOverride is the collection of overridden accounts.
type StateOverride map[common.Address]OverrideAccount type StateOverride map[common.Address]OverrideAccount