Skip to content

Commit

Permalink
fix: removed hard-coded bitcoin Regnet chainID in E2E withdraw tests (#…
Browse files Browse the repository at this point in the history
…2240)

* removed hard-coded bitcoin Regnet chainID in E2E withdraw tests

* added changelog entry

* fix code format
  • Loading branch information
ws4charlie authored May 22, 2024
1 parent 6fab9e2 commit a608c4d
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 16 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 16 additions & 6 deletions e2e/e2etests/helper_bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
14 changes: 11 additions & 3 deletions e2e/e2etests/test_bitcoin_withdraw_invalid_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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{}{}
}
}
2 changes: 1 addition & 1 deletion e2e/e2etests/test_bitcoin_withdraw_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_bitcoin_withdraw_p2sh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_bitcoin_withdraw_p2wsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_bitcoin_withdraw_segwit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_bitcoin_withdraw_taproot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
13 changes: 11 additions & 2 deletions e2e/e2etests/test_crosschain_swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -246,5 +253,7 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) {
}

// stop mining
stop <- struct{}{}
if isRegnet {
stop <- struct{}{}
}
}
9 changes: 9 additions & 0 deletions e2e/runner/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down
14 changes: 14 additions & 0 deletions pkg/chains/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions pkg/chains/bitcoin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit a608c4d

Please sign in to comment.