Skip to content

Commit

Permalink
test(e2e): eliminate panic usage across testing suite (#2368)
Browse files Browse the repository at this point in the history
* Change runner receiver name to `r`

* Remove panics from e2e runner

* Remove panics from e2e tests

* Remove panics from wait* helpers; minor improvements

* Remove panics from "routines"

* Remove panics from zetae2e cmd

* Update changelog

* Lint

* Address PR comments [1]

* Address PR comments [2]

* Lint

* Fix conflict
  • Loading branch information
swift1337 authored Jun 24, 2024
1 parent bae5b6a commit 72dc5f0
Show file tree
Hide file tree
Showing 80 changed files with 1,831 additions and 2,980 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@
* [2299](https://github.com/zeta-chain/node/pull/2299) - add `zetae2e` command to deploy test contracts
* [2360](https://github.com/zeta-chain/node/pull/2360) - add stateful e2e tests.
* [2349](https://github.com/zeta-chain/node/pull/2349) - add TestBitcoinDepositRefund and WithdrawBitcoinMultipleTimes E2E tests
* [2368](https://github.com/zeta-chain/node/pull/2368) - eliminate panic usage across testing suite
* [2369](https://github.com/zeta-chain/node/pull/2369) - fix random cross-chain swap failure caused by using tiny UTXO


### Fixes

* [1484](https://github.com/zeta-chain/node/issues/1484) - replaced hard-coded `MaxLookaheadNonce` with a default lookback factor
Expand Down
2 changes: 1 addition & 1 deletion cmd/zetaclientd/gen_pre_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var GenPrePramsCmd = &cobra.Command{
Use: "gen-pre-params <path>",
Short: "Generate pre parameters for TSS",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
startTime := time.Now()
preParams, err := keygen.GeneratePreParams(time.Second * 300)
if err != nil {
Expand Down
12 changes: 0 additions & 12 deletions cmd/zetae2e/local/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package local

import (
"fmt"
"runtime"
"time"

"github.com/fatih/color"
Expand All @@ -20,17 +19,6 @@ func adminTestRoutine(
testNames ...string,
) func() error {
return func() (err error) {
// return an error on panic
// https://github.com/zeta-chain/node/issues/1500
defer func() {
if r := recover(); r != nil {
// print stack trace
stack := make([]byte, 4096)
n := runtime.Stack(stack, false)
err = fmt.Errorf("admin panic: %v, stack trace %s", r, stack[:n])
}
}()

// initialize runner for erc20 advanced test
adminRunner, err := initTestRunner(
"admin",
Expand Down
13 changes: 0 additions & 13 deletions cmd/zetae2e/local/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package local

import (
"fmt"
"runtime"
"time"

"github.com/fatih/color"
Expand All @@ -22,18 +21,6 @@ func bitcoinTestRoutine(
testNames ...string,
) func() error {
return func() (err error) {
// return an error on panic
// TODO: remove and instead return errors in the tests
// https://github.com/zeta-chain/node/issues/1500
defer func() {
if r := recover(); r != nil {
// print stack trace
stack := make([]byte, 4096)
n := runtime.Stack(stack, false)
err = fmt.Errorf("bitcoin panic: %v, stack trace %s", r, stack[:n])
}
}()

// initialize runner for bitcoin test
bitcoinRunner, err := initTestRunner(
"bitcoin",
Expand Down
13 changes: 0 additions & 13 deletions cmd/zetae2e/local/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package local

import (
"fmt"
"runtime"
"time"

"github.com/fatih/color"
Expand All @@ -20,18 +19,6 @@ func erc20TestRoutine(
testNames ...string,
) func() error {
return func() (err error) {
// return an error on panic
// TODO: remove and instead return errors in the tests
// https://github.com/zeta-chain/node/issues/1500
defer func() {
if r := recover(); r != nil {
// print stack trace
stack := make([]byte, 4096)
n := runtime.Stack(stack, false)
err = fmt.Errorf("erc20 panic: %v, stack trace %s", r, stack[:n])
}
}()

// initialize runner for erc20 test
erc20Runner, err := initTestRunner(
"erc20",
Expand Down
13 changes: 0 additions & 13 deletions cmd/zetae2e/local/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package local

import (
"fmt"
"runtime"
"time"

"github.com/fatih/color"
Expand All @@ -21,18 +20,6 @@ func ethereumTestRoutine(
testNames ...string,
) func() error {
return func() (err error) {
// return an error on panic
// TODO: remove and instead return errors in the tests
// https://github.com/zeta-chain/node/issues/1500
defer func() {
if r := recover(); r != nil {
// print stack trace
stack := make([]byte, 4096)
n := runtime.Stack(stack, false)
err = fmt.Errorf("ethereum panic: %v, stack trace %s", r, stack[:n])
}
}()

// initialize runner for ether test
ethereumRunner, err := initTestRunner(
"ether",
Expand Down
120 changes: 36 additions & 84 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package local

import (
"context"
"fmt"
"os"
"path/filepath"
"time"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/zeta-chain/zetacore/e2e/txserver"
"github.com/zeta-chain/zetacore/e2e/utils"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/testutil"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
)

Expand All @@ -42,6 +42,8 @@ var (
TestTimeout = 15 * time.Minute
)

var noError = testutil.NoError

// NewLocalCmd returns the local command
// which runs the E2E tests locally on the machine with localnet for each blockchain
func NewLocalCmd() *cobra.Command {
Expand Down Expand Up @@ -70,58 +72,21 @@ func NewLocalCmd() *cobra.Command {

func localE2ETest(cmd *cobra.Command, _ []string) {
// fetch flags
waitForHeight, err := cmd.Flags().GetInt64(flagWaitForHeight)
if err != nil {
panic(err)
}
contractsDeployed, err := cmd.Flags().GetBool(flagContractsDeployed)
if err != nil {
panic(err)
}
verbose, err := cmd.Flags().GetBool(flagVerbose)
if err != nil {
panic(err)
}
configOut, err := cmd.Flags().GetString(flagConfigOut)
if err != nil {
panic(err)
}
testAdmin, err := cmd.Flags().GetBool(flagTestAdmin)
if err != nil {
panic(err)
}
testPerformance, err := cmd.Flags().GetBool(flagTestPerformance)
if err != nil {
panic(err)
}
testCustom, err := cmd.Flags().GetBool(flagTestCustom)
if err != nil {
panic(err)
}
skipRegular, err := cmd.Flags().GetBool(flagSkipRegular)
if err != nil {
panic(err)
}
light, err := cmd.Flags().GetBool(flagLight)
if err != nil {
panic(err)
}
setupOnly, err := cmd.Flags().GetBool(flagSetupOnly)
if err != nil {
panic(err)
}
skipSetup, err := cmd.Flags().GetBool(flagSkipSetup)
if err != nil {
panic(err)
}
skipBitcoinSetup, err := cmd.Flags().GetBool(flagSkipBitcoinSetup)
if err != nil {
panic(err)
}
skipHeaderProof, err := cmd.Flags().GetBool(flagSkipHeaderProof)
if err != nil {
panic(err)
}
var (
waitForHeight = must(cmd.Flags().GetInt64(flagWaitForHeight))
contractsDeployed = must(cmd.Flags().GetBool(flagContractsDeployed))
verbose = must(cmd.Flags().GetBool(flagVerbose))
configOut = must(cmd.Flags().GetString(flagConfigOut))
testAdmin = must(cmd.Flags().GetBool(flagTestAdmin))
testPerformance = must(cmd.Flags().GetBool(flagTestPerformance))
testCustom = must(cmd.Flags().GetBool(flagTestCustom))
skipRegular = must(cmd.Flags().GetBool(flagSkipRegular))
light = must(cmd.Flags().GetBool(flagLight))
setupOnly = must(cmd.Flags().GetBool(flagSetupOnly))
skipSetup = must(cmd.Flags().GetBool(flagSkipSetup))
skipBitcoinSetup = must(cmd.Flags().GetBool(flagSkipBitcoinSetup))
skipHeaderProof = must(cmd.Flags().GetBool(flagSkipHeaderProof))
)

logger := runner.NewLogger(verbose, color.FgWhite, "setup")

Expand All @@ -146,16 +111,14 @@ func localE2ETest(cmd *cobra.Command, _ []string) {

// initialize tests config
conf, err := GetConfig(cmd)
if err != nil {
panic(err)
}
noError(err)

// initialize context
ctx, cancel := context.WithCancel(context.Background())

// wait for a specific height on ZetaChain
if waitForHeight != 0 {
utils.WaitForBlockHeight(ctx, waitForHeight, conf.RPCs.ZetaCoreRPC, logger)
noError(utils.WaitForBlockHeight(ctx, waitForHeight, conf.RPCs.ZetaCoreRPC, logger))
}

// set account prefix to zeta
Expand All @@ -167,9 +130,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
[]string{UserFungibleAdminPrivateKey},
conf.ZetaChainID,
)
if err != nil {
panic(fmt.Errorf("failed to initialize ZetaChain tx server: %w", err))
}
noError(err)

// initialize deployer runner with config
deployerRunner, err := zetae2econfig.RunnerFromConfig(
Expand All @@ -182,9 +143,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
logger,
runner.WithZetaTxServer(zetaTxServer),
)
if err != nil {
panic(err)
}
noError(err)

// wait for keygen to be completed
// if setup is skipped, we assume that the keygen is already completed
Expand All @@ -193,16 +152,13 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
}

// query and set the TSS
if err := deployerRunner.SetTSSAddresses(); err != nil {
panic(err)
}
noError(deployerRunner.SetTSSAddresses())

if !skipHeaderProof {
if err := deployerRunner.EnableHeaderVerification([]int64{
noError(deployerRunner.EnableHeaderVerification([]int64{
chains.GoerliLocalnet.ChainId,
chains.BitcoinRegtest.ChainId}); err != nil {
panic(err)
}
chains.BitcoinRegtest.ChainId,
}))
}

// setting up the networks
Expand All @@ -212,30 +168,22 @@ func localE2ETest(cmd *cobra.Command, _ []string) {

deployerRunner.SetupEVM(contractsDeployed, true)
deployerRunner.SetZEVMContracts()

// NOTE: this method return an error so we handle it and panic if it occurs unlike other method that panics directly
// TODO: all methods should return errors instead of panicking and this current function should also return an error
// https://github.com/zeta-chain/node/issues/1500
if err := deployerRunner.FundEmissionsPool(); err != nil {
panic(err)
}
noError(deployerRunner.FundEmissionsPool())

deployerRunner.MintERC20OnEvm(10000)

logger.Print("✅ setup completed in %s", time.Since(startTime))
}

// if a config output is specified, write the config
if configOut != "" {
newConfig := zetae2econfig.ExportContractsFromRunner(deployerRunner, conf)
configOut, err := filepath.Abs(configOut)
if err != nil {
panic(err)
}

// write config into stdout
if err := config.WriteConfig(configOut, newConfig); err != nil {
panic(err)
}
configOut, err := filepath.Abs(configOut)
noError(err)

noError(config.WriteConfig(configOut, newConfig))

logger.Print("✅ config file written in %s", configOut)
}
Expand Down Expand Up @@ -407,3 +355,7 @@ func waitKeygenHeight(
logger.Info("Last ZetaHeight: %d", response.Height)
}
}

func must[T any](v T, err error) T {
return testutil.Must(v, err)
}
13 changes: 0 additions & 13 deletions cmd/zetae2e/local/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package local

import (
"fmt"
"runtime"
"time"

"github.com/fatih/color"
Expand All @@ -20,18 +19,6 @@ func miscTestRoutine(
testNames ...string,
) func() error {
return func() (err error) {
// return an error on panic
// TODO: remove and instead return errors in the tests
// https://github.com/zeta-chain/node/issues/1500
defer func() {
if r := recover(); r != nil {
// print stack trace
stack := make([]byte, 4096)
n := runtime.Stack(stack, false)
err = fmt.Errorf("misc panic: %v, stack trace %s", r, stack[:n])
}
}()

// initialize runner for misc test
miscRunner, err := initTestRunner(
"misc",
Expand Down
Loading

0 comments on commit 72dc5f0

Please sign in to comment.