Skip to content

Commit

Permalink
fix: tx amount can be nil or zero (#117)
Browse files Browse the repository at this point in the history
* amount can be nil or zero

* fix cost function

* fix tests
  • Loading branch information
thomas-nguy authored Jun 14, 2021
1 parent f762087 commit 9d18414
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
4 changes: 3 additions & 1 deletion x/evm/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion x/evm/types/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: &ethtypes.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: &ethcmn.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},
Expand Down
6 changes: 1 addition & 5 deletions x/evm/types/tx_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 9d18414

Please sign in to comment.