Skip to content

Commit

Permalink
chore: removed txInclusionHeightFlag flag and getting the block heigh…
Browse files Browse the repository at this point in the history
…t by querying the btc
  • Loading branch information
RafilxTenfen committed Nov 28, 2024
1 parent 5e096b1 commit a97cd8c
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 15 deletions.
15 changes: 7 additions & 8 deletions cmd/stakercli/daemon/daemoncommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@ var stakeFromPhase1Cmd = cli.Command{
Usage: "BTC address of the staker in hex",
Required: true,
},
cli.Uint64Flag{
Name: txInclusionHeightFlag,
Usage: "Expected BTC height at which transaction was included. This value is important to choose correct global parameters for transaction",
Required: true,
},
},
Action: stakeFromPhase1TxBTC,
}
Expand Down Expand Up @@ -399,10 +394,14 @@ func stakeFromPhase1TxBTC(ctx *cli.Context) error {
return fmt.Errorf("error parsing file %s: %w", inputGlobalParamsFilePath, err)
}

stakingTxInclusionHeight := ctx.Uint64(txInclusionHeightFlag)
paramsForHeight := globalParams.GetVersionedGlobalParamsByHeight(stakingTxInclusionHeight)
resp, err := client.BtcTxDetails(sctx, stakingTransactionHash)
if err != nil {
return fmt.Errorf("error to get btc tx and block data from staking tx %s: %w", stakingTransactionHash, err)
}

paramsForHeight := globalParams.GetVersionedGlobalParamsByHeight(uint64(resp.Blk.Height))
if paramsForHeight == nil {
return fmt.Errorf("error getting param version from global params %s with height %d", inputGlobalParamsFilePath, stakingTxInclusionHeight)
return fmt.Errorf("error getting param version from global params %s with height %d", inputGlobalParamsFilePath, resp.Blk.Height)
}

stakerAddress := ctx.String(stakerAddressFlag)
Expand Down
20 changes: 20 additions & 0 deletions staker/stakerapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"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"
Expand Down Expand Up @@ -2334,6 +2335,25 @@ func (app *App) unlockAndCreatePop(stakerAddress btcutil.Address) (*cl.BabylonPo
)
}

func (app *App) BtcTxAndBlock(txHash *chainhash.Hash) (*btcjson.TxRawResult, *btcjson.GetBlockHeaderVerboseResult, error) {
tx, err := app.wc.TxVerbose(txHash)
if err != nil {
return nil, nil, err
}

blockHash, err := chainhash.NewHashFromStr(tx.BlockHash)
if err != nil {
return nil, nil, err
}

blk, err := app.wc.BlockHeaderVerbose(blockHash)
if err != nil {
return nil, nil, err
}

return tx, blk, nil
}

func checkConfirmationDepth(tipBlockHeight, txInclusionBlockHeight, confirmationTimeBlocks uint32) error {
if txInclusionBlockHeight >= tipBlockHeight {
return fmt.Errorf("inclusion block height: %d should be lower than current tip: %d", txInclusionBlockHeight, tipBlockHeight)
Expand Down
16 changes: 16 additions & 0 deletions stakerservice/client/rpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ func (c *StakerServiceJSONRPCClient) BtcDelegationFromBtcStakingTx(
return result, nil
}

func (c *StakerServiceJSONRPCClient) BtcTxDetails(
ctx context.Context,
txHash string,
) (*service.BtcTxAndBlockResponse, error) {
result := new(service.BtcTxAndBlockResponse)

params := make(map[string]interface{})
params["txHashStr"] = txHash

_, err := c.client.Call(ctx, "btc_tx_blk_details", params, result)
if err != nil {
return nil, err
}
return result, nil
}

func parseCovenantsPubKeyToHex(pks ...*btcec.PublicKey) []string {
pksHex := make([]string, len(pks))
for i, pk := range pks {
Expand Down
27 changes: 25 additions & 2 deletions stakerservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,30 @@ func parseCovenantPubKeyFromHex(pkStr string) (*btcec.PublicKey, error) {
return pk, nil
}

func (s *StakerService) stakingDetails(_ *rpctypes.Context,
stakingTxHash string) (*StakingDetails, error) {
func (s *StakerService) btcTxBlkDetails(
_ *rpctypes.Context,
txHashStr string,
) (*BtcTxAndBlockResponse, error) {
txHash, err := chainhash.NewHashFromStr(txHashStr)
if err != nil {
return nil, err
}

tx, blk, err := s.staker.BtcTxAndBlock(txHash)
if err != nil {
return nil, err
}

return &BtcTxAndBlockResponse{
Tx: tx,
Blk: blk,
}, nil
}

func (s *StakerService) stakingDetails(
_ *rpctypes.Context,
stakingTxHash string,
) (*StakingDetails, error) {
txHash, err := chainhash.NewHashFromStr(stakingTxHash)
if err != nil {
return nil, err
Expand Down Expand Up @@ -619,6 +641,7 @@ func (s *StakerService) GetRoutes() RoutesMap {
"list_staking_transactions": rpc.NewRPCFunc(s.listStakingTransactions, "offset,limit"),
"unbond_staking": rpc.NewRPCFunc(s.unbondStaking, "stakingTxHash"),
"withdrawable_transactions": rpc.NewRPCFunc(s.withdrawableTransactions, "offset,limit"),
"btc_tx_blk_details": rpc.NewRPCFunc(s.btcTxBlkDetails, "txHashStr"),
// watch api
"watch_staking_tx": rpc.NewRPCFunc(s.watchStaking, "stakingTx,stakingTime,stakingValue,stakerBtcPk,fpBtcPks,slashingTx,slashingTxSig,stakerBabylonAddr,stakerAddress,stakerBtcSig,unbondingTx,slashUnbondingTx,slashUnbondingTxSig,unbondingTime,popType"),

Expand Down
7 changes: 7 additions & 0 deletions stakerservice/stakerdresponses.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package stakerservice

import "github.com/btcsuite/btcd/btcjson"

type ResultHealth struct{}

type ResultBtcDelegationFromBtcStakingTx struct {
Expand Down Expand Up @@ -57,3 +59,8 @@ type WithdrawableTransactionsResponse struct {
LastWithdrawableTransactionIndex string `json:"last_transaction_index"`
TotalTransactionCount string `json:"total_transaction_count"`
}

type BtcTxAndBlockResponse struct {
Tx *btcjson.TxRawResult `json:"tx"`
Blk *btcjson.GetBlockHeaderVerboseResult `json:"blk"`
}
15 changes: 10 additions & 5 deletions walletcontroller/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,17 @@ func (w *RPCWalletController) getTxDetails(req notifier.ConfRequest, msg string)

// Tx returns the raw transaction based on the transaction hash
func (w *RPCWalletController) Tx(txHash *chainhash.Hash) (*btcutil.Tx, error) {
rawTx, err := w.Client.GetRawTransaction(txHash)
if err != nil {
return nil, err
}
return w.Client.GetRawTransaction(txHash)
}

// TxVerbose returns the raw transaction verbose based on the transaction hash
func (w *RPCWalletController) TxVerbose(txHash *chainhash.Hash) (*btcjson.TxRawResult, error) {
return w.Client.GetRawTransactionVerbose(txHash)
}

return rawTx, nil
// BlockHeaderVerbose returns the block header data based on the block hash
func (w *RPCWalletController) BlockHeaderVerbose(blockHash *chainhash.Hash) (*btcjson.GetBlockHeaderVerboseResult, error) {
return w.Client.GetBlockHeaderVerbose(blockHash)
}

// Fetch info about transaction from mempool or blockchain, requires node to have enabled transaction index
Expand Down
3 changes: 3 additions & 0 deletions walletcontroller/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
staking "github.com/babylonlabs-io/babylon/btcstaking"
"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"
Expand Down Expand Up @@ -71,6 +72,8 @@ type WalletController interface {
ListOutputs(onlySpendable bool) ([]Utxo, error)
TxDetails(txHash *chainhash.Hash, pkScript []byte) (*notifier.TxConfirmation, TxStatus, error)
Tx(txHash *chainhash.Hash) (*btcutil.Tx, error)
TxVerbose(txHash *chainhash.Hash) (*btcjson.TxRawResult, error)
BlockHeaderVerbose(blockHash *chainhash.Hash) (*btcjson.GetBlockHeaderVerboseResult, error)
SignBip322NativeSegwit(msg []byte, address btcutil.Address) (wire.TxWitness, error)
// SignOneInputTaprootSpendingTransaction signs transactions with one taproot input that
// uses script spending path.
Expand Down

0 comments on commit a97cd8c

Please sign in to comment.