Skip to content

Commit

Permalink
fix: try fixing E2E crosschain_swap failure (#2266)
Browse files Browse the repository at this point in the history
* try fixing E2E crosschain_swap failure

* added changelog entry

* added log prints to crosschain_swap E2E test

* filter by deployer address when listing UTXOs

* filter UTXOs by amount in E2E test to avoid 'not enough input amount'

* revert the log prints for debugging

* remove panic from btc runner method ListDeployerUTXOs()

* added comments to ListDeployerUTXOs method and places it is used

---------

Co-authored-by: Lucas Bertrand <[email protected]>
  • Loading branch information
ws4charlie and lumtis authored May 30, 2024
1 parent 42458ae commit 14c1744
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* [2184](https://github.com/zeta-chain/node/pull/2184) - add tx priority checks to e2e tests
* [2199](https://github.com/zeta-chain/node/pull/2199) - custom priority mempool unit tests
* [2240](https://github.com/zeta-chain/node/pull/2240) - removed hard-coded Bitcoin regnet chainID in E2E withdraw tests
* [2266](https://github.com/zeta-chain/node/pull/2266) - try fixing E2E test `crosschain_swap` failure `btc transaction not signed`

### Fixes

Expand Down
22 changes: 4 additions & 18 deletions e2e/e2etests/test_crosschain_swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +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")
utxos, err := r.BtcRPCClient.ListUnspent()
// list deployer utxos that have at least 1 BTC
utxos, err := r.ListDeployerUTXOs(1.0)
if err != nil {
panic(err)
}
Expand All @@ -153,19 +154,14 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) {
txID, err := r.SendToTSSFromDeployerWithMemo(
r.BTCTSSAddress,
0.01,
utxos[0:2],
utxos[0:1],
r.BtcRPCClient,
memo,
r.BTCDeployerAddress,
)
if err != nil {
panic(err)
}
r.Logger.Info("Sent BTC to TSS txid %s; now mining 10 blocks for confirmation", txID)
_, err = r.BtcRPCClient.GenerateToAddress(10, r.BTCDeployerAddress, nil)
if err != nil {
panic(err)
}

cctx3 := utils.WaitCctxMinedByInboundHash(r.Ctx, txID.String(), r.CctxClient, r.Logger, r.CctxTimeout)
if cctx3.CctxStatus.Status != types.CctxStatus_OutboundMined {
Expand Down Expand Up @@ -194,11 +190,6 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) {

{
r.Logger.Info("******* Third test: BTC -> ETH with contract call reverted; should refund BTC")
utxos, err := r.BtcRPCClient.ListUnspent()
if err != nil {
panic(err)
}
r.Logger.Info("#utxos %d", len(utxos))
// the following memo will result in a revert in the contract call as targetZRC20 is set to DeployerAddress
// which is apparently not a ZRC20 contract; the UNISWAP call will revert
memo, err := r.ZEVMSwapApp.EncodeMemo(&bind.CallOpts{}, r.DeployerAddress, r.DeployerAddress.Bytes())
Expand All @@ -212,19 +203,14 @@ func TestCrosschainSwap(r *runner.E2ERunner, _ []string) {
txid, err := r.SendToTSSFromDeployerWithMemo(
r.BTCTSSAddress,
amount,
utxos[0:2],
utxos[1:2],
r.BtcRPCClient,
memo,
r.BTCDeployerAddress,
)
if err != nil {
panic(err)
}
r.Logger.Info("Sent BTC to TSS txid %s; now mining 10 blocks for confirmation", txid)
_, err = r.BtcRPCClient.GenerateToAddress(10, r.BTCDeployerAddress, nil)
if err != nil {
panic(err)
}

cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, txid.String(), r.CctxClient, r.Logger, r.CctxTimeout)
r.Logger.Info("cctx3 index %s", cctx.Index)
Expand Down
49 changes: 36 additions & 13 deletions e2e/runner/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,35 @@ import (

var blockHeaderBTCTimeout = 5 * time.Minute

// 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")

// fetch utxos
// ListDeployerUTXOs list the deployer's UTXOs that have at least `minAmount`
func (runner *E2ERunner) ListDeployerUTXOs(minAmount float64) ([]btcjson.ListUnspentResult, error) {
// query UTXOs from node
utxos, err := runner.BtcRPCClient.ListUnspentMinMaxAddresses(
1,
9999999,
[]btcutil.Address{runner.BTCDeployerAddress},
)
if err != nil {
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
}

// 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)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -85,12 +104,12 @@ func (runner *E2ERunner) DepositBTC(testHeader bool) {
runner.Logger.Print("✅ BTC deposited in %s", time.Since(startTime))
}()

// fetch utxos
btc := runner.BtcRPCClient
utxos, err := runner.BtcRPCClient.ListUnspent()
// list deployer utxos that have at least 1 BTC
utxos, err := runner.ListDeployerUTXOs(1.0)
if err != nil {
panic(err)
}

spendableAmount := 0.0
spendableUTXOs := 0
for _, utxo := range utxos {
Expand Down Expand Up @@ -118,7 +137,7 @@ func (runner *E2ERunner) DepositBTC(testHeader bool) {
runner.BTCTSSAddress,
amount1,
utxos[:2],
btc,
runner.BtcRPCClient,
runner.BTCDeployerAddress,
)
if err != nil {
Expand All @@ -129,7 +148,7 @@ func (runner *E2ERunner) DepositBTC(testHeader bool) {
runner.BTCTSSAddress,
amount2,
utxos[2:4],
btc,
runner.BtcRPCClient,
runner.BTCDeployerAddress,
)
if err != nil {
Expand All @@ -142,7 +161,7 @@ func (runner *E2ERunner) DepositBTC(testHeader bool) {
runner.BTCTSSAddress,
0.11,
utxos[4:5],
btc,
runner.BtcRPCClient,
[]byte(constant.DonationMessage),
runner.BTCDeployerAddress,
)
Expand Down Expand Up @@ -214,7 +233,10 @@ func (runner *E2ERunner) SendToTSSFromDeployerWithMemo(
scriptPubkeys := make([]string, len(inputUTXOs))

for i, utxo := range inputUTXOs {
inputs[i] = btcjson.TransactionInput{utxo.TxID, utxo.Vout}
inputs[i] = btcjson.TransactionInput{
Txid: utxo.TxID,
Vout: utxo.Vout,
}
inputSats += btcutil.Amount(utxo.Amount * btcutil.SatoshiPerBitcoin)
amounts[i] = utxo.Amount
scriptPubkeys[i] = utxo.ScriptPubKey
Expand Down Expand Up @@ -253,7 +275,7 @@ func (runner *E2ERunner) SendToTSSFromDeployerWithMemo(
tx.TxOut[1], tx.TxOut[2] = tx.TxOut[2], tx.TxOut[1]

// make sure that TxOut[0] is sent to "to" address; TxOut[2] is change to oneself. TxOut[1] is memo.
if bytes.Compare(tx.TxOut[0].PkScript[2:], to.ScriptAddress()) != 0 {
if !bytes.Equal(tx.TxOut[0].PkScript[2:], to.ScriptAddress()) {
runner.Logger.Info("tx.TxOut[0].PkScript: %x", tx.TxOut[0].PkScript)
runner.Logger.Info("to.ScriptAddress(): %x", to.ScriptAddress())
runner.Logger.Info("swapping txout[0] with txout[2]")
Expand Down Expand Up @@ -281,6 +303,7 @@ func (runner *E2ERunner) SendToTSSFromDeployerWithMemo(
if err != nil {
panic(err)
}

if !signed {
panic("btc transaction not signed")
}
Expand Down

0 comments on commit 14c1744

Please sign in to comment.