Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(e2e): TestBitcoinDepositRefund, WithdrawBitcoinMultipleTimes #2349

Merged
merged 22 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,13 @@ start-upgrade-test-light: zetanode-upgrade
@echo "--> Starting light upgrade test (no ZetaChain state populating before upgrade)"
cd contrib/localnet/ && $(DOCKER) compose -f docker-compose.yml -f docker-compose-upgrade.yml -f docker-compose-upgrade-light.yml up -d

start-localnet: zetanode
start-localnet: zetanode start-localnet-skip-build

start-localnet-skip-build:
@echo "--> Starting localnet"
cd contrib/localnet/ && $(DOCKER) compose -f docker-compose.yml -f docker-compose-setup-only.yml up -d

stop-test:
stop-localnet:
cd contrib/localnet/ && $(DOCKER) compose down --remove-orphans

###############################################################################
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* [2266](https://github.com/zeta-chain/node/pull/2266) - try fixing E2E test `crosschain_swap` failure `btc transaction not signed`
* [2294](https://github.com/zeta-chain/node/pull/2294) - add and fix existing ethermint rpc unit test
* [2299](https://github.com/zeta-chain/node/pull/2299) - add `zetae2e` command to deploy test contracts
* [2349](https://github.com/zeta-chain/node/pull/2349) - add TestBitcoinDepositRefund and WithdrawBitcoinMultipleTimes E2E tests

### Fixes

Expand Down
3 changes: 3 additions & 0 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
e2etests.TestMessagePassingEVMtoZEVMRevertFailName,
}
bitcoinTests := []string{
e2etests.TestBitcoinDepositName,
e2etests.TestBitcoinDepositRefundName,
e2etests.TestBitcoinWithdrawSegWitName,
e2etests.TestBitcoinWithdrawInvalidAddressName,
e2etests.TestZetaWithdrawBTCRevertName,
Expand All @@ -286,6 +288,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
bitcoinAdvancedTests := []string{
e2etests.TestBitcoinWithdrawTaprootName,
e2etests.TestBitcoinWithdrawLegacyName,
e2etests.TestBitcoinWithdrawMultipleName,
e2etests.TestBitcoinWithdrawP2SHName,
e2etests.TestBitcoinWithdrawP2WSHName,
e2etests.TestBitcoinWithdrawRestrictedName,
Expand Down
23 changes: 19 additions & 4 deletions cmd/zetae2e/stress.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"
"os"
"sort"
"sync"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -225,11 +226,25 @@ func StressTest(cmd *cobra.Command, _ []string) {
fmt.Println(" 1. Periodically Withdraw ETH from ZEVM to EVM")
fmt.Println(" 2. Display Network metrics to monitor performance [Num Pending outbound tx], [Num Trackers]")

e2eTest.WG.Add(2)
go WithdrawCCtx(e2eTest) // Withdraw from ZEVM to EVM
go EchoNetworkMetrics(e2eTest) // Display Network metrics periodically to monitor performance
var wg sync.WaitGroup

e2eTest.WG.Wait()
wg.Add(2)

go func() {
defer wg.Done()

// Withdraw from ZEVM to EVM
WithdrawCCtx(e2eTest)
}()

go func() {
defer wg.Done()

// Display Network metrics periodically to monitor performance
EchoNetworkMetrics(e2eTest)
}()

wg.Wait()
}

// WithdrawCCtx withdraw ETHZRC20 from ZEVM to EVM
Expand Down
2 changes: 1 addition & 1 deletion docs/development/LOCAL_TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ $ docker logs -f orchestrator

To stop the tests,
```bash
make stop-test
make stop-localnet
```

### Run monitoring setup
Expand Down
37 changes: 31 additions & 6 deletions e2e/README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
# `e2e`

`e2e` is a comprehensive suite of E2E tests designed to validate the integration and functionality of the ZetaChain network, particularly its interactions with Bitcoin and EVM (Ethereum Virtual Machine) networks. This tool is essential for ensuring the robustness and reliability of ZetaChain's cross-chain functionalities.
`e2e` is a comprehensive suite of E2E tests designed to validate the integration and functionality of the ZetaChain
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
network, particularly its interactions with Bitcoin and EVM (Ethereum Virtual Machine) networks. This tool is essential
for ensuring the robustness and reliability of ZetaChain's cross-chain functionalities.

## Packages

The E2E testing project is organized into several packages, each with a specific role:

- `config`: Provides general configuration for E2E tests, including RPC addresses for connected networks, addresses of deployed smart contracts, and account details for test transactions.
- `config`: Provides general configuration for E2E tests, including RPC addresses for connected networks, addresses of
deployed smart contracts, and account details for test transactions.
- `contracts`: Includes sample Solidity smart contracts used in testing scenarios.
- `runner`: Responsible for executing E2E tests, handling interactions with various network clients.
- `e2etests`: Houses a collection of E2E tests that can be run against the ZetaChain network. Each test is implemented as a separate Go file prefixed with `test_`.
- `e2etests`: Houses a collection of E2E tests that can be run against the ZetaChain network. Each test is implemented
as a separate Go file prefixed with `test_`.
- `txserver`: A minimalistic client for interacting with the ZetaChain RPC interface.
- `utils`: Offers utility functions to facilitate interactions with the different blockchain networks involved in testing.
- `utils`: Offers utility functions to facilitate interactions with the different blockchain networks involved in
testing.

## Config

The E2E testing suite utilizes a flexible and comprehensive configuration system defined in the config package, which is central to setting up and customizing your test environments. The configuration is structured as follows:
The E2E testing suite utilizes a flexible and comprehensive configuration system defined in the config package, which is
central to setting up and customizing your test environments. The configuration is structured as follows:

A config YAML file can be provided to the E2E test tool via the `--config` flag. If no config file is provided, the tool will use default values for all configuration parameters.
A config YAML file can be provided to the E2E test tool via the `--config` flag. If no config file is provided, the tool
will use default values for all configuration parameters.

### Config Structure

- `RPCs`: Defines the RPC endpoints for various networks involved in the testing.
- `Contracts`: Specifies the addresses of pre-deployed smart contracts relevant to the tests.
- `ZetaChainID`: The specific chain ID of the ZetaChain network being tested.
Expand All @@ -34,6 +43,7 @@ A config YAML file can be provided to the E2E test tool via the `--config` flag.
### Contracts Configuration:

**EVM Contracts**

- `ZetaEthAddress`: Address of Zeta token contract on EVM chain.
- `ConnectorEthAddr`: Address of a connector contract on EVM chain.
- `ERC20`: Address of the ERC20 token contract on EVM chain.
Expand All @@ -56,3 +66,18 @@ zeta_chain_id: "zetachain-1"
```

NOTE: config is in progress, contracts on the zEVM must be added

## Debugging

It's possible to debug a single test using Delve debugger.

1. Make sure delve is installed. `go install github.com/go-delve/delve/cmd/dlv@latest`
2. Configure your IDE to use Delve as the debugger. For Goland, you can do the following:
- Go to "Run" > "Edit Run Configurations"
- Hit "+" > "Go Remote". Keep port as default (`2345`). Toggle "On Disconnect" > "Stop Delve process"
3. Make sure that localnet is running. For a quick start, you can use `make start-localnet-skip-build`.
Networks need some time to generate blocks.
4. Run test as following: `./e2e/scripts/debug.sh my_test_name arg1 arg2 arg_n`.
Example: `./e2e/scripts/debug.sh bitcoin_withdraw_restricted 0.001`
5. Place a breakpoint in the code.
6. Go to the editor's debug panel and hit "Debug" button.
18 changes: 18 additions & 0 deletions e2e/e2etests/e2etests.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ const (
Test transfer of Bitcoin asset across chains
*/
TestBitcoinDepositName = "bitcoin_deposit"
TestBitcoinDepositRefundName = "bitcoin_deposit_refund"
TestBitcoinWithdrawSegWitName = "bitcoin_withdraw_segwit"
TestBitcoinWithdrawTaprootName = "bitcoin_withdraw_taproot"
TestBitcoinWithdrawMultipleName = "bitcoin_withdraw_multiple"
TestBitcoinWithdrawLegacyName = "bitcoin_withdraw_legacy"
TestBitcoinWithdrawP2WSHName = "bitcoin_withdraw_p2wsh"
TestBitcoinWithdrawP2SHName = "bitcoin_withdraw_p2sh"
Expand Down Expand Up @@ -332,6 +334,13 @@ var AllE2ETests = []runner.E2ETest{
},
TestBitcoinDeposit,
),
runner.NewE2ETest(
TestBitcoinDepositRefundName,
"deposit Bitcoin into ZEVM; expect refund", []runner.ArgDefinition{
{Description: "amount in btc", DefaultValue: "0.1"},
},
TestBitcoinDepositRefund,
),
runner.NewE2ETest(
TestBitcoinWithdrawSegWitName,
"withdraw BTC from ZEVM to a SegWit address",
Expand Down Expand Up @@ -359,6 +368,15 @@ var AllE2ETests = []runner.E2ETest{
},
TestBitcoinWithdrawLegacy,
),
runner.NewE2ETest(
TestBitcoinWithdrawMultipleName,
"withdraw BTC from ZEVM multiple times",
[]runner.ArgDefinition{
{Description: "amount", DefaultValue: "0.01"},
{Description: "times", DefaultValue: "2"},
},
WithdrawBitcoinMultipleTimes,
),
runner.NewE2ETest(
TestBitcoinWithdrawP2WSHName,
"withdraw BTC from ZEVM to a P2WSH address",
Expand Down
43 changes: 17 additions & 26 deletions e2e/e2etests/helper_bitcoin.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package e2etests

import (
"fmt"
"math/big"
"strconv"

"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/e2e/utils"
Expand Down Expand Up @@ -55,50 +55,41 @@ func withdrawBTCZRC20(r *runner.E2ERunner, to btcutil.Address, amount *big.Int)
r.BTCZRC20Addr,
big.NewInt(amount.Int64()*2),
) // approve more to cover withdraw fee
if err != nil {
panic(err)
}
require.NoError(r, err)

receipt := utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
if receipt.Status != 1 {
panic(fmt.Errorf("approve receipt status is not 1"))
}
require.Equal(r, uint64(1), receipt.Status, "approve receipt status is not 1")
swift1337 marked this conversation as resolved.
Show resolved Hide resolved

// mine blocks if testing on regnet
stop := r.MineBlocksIfLocalBitcoin()

// withdraw 'amount' of BTC from ZRC20 to BTC address
tx, err = r.BTCZRC20.Withdraw(r.ZEVMAuth, []byte(to.EncodeAddress()), amount)
if err != nil {
panic(err)
}
require.NoError(r, err)

receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
if receipt.Status != 1 {
panic(fmt.Errorf("withdraw receipt status is not 1"))
}
require.Equal(r, uint64(1), receipt.Status, "withdraw receipt status is not 1")

// mine 10 blocks to confirm the withdraw tx
// mine 10 blocks to confirm the withdrawal tx
_, err = r.GenerateToAddressIfLocalBitcoin(10, to)
if err != nil {
panic(err)
}
require.NoError(r, err)

// get cctx and check status
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, receipt.TxHash.Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
if cctx.CctxStatus.Status != crosschaintypes.CctxStatus_OutboundMined {
panic(fmt.Errorf("cctx status is not OutboundMined"))
}
require.Equal(
r,
crosschaintypes.CctxStatus_OutboundMined,
cctx.CctxStatus.Status,
"cctx status is not OutboundMined",
)
swift1337 marked this conversation as resolved.
Show resolved Hide resolved

// get bitcoin tx according to the outTxHash in cctx
outTxHash := cctx.GetCurrentOutboundParam().Hash
hash, err := chainhash.NewHashFromStr(outTxHash)
if err != nil {
panic(err)
}
require.NoError(r, err)

rawTx, err := r.BtcRPCClient.GetRawTransactionVerbose(hash)
if err != nil {
panic(err)
}
require.NoError(r, err)
r.Logger.Info("raw tx:")
r.Logger.Info(" TxIn: %d", len(rawTx.Vin))
for idx, txIn := range rawTx.Vin {
Expand Down
Loading
Loading