From a608c4d403d1290d3309403f85cc70dd74397df6 Mon Sep 17 00:00:00 2001 From: Charlie Chen <34498985+ws4charlie@users.noreply.github.com> Date: Wed, 22 May 2024 13:14:23 -0500 Subject: [PATCH] fix: removed hard-coded bitcoin Regnet chainID in E2E withdraw tests (#2240) * removed hard-coded bitcoin Regnet chainID in E2E withdraw tests * added changelog entry * fix code format --- changelog.md | 1 + e2e/e2etests/helper_bitcoin.go | 22 ++++++++++----- .../test_bitcoin_withdraw_invalid_address.go | 14 +++++++--- e2e/e2etests/test_bitcoin_withdraw_legacy.go | 2 +- e2e/e2etests/test_bitcoin_withdraw_p2sh.go | 2 +- e2e/e2etests/test_bitcoin_withdraw_p2wsh.go | 2 +- e2e/e2etests/test_bitcoin_withdraw_segwit.go | 2 +- e2e/e2etests/test_bitcoin_withdraw_taproot.go | 2 +- e2e/e2etests/test_crosschain_swap.go | 13 +++++++-- e2e/runner/bitcoin.go | 9 +++++++ pkg/chains/bitcoin.go | 14 ++++++++++ pkg/chains/bitcoin_test.go | 27 +++++++++++++++++++ 12 files changed, 94 insertions(+), 16 deletions(-) diff --git a/changelog.md b/changelog.md index d03a2aa6da..ffb96c3be9 100644 --- a/changelog.md +++ b/changelog.md @@ -35,6 +35,7 @@ * [2047](https://github.com/zeta-chain/node/pull/2047) - fix liquidity cap advanced test * [2181](https://github.com/zeta-chain/node/pull/2181) - add more assertion and test cases in ZEVM message passing E2E tests * [2184](https://github.com/zeta-chain/node/pull/2184) - add tx priority checks to e2e tests +* [2240](https://github.com/zeta-chain/node/pull/2240) - removed hard-coded Bitcoin regnet chainID in E2E withdraw tests ### Fixes diff --git a/e2e/e2etests/helper_bitcoin.go b/e2e/e2etests/helper_bitcoin.go index 84d6dac246..338cd7027f 100644 --- a/e2e/e2etests/helper_bitcoin.go +++ b/e2e/e2etests/helper_bitcoin.go @@ -15,22 +15,26 @@ import ( crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types" ) -func parseBitcoinWithdrawArgs(args []string, defaultReceiver string) (btcutil.Address, *big.Int) { +func parseBitcoinWithdrawArgs(r *runner.E2ERunner, args []string, defaultReceiver string) (btcutil.Address, *big.Int) { + // get bitcoin chain id + chainID := r.GetBitcoinChainID() + // parse receiver address var err error var receiver btcutil.Address if args[0] == "" { // use the default receiver - receiver, err = chains.DecodeBtcAddress(defaultReceiver, chains.BtcRegtestChain.ChainId) + receiver, err = chains.DecodeBtcAddress(defaultReceiver, chainID) if err != nil { panic("Invalid default receiver address specified for TestBitcoinWithdraw.") } } else { - receiver, err = chains.DecodeBtcAddress(args[0], chains.BtcRegtestChain.ChainId) + receiver, err = chains.DecodeBtcAddress(args[0], chainID) if err != nil { panic("Invalid receiver address specified for TestBitcoinWithdraw.") } } + // parse the withdrawal amount withdrawalAmount, err := strconv.ParseFloat(args[1], 64) if err != nil { @@ -59,8 +63,12 @@ func withdrawBTCZRC20(r *runner.E2ERunner, to btcutil.Address, amount *big.Int) panic(fmt.Errorf("approve receipt status is not 1")) } - // mine blocks - stop := r.MineBlocks() + // mine blocks if testing on regnet + var stop chan struct{} + isRegnet := chains.IsBitcoinRegnet(r.GetBitcoinChainID()) + if isRegnet { + stop = r.MineBlocks() + } // withdraw 'amount' of BTC from ZRC20 to BTC address tx, err = r.BTCZRC20.Withdraw(r.ZEVMAuth, []byte(to.EncodeAddress()), amount) @@ -110,7 +118,9 @@ func withdrawBTCZRC20(r *runner.E2ERunner, to btcutil.Address, amount *big.Int) } // stop mining - stop <- struct{}{} + if isRegnet { + stop <- struct{}{} + } return rawTx } diff --git a/e2e/e2etests/test_bitcoin_withdraw_invalid_address.go b/e2e/e2etests/test_bitcoin_withdraw_invalid_address.go index 77cde7ad64..8d4698f489 100644 --- a/e2e/e2etests/test_bitcoin_withdraw_invalid_address.go +++ b/e2e/e2etests/test_bitcoin_withdraw_invalid_address.go @@ -9,6 +9,7 @@ import ( "github.com/zeta-chain/zetacore/e2e/runner" "github.com/zeta-chain/zetacore/e2e/utils" + "github.com/zeta-chain/zetacore/pkg/chains" ) func TestBitcoinWithdrawToInvalidAddress(r *runner.E2ERunner, args []string) { @@ -45,8 +46,12 @@ func withdrawToInvalidAddress(r *runner.E2ERunner, amount *big.Int) { panic(fmt.Errorf("approve receipt status is not 1")) } - // mine blocks - stop := r.MineBlocks() + // mine blocks if testing on regnet + var stop chan struct{} + isRegnet := chains.IsBitcoinRegnet(r.GetBitcoinChainID()) + if isRegnet { + stop = r.MineBlocks() + } // withdraw amount provided as test arg BTC from ZRC20 to BTC legacy address // the address "1EYVvXLusCxtVuEwoYvWRyN5EZTXwPVvo3" is for mainnet, not regtest @@ -58,6 +63,9 @@ func withdrawToInvalidAddress(r *runner.E2ERunner, amount *big.Int) { if receipt.Status == 1 { panic(fmt.Errorf("withdraw receipt status is successful for an invalid BTC address")) } + // stop mining - stop <- struct{}{} + if isRegnet { + stop <- struct{}{} + } } diff --git a/e2e/e2etests/test_bitcoin_withdraw_legacy.go b/e2e/e2etests/test_bitcoin_withdraw_legacy.go index a40fc0e852..fc2f7b2ca7 100644 --- a/e2e/e2etests/test_bitcoin_withdraw_legacy.go +++ b/e2e/e2etests/test_bitcoin_withdraw_legacy.go @@ -15,7 +15,7 @@ func TestBitcoinWithdrawLegacy(r *runner.E2ERunner, args []string) { // parse arguments and withdraw BTC defaultReceiver := "mxpYha3UJKUgSwsAz2qYRqaDSwAkKZ3YEY" - receiver, amount := parseBitcoinWithdrawArgs(args, defaultReceiver) + receiver, amount := parseBitcoinWithdrawArgs(r, args, defaultReceiver) _, ok := receiver.(*btcutil.AddressPubKeyHash) if !ok { panic("Invalid receiver address specified for TestBitcoinWithdrawLegacy.") diff --git a/e2e/e2etests/test_bitcoin_withdraw_p2sh.go b/e2e/e2etests/test_bitcoin_withdraw_p2sh.go index 086f45a4dc..97de7675ca 100644 --- a/e2e/e2etests/test_bitcoin_withdraw_p2sh.go +++ b/e2e/e2etests/test_bitcoin_withdraw_p2sh.go @@ -15,7 +15,7 @@ func TestBitcoinWithdrawP2SH(r *runner.E2ERunner, args []string) { // parse arguments and withdraw BTC defaultReceiver := "2N6AoUj3KPS7wNGZXuCckh8YEWcSYNsGbqd" - receiver, amount := parseBitcoinWithdrawArgs(args, defaultReceiver) + receiver, amount := parseBitcoinWithdrawArgs(r, args, defaultReceiver) _, ok := receiver.(*btcutil.AddressScriptHash) if !ok { panic("Invalid receiver address specified for TestBitcoinWithdrawP2SH.") diff --git a/e2e/e2etests/test_bitcoin_withdraw_p2wsh.go b/e2e/e2etests/test_bitcoin_withdraw_p2wsh.go index 524db32cda..79943dc948 100644 --- a/e2e/e2etests/test_bitcoin_withdraw_p2wsh.go +++ b/e2e/e2etests/test_bitcoin_withdraw_p2wsh.go @@ -15,7 +15,7 @@ func TestBitcoinWithdrawP2WSH(r *runner.E2ERunner, args []string) { // parse arguments and withdraw BTC defaultReceiver := "bcrt1qm9mzhyky4w853ft2ms6dtqdyyu3z2tmrq8jg8xglhyuv0dsxzmgs2f0sqy" - receiver, amount := parseBitcoinWithdrawArgs(args, defaultReceiver) + receiver, amount := parseBitcoinWithdrawArgs(r, args, defaultReceiver) _, ok := receiver.(*btcutil.AddressWitnessScriptHash) if !ok { panic("Invalid receiver address specified for TestBitcoinWithdrawP2WSH.") diff --git a/e2e/e2etests/test_bitcoin_withdraw_segwit.go b/e2e/e2etests/test_bitcoin_withdraw_segwit.go index 9f85044f89..31d5d1176b 100644 --- a/e2e/e2etests/test_bitcoin_withdraw_segwit.go +++ b/e2e/e2etests/test_bitcoin_withdraw_segwit.go @@ -15,7 +15,7 @@ func TestBitcoinWithdrawSegWit(r *runner.E2ERunner, args []string) { // parse arguments defaultReceiver := r.BTCDeployerAddress.EncodeAddress() - receiver, amount := parseBitcoinWithdrawArgs(args, defaultReceiver) + receiver, amount := parseBitcoinWithdrawArgs(r, args, defaultReceiver) _, ok := receiver.(*btcutil.AddressWitnessPubKeyHash) if !ok { panic("Invalid receiver address specified for TestBitcoinWithdrawSegWit.") diff --git a/e2e/e2etests/test_bitcoin_withdraw_taproot.go b/e2e/e2etests/test_bitcoin_withdraw_taproot.go index b3ac26bd23..5473a947e5 100644 --- a/e2e/e2etests/test_bitcoin_withdraw_taproot.go +++ b/e2e/e2etests/test_bitcoin_withdraw_taproot.go @@ -14,7 +14,7 @@ func TestBitcoinWithdrawTaproot(r *runner.E2ERunner, args []string) { // parse arguments and withdraw BTC defaultReceiver := "bcrt1pqqqsyqcyq5rqwzqfpg9scrgwpugpzysnzs23v9ccrydpk8qarc0sj9hjuh" - receiver, amount := parseBitcoinWithdrawArgs(args, defaultReceiver) + receiver, amount := parseBitcoinWithdrawArgs(r, args, defaultReceiver) _, ok := receiver.(*chains.AddressTaproot) if !ok { panic("Invalid receiver address specified for TestBitcoinWithdrawTaproot.") diff --git a/e2e/e2etests/test_crosschain_swap.go b/e2e/e2etests/test_crosschain_swap.go index fb5e9b97de..ef5680b331 100644 --- a/e2e/e2etests/test_crosschain_swap.go +++ b/e2e/e2etests/test_crosschain_swap.go @@ -10,6 +10,7 @@ import ( "github.com/zeta-chain/zetacore/e2e/runner" "github.com/zeta-chain/zetacore/e2e/utils" + "github.com/zeta-chain/zetacore/pkg/chains" "github.com/zeta-chain/zetacore/x/crosschain/types" ) @@ -113,7 +114,13 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) { if err != nil { panic(err) } - stop := r.MineBlocks() + + // mine blocks if testing on regnet + var stop chan struct{} + isRegnet := chains.IsBitcoinRegnet(r.GetBitcoinChainID()) + if isRegnet { + stop = r.MineBlocks() + } // cctx1 index acts like the inboundHash for the second cctx (the one that withdraws BTC) cctx2 := utils.WaitCctxMinedByInboundHash(r.Ctx, cctx1.Index, r.CctxClient, r.Logger, r.CctxTimeout) @@ -246,5 +253,7 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) { } // stop mining - stop <- struct{}{} + if isRegnet { + stop <- struct{}{} + } } diff --git a/e2e/runner/bitcoin.go b/e2e/runner/bitcoin.go index f976253474..1d82c0334c 100644 --- a/e2e/runner/bitcoin.go +++ b/e2e/runner/bitcoin.go @@ -327,6 +327,15 @@ func (runner *E2ERunner) SendToTSSFromDeployerWithMemo( return txid, nil } +// GetBitcoinChainID gets the bitcoin chain ID from the network params +func (runner *E2ERunner) GetBitcoinChainID() int64 { + chainID, err := chains.BitcoinChainIDFromNetworkName(runner.BitcoinParams.Name) + if err != nil { + panic(err) + } + return chainID +} + // MineBlocks mines blocks on the BTC chain at a rate of 1 blocks every 5 seconds // and returns a channel that can be used to stop the mining func (runner *E2ERunner) MineBlocks() chan struct{} { diff --git a/pkg/chains/bitcoin.go b/pkg/chains/bitcoin.go index 0492a5cd32..5edf9fe73e 100644 --- a/pkg/chains/bitcoin.go +++ b/pkg/chains/bitcoin.go @@ -26,6 +26,20 @@ func BitcoinNetParamsFromChainID(chainID int64) (*chaincfg.Params, error) { } } +// BitcoinChainIDFromNetworkName returns the chain id for the given bitcoin network name +func BitcoinChainIDFromNetworkName(name string) (int64, error) { + switch name { + case BitcoinRegnetParams.Name: + return BtcRegtestChain.ChainId, nil + case BitcoinMainnetParams.Name: + return BtcMainnetChain.ChainId, nil + case BitcoinTestnetParams.Name: + return BtcTestNetChain.ChainId, nil + default: + return 0, fmt.Errorf("invalid Bitcoin network name: %s", name) + } +} + // IsBitcoinRegnet returns true if the chain id is for the regnet func IsBitcoinRegnet(chainID int64) bool { return chainID == BtcRegtestChain.ChainId diff --git a/pkg/chains/bitcoin_test.go b/pkg/chains/bitcoin_test.go index c1c1ff2852..ab7c11ff48 100644 --- a/pkg/chains/bitcoin_test.go +++ b/pkg/chains/bitcoin_test.go @@ -33,6 +33,33 @@ func TestBitcoinNetParamsFromChainID(t *testing.T) { } } +func TestBitcoinChainIDFromNetParams(t *testing.T) { + tests := []struct { + name string + networkName string + expectedChainID int64 + wantErr bool + }{ + {"Regnet", BitcoinRegnetParams.Name, BtcRegtestChain.ChainId, false}, + {"Mainnet", BitcoinMainnetParams.Name, BtcMainnetChain.ChainId, false}, + {"Testnet", BitcoinTestnetParams.Name, BtcTestNetChain.ChainId, false}, + {"Unknown", "Unknown", 0, true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + chainID, err := BitcoinChainIDFromNetworkName(tt.networkName) + if tt.wantErr { + require.Error(t, err) + require.Zero(t, chainID) + } else { + require.NoError(t, err) + require.Equal(t, tt.expectedChainID, chainID) + } + }) + } +} + func TestIsBitcoinRegnet(t *testing.T) { require.True(t, IsBitcoinRegnet(BtcRegtestChain.ChainId)) require.False(t, IsBitcoinRegnet(BtcMainnetChain.ChainId))