Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Sep 20, 2024
1 parent 3f13965 commit 7753e04
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
51 changes: 51 additions & 0 deletions btcclient/client_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package btcclient

import (
"fmt"
notifier "github.com/lightningnetwork/lnd/chainntnfs"

"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
Expand All @@ -15,6 +16,17 @@ import (
"github.com/babylonlabs-io/vigilante/netparams"
)

type TxStatus int

const (
TxNotFound TxStatus = iota
TxInMemPool
TxInChain
)
const (
txNotFoundErrMsgBitcoind = "No such mempool or blockchain transaction"
)

// NewWallet creates a new BTC wallet
// used by vigilant submitter
// a wallet is essentially a BTC client
Expand Down Expand Up @@ -125,3 +137,42 @@ func (c *Client) SignRawTransactionWithWallet(tx *wire.MsgTx) (*wire.MsgTx, bool
func (c *Client) GetRawTransaction(txHash *chainhash.Hash) (*btcutil.Tx, error) {
return c.Client.GetRawTransaction(txHash)
}

func notifierStateToWalletState(state notifier.TxConfStatus) TxStatus {
switch state {
case notifier.TxNotFoundIndex:
return TxNotFound
case notifier.TxFoundMempool:
return TxInMemPool
case notifier.TxFoundIndex:
return TxInChain
case notifier.TxNotFoundManually:
return TxNotFound
case notifier.TxFoundManually:
return TxInChain
default:
panic(fmt.Sprintf("unknown notifier state: %s", state))
}
}

func (c *Client) getTxDetails(req notifier.ConfRequest, msg string) (*notifier.TxConfirmation, TxStatus, error) {
res, state, err := notifier.ConfDetailsFromTxIndex(c.Client, req, msg)

if err != nil {
return nil, TxNotFound, err
}

return res, notifierStateToWalletState(state), nil
}

// TxDetails Fetch info about transaction from mempool or blockchain, requires node to have enabled transaction index
func (c *Client) TxDetails(txHash *chainhash.Hash, pkScript []byte) (*notifier.TxConfirmation, TxStatus, error) {
req, err := notifier.NewConfRequest(txHash, pkScript)

if err != nil {
return nil, TxNotFound, err
}

return c.getTxDetails(req, txNotFoundErrMsgBitcoind)

}
2 changes: 2 additions & 0 deletions btcclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
notifier "github.com/lightningnetwork/lnd/chainntnfs"

"github.com/babylonlabs-io/vigilante/config"
"github.com/babylonlabs-io/vigilante/types"
Expand Down Expand Up @@ -39,4 +40,5 @@ type BTCWallet interface {
FundRawTransaction(tx *wire.MsgTx, opts btcjson.FundRawTransactionOpts, isWitness *bool) (*btcjson.FundRawTransactionResult, error)
SignRawTransactionWithWallet(tx *wire.MsgTx) (*wire.MsgTx, bool, error)
GetRawTransaction(txHash *chainhash.Hash) (*btcutil.Tx, error)
TxDetails(txHash *chainhash.Hash, pkScript []byte) (*notifier.TxConfirmation, TxStatus, error)
}
7 changes: 5 additions & 2 deletions submitter/relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,16 @@ func (rl *Relayer) calculateBumpedFee(ckptInfo *types.CheckpointInfo) btcutil.Am

// resendSecondTxOfCheckpointToBTC resends the second tx of the checkpoint with bumpedFee
func (rl *Relayer) resendSecondTxOfCheckpointToBTC(tx2 *types.BtcTxInfo, bumpedFee btcutil.Amount) (*types.BtcTxInfo, error) {
found, err := rl.inMempool()
_, status, err := rl.TxDetails(rl.lastSubmittedCheckpoint.Tx2.TxId,
rl.lastSubmittedCheckpoint.Tx2.Tx.TxOut[changePosition].PkScript)
if err != nil {
return nil, err
}

// No need to resend, transaction already confirmed
if !found {
if status != btcclient.TxInMemPool && status != btcclient.TxNotFound {
rl.logger.Debugf("Transaction %v is already confirmed or has an unexpected state: %v",
rl.lastSubmittedCheckpoint.Tx2.TxId, status)
return nil, nil
}

Expand Down
18 changes: 18 additions & 0 deletions testutil/mocks/btcclient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7753e04

Please sign in to comment.