2023-03-31 17:30:37 +00:00
|
|
|
package e2e_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
|
2024-08-03 09:51:56 +00:00
|
|
|
"github.com/0glabs/0g-chain/app"
|
|
|
|
"github.com/0glabs/0g-chain/tests/util"
|
2023-03-31 17:30:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (suite *IntegrationTestSuite) TestEthGasPriceReturnsMinFee() {
|
2023-10-31 23:37:40 +00:00
|
|
|
suite.SkipIfKvtoolDisabled()
|
|
|
|
|
2023-03-31 17:30:37 +00:00
|
|
|
// read expected min fee from app.toml
|
2023-10-31 23:37:40 +00:00
|
|
|
minGasPrices, err := getMinFeeFromAppToml(util.KavaHomePath())
|
2023-03-31 17:30:37 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// evm uses akava, get akava min fee
|
|
|
|
evmMinGas := minGasPrices.AmountOf("akava").TruncateInt().BigInt()
|
|
|
|
|
|
|
|
// returns eth_gasPrice, units in kava
|
|
|
|
gasPrice, err := suite.Kava.EvmClient.SuggestGasPrice(context.Background())
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
suite.Equal(evmMinGas, gasPrice)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *IntegrationTestSuite) TestEvmRespectsMinFee() {
|
2023-10-31 23:37:40 +00:00
|
|
|
suite.SkipIfKvtoolDisabled()
|
|
|
|
|
2023-03-31 17:30:37 +00:00
|
|
|
// setup sender & receiver
|
2023-06-26 22:03:51 +00:00
|
|
|
sender := suite.Kava.NewFundedAccount("evm-min-fee-test-sender", sdk.NewCoins(ukava(1e3)))
|
2023-03-31 17:30:37 +00:00
|
|
|
randoReceiver := util.SdkToEvmAddress(app.RandomAddress())
|
|
|
|
|
|
|
|
// get min gas price for evm (from app.toml)
|
2023-10-31 23:37:40 +00:00
|
|
|
minFees, err := getMinFeeFromAppToml(util.KavaHomePath())
|
2023-03-31 17:30:37 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
minGasPrice := minFees.AmountOf("akava").TruncateInt()
|
|
|
|
|
|
|
|
// attempt tx with less than min gas price (min fee - 1)
|
|
|
|
tooLowGasPrice := minGasPrice.Sub(sdk.OneInt()).BigInt()
|
|
|
|
req := util.EvmTxRequest{
|
2023-06-26 22:03:51 +00:00
|
|
|
Tx: ethtypes.NewTransaction(0, randoReceiver, big.NewInt(5e2), 1e5, tooLowGasPrice, nil),
|
2023-03-31 17:30:37 +00:00
|
|
|
Data: "this tx should fail because it's gas price is too low",
|
|
|
|
}
|
|
|
|
res := sender.SignAndBroadcastEvmTx(req)
|
|
|
|
|
|
|
|
// expect the tx to fail!
|
|
|
|
suite.ErrorAs(res.Err, &util.ErrEvmFailedToBroadcast{})
|
2023-04-04 21:22:18 +00:00
|
|
|
suite.ErrorContains(res.Err, "insufficient fee")
|
2023-03-31 17:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getMinFeeFromAppToml(kavaHome string) (sdk.DecCoins, error) {
|
|
|
|
// read the expected min gas price from app.toml
|
|
|
|
parsed := struct {
|
|
|
|
MinGasPrices string `toml:"minimum-gas-prices"`
|
|
|
|
}{}
|
|
|
|
appToml, err := os.ReadFile(filepath.Join(kavaHome, "config", "app.toml"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = toml.Unmarshal(appToml, &parsed)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert to dec coins
|
|
|
|
return sdk.ParseDecCoins(strings.ReplaceAll(parsed.MinGasPrices, ";", ","))
|
|
|
|
}
|