Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Merge branch 'fedekunze/derive-chain-id' of github.com:tharsis/etherm…
Browse files Browse the repository at this point in the history
…int into fedekunze/derive-chain-id
  • Loading branch information
fedekunze committed Sep 21, 2021
2 parents 928b972 + 1bc982b commit 4dc00c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
14 changes: 12 additions & 2 deletions x/evm/keeper/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func DeductTxCostsFromUserBalance(
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(
ctx sdk.Context,
bankKeeper evmtypes.BankKeeper,
Expand All @@ -83,7 +84,16 @@ func CheckSenderBalance(
balance := bankKeeper.GetBalance(ctx, sender, denom)
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(
sdkerrors.Wrapf(
sdkerrors.ErrInsufficientFunds,
Expand Down
13 changes: 12 additions & 1 deletion x/evm/keeper/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
func (suite *KeeperTestSuite) TestCheckSenderBalance() {
hundredInt := sdk.NewInt(100)
zeroInt := sdk.ZeroInt()
oneInt := sdk.NewInt(1)
oneInt := sdk.OneInt()
fiveInt := sdk.NewInt(5)
fiftyInt := sdk.NewInt(50)
negInt := sdk.NewInt(-10)

testCases := []struct {
name string
Expand Down Expand Up @@ -47,6 +48,16 @@ func (suite *KeeperTestSuite) TestCheckSenderBalance() {
accessList: &ethtypes.AccessList{},
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",
to: suite.address.String(),
Expand Down

0 comments on commit 4dc00c3

Please sign in to comment.