fix: tx amount can be nil or zero (#117)

* amount can be nil or zero

* fix cost function

* fix tests
This commit is contained in:
Thomas Nguy 2021-06-14 21:42:34 +09:00 committed by GitHub
parent f762087d36
commit 9d18414c93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 7 deletions

View File

@ -184,7 +184,9 @@ func (msg *MsgEthereumTx) ChainID() *big.Int {
// Cost returns amount + gasprice * gaslimit. // Cost returns amount + gasprice * gaslimit.
func (msg MsgEthereumTx) Cost() *big.Int { func (msg MsgEthereumTx) Cost() *big.Int {
total := msg.Fee() total := msg.Fee()
total.Add(total, msg.Data.amount()) if msg.Data.amount() != nil {
total.Add(total, msg.Data.amount())
}
return total return total
} }

View File

@ -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 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: "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: "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: "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: "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}, {msg: "negative gas price", to: &suite.to, amount: big.NewInt(100), gasPrice: big.NewInt(-1), expectPass: true},

View File

@ -180,12 +180,8 @@ func (data TxData) Validate() error {
} }
amount := data.amount() amount := data.amount()
if amount == nil {
return sdkerrors.Wrap(ErrInvalidAmount, "cannot be nil")
}
// Amount can be 0 // Amount can be 0
if amount.Sign() == -1 { if amount != nil && amount.Sign() == -1 {
return sdkerrors.Wrapf(ErrInvalidAmount, "amount cannot be negative %s", amount) return sdkerrors.Wrapf(ErrInvalidAmount, "amount cannot be negative %s", amount)
} }