Skip to content

Commit a608c4d

Browse files
authored
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
1 parent 6fab9e2 commit a608c4d

12 files changed

+94
-16
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* [2047](https://github.com/zeta-chain/node/pull/2047) - fix liquidity cap advanced test
3636
* [2181](https://github.com/zeta-chain/node/pull/2181) - add more assertion and test cases in ZEVM message passing E2E tests
3737
* [2184](https://github.com/zeta-chain/node/pull/2184) - add tx priority checks to e2e tests
38+
* [2240](https://github.com/zeta-chain/node/pull/2240) - removed hard-coded Bitcoin regnet chainID in E2E withdraw tests
3839

3940
### Fixes
4041

e2e/e2etests/helper_bitcoin.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,26 @@ import (
1515
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
1616
)
1717

18-
func parseBitcoinWithdrawArgs(args []string, defaultReceiver string) (btcutil.Address, *big.Int) {
18+
func parseBitcoinWithdrawArgs(r *runner.E2ERunner, args []string, defaultReceiver string) (btcutil.Address, *big.Int) {
19+
// get bitcoin chain id
20+
chainID := r.GetBitcoinChainID()
21+
1922
// parse receiver address
2023
var err error
2124
var receiver btcutil.Address
2225
if args[0] == "" {
2326
// use the default receiver
24-
receiver, err = chains.DecodeBtcAddress(defaultReceiver, chains.BtcRegtestChain.ChainId)
27+
receiver, err = chains.DecodeBtcAddress(defaultReceiver, chainID)
2528
if err != nil {
2629
panic("Invalid default receiver address specified for TestBitcoinWithdraw.")
2730
}
2831
} else {
29-
receiver, err = chains.DecodeBtcAddress(args[0], chains.BtcRegtestChain.ChainId)
32+
receiver, err = chains.DecodeBtcAddress(args[0], chainID)
3033
if err != nil {
3134
panic("Invalid receiver address specified for TestBitcoinWithdraw.")
3235
}
3336
}
37+
3438
// parse the withdrawal amount
3539
withdrawalAmount, err := strconv.ParseFloat(args[1], 64)
3640
if err != nil {
@@ -59,8 +63,12 @@ func withdrawBTCZRC20(r *runner.E2ERunner, to btcutil.Address, amount *big.Int)
5963
panic(fmt.Errorf("approve receipt status is not 1"))
6064
}
6165

62-
// mine blocks
63-
stop := r.MineBlocks()
66+
// mine blocks if testing on regnet
67+
var stop chan struct{}
68+
isRegnet := chains.IsBitcoinRegnet(r.GetBitcoinChainID())
69+
if isRegnet {
70+
stop = r.MineBlocks()
71+
}
6472

6573
// withdraw 'amount' of BTC from ZRC20 to BTC address
6674
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)
110118
}
111119

112120
// stop mining
113-
stop <- struct{}{}
121+
if isRegnet {
122+
stop <- struct{}{}
123+
}
114124

115125
return rawTx
116126
}

e2e/e2etests/test_bitcoin_withdraw_invalid_address.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/zeta-chain/zetacore/e2e/runner"
1111
"github.com/zeta-chain/zetacore/e2e/utils"
12+
"github.com/zeta-chain/zetacore/pkg/chains"
1213
)
1314

1415
func TestBitcoinWithdrawToInvalidAddress(r *runner.E2ERunner, args []string) {
@@ -45,8 +46,12 @@ func withdrawToInvalidAddress(r *runner.E2ERunner, amount *big.Int) {
4546
panic(fmt.Errorf("approve receipt status is not 1"))
4647
}
4748

48-
// mine blocks
49-
stop := r.MineBlocks()
49+
// mine blocks if testing on regnet
50+
var stop chan struct{}
51+
isRegnet := chains.IsBitcoinRegnet(r.GetBitcoinChainID())
52+
if isRegnet {
53+
stop = r.MineBlocks()
54+
}
5055

5156
// withdraw amount provided as test arg BTC from ZRC20 to BTC legacy address
5257
// the address "1EYVvXLusCxtVuEwoYvWRyN5EZTXwPVvo3" is for mainnet, not regtest
@@ -58,6 +63,9 @@ func withdrawToInvalidAddress(r *runner.E2ERunner, amount *big.Int) {
5863
if receipt.Status == 1 {
5964
panic(fmt.Errorf("withdraw receipt status is successful for an invalid BTC address"))
6065
}
66+
6167
// stop mining
62-
stop <- struct{}{}
68+
if isRegnet {
69+
stop <- struct{}{}
70+
}
6371
}

e2e/e2etests/test_bitcoin_withdraw_legacy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestBitcoinWithdrawLegacy(r *runner.E2ERunner, args []string) {
1515

1616
// parse arguments and withdraw BTC
1717
defaultReceiver := "mxpYha3UJKUgSwsAz2qYRqaDSwAkKZ3YEY"
18-
receiver, amount := parseBitcoinWithdrawArgs(args, defaultReceiver)
18+
receiver, amount := parseBitcoinWithdrawArgs(r, args, defaultReceiver)
1919
_, ok := receiver.(*btcutil.AddressPubKeyHash)
2020
if !ok {
2121
panic("Invalid receiver address specified for TestBitcoinWithdrawLegacy.")

e2e/e2etests/test_bitcoin_withdraw_p2sh.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestBitcoinWithdrawP2SH(r *runner.E2ERunner, args []string) {
1515

1616
// parse arguments and withdraw BTC
1717
defaultReceiver := "2N6AoUj3KPS7wNGZXuCckh8YEWcSYNsGbqd"
18-
receiver, amount := parseBitcoinWithdrawArgs(args, defaultReceiver)
18+
receiver, amount := parseBitcoinWithdrawArgs(r, args, defaultReceiver)
1919
_, ok := receiver.(*btcutil.AddressScriptHash)
2020
if !ok {
2121
panic("Invalid receiver address specified for TestBitcoinWithdrawP2SH.")

e2e/e2etests/test_bitcoin_withdraw_p2wsh.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestBitcoinWithdrawP2WSH(r *runner.E2ERunner, args []string) {
1515

1616
// parse arguments and withdraw BTC
1717
defaultReceiver := "bcrt1qm9mzhyky4w853ft2ms6dtqdyyu3z2tmrq8jg8xglhyuv0dsxzmgs2f0sqy"
18-
receiver, amount := parseBitcoinWithdrawArgs(args, defaultReceiver)
18+
receiver, amount := parseBitcoinWithdrawArgs(r, args, defaultReceiver)
1919
_, ok := receiver.(*btcutil.AddressWitnessScriptHash)
2020
if !ok {
2121
panic("Invalid receiver address specified for TestBitcoinWithdrawP2WSH.")

e2e/e2etests/test_bitcoin_withdraw_segwit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestBitcoinWithdrawSegWit(r *runner.E2ERunner, args []string) {
1515

1616
// parse arguments
1717
defaultReceiver := r.BTCDeployerAddress.EncodeAddress()
18-
receiver, amount := parseBitcoinWithdrawArgs(args, defaultReceiver)
18+
receiver, amount := parseBitcoinWithdrawArgs(r, args, defaultReceiver)
1919
_, ok := receiver.(*btcutil.AddressWitnessPubKeyHash)
2020
if !ok {
2121
panic("Invalid receiver address specified for TestBitcoinWithdrawSegWit.")

e2e/e2etests/test_bitcoin_withdraw_taproot.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestBitcoinWithdrawTaproot(r *runner.E2ERunner, args []string) {
1414

1515
// parse arguments and withdraw BTC
1616
defaultReceiver := "bcrt1pqqqsyqcyq5rqwzqfpg9scrgwpugpzysnzs23v9ccrydpk8qarc0sj9hjuh"
17-
receiver, amount := parseBitcoinWithdrawArgs(args, defaultReceiver)
17+
receiver, amount := parseBitcoinWithdrawArgs(r, args, defaultReceiver)
1818
_, ok := receiver.(*chains.AddressTaproot)
1919
if !ok {
2020
panic("Invalid receiver address specified for TestBitcoinWithdrawTaproot.")

e2e/e2etests/test_crosschain_swap.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/zeta-chain/zetacore/e2e/runner"
1212
"github.com/zeta-chain/zetacore/e2e/utils"
13+
"github.com/zeta-chain/zetacore/pkg/chains"
1314
"github.com/zeta-chain/zetacore/x/crosschain/types"
1415
)
1516

@@ -113,7 +114,13 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) {
113114
if err != nil {
114115
panic(err)
115116
}
116-
stop := r.MineBlocks()
117+
118+
// mine blocks if testing on regnet
119+
var stop chan struct{}
120+
isRegnet := chains.IsBitcoinRegnet(r.GetBitcoinChainID())
121+
if isRegnet {
122+
stop = r.MineBlocks()
123+
}
117124

118125
// cctx1 index acts like the inboundHash for the second cctx (the one that withdraws BTC)
119126
cctx2 := utils.WaitCctxMinedByInboundHash(r.Ctx, cctx1.Index, r.CctxClient, r.Logger, r.CctxTimeout)
@@ -246,5 +253,7 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) {
246253
}
247254

248255
// stop mining
249-
stop <- struct{}{}
256+
if isRegnet {
257+
stop <- struct{}{}
258+
}
250259
}

e2e/runner/bitcoin.go

+9
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,15 @@ func (runner *E2ERunner) SendToTSSFromDeployerWithMemo(
327327
return txid, nil
328328
}
329329

330+
// GetBitcoinChainID gets the bitcoin chain ID from the network params
331+
func (runner *E2ERunner) GetBitcoinChainID() int64 {
332+
chainID, err := chains.BitcoinChainIDFromNetworkName(runner.BitcoinParams.Name)
333+
if err != nil {
334+
panic(err)
335+
}
336+
return chainID
337+
}
338+
330339
// MineBlocks mines blocks on the BTC chain at a rate of 1 blocks every 5 seconds
331340
// and returns a channel that can be used to stop the mining
332341
func (runner *E2ERunner) MineBlocks() chan struct{} {

pkg/chains/bitcoin.go

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ func BitcoinNetParamsFromChainID(chainID int64) (*chaincfg.Params, error) {
2626
}
2727
}
2828

29+
// BitcoinChainIDFromNetworkName returns the chain id for the given bitcoin network name
30+
func BitcoinChainIDFromNetworkName(name string) (int64, error) {
31+
switch name {
32+
case BitcoinRegnetParams.Name:
33+
return BtcRegtestChain.ChainId, nil
34+
case BitcoinMainnetParams.Name:
35+
return BtcMainnetChain.ChainId, nil
36+
case BitcoinTestnetParams.Name:
37+
return BtcTestNetChain.ChainId, nil
38+
default:
39+
return 0, fmt.Errorf("invalid Bitcoin network name: %s", name)
40+
}
41+
}
42+
2943
// IsBitcoinRegnet returns true if the chain id is for the regnet
3044
func IsBitcoinRegnet(chainID int64) bool {
3145
return chainID == BtcRegtestChain.ChainId

pkg/chains/bitcoin_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,33 @@ func TestBitcoinNetParamsFromChainID(t *testing.T) {
3333
}
3434
}
3535

36+
func TestBitcoinChainIDFromNetParams(t *testing.T) {
37+
tests := []struct {
38+
name string
39+
networkName string
40+
expectedChainID int64
41+
wantErr bool
42+
}{
43+
{"Regnet", BitcoinRegnetParams.Name, BtcRegtestChain.ChainId, false},
44+
{"Mainnet", BitcoinMainnetParams.Name, BtcMainnetChain.ChainId, false},
45+
{"Testnet", BitcoinTestnetParams.Name, BtcTestNetChain.ChainId, false},
46+
{"Unknown", "Unknown", 0, true},
47+
}
48+
49+
for _, tt := range tests {
50+
t.Run(tt.name, func(t *testing.T) {
51+
chainID, err := BitcoinChainIDFromNetworkName(tt.networkName)
52+
if tt.wantErr {
53+
require.Error(t, err)
54+
require.Zero(t, chainID)
55+
} else {
56+
require.NoError(t, err)
57+
require.Equal(t, tt.expectedChainID, chainID)
58+
}
59+
})
60+
}
61+
}
62+
3663
func TestIsBitcoinRegnet(t *testing.T) {
3764
require.True(t, IsBitcoinRegnet(BtcRegtestChain.ChainId))
3865
require.False(t, IsBitcoinRegnet(BtcMainnetChain.ChainId))

0 commit comments

Comments
 (0)