From 4c379798b76bc2af9dce9a3261c5709a5d679cff Mon Sep 17 00:00:00 2001 From: lumtis Date: Mon, 3 Jun 2024 14:04:11 +0200 Subject: [PATCH 1/8] add local check --- e2e/runner/bitcoin.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/e2e/runner/bitcoin.go b/e2e/runner/bitcoin.go index 9143926d06..768c872743 100644 --- a/e2e/runner/bitcoin.go +++ b/e2e/runner/bitcoin.go @@ -29,6 +29,11 @@ import ( var blockHeaderBTCTimeout = 5 * time.Minute +// IsLocalBitcoin returns true if the runner is running on a local bitcoin network +func (runner *E2ERunner) IsLocalBitcoin() bool { + return runner.BitcoinParams.Name == chains.BitcoinRegnetParams.Name +} + // ListDeployerUTXOs list the deployer's UTXOs that have at least `minAmount` func (runner *E2ERunner) ListDeployerUTXOs(minAmount float64) ([]btcjson.ListUnspentResult, error) { // query UTXOs from node From 90fd73c67015fbb3d0ca73fbb16c2448b1c66c74 Mon Sep 17 00:00:00 2001 From: lumtis Date: Mon, 3 Jun 2024 14:14:29 +0200 Subject: [PATCH 2/8] improve mineblocks --- e2e/e2etests/helper_bitcoin.go | 4 ++-- e2e/e2etests/test_bitcoin_withdraw_invalid_address.go | 4 ++-- e2e/e2etests/test_crosschain_swap.go | 4 ++-- e2e/runner/bitcoin.go | 11 +++++++---- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/e2e/e2etests/helper_bitcoin.go b/e2e/e2etests/helper_bitcoin.go index 338cd7027f..c5cd113f40 100644 --- a/e2e/e2etests/helper_bitcoin.go +++ b/e2e/e2etests/helper_bitcoin.go @@ -64,7 +64,7 @@ func withdrawBTCZRC20(r *runner.E2ERunner, to btcutil.Address, amount *big.Int) } // mine blocks if testing on regnet - var stop chan struct{} + var stop func() isRegnet := chains.IsBitcoinRegnet(r.GetBitcoinChainID()) if isRegnet { stop = r.MineBlocks() @@ -119,7 +119,7 @@ func withdrawBTCZRC20(r *runner.E2ERunner, to btcutil.Address, amount *big.Int) // stop mining if isRegnet { - stop <- struct{}{} + stop() } return rawTx diff --git a/e2e/e2etests/test_bitcoin_withdraw_invalid_address.go b/e2e/e2etests/test_bitcoin_withdraw_invalid_address.go index 8d4698f489..a682aa5629 100644 --- a/e2e/e2etests/test_bitcoin_withdraw_invalid_address.go +++ b/e2e/e2etests/test_bitcoin_withdraw_invalid_address.go @@ -47,7 +47,7 @@ func withdrawToInvalidAddress(r *runner.E2ERunner, amount *big.Int) { } // mine blocks if testing on regnet - var stop chan struct{} + var stop func() isRegnet := chains.IsBitcoinRegnet(r.GetBitcoinChainID()) if isRegnet { stop = r.MineBlocks() @@ -66,6 +66,6 @@ func withdrawToInvalidAddress(r *runner.E2ERunner, amount *big.Int) { // stop mining if isRegnet { - stop <- struct{}{} + stop() } } diff --git a/e2e/e2etests/test_crosschain_swap.go b/e2e/e2etests/test_crosschain_swap.go index cb74056710..5d5cd18c62 100644 --- a/e2e/e2etests/test_crosschain_swap.go +++ b/e2e/e2etests/test_crosschain_swap.go @@ -116,7 +116,7 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) { } // mine blocks if testing on regnet - var stop chan struct{} + var stop func() isRegnet := chains.IsBitcoinRegnet(r.GetBitcoinChainID()) if isRegnet { stop = r.MineBlocks() @@ -240,6 +240,6 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) { // stop mining if isRegnet { - stop <- struct{}{} + stop() } } diff --git a/e2e/runner/bitcoin.go b/e2e/runner/bitcoin.go index 768c872743..4c5e15d298 100644 --- a/e2e/runner/bitcoin.go +++ b/e2e/runner/bitcoin.go @@ -366,12 +366,12 @@ func (runner *E2ERunner) GetBitcoinChainID() int64 { // 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{} { - stop := make(chan struct{}) +func (runner *E2ERunner) MineBlocks() func() { + stopChan := make(chan struct{}) go func() { for { select { - case <-stop: + case <-stopChan: return default: _, err := runner.BtcRPCClient.GenerateToAddress(1, runner.BTCDeployerAddress, nil) @@ -382,7 +382,10 @@ func (runner *E2ERunner) MineBlocks() chan struct{} { } } }() - return stop + + return func() { + close(stopChan) + } } // ProveBTCTransaction proves that a BTC transaction is in a block header and that the block header is in ZetaChain From 4661bde563ce3e3bd1b6766be2b5e850fb882875 Mon Sep 17 00:00:00 2001 From: lumtis Date: Mon, 3 Jun 2024 14:28:09 +0200 Subject: [PATCH 3/8] only generate on local bitcoin --- e2e/e2etests/helper_bitcoin.go | 2 +- .../test_bitcoin_withdraw_multiple.go | 4 +-- e2e/e2etests/test_crosschain_swap.go | 2 +- e2e/runner/bitcoin.go | 27 ++++++++++++++----- e2e/runner/setup_bitcoin.go | 4 +-- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/e2e/e2etests/helper_bitcoin.go b/e2e/e2etests/helper_bitcoin.go index c5cd113f40..d2f044c42f 100644 --- a/e2e/e2etests/helper_bitcoin.go +++ b/e2e/e2etests/helper_bitcoin.go @@ -81,7 +81,7 @@ func withdrawBTCZRC20(r *runner.E2ERunner, to btcutil.Address, amount *big.Int) } // mine 10 blocks to confirm the withdraw tx - _, err = r.BtcRPCClient.GenerateToAddress(10, to, nil) + _, err = r.GenerateToAddressOnLocalBitcoin(10, to) if err != nil { panic(err) } diff --git a/e2e/e2etests/test_bitcoin_withdraw_multiple.go b/e2e/e2etests/test_bitcoin_withdraw_multiple.go index 683b9fb0f9..7bd130528a 100644 --- a/e2e/e2etests/test_bitcoin_withdraw_multiple.go +++ b/e2e/e2etests/test_bitcoin_withdraw_multiple.go @@ -42,7 +42,7 @@ package e2etests // go func() { // for { // time.Sleep(3 * time.Second) -// _, err = r.BtcRPCClient.GenerateToAddress(1, r.BTCDeployerAddress, nil) +// _, err = r.GenerateToAddressOnLocalBitcoin(1, r.BTCDeployerAddress) // if err != nil { // panic(err) // } @@ -64,7 +64,7 @@ package e2etests // if receipt.Status != 1 { // panic(fmt.Errorf("withdraw receipt status is not 1")) // } -// _, err = r.BtcRPCClient.GenerateToAddress(10, r.BTCDeployerAddress, nil) +// _, err = r.GenerateToAddressOnLocalBitcoin(10, r.BTCDeployerAddress) // if err != nil { // panic(err) // } diff --git a/e2e/e2etests/test_crosschain_swap.go b/e2e/e2etests/test_crosschain_swap.go index 5d5cd18c62..bd3656734c 100644 --- a/e2e/e2etests/test_crosschain_swap.go +++ b/e2e/e2etests/test_crosschain_swap.go @@ -110,7 +110,7 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) { } // mine 10 blocks to confirm the outbound tx - _, err = r.BtcRPCClient.GenerateToAddress(10, r.BTCDeployerAddress, nil) + _, err = r.GenerateToAddressOnLocalBitcoin(10, r.BTCDeployerAddress) if err != nil { panic(err) } diff --git a/e2e/runner/bitcoin.go b/e2e/runner/bitcoin.go index 4c5e15d298..eebea69921 100644 --- a/e2e/runner/bitcoin.go +++ b/e2e/runner/bitcoin.go @@ -29,11 +29,6 @@ import ( var blockHeaderBTCTimeout = 5 * time.Minute -// IsLocalBitcoin returns true if the runner is running on a local bitcoin network -func (runner *E2ERunner) IsLocalBitcoin() bool { - return runner.BitcoinParams.Name == chains.BitcoinRegnetParams.Name -} - // ListDeployerUTXOs list the deployer's UTXOs that have at least `minAmount` func (runner *E2ERunner) ListDeployerUTXOs(minAmount float64) ([]btcjson.ListUnspentResult, error) { // query UTXOs from node @@ -317,7 +312,7 @@ func (runner *E2ERunner) SendToTSSFromDeployerWithMemo( panic(err) } runner.Logger.Info("txid: %+v", txid) - _, err = btcRPC.GenerateToAddress(6, btcDeployerAddress, nil) + _, err = runner.GenerateToAddressOnLocalBitcoin(6, btcDeployerAddress) if err != nil { panic(err) } @@ -364,6 +359,24 @@ func (runner *E2ERunner) GetBitcoinChainID() int64 { return chainID } +// IsLocalBitcoin returns true if the runner is running on a local bitcoin network +func (runner *E2ERunner) IsLocalBitcoin() bool { + return runner.BitcoinParams.Name == chains.BitcoinRegnetParams.Name +} + +// GenerateToAddressOnLocalBitcoin generates blocks to an address if the runner is interacting +// with a local bitcoin network +func (runner *E2ERunner) GenerateToAddressOnLocalBitcoin( + numBlocks int64, + address btcutil.Address, +) ([]*chainhash.Hash, error) { + // if not local bitcoin network, do nothing + if runner.IsLocalBitcoin() { + return runner.BtcRPCClient.GenerateToAddress(numBlocks, address, nil) + } + return nil, nil +} + // 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() func() { @@ -374,7 +387,7 @@ func (runner *E2ERunner) MineBlocks() func() { case <-stopChan: return default: - _, err := runner.BtcRPCClient.GenerateToAddress(1, runner.BTCDeployerAddress, nil) + _, err := runner.GenerateToAddressOnLocalBitcoin(1, runner.BTCDeployerAddress) if err != nil { panic(err) } diff --git a/e2e/runner/setup_bitcoin.go b/e2e/runner/setup_bitcoin.go index 5872fc4101..21a2938c39 100644 --- a/e2e/runner/setup_bitcoin.go +++ b/e2e/runner/setup_bitcoin.go @@ -34,12 +34,12 @@ func (runner *E2ERunner) SetupBitcoinAccount(initNetwork bool) { } // mine some blocks to get some BTC into the deployer address - _, err = runner.BtcRPCClient.GenerateToAddress(101, runner.BTCDeployerAddress, nil) + _, err = runner.GenerateToAddressOnLocalBitcoin(101, runner.BTCDeployerAddress) if err != nil { panic(err) } - _, err = runner.BtcRPCClient.GenerateToAddress(4, runner.BTCDeployerAddress, nil) + _, err = runner.GenerateToAddressOnLocalBitcoin(4, runner.BTCDeployerAddress) if err != nil { panic(err) } From 9d916109689cfe464edad8c0685fd3e23e286b27 Mon Sep 17 00:00:00 2001 From: lumtis Date: Mon, 3 Jun 2024 14:51:27 +0200 Subject: [PATCH 4/8] simplify some functions --- e2e/e2etests/test_crosschain_swap.go | 18 +------- e2e/runner/bitcoin.go | 61 ++++++---------------------- 2 files changed, 15 insertions(+), 64 deletions(-) diff --git a/e2e/e2etests/test_crosschain_swap.go b/e2e/e2etests/test_crosschain_swap.go index bd3656734c..2007059e7c 100644 --- a/e2e/e2etests/test_crosschain_swap.go +++ b/e2e/e2etests/test_crosschain_swap.go @@ -151,14 +151,7 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) { memo = append(r.ZEVMSwapAppAddr.Bytes(), memo...) r.Logger.Info("memo length %d", len(memo)) - txID, err := r.SendToTSSFromDeployerWithMemo( - r.BTCTSSAddress, - 0.01, - utxos[0:1], - r.BtcRPCClient, - memo, - r.BTCDeployerAddress, - ) + txID, err := r.SendToTSSFromDeployerWithMemo(0.01, utxos[0:1], memo) if err != nil { panic(err) } @@ -200,14 +193,7 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) { r.Logger.Info("memo length %d", len(memo)) amount := 0.1 - txid, err := r.SendToTSSFromDeployerWithMemo( - r.BTCTSSAddress, - amount, - utxos[1:2], - r.BtcRPCClient, - memo, - r.BTCDeployerAddress, - ) + txid, err := r.SendToTSSFromDeployerWithMemo(amount, utxos[1:2], memo) if err != nil { panic(err) } diff --git a/e2e/runner/bitcoin.go b/e2e/runner/bitcoin.go index eebea69921..8b01de5d0a 100644 --- a/e2e/runner/bitcoin.go +++ b/e2e/runner/bitcoin.go @@ -9,7 +9,6 @@ import ( "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/chaincfg/chainhash" - "github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" @@ -81,13 +80,7 @@ func (runner *E2ERunner) DepositBTCWithAmount(amount float64) (txHash *chainhash runner.Logger.Info("Now sending two txs to TSS address...") amount = amount + zetabitcoin.DefaultDepositorFee - txHash, err = runner.SendToTSSFromDeployerToDeposit( - runner.BTCTSSAddress, - amount, - utxos, - runner.BtcRPCClient, - runner.BTCDeployerAddress, - ) + txHash, err = runner.SendToTSSFromDeployerToDeposit(amount, utxos) if err != nil { panic(err) } @@ -133,38 +126,19 @@ func (runner *E2ERunner) DepositBTC(testHeader bool) { // send two transactions to the TSS address amount1 := 1.1 + zetabitcoin.DefaultDepositorFee - txHash1, err := runner.SendToTSSFromDeployerToDeposit( - runner.BTCTSSAddress, - amount1, - utxos[:2], - runner.BtcRPCClient, - runner.BTCDeployerAddress, - ) + txHash1, err := runner.SendToTSSFromDeployerToDeposit(amount1, utxos[:2]) if err != nil { panic(err) } amount2 := 0.05 + zetabitcoin.DefaultDepositorFee - txHash2, err := runner.SendToTSSFromDeployerToDeposit( - runner.BTCTSSAddress, - amount2, - utxos[2:4], - runner.BtcRPCClient, - runner.BTCDeployerAddress, - ) + txHash2, err := runner.SendToTSSFromDeployerToDeposit(amount2, utxos[2:4]) if err != nil { panic(err) } // send a donation to the TSS address to compensate for the funds minted automatically during pool creation // and prevent accounting errors - _, err = runner.SendToTSSFromDeployerWithMemo( - runner.BTCTSSAddress, - 0.11, - utxos[4:5], - runner.BtcRPCClient, - []byte(constant.DonationMessage), - runner.BTCDeployerAddress, - ) + _, err = runner.SendToTSSFromDeployerWithMemo(0.11, utxos[4:5], []byte(constant.DonationMessage)) if err != nil { panic(err) } @@ -201,31 +175,22 @@ func (runner *E2ERunner) DepositBTC(testHeader bool) { } } -func (runner *E2ERunner) SendToTSSFromDeployerToDeposit( - to btcutil.Address, - amount float64, - inputUTXOs []btcjson.ListUnspentResult, - btc *rpcclient.Client, - btcDeployerAddress *btcutil.AddressWitnessPubKeyHash, -) (*chainhash.Hash, error) { - return runner.SendToTSSFromDeployerWithMemo( - to, - amount, - inputUTXOs, - btc, - runner.DeployerAddress.Bytes(), - btcDeployerAddress, - ) +func (runner *E2ERunner) SendToTSSFromDeployerToDeposit(amount float64, inputUTXOs []btcjson.ListUnspentResult) ( + *chainhash.Hash, + error, +) { + return runner.SendToTSSFromDeployerWithMemo(amount, inputUTXOs, runner.DeployerAddress.Bytes()) } func (runner *E2ERunner) SendToTSSFromDeployerWithMemo( - to btcutil.Address, amount float64, inputUTXOs []btcjson.ListUnspentResult, - btcRPC *rpcclient.Client, memo []byte, - btcDeployerAddress *btcutil.AddressWitnessPubKeyHash, ) (*chainhash.Hash, error) { + btcRPC := runner.BtcRPCClient + to := runner.BTCTSSAddress + btcDeployerAddress := runner.BTCDeployerAddress + // prepare inputs inputs := make([]btcjson.TransactionInput, len(inputUTXOs)) inputSats := btcutil.Amount(0) From 65954b343ed27773fdd95f8cd34ebbf08e7f43ab Mon Sep 17 00:00:00 2001 From: lumtis Date: Tue, 4 Jun 2024 15:41:01 +0200 Subject: [PATCH 5/8] try removing min amount --- e2e/runner/bitcoin.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/e2e/runner/bitcoin.go b/e2e/runner/bitcoin.go index 8b01de5d0a..b15030edca 100644 --- a/e2e/runner/bitcoin.go +++ b/e2e/runner/bitcoin.go @@ -29,7 +29,7 @@ import ( var blockHeaderBTCTimeout = 5 * time.Minute // ListDeployerUTXOs list the deployer's UTXOs that have at least `minAmount` -func (runner *E2ERunner) ListDeployerUTXOs(minAmount float64) ([]btcjson.ListUnspentResult, error) { +func (runner *E2ERunner) ListDeployerUTXOs(_ float64) ([]btcjson.ListUnspentResult, error) { // query UTXOs from node utxos, err := runner.BtcRPCClient.ListUnspentMinMaxAddresses( 1, @@ -43,9 +43,9 @@ func (runner *E2ERunner) ListDeployerUTXOs(minAmount float64) ([]btcjson.ListUns // filter UTXOs by `minAmount` filtered := []btcjson.ListUnspentResult{} for _, utxo := range utxos { - if utxo.Amount >= minAmount { - filtered = append(filtered, utxo) - } + //if utxo.Amount >= minAmount { + filtered = append(filtered, utxo) + //} } return filtered, nil From dc93dc6af0261a18627c8ee5759c0366885ab72c Mon Sep 17 00:00:00 2001 From: lumtis Date: Tue, 4 Jun 2024 16:27:53 +0200 Subject: [PATCH 6/8] simplify error message --- e2e/runner/bitcoin.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/e2e/runner/bitcoin.go b/e2e/runner/bitcoin.go index b15030edca..707b3ee2d1 100644 --- a/e2e/runner/bitcoin.go +++ b/e2e/runner/bitcoin.go @@ -71,7 +71,11 @@ func (runner *E2ERunner) DepositBTCWithAmount(amount float64) (txHash *chainhash } if spendableAmount < amount { - panic(fmt.Errorf("not enough spendable BTC to run the test; have %f", spendableAmount)) + panic(fmt.Errorf( + "not enough spendable BTC to run the test; have %f, require %f", + spendableAmount, + amount, + )) } runner.Logger.Info("ListUnspent:") From 47c08b6fc6e4cfc3e04ef71f4d05605fdcc43ffd Mon Sep 17 00:00:00 2001 From: lumtis Date: Wed, 5 Jun 2024 09:10:09 +0200 Subject: [PATCH 7/8] refactor utxos list --- e2e/e2etests/test_crosschain_swap.go | 4 ++-- e2e/runner/bitcoin.go | 22 +++++++--------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/e2e/e2etests/test_crosschain_swap.go b/e2e/e2etests/test_crosschain_swap.go index 2007059e7c..054d5f5692 100644 --- a/e2e/e2etests/test_crosschain_swap.go +++ b/e2e/e2etests/test_crosschain_swap.go @@ -137,8 +137,8 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) { r.Logger.Info("cctx2 outbound tx hash %s", cctx2.GetCurrentOutboundParam().Hash) r.Logger.Info("******* Second test: BTC -> ERC20ZRC20") - // list deployer utxos that have at least 1 BTC - utxos, err := r.ListDeployerUTXOs(1.0) + // list deployer utxos + utxos, err := r.ListDeployerUTXOs() if err != nil { panic(err) } diff --git a/e2e/runner/bitcoin.go b/e2e/runner/bitcoin.go index 707b3ee2d1..4004a7a036 100644 --- a/e2e/runner/bitcoin.go +++ b/e2e/runner/bitcoin.go @@ -28,8 +28,8 @@ import ( var blockHeaderBTCTimeout = 5 * time.Minute -// ListDeployerUTXOs list the deployer's UTXOs that have at least `minAmount` -func (runner *E2ERunner) ListDeployerUTXOs(_ float64) ([]btcjson.ListUnspentResult, error) { +// ListDeployerUTXOs list the deployer's UTXOs +func (runner *E2ERunner) ListDeployerUTXOs() ([]btcjson.ListUnspentResult, error) { // query UTXOs from node utxos, err := runner.BtcRPCClient.ListUnspentMinMaxAddresses( 1, @@ -40,23 +40,15 @@ func (runner *E2ERunner) ListDeployerUTXOs(_ float64) ([]btcjson.ListUnspentResu return nil, err } - // filter UTXOs by `minAmount` - filtered := []btcjson.ListUnspentResult{} - for _, utxo := range utxos { - //if utxo.Amount >= minAmount { - filtered = append(filtered, utxo) - //} - } - - return filtered, nil + return utxos, nil } // DepositBTCWithAmount deposits BTC on ZetaChain with a specific amount func (runner *E2ERunner) DepositBTCWithAmount(amount float64) (txHash *chainhash.Hash) { runner.Logger.Print("⏳ depositing BTC into ZEVM") - // list deployer utxos that have at least 1 BTC - utxos, err := runner.ListDeployerUTXOs(1.0) + // list deployer utxos + utxos, err := runner.ListDeployerUTXOs() if err != nil { panic(err) } @@ -101,8 +93,8 @@ func (runner *E2ERunner) DepositBTC(testHeader bool) { runner.Logger.Print("✅ BTC deposited in %s", time.Since(startTime)) }() - // list deployer utxos that have at least 1 BTC - utxos, err := runner.ListDeployerUTXOs(1.0) + // list deployer utxos + utxos, err := runner.ListDeployerUTXOs() if err != nil { panic(err) } From 8a15b0c203351c1699555b548e78e1bea19f28ad Mon Sep 17 00:00:00 2001 From: lumtis Date: Thu, 6 Jun 2024 14:42:57 +0200 Subject: [PATCH 8/8] comments --- e2e/e2etests/helper_bitcoin.go | 12 +++--------- .../test_bitcoin_withdraw_invalid_address.go | 11 ++--------- e2e/e2etests/test_bitcoin_withdraw_multiple.go | 4 ++-- e2e/e2etests/test_crosschain_swap.go | 13 +++---------- e2e/runner/bitcoin.go | 13 +++++++------ e2e/runner/setup_bitcoin.go | 4 ++-- 6 files changed, 19 insertions(+), 38 deletions(-) diff --git a/e2e/e2etests/helper_bitcoin.go b/e2e/e2etests/helper_bitcoin.go index d2f044c42f..09cb4ded96 100644 --- a/e2e/e2etests/helper_bitcoin.go +++ b/e2e/e2etests/helper_bitcoin.go @@ -64,11 +64,7 @@ func withdrawBTCZRC20(r *runner.E2ERunner, to btcutil.Address, amount *big.Int) } // mine blocks if testing on regnet - var stop func() - isRegnet := chains.IsBitcoinRegnet(r.GetBitcoinChainID()) - if isRegnet { - stop = r.MineBlocks() - } + stop := r.MineBlocksIfLocalBitcoin() // withdraw 'amount' of BTC from ZRC20 to BTC address tx, err = r.BTCZRC20.Withdraw(r.ZEVMAuth, []byte(to.EncodeAddress()), amount) @@ -81,7 +77,7 @@ func withdrawBTCZRC20(r *runner.E2ERunner, to btcutil.Address, amount *big.Int) } // mine 10 blocks to confirm the withdraw tx - _, err = r.GenerateToAddressOnLocalBitcoin(10, to) + _, err = r.GenerateToAddressIfLocalBitcoin(10, to) if err != nil { panic(err) } @@ -118,9 +114,7 @@ func withdrawBTCZRC20(r *runner.E2ERunner, to btcutil.Address, amount *big.Int) } // stop mining - if isRegnet { - stop() - } + stop() return rawTx } diff --git a/e2e/e2etests/test_bitcoin_withdraw_invalid_address.go b/e2e/e2etests/test_bitcoin_withdraw_invalid_address.go index a682aa5629..aaa0725a99 100644 --- a/e2e/e2etests/test_bitcoin_withdraw_invalid_address.go +++ b/e2e/e2etests/test_bitcoin_withdraw_invalid_address.go @@ -9,7 +9,6 @@ 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) { @@ -47,11 +46,7 @@ func withdrawToInvalidAddress(r *runner.E2ERunner, amount *big.Int) { } // mine blocks if testing on regnet - var stop func() - isRegnet := chains.IsBitcoinRegnet(r.GetBitcoinChainID()) - if isRegnet { - stop = r.MineBlocks() - } + stop := r.MineBlocksIfLocalBitcoin() // withdraw amount provided as test arg BTC from ZRC20 to BTC legacy address // the address "1EYVvXLusCxtVuEwoYvWRyN5EZTXwPVvo3" is for mainnet, not regtest @@ -65,7 +60,5 @@ func withdrawToInvalidAddress(r *runner.E2ERunner, amount *big.Int) { } // stop mining - if isRegnet { - stop() - } + stop() } diff --git a/e2e/e2etests/test_bitcoin_withdraw_multiple.go b/e2e/e2etests/test_bitcoin_withdraw_multiple.go index 7bd130528a..b9271b8b91 100644 --- a/e2e/e2etests/test_bitcoin_withdraw_multiple.go +++ b/e2e/e2etests/test_bitcoin_withdraw_multiple.go @@ -42,7 +42,7 @@ package e2etests // go func() { // for { // time.Sleep(3 * time.Second) -// _, err = r.GenerateToAddressOnLocalBitcoin(1, r.BTCDeployerAddress) +// _, err = r.GenerateToAddressIfLocalBitcoin(1, r.BTCDeployerAddress) // if err != nil { // panic(err) // } @@ -64,7 +64,7 @@ package e2etests // if receipt.Status != 1 { // panic(fmt.Errorf("withdraw receipt status is not 1")) // } -// _, err = r.GenerateToAddressOnLocalBitcoin(10, r.BTCDeployerAddress) +// _, err = r.GenerateToAddressIfLocalBitcoin(10, r.BTCDeployerAddress) // if err != nil { // panic(err) // } diff --git a/e2e/e2etests/test_crosschain_swap.go b/e2e/e2etests/test_crosschain_swap.go index 054d5f5692..358e9ba7d7 100644 --- a/e2e/e2etests/test_crosschain_swap.go +++ b/e2e/e2etests/test_crosschain_swap.go @@ -10,7 +10,6 @@ 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" ) @@ -110,17 +109,13 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) { } // mine 10 blocks to confirm the outbound tx - _, err = r.GenerateToAddressOnLocalBitcoin(10, r.BTCDeployerAddress) + _, err = r.GenerateToAddressIfLocalBitcoin(10, r.BTCDeployerAddress) if err != nil { panic(err) } // mine blocks if testing on regnet - var stop func() - isRegnet := chains.IsBitcoinRegnet(r.GetBitcoinChainID()) - if isRegnet { - stop = r.MineBlocks() - } + stop := r.MineBlocksIfLocalBitcoin() // 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) @@ -225,7 +220,5 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) { } // stop mining - if isRegnet { - stop() - } + stop() } diff --git a/e2e/runner/bitcoin.go b/e2e/runner/bitcoin.go index 4004a7a036..493a7a3fd3 100644 --- a/e2e/runner/bitcoin.go +++ b/e2e/runner/bitcoin.go @@ -273,7 +273,7 @@ func (runner *E2ERunner) SendToTSSFromDeployerWithMemo( panic(err) } runner.Logger.Info("txid: %+v", txid) - _, err = runner.GenerateToAddressOnLocalBitcoin(6, btcDeployerAddress) + _, err = runner.GenerateToAddressIfLocalBitcoin(6, btcDeployerAddress) if err != nil { panic(err) } @@ -325,9 +325,9 @@ func (runner *E2ERunner) IsLocalBitcoin() bool { return runner.BitcoinParams.Name == chains.BitcoinRegnetParams.Name } -// GenerateToAddressOnLocalBitcoin generates blocks to an address if the runner is interacting +// GenerateToAddressIfLocalBitcoin generates blocks to an address if the runner is interacting // with a local bitcoin network -func (runner *E2ERunner) GenerateToAddressOnLocalBitcoin( +func (runner *E2ERunner) GenerateToAddressIfLocalBitcoin( numBlocks int64, address btcutil.Address, ) ([]*chainhash.Hash, error) { @@ -338,9 +338,10 @@ func (runner *E2ERunner) GenerateToAddressOnLocalBitcoin( return nil, nil } -// MineBlocks mines blocks on the BTC chain at a rate of 1 blocks every 5 seconds +// MineBlocksIfLocalBitcoin mines blocks on the local 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() func() { +// If the chain is not local, the function does nothing +func (runner *E2ERunner) MineBlocksIfLocalBitcoin() func() { stopChan := make(chan struct{}) go func() { for { @@ -348,7 +349,7 @@ func (runner *E2ERunner) MineBlocks() func() { case <-stopChan: return default: - _, err := runner.GenerateToAddressOnLocalBitcoin(1, runner.BTCDeployerAddress) + _, err := runner.GenerateToAddressIfLocalBitcoin(1, runner.BTCDeployerAddress) if err != nil { panic(err) } diff --git a/e2e/runner/setup_bitcoin.go b/e2e/runner/setup_bitcoin.go index 21a2938c39..15af182915 100644 --- a/e2e/runner/setup_bitcoin.go +++ b/e2e/runner/setup_bitcoin.go @@ -34,12 +34,12 @@ func (runner *E2ERunner) SetupBitcoinAccount(initNetwork bool) { } // mine some blocks to get some BTC into the deployer address - _, err = runner.GenerateToAddressOnLocalBitcoin(101, runner.BTCDeployerAddress) + _, err = runner.GenerateToAddressIfLocalBitcoin(101, runner.BTCDeployerAddress) if err != nil { panic(err) } - _, err = runner.GenerateToAddressOnLocalBitcoin(4, runner.BTCDeployerAddress) + _, err = runner.GenerateToAddressIfLocalBitcoin(4, runner.BTCDeployerAddress) if err != nil { panic(err) }