Skip to content
51 changes: 42 additions & 9 deletions contrib/localnet/orchestrator/smoketest/test_stress.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"context"
"errors"
"fmt"
"math/big"
"os"
Expand All @@ -18,7 +19,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/spf13/cobra"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/protocol-contracts/pkg/contracts/zevm/zrc20.sol"
"github.com/zeta-chain/zetacore/x/crosschain/types"
types2 "github.com/zeta-chain/zetacore/x/crosschain/types"
fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types"
Expand All @@ -28,7 +29,6 @@ import (

const (
StatInterval = 5
WithdrawInterval = 500
StressTestTimeout = 100 * time.Minute
)

Expand All @@ -48,7 +48,8 @@ type stressArguments struct {
zevmURL string
deployerAddress string
deployerPrivateKey string
local bool
network string
txnInterval int64
}

var stressTestArgs = stressArguments{}
Expand All @@ -60,7 +61,8 @@ func init() {
StressCmd.Flags().StringVar(&stressTestArgs.zevmURL, "zevmURL", "http://zetacore0:8545", "--zevmURL http://zetacore0:8545")
StressCmd.Flags().StringVar(&stressTestArgs.deployerAddress, "addr", "0xE5C5367B8224807Ac2207d350E60e1b6F27a7ecC", "--addr <eth address>")
StressCmd.Flags().StringVar(&stressTestArgs.deployerPrivateKey, "privKey", "d87baf7bf6dc560a252596678c12e41f7d1682837f05b29d411bc3f78ae2c263", "--privKey <eth private key>")
StressCmd.Flags().BoolVar(&stressTestArgs.local, "local", true, "--local")
StressCmd.Flags().StringVar(&stressTestArgs.network, "network", "PRIVNET", "--network PRIVNET")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we use privnet naming anywhere else but I think localnet is a more common language to specify the local network

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it isn't the correct language for this case but it is the name of the build flag used for the smoketest and config files.

StressCmd.Flags().Int64Var(&stressTestArgs.txnInterval, "tx-interval", 500, "--tx-interval [TIME_INTERVAL_MILLISECONDS]")

DeployerAddress = ethcommon.HexToAddress(stressTestArgs.deployerAddress)
}
Expand All @@ -85,7 +87,7 @@ func StressTest(_ *cobra.Command, _ []string) {
if err != nil {
panic(err)
}
fmt.Printf("Deployer address: %s, balance: %d Ether\n", DeployerAddress.Hex(), bal.Div(bal, big.NewInt(1e18)))
fmt.Printf("Deployer address: %s, balance: %d Wei\n", DeployerAddress.Hex(), bal)

chainid, err := goerliClient.ChainID(context.Background())
deployerPrivkey, err := crypto.HexToECDSA(stressTestArgs.deployerPrivateKey)
Expand Down Expand Up @@ -158,13 +160,27 @@ func StressTest(_ *cobra.Command, _ []string) {
)

// If stress test is running on local docker environment
if stressTestArgs.local {
if stressTestArgs.network == "PRIVNET" {
smokeTest.TestSetupZetaTokenAndConnectorAndZEVMContracts()
smokeTest.TestDepositEtherIntoZRC20()
smokeTest.TestSendZetaIn()
} else if stressTestArgs.network == "TESTNET" {
ethZRC20Addr, _ := smokeTest.SystemContract.GasCoinZRC20ByChainId(&bind.CallOpts{}, big.NewInt(5))
smokeTest.ETHZRC20Addr = ethZRC20Addr
smokeTest.ETHZRC20, _ = zrc20.NewZRC20(smokeTest.ETHZRC20Addr, smokeTest.zevmClient)
} else {
err := errors.New("invalid network argument: " + stressTestArgs.network)
panic(err)
}

// Check zrc20 balance of Deployer address
ethZRC20Balance, err := smokeTest.ETHZRC20.BalanceOf(nil, DeployerAddress)
if err != nil {
panic(err)
}
fmt.Printf("eth zrc20 balance: %s Wei \n", ethZRC20Balance.String())

//Pre-approve USDT withdraw on ZEVM
//Pre-approve ETH withdraw on ZEVM
fmt.Printf("approving ETH ZRC20...\n")
ethZRC20 := smokeTest.ETHZRC20
tx, err := ethZRC20.Approve(smokeTest.zevmAuth, smokeTest.ETHZRC20Addr, big.NewInt(1e18))
Expand Down Expand Up @@ -197,7 +213,7 @@ func StressTest(_ *cobra.Command, _ []string) {

// WithdrawCCtx withdraw USDT from ZEVM to EVM
func (sm *SmokeTest) WithdrawCCtx() {
ticker := time.NewTicker(time.Millisecond * WithdrawInterval)
ticker := time.NewTicker(time.Millisecond * time.Duration(stressTestArgs.txnInterval))
for {
select {
case <-ticker.C:
Expand All @@ -212,13 +228,14 @@ func (sm *SmokeTest) EchoNetworkMetrics() {
var numTicks = 0
var totalMinedTxns = uint64(0)
var previousMinedTxns = uint64(0)

for {
select {
case <-ticker.C:
numTicks++
// Get all pending outbound transactions
cctxResp, err := sm.cctxClient.CctxAllPending(context.Background(), &types2.QueryAllCctxPendingRequest{
ChainId: common.GoerliChain().ChainId,
ChainId: getChainID(),
})
if err != nil {
continue
Expand All @@ -229,6 +246,8 @@ func (sm *SmokeTest) EchoNetworkMetrics() {
})
if len(sends) > 0 {
fmt.Printf("pending nonces %d to %d\n", sends[0].GetCurrentOutTxParam().OutboundTxTssNonce, sends[len(sends)-1].GetCurrentOutTxParam().OutboundTxTssNonce)
} else {
continue
}
//
// Get all trackers
Expand Down Expand Up @@ -274,3 +293,17 @@ func (sm *SmokeTest) WithdrawETHZRC20() {
panic(err)
}
}

// Get ETH based chain ID - Build flags are conflicting with current lib, need to do this manually
func getChainID() int64 {
switch stressTestArgs.network {
case "PRIVNET":
return 1337
case "TESTNET":
return 5
case "MAINNET":
return 1
default:
return 1337
}
}