2023-02-22 23:40:56 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-04-06 17:51:13 +00:00
|
|
|
"math/big"
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-04-06 17:51:13 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-02-22 23:40:56 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2023-06-30 05:30:02 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2024-04-24 07:10:22 +00:00
|
|
|
"github.com/0glabs/0g-chain/app"
|
|
|
|
"github.com/0glabs/0g-chain/tests/e2e/runner"
|
|
|
|
"github.com/0glabs/0g-chain/tests/util"
|
2023-02-22 23:40:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
FundedAccountName = "whale"
|
2023-03-02 01:05:53 +00:00
|
|
|
// use coin type 60 so we are compatible with accounts from `kava add keys --eth <name>`
|
|
|
|
// these accounts use the ethsecp256k1 signing algorithm that allows the signing client
|
|
|
|
// to manage both sdk & evm txs.
|
|
|
|
Bip44CoinType = 60
|
2023-03-07 22:37:45 +00:00
|
|
|
|
|
|
|
IbcPort = "transfer"
|
|
|
|
IbcChannel = "channel-0"
|
2023-02-22 23:40:56 +00:00
|
|
|
)
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// DeployedErc20 is a type that wraps the details of the pre-deployed erc20 used by the e2e test suite.
|
2024-04-24 07:10:22 +00:00
|
|
|
// The Address comes from SuiteConfig.ZgchainERC20Address
|
2023-06-26 22:03:51 +00:00
|
|
|
// The CosmosDenom is fetched from the EnabledConversionPairs param of x/evmutil.
|
|
|
|
// The tests expect the following:
|
|
|
|
// - the funded account has a nonzero balance of the erc20
|
|
|
|
// - the erc20 is enabled for conversion to sdk.Coin
|
2023-08-02 21:52:48 +00:00
|
|
|
// - the corresponding sdk.Coin is enabled as a cdp collateral type
|
2023-06-26 22:03:51 +00:00
|
|
|
// These requirements are checked in InitKavaEvmData().
|
|
|
|
type DeployedErc20 struct {
|
|
|
|
Address common.Address
|
|
|
|
CosmosDenom string
|
2023-08-02 21:52:48 +00:00
|
|
|
|
|
|
|
CdpCollateralType string
|
2023-06-26 22:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// E2eTestSuite is a testify test suite for running end-to-end integration tests on Kava.
|
2023-02-22 23:40:56 +00:00
|
|
|
type E2eTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
config SuiteConfig
|
|
|
|
runner runner.NodeRunner
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
Kava *Chain
|
|
|
|
Ibc *Chain
|
2023-03-28 22:32:36 +00:00
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
UpgradeHeight int64
|
|
|
|
DeployedErc20 DeployedErc20
|
2023-06-30 05:30:02 +00:00
|
|
|
|
|
|
|
cost costSummary
|
|
|
|
enableRefunds bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// costSummary wraps info about what funds get irrecoverably spent by the test suite run
|
|
|
|
type costSummary struct {
|
|
|
|
sdkAddress string
|
|
|
|
evmAddress string
|
|
|
|
|
|
|
|
erc20BalanceBefore *big.Int
|
|
|
|
erc20BalanceAfter *big.Int
|
|
|
|
|
|
|
|
sdkBalanceBefore sdk.Coins
|
|
|
|
sdkBalanceAfter sdk.Coins
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements fmt.Stringer
|
|
|
|
func (s costSummary) String() string {
|
|
|
|
before := sdk.NewCoins(s.sdkBalanceBefore...).Add(sdk.NewCoin("erc20", sdkmath.NewIntFromBigInt(s.erc20BalanceBefore)))
|
|
|
|
after := sdk.NewCoins(s.sdkBalanceAfter...).Add(sdk.NewCoin("erc20", sdkmath.NewIntFromBigInt(s.erc20BalanceAfter)))
|
|
|
|
cost, _ := before.SafeSub(after...)
|
|
|
|
return fmt.Sprintf("Cost Summary for %s (%s):\nbefore:\n%s\nafter:\n%s\ncost:\n%s\n",
|
|
|
|
s.sdkAddress, s.evmAddress, util.PrettyPrintCoins(before), util.PrettyPrintCoins(after), util.PrettyPrintCoins(cost),
|
|
|
|
)
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// SetupSuite is run before all tests. It initializes chain connections and sets up the
|
|
|
|
// account used for funding accounts in the tests.
|
2023-02-22 23:40:56 +00:00
|
|
|
func (suite *E2eTestSuite) SetupSuite() {
|
2023-03-07 22:37:45 +00:00
|
|
|
var err error
|
2023-03-02 01:05:53 +00:00
|
|
|
fmt.Println("setting up test suite.")
|
2023-02-22 23:40:56 +00:00
|
|
|
app.SetSDKConfig()
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
suiteConfig := ParseSuiteConfig()
|
|
|
|
suite.config = suiteConfig
|
2023-06-26 22:03:51 +00:00
|
|
|
suite.DeployedErc20 = DeployedErc20{
|
2024-04-24 07:10:22 +00:00
|
|
|
Address: common.HexToAddress(suiteConfig.ZgchainERC20Address),
|
2023-08-02 21:52:48 +00:00
|
|
|
// Denom & CdpCollateralType are fetched in InitKavaEvmData()
|
2023-06-26 22:03:51 +00:00
|
|
|
}
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// setup the correct NodeRunner for the given config
|
2023-06-20 16:29:25 +00:00
|
|
|
if suiteConfig.Kvtool != nil {
|
2023-06-26 22:03:51 +00:00
|
|
|
suite.runner = suite.SetupKvtoolNodeRunner()
|
|
|
|
} else if suiteConfig.LiveNetwork != nil {
|
|
|
|
suite.runner = suite.SetupLiveNetworkNodeRunner()
|
|
|
|
} else {
|
|
|
|
panic("expected either kvtool or live network configs to be defined")
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
chains := suite.runner.StartChains()
|
|
|
|
kavachain := chains.MustGetChain("kava")
|
|
|
|
suite.Kava, err = NewChain(suite.T(), kavachain, suiteConfig.FundedAccountMnemonic)
|
2023-02-22 23:40:56 +00:00
|
|
|
if err != nil {
|
|
|
|
suite.runner.Shutdown()
|
2023-03-07 22:37:45 +00:00
|
|
|
suite.T().Fatalf("failed to create kava chain querier: %s", err)
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
if suiteConfig.IncludeIbcTests {
|
|
|
|
ibcchain := chains.MustGetChain("ibc")
|
|
|
|
suite.Ibc, err = NewChain(suite.T(), ibcchain, suiteConfig.FundedAccountMnemonic)
|
|
|
|
if err != nil {
|
|
|
|
suite.runner.Shutdown()
|
|
|
|
suite.T().Fatalf("failed to create ibc chain querier: %s", err)
|
|
|
|
}
|
2023-03-02 01:05:53 +00:00
|
|
|
}
|
2023-04-03 16:58:45 +00:00
|
|
|
|
|
|
|
suite.InitKavaEvmData()
|
2023-06-30 05:30:02 +00:00
|
|
|
|
|
|
|
whale := suite.Kava.GetAccount(FundedAccountName)
|
|
|
|
suite.cost = costSummary{
|
|
|
|
sdkAddress: whale.SdkAddress.String(),
|
|
|
|
evmAddress: whale.EvmAddress.Hex(),
|
|
|
|
sdkBalanceBefore: suite.Kava.QuerySdkForBalances(whale.SdkAddress),
|
|
|
|
erc20BalanceBefore: suite.Kava.GetErc20Balance(suite.DeployedErc20.Address, whale.EvmAddress),
|
|
|
|
}
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// TearDownSuite is run after all tests have run.
|
|
|
|
// In the event of a panic during the tests, it is run after testify recovers.
|
2023-02-22 23:40:56 +00:00
|
|
|
func (suite *E2eTestSuite) TearDownSuite() {
|
2023-03-02 01:05:53 +00:00
|
|
|
fmt.Println("tearing down test suite.")
|
2023-06-26 22:03:51 +00:00
|
|
|
|
2023-06-30 05:30:02 +00:00
|
|
|
whale := suite.Kava.GetAccount(FundedAccountName)
|
|
|
|
|
|
|
|
if suite.enableRefunds {
|
|
|
|
suite.cost.sdkBalanceAfter = suite.Kava.QuerySdkForBalances(whale.SdkAddress)
|
|
|
|
suite.cost.erc20BalanceAfter = suite.Kava.GetErc20Balance(suite.DeployedErc20.Address, whale.EvmAddress)
|
|
|
|
fmt.Println("==BEFORE REFUNDS==")
|
|
|
|
fmt.Println(suite.cost)
|
|
|
|
|
|
|
|
fmt.Println("attempting to return all unused funds")
|
|
|
|
suite.Kava.ReturnAllFunds()
|
|
|
|
|
|
|
|
fmt.Println("==AFTER REFUNDS==")
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate & output cost summary for funded account
|
|
|
|
suite.cost.sdkBalanceAfter = suite.Kava.QuerySdkForBalances(whale.SdkAddress)
|
|
|
|
suite.cost.erc20BalanceAfter = suite.Kava.GetErc20Balance(suite.DeployedErc20.Address, whale.EvmAddress)
|
|
|
|
fmt.Println(suite.cost)
|
|
|
|
|
2023-02-22 23:40:56 +00:00
|
|
|
// close all account request channels
|
2023-03-07 22:37:45 +00:00
|
|
|
suite.Kava.Shutdown()
|
|
|
|
if suite.Ibc != nil {
|
|
|
|
suite.Ibc.Shutdown()
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
// gracefully shutdown docker container(s)
|
2023-03-28 22:32:36 +00:00
|
|
|
suite.runner.Shutdown()
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
2023-03-02 01:05:53 +00:00
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// SetupKvtoolNodeRunner is a helper method for building a KvtoolRunnerConfig from the suite config.
|
|
|
|
func (suite *E2eTestSuite) SetupKvtoolNodeRunner() *runner.KvtoolRunner {
|
|
|
|
// upgrade tests are only supported on kvtool networks
|
|
|
|
suite.UpgradeHeight = suite.config.Kvtool.KavaUpgradeHeight
|
2023-06-30 05:30:02 +00:00
|
|
|
suite.enableRefunds = false
|
2023-06-26 22:03:51 +00:00
|
|
|
|
|
|
|
runnerConfig := runner.KvtoolRunnerConfig{
|
|
|
|
KavaConfigTemplate: suite.config.Kvtool.KavaConfigTemplate,
|
|
|
|
|
|
|
|
IncludeIBC: suite.config.IncludeIbcTests,
|
|
|
|
ImageTag: "local",
|
|
|
|
|
|
|
|
EnableAutomatedUpgrade: suite.config.Kvtool.IncludeAutomatedUpgrade,
|
|
|
|
KavaUpgradeName: suite.config.Kvtool.KavaUpgradeName,
|
|
|
|
KavaUpgradeHeight: suite.config.Kvtool.KavaUpgradeHeight,
|
|
|
|
KavaUpgradeBaseImageTag: suite.config.Kvtool.KavaUpgradeBaseImageTag,
|
|
|
|
|
|
|
|
SkipShutdown: suite.config.SkipShutdown,
|
|
|
|
}
|
|
|
|
|
|
|
|
return runner.NewKvtoolRunner(runnerConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupLiveNetworkNodeRunner is a helper method for building a LiveNodeRunner from the suite config.
|
|
|
|
func (suite *E2eTestSuite) SetupLiveNetworkNodeRunner() *runner.LiveNodeRunner {
|
|
|
|
// live network setup doesn't presently support ibc
|
|
|
|
if suite.config.IncludeIbcTests {
|
|
|
|
panic("ibc tests not supported for live network configuration")
|
|
|
|
}
|
2023-10-31 23:37:40 +00:00
|
|
|
|
|
|
|
// Manually set upgrade height for live networks
|
|
|
|
suite.UpgradeHeight = suite.config.LiveNetwork.UpgradeHeight
|
2023-06-30 05:30:02 +00:00
|
|
|
suite.enableRefunds = true
|
2023-06-26 22:03:51 +00:00
|
|
|
|
|
|
|
runnerConfig := runner.LiveNodeRunnerConfig{
|
|
|
|
KavaRpcUrl: suite.config.LiveNetwork.KavaRpcUrl,
|
|
|
|
KavaGrpcUrl: suite.config.LiveNetwork.KavaGrpcUrl,
|
|
|
|
KavaEvmRpcUrl: suite.config.LiveNetwork.KavaEvmRpcUrl,
|
2023-10-31 23:37:40 +00:00
|
|
|
UpgradeHeight: suite.config.LiveNetwork.UpgradeHeight,
|
2023-06-26 22:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return runner.NewLiveNodeRunner(runnerConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SkipIfIbcDisabled should be called at the start of tests that require IBC.
|
|
|
|
// It gracefully skips the current test if IBC tests are disabled.
|
2023-03-07 22:37:45 +00:00
|
|
|
func (suite *E2eTestSuite) SkipIfIbcDisabled() {
|
|
|
|
if !suite.config.IncludeIbcTests {
|
|
|
|
suite.T().SkipNow()
|
|
|
|
}
|
2023-03-02 01:05:53 +00:00
|
|
|
}
|
2023-03-28 22:32:36 +00:00
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// SkipIfUpgradeDisabled should be called at the start of tests that require automated upgrades.
|
|
|
|
// It gracefully skips the current test if upgrades are dissabled.
|
|
|
|
// Note: automated upgrade tests are currently only enabled for Kvtool suite runs.
|
2023-03-28 22:32:36 +00:00
|
|
|
func (suite *E2eTestSuite) SkipIfUpgradeDisabled() {
|
2023-10-31 23:37:40 +00:00
|
|
|
if suite.config.Kvtool != nil && !suite.config.Kvtool.IncludeAutomatedUpgrade {
|
|
|
|
fmt.Println("skipping upgrade test, kvtool automated upgrade is disabled")
|
|
|
|
suite.T().SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there isn't a manual upgrade height set in the config, skip the test
|
|
|
|
if suite.config.LiveNetwork != nil && suite.config.LiveNetwork.UpgradeHeight == 0 {
|
|
|
|
fmt.Println("skipping upgrade test, live network upgrade height is not set")
|
2023-03-28 22:32:36 +00:00
|
|
|
suite.T().SkipNow()
|
|
|
|
}
|
|
|
|
}
|
2023-03-31 17:30:37 +00:00
|
|
|
|
2023-10-31 23:37:40 +00:00
|
|
|
// SkipIfKvtoolDisabled should be called at the start of tests that require kvtool.
|
|
|
|
func (suite *E2eTestSuite) SkipIfKvtoolDisabled() {
|
|
|
|
if suite.config.Kvtool == nil {
|
|
|
|
suite.T().SkipNow()
|
|
|
|
}
|
2023-03-31 17:30:37 +00:00
|
|
|
}
|
2023-04-06 17:51:13 +00:00
|
|
|
|
|
|
|
// BigIntsEqual is a helper method for comparing the equality of two big ints
|
|
|
|
func (suite *E2eTestSuite) BigIntsEqual(expected *big.Int, actual *big.Int, msg string) {
|
|
|
|
suite.Truef(expected.Cmp(actual) == 0, "%s (expected: %s, actual: %s)", msg, expected.String(), actual.String())
|
|
|
|
}
|