evm: check tx cost not negative (#578)

This commit is contained in:
Federico Kunze Küllmer 2021-09-21 12:29:59 +02:00 committed by GitHub
parent 1a01c6a992
commit 20785afeb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -72,7 +72,8 @@ func DeductTxCostsFromUserBalance(
return fees, nil return fees, nil
} }
// CheckSenderBalance validates sender has enough funds to pay for tx cost // CheckSenderBalance validates that the tx cost value is positive and that the
// sender has enough funds to pay for the fees and value of the transaction.
func CheckSenderBalance( func CheckSenderBalance(
ctx sdk.Context, ctx sdk.Context,
bankKeeper evmtypes.BankKeeper, bankKeeper evmtypes.BankKeeper,
@ -83,7 +84,16 @@ func CheckSenderBalance(
balance := bankKeeper.GetBalance(ctx, sender, denom) balance := bankKeeper.GetBalance(ctx, sender, denom)
cost := txData.Cost() cost := txData.Cost()
if balance.Amount.BigInt().Cmp(cost) < 0 { if cost.Sign() < 0 {
return stacktrace.Propagate(
sdkerrors.Wrapf(
sdkerrors.ErrInvalidCoins,
"tx cost (%s%s) is negative and invalid", cost, denom,
),
"tx cost amount should never be negative")
}
if balance.IsNegative() || balance.Amount.BigInt().Cmp(cost) < 0 {
return stacktrace.Propagate( return stacktrace.Propagate(
sdkerrors.Wrapf( sdkerrors.Wrapf(
sdkerrors.ErrInsufficientFunds, sdkerrors.ErrInsufficientFunds,

View File

@ -13,9 +13,10 @@ import (
func (suite *KeeperTestSuite) TestCheckSenderBalance() { func (suite *KeeperTestSuite) TestCheckSenderBalance() {
hundredInt := sdk.NewInt(100) hundredInt := sdk.NewInt(100)
zeroInt := sdk.ZeroInt() zeroInt := sdk.ZeroInt()
oneInt := sdk.NewInt(1) oneInt := sdk.OneInt()
fiveInt := sdk.NewInt(5) fiveInt := sdk.NewInt(5)
fiftyInt := sdk.NewInt(50) fiftyInt := sdk.NewInt(50)
negInt := sdk.NewInt(-10)
testCases := []struct { testCases := []struct {
name string name string
@ -47,6 +48,16 @@ func (suite *KeeperTestSuite) TestCheckSenderBalance() {
accessList: &ethtypes.AccessList{}, accessList: &ethtypes.AccessList{},
expectPass: true, expectPass: true,
}, },
{
name: "negative cost",
to: suite.address.String(),
gasLimit: 1,
gasPrice: &oneInt,
cost: &negInt,
from: suite.address.String(),
accessList: &ethtypes.AccessList{},
expectPass: false,
},
{ {
name: "Higher gas limit, not enough balance", name: "Higher gas limit, not enough balance",
to: suite.address.String(), to: suite.address.String(),