Skip to content

Commit

Permalink
conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Oct 3, 2023
2 parents 958c0ab + 4de526b commit 2f3c369
Show file tree
Hide file tree
Showing 26 changed files with 1,698 additions and 169 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
run: rm -rf *

smoke-test:
runs-on: ["zeta-runners"] # 20 minutes
runs-on: ["zeta-runners"] # 25 minutes
timeout-minutes: 25
steps:
- uses: actions/checkout@v3
Expand Down
3 changes: 3 additions & 0 deletions contrib/localnet/orchestrator/smoketest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ func LocalSmokeTest(_ *cobra.Command, _ []string) {
smokeTest.TestUpdateBytecode()
smokeTest.CheckZRC20ReserveAndSupply()

smokeTest.TestDepositEtherLiquidityCap()
smokeTest.CheckZRC20ReserveAndSupply()

// add your dev test here
smokeTest.TestMyTest()

Expand Down
110 changes: 107 additions & 3 deletions contrib/localnet/orchestrator/smoketest/test_deposit_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (
"math/big"
"time"

"github.com/zeta-chain/zetacore/common/ethereum"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"

"cosmossdk.io/math"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethcommon "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
zrc20 "github.com/zeta-chain/protocol-contracts/pkg/contracts/zevm/zrc20.sol"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/common/ethereum"
"github.com/zeta-chain/zetacore/x/crosschain/types"
fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/zetaclient"
)

Expand Down Expand Up @@ -285,6 +286,109 @@ func (sm *SmokeTest) TestDepositAndCallRefund() {
}()
}

// TestDepositEtherLiquidityCap tests depositing Ethers in a context where a liquidity cap is set
func (sm *SmokeTest) TestDepositEtherLiquidityCap() {
startTime := time.Now()
defer func() {
fmt.Printf("test finishes in %s\n", time.Since(startTime))
}()
LoudPrintf("Deposit Ethers into ZEVM with a liquidity cap\n")

supply, err := sm.ETHZRC20.TotalSupply(&bind.CallOpts{})
if err != nil {
panic(err)
}

// Set a liquidity cap slightly above the current supply
fmt.Println("Setting a liquidity cap")
liquidityCap := math.NewUintFromBigInt(supply).Add(math.NewUint(1e16))
msg := fungibletypes.NewMsgUpdateZRC20LiquidityCap(
FungibleAdminAddress,
sm.ETHZRC20Addr.Hex(),
liquidityCap,
)
res, err := sm.zetaTxServer.BroadcastTx(FungibleAdminName, msg)
if err != nil {
panic(err)
}
fmt.Printf("set liquidity cap tx hash: %s\n", res.TxHash)

fmt.Println("Depositing more than liquidity cap should make cctx reverted")
signedTx, err := sm.SendEther(TSSAddress, big.NewInt(1e17), nil)
if err != nil {
panic(err)
}
receipt := MustWaitForTxReceipt(sm.goerliClient, signedTx)
if receipt.Status == 0 {
panic("deposit eth tx failed")
}
cctx := WaitCctxMinedByInTxHash(signedTx.Hash().Hex(), sm.cctxClient)
if cctx.CctxStatus.Status != types.CctxStatus_Reverted {
panic(fmt.Sprintf("expected cctx status to be Reverted; got %s", cctx.CctxStatus.Status))
}
fmt.Println("CCTX has been reverted")

fmt.Println("Depositing less than liquidity cap should still succeed")
initialBal, err := sm.ETHZRC20.BalanceOf(&bind.CallOpts{}, DeployerAddress)
if err != nil {
panic(err)
}
signedTx, err = sm.SendEther(TSSAddress, big.NewInt(1e15), nil)
if err != nil {
panic(err)
}
receipt = MustWaitForTxReceipt(sm.goerliClient, signedTx)
if receipt.Status == 0 {
panic("deposit eth tx failed")
}
WaitCctxMinedByInTxHash(signedTx.Hash().Hex(), sm.cctxClient)
expectedBalance := big.NewInt(0).Add(initialBal, big.NewInt(1e15))

bal, err := sm.ETHZRC20.BalanceOf(&bind.CallOpts{}, DeployerAddress)
if err != nil {
panic(err)
}
if bal.Cmp(expectedBalance) != 0 {
panic(fmt.Sprintf("expected balance to be %s; got %s", expectedBalance.String(), bal.String()))
}
fmt.Println("Deposit succeeded")

fmt.Println("Removing the liquidity cap")
msg = fungibletypes.NewMsgUpdateZRC20LiquidityCap(
FungibleAdminAddress,
sm.ETHZRC20Addr.Hex(),
math.ZeroUint(),
)
res, err = sm.zetaTxServer.BroadcastTx(FungibleAdminName, msg)
if err != nil {
panic(err)
}
fmt.Printf("remove liquidity cap tx hash: %s\n", res.TxHash)
initialBal, err = sm.ETHZRC20.BalanceOf(&bind.CallOpts{}, DeployerAddress)
if err != nil {
panic(err)
}
signedTx, err = sm.SendEther(TSSAddress, big.NewInt(1e17), nil)
if err != nil {
panic(err)
}
receipt = MustWaitForTxReceipt(sm.goerliClient, signedTx)
if receipt.Status == 0 {
panic("deposit eth tx failed")
}
WaitCctxMinedByInTxHash(signedTx.Hash().Hex(), sm.cctxClient)
expectedBalance = big.NewInt(0).Add(initialBal, big.NewInt(1e17))

bal, err = sm.ETHZRC20.BalanceOf(&bind.CallOpts{}, DeployerAddress)
if err != nil {
panic(err)
}
if bal.Cmp(expectedBalance) != 0 {
panic(fmt.Sprintf("expected balance to be %s; got %s", expectedBalance.String(), bal.String()))
}
fmt.Println("New deposit succeeded")
}

func (sm *SmokeTest) SendEther(to ethcommon.Address, value *big.Int, data []byte) (*ethtypes.Transaction, error) {
goerliClient := sm.goerliClient

Expand Down
4 changes: 3 additions & 1 deletion contrib/localnet/orchestrator/smoketest/test_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func (sm *SmokeTest) DepositERC20(amount *big.Int, msg []byte) ethcommon.Hash {
panic(err)
}
receipt = MustWaitForTxReceipt(sm.goerliClient, tx)
if receipt.Status == 0 {
panic("deposit failed")
}
fmt.Printf("Deposit receipt tx hash: %s, status %d\n", receipt.TxHash.Hex(), receipt.Status)
for _, log := range receipt.Logs {
event, err := sm.ERC20Custody.ParseDeposited(*log)
Expand All @@ -83,7 +86,6 @@ func (sm *SmokeTest) DepositERC20(amount *big.Int, msg []byte) ethcommon.Hash {
}
fmt.Printf("gas limit %d\n", sm.zevmAuth.GasLimit)
return tx.Hash()
//WaitCctxMinedByInTxHash(tx.Hash().Hex(), sm.cctxClient)
}

func (sm *SmokeTest) TestERC20Withdraw() {
Expand Down
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")
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
}
}
4 changes: 4 additions & 0 deletions docs/openapi/openapi.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50783,6 +50783,8 @@ definitions:
format: uint64
paused:
type: boolean
liquidity_cap:
type: string
fungibleMsgDeployFungibleCoinZRC20Response:
type: object
properties:
Expand All @@ -50798,6 +50800,8 @@ definitions:
format: byte
fungibleMsgUpdateSystemContractResponse:
type: object
fungibleMsgUpdateZRC20LiquidityCapResponse:
type: object
fungibleMsgUpdateZRC20PausedStatusResponse:
type: object
fungibleMsgUpdateZRC20WithdrawFeeResponse:
Expand Down
32 changes: 22 additions & 10 deletions docs/spec/fungible/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,6 @@ message MsgUpdateSystemContract {
}
```

## MsgUpdateZRC20WithdrawFee

```proto
message MsgUpdateZRC20WithdrawFee {
string creator = 1;
string zrc20_address = 2;
string new_withdraw_fee = 6;
}
```

## MsgUpdateContractBytecode

UpdateContractBytecode updates the bytecode of a contract from the bytecode of an existing contract
Expand All @@ -81,6 +71,16 @@ message MsgUpdateContractBytecode {
}
```

## MsgUpdateZRC20WithdrawFee

```proto
message MsgUpdateZRC20WithdrawFee {
string creator = 1;
string zrc20_address = 2;
string new_withdraw_fee = 6;
}
```

## MsgUpdateZRC20PausedStatus

UpdateZRC20PausedStatus updates the paused status of a ZRC20
Expand All @@ -94,3 +94,15 @@ message MsgUpdateZRC20PausedStatus {
}
```

## MsgUpdateZRC20LiquidityCap

UpdateZRC20LiquidityCap updates the liquidity cap for a ZRC20 token.

```proto
message MsgUpdateZRC20LiquidityCap {
string creator = 1;
string zrc20_address = 2;
string liquidity_cap = 3;
}
```

5 changes: 5 additions & 0 deletions proto/fungible/foreign_coins.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
package zetachain.zetacore.fungible;

import "common/common.proto";
import "gogoproto/gogo.proto";

option go_package = "github.com/zeta-chain/zetacore/x/fungible/types";

Expand All @@ -16,4 +17,8 @@ message ForeignCoins {
common.CoinType coin_type = 8;
uint64 gas_limit = 9;
bool paused = 10;
string liquidity_cap = 11 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
(gogoproto.nullable) = false
];
}
Loading

0 comments on commit 2f3c369

Please sign in to comment.