From 9d18414c93f524bd54499c35b9d06af956088d45 Mon Sep 17 00:00:00 2001 From: Thomas Nguy <81727899+thomas-nguy@users.noreply.github.com> Date: Mon, 14 Jun 2021 21:42:34 +0900 Subject: [PATCH] fix: tx amount can be nil or zero (#117) * amount can be nil or zero * fix cost function * fix tests --- x/evm/types/msg.go | 4 +++- x/evm/types/msg_test.go | 2 +- x/evm/types/tx_data.go | 6 +----- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/x/evm/types/msg.go b/x/evm/types/msg.go index 4a8f3eaa..b139755a 100644 --- a/x/evm/types/msg.go +++ b/x/evm/types/msg.go @@ -184,7 +184,9 @@ func (msg *MsgEthereumTx) ChainID() *big.Int { // Cost returns amount + gasprice * gaslimit. func (msg MsgEthereumTx) Cost() *big.Int { total := msg.Fee() - total.Add(total, msg.Data.amount()) + if msg.Data.amount() != nil { + total.Add(total, msg.Data.amount()) + } return total } diff --git a/x/evm/types/msg_test.go b/x/evm/types/msg_test.go index 12cad7c5..da4e8226 100644 --- a/x/evm/types/msg_test.go +++ b/x/evm/types/msg_test.go @@ -75,7 +75,7 @@ func (suite *MsgsTestSuite) TestMsgEthereumTx_ValidateBasic() { {msg: "pass with recipient - AccessList Tx", to: &suite.to, amount: big.NewInt(100), gasPrice: big.NewInt(0), accessList: ðtypes.AccessList{}, chainID: big.NewInt(1), expectPass: true}, {msg: "pass contract - Legacy Tx", to: nil, amount: big.NewInt(100), gasPrice: big.NewInt(100000), expectPass: true}, {msg: "invalid recipient", to: ðcmn.Address{}, amount: big.NewInt(-1), gasPrice: big.NewInt(1000), expectPass: false}, - {msg: "nil amount", to: &suite.to, amount: nil, gasPrice: big.NewInt(1000), expectPass: false}, + {msg: "nil amount", to: &suite.to, amount: nil, gasPrice: big.NewInt(1000), expectPass: true}, {msg: "negative amount", to: &suite.to, amount: big.NewInt(-1), gasPrice: big.NewInt(1000), expectPass: true}, {msg: "nil gas price", to: &suite.to, amount: big.NewInt(100), gasPrice: nil, expectPass: false}, {msg: "negative gas price", to: &suite.to, amount: big.NewInt(100), gasPrice: big.NewInt(-1), expectPass: true}, diff --git a/x/evm/types/tx_data.go b/x/evm/types/tx_data.go index 8d4712a9..bc060abc 100644 --- a/x/evm/types/tx_data.go +++ b/x/evm/types/tx_data.go @@ -180,12 +180,8 @@ func (data TxData) Validate() error { } amount := data.amount() - if amount == nil { - return sdkerrors.Wrap(ErrInvalidAmount, "cannot be nil") - } - // Amount can be 0 - if amount.Sign() == -1 { + if amount != nil && amount.Sign() == -1 { return sdkerrors.Wrapf(ErrInvalidAmount, "amount cannot be negative %s", amount) }