Skip to content

Commit

Permalink
fix(rpc/types): catch invalid values in CheckTxFee
Browse files Browse the repository at this point in the history
This change catches invalid values in CheckTxFee
and returns an error for:
* nil gasPrice
* negative gasPrice

which improves the reliability of the system.

Fixes evmos#2413
Fixes evmos#2414
  • Loading branch information
odeke-em committed Mar 14, 2024
1 parent ad1e289 commit 93ad6d0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions rpc/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package types

import (
"context"
"errors"
"fmt"
"math/big"
"strings"
Expand Down Expand Up @@ -253,6 +254,12 @@ func CheckTxFee(gasPrice *big.Int, gas uint64, cap float64) error {
if cap == 0 {
return nil
}
if gasPrice == nil {
return errors.New("gasPrice is nil")
}
if gasPrice.Sign() != 1 {
return errors.New("gasPrice must be positive!")
}
totalfee := new(big.Float).SetInt(new(big.Int).Mul(gasPrice, new(big.Int).SetUint64(gas)))
// 1 evmos in 10^18 aevmos
oneToken := new(big.Float).SetInt(big.NewInt(params.Ether))
Expand Down
17 changes: 17 additions & 0 deletions rpc/types/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package types

import (
"math/big"
"testing"
)

func TestCheckTxFeeInvalidArgs(t *testing.T) {
if err := CheckTxFee(nil, 10, 100); err == nil {
t.Fatal("expecting a non-nil error")
}

gp := big.NewInt(-1)
if err := CheckTxFee(gp, 10, 100); err == nil {
t.Fatal("expecting a non-nil error")
}
}

0 comments on commit 93ad6d0

Please sign in to comment.