From 323cdcf60d2b696726263f74dfadb79fd917c0b6 Mon Sep 17 00:00:00 2001
From: RafilxTenfen <rafaeltenfen.rt@gmail.com>
Date: Thu, 21 Nov 2024 17:28:22 -0300
Subject: [PATCH] chore: add last err to debug

---
 itest/e2e_test.go        | 35 ++++++++++++++++++++---------------
 itest/manager.go         |  3 +++
 stakerservice/service.go |  4 +++-
 3 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/itest/e2e_test.go b/itest/e2e_test.go
index d961126..791027b 100644
--- a/itest/e2e_test.go
+++ b/itest/e2e_test.go
@@ -17,7 +17,6 @@ import (
 	"github.com/babylonlabs-io/btc-staker/stakerservice"
 	"github.com/babylonlabs-io/networks/parameters/parser"
 
-	"github.com/babylonlabs-io/babylon/btcstaking"
 	"github.com/babylonlabs-io/babylon/crypto/bip322"
 	btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types"
 
@@ -860,10 +859,6 @@ func TestStakeFromPhase1(t *testing.T) {
 	require.NoError(t, err)
 
 	tmBTC := StartManagerBtc(t, ctx, numMatureOutputsInWallet, manager)
-	defer func() {
-		cancel()
-		manager.ClearResources()
-	}()
 
 	minStakingTime := uint16(100)
 	stakerAddr := datagen.GenRandomAccount().GetAddress()
@@ -873,18 +868,11 @@ func TestStakeFromPhase1(t *testing.T) {
 	btcStakerPkHex := hex.EncodeToString(schnorr.SerializePubKey(testStakingData.StakerKey))
 
 	appCli := testutil.TestApp()
-	tagHex := datagen.GenRandomHexStr(r, btcstaking.TagLen)
 
 	coventantPrivKeys := genCovenants(t, 1)
 	covenantPkSerializedHex := hex.EncodeToString(schnorr.SerializePubKey(coventantPrivKeys[0].PubKey()))
 	covenantPkHex := hex.EncodeToString(coventantPrivKeys[0].PubKey().SerializeCompressed())
 
-	commonFlags := []string{
-		fmt.Sprintf("--covenant-committee-pks=%s", covenantPkSerializedHex),
-		fmt.Sprintf("--tag=%s", tagHex),
-		"--covenant-quorum=1", "--network=regtest",
-	}
-
 	lastParams := &parser.VersionedGlobalParams{
 		Version:          0,
 		ActivationHeight: 100,
@@ -918,6 +906,12 @@ func TestStakeFromPhase1(t *testing.T) {
 	inclusionHeight := lastParams.ActivationHeight + 1
 	stakingTime := lastParams.MaxStakingTime
 
+	commonFlags := []string{
+		fmt.Sprintf("--covenant-committee-pks=%s", covenantPkSerializedHex),
+		fmt.Sprintf("--tag=%s", lastParams.Tag),
+		"--covenant-quorum=1", "--network=regtest",
+	}
+
 	createTxCmdArgs := []string{
 		paramsFilePath,
 		fmt.Sprintf("--staker-pk=%s", btcStakerPkHex),
@@ -965,7 +959,7 @@ func TestStakeFromPhase1(t *testing.T) {
 	parsedGlobalParams, err := parser.ParseGlobalParams(&globalParams)
 	require.NoError(t, err)
 
-	// just to make sure it is able to parse the staking tx
+	// Makes sure it is able to parse the staking tx
 	paserdStkTx, err := stakerservice.ParseV0StakingTx(parsedGlobalParams, regtestParams, signedStkTx)
 	require.NoError(t, err)
 	require.NotNil(t, paserdStkTx)
@@ -982,15 +976,26 @@ func TestStakeFromPhase1(t *testing.T) {
 	}
 	defer tm.Stop(t, cancel)
 
+	// verify that the chain is healthy
+	require.Eventually(t, func() bool {
+		_, err := tm.BabylonClient.Params()
+		return err == nil
+	}, time.Minute, 200*time.Millisecond)
+
+	// funds the fpd
 	_, _, err = tm.manager.BabylondTxBankMultiSend(t, "node0", "1000000ubbn", testStakingData.FinalityProviderBabylonAddrs[0].String())
 	require.NoError(t, err)
 
 	tm.insertAllMinedBlocksToBabylon(t)
 	tm.createAndRegisterFinalityProviders(t, testStakingData)
 
+	// tmBTC.WalletAddrInfo.
 	stakerAddrStr := tmBTC.MinerAddr.String()
-	stkTxHash := txHash.String()
-	// miner address and the staker addr are the same guy
+	// stakerAddrStr := tmBTC.WalletPubKey
+	// stakerAddrStr := btcStakerPkHex
+	stkTxHash := signedStkTx.TxHash().String()
+
+	// miner address and the staker addr are the same guy, maybe not
 	res, err := tmStakerApp.StakerClient.BtcDelegationFromBtcStakingTx(ctx, stakerAddrStr, stkTxHash, parsedGlobalParams)
 	require.NoError(t, err)
 	require.NotNil(t, res)
diff --git a/itest/manager.go b/itest/manager.go
index 79e5810..2620676 100644
--- a/itest/manager.go
+++ b/itest/manager.go
@@ -44,6 +44,7 @@ import (
 	"github.com/babylonlabs-io/btc-staker/walletcontroller"
 	"github.com/btcsuite/btcd/btcec/v2"
 	"github.com/btcsuite/btcd/btcec/v2/schnorr"
+	"github.com/btcsuite/btcd/btcjson"
 	"github.com/btcsuite/btcd/btcutil"
 	"github.com/btcsuite/btcd/chaincfg"
 	"github.com/btcsuite/btcd/chaincfg/chainhash"
@@ -170,6 +171,7 @@ type TestManagerBTC struct {
 	WalletPassphrase string
 	BitcoindHost     string
 	WalletPubKey     *btcec.PublicKey
+	WalletAddrInfo   *btcjson.GetAddressInfoResult
 	TestRpcBtcClient *rpcclient.Client
 }
 
@@ -294,6 +296,7 @@ func StartManagerBtc(
 		WalletPassphrase: passphrase,
 		BitcoindHost:     bitcoindHost,
 		WalletPubKey:     walletPubKey,
+		WalletAddrInfo:   info,
 		TestRpcBtcClient: rpcBtc,
 	}
 }
diff --git a/stakerservice/service.go b/stakerservice/service.go
index f72cb65..b5979a0 100644
--- a/stakerservice/service.go
+++ b/stakerservice/service.go
@@ -168,6 +168,7 @@ func (s *StakerService) btcDelegationFromBtcStakingTx(
 }
 
 func ParseV0StakingTx(globalParams *parser.ParsedGlobalParams, btcParams *chaincfg.Params, wireStkTx *wire.MsgTx) (*btcstaking.ParsedV0StakingTx, error) {
+	var lastErr error
 	for i := len(globalParams.Versions) - 1; i >= 0; i-- {
 		params := globalParams.Versions[i]
 		parsedStakingTx, err := btcstaking.ParseV0StakingTx(
@@ -178,11 +179,12 @@ func ParseV0StakingTx(globalParams *parser.ParsedGlobalParams, btcParams *chainc
 			btcParams,
 		)
 		if err != nil {
+			lastErr = err
 			continue
 		}
 		return parsedStakingTx, nil
 	}
-	return nil, fmt.Errorf("failed to parse BTC staking tx %s using the global params %+v", wireStkTx.TxHash().String(), globalParams)
+	return nil, fmt.Errorf("err: %s failed to parse BTC staking tx %s using the global params %+v", lastErr.Error(), wireStkTx.TxHash().String(), globalParams)
 }
 
 func (s *StakerService) stakingDetails(_ *rpctypes.Context,