Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(submitter): handle no change output #79

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Improvements

* [#79](https://github.com/babylonlabs-io/vigilante/pull/79) handle no change output when building tx

* [#77](https://github.com/babylonlabs-io/vigilante/pull/77) add arm64 static build

* [#76](https://github.com/babylonlabs-io/vigilante/pull/76) add goreleaser
Expand Down
55 changes: 34 additions & 21 deletions submitter/relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,9 @@ func (rl *Relayer) ChainTwoTxAndSend(data1 []byte, data2 []byte) (*types.BtcTxIn
func (rl *Relayer) buildTxWithData(data []byte, firstTx *wire.MsgTx) (*types.BtcTxInfo, error) {
tx := wire.NewMsgTx(wire.TxVersion)

if firstTx != nil {
isFirstTx := firstTx != nil
Lazar955 marked this conversation as resolved.
Show resolved Hide resolved

if isFirstTx {
txID := firstTx.TxHash()
outPoint := wire.NewOutPoint(&txID, changePosition)
txIn := wire.NewTxIn(outPoint, nil, nil)
Expand All @@ -514,33 +516,45 @@ func (rl *Relayer) buildTxWithData(data []byte, firstTx *wire.MsgTx) (*types.Btc
return nil, err
}

// we want to ensure that firstTx has change output, but for the second transaction we can ignore this
hasChange := len(rawTxResult.Transaction.TxOut) > changePosition
// fail so we retry, first tx must have change output
if isFirstTx && !hasChange {
return nil, fmt.Errorf("transaction doesn't have change output %s", rawTxResult.Transaction.TxID())
Lazar955 marked this conversation as resolved.
Show resolved Hide resolved
}

rl.logger.Debugf("Building a BTC tx using %s with data %x", rawTxResult.Transaction.TxID(), data)

_, addresses, _, err := txscript.ExtractPkScriptAddrs(
rawTxResult.Transaction.TxOut[changePosition].PkScript,
rl.GetNetParams(),
)
if hasChange {
_, addresses, _, err := txscript.ExtractPkScriptAddrs(
rawTxResult.Transaction.TxOut[changePosition].PkScript,
rl.GetNetParams(),
)

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

if len(addresses) == 0 {
return nil, errors.New("no change address found")
}
if len(addresses) == 0 {
return nil, errors.New("no change address found")
}

changeAddr := addresses[0]
rl.logger.Debugf("Got a change address %v", changeAddr.String())
rl.logger.Debugf("Got a change address %v", addresses[0].String())
}

txSize, err := calculateTxVirtualSize(rawTxResult.Transaction)
if err != nil {
return nil, err
}

changeAmount := btcutil.Amount(rawTxResult.Transaction.TxOut[changePosition].Value)
var changeAmount btcutil.Amount
if hasChange {
changeAmount = btcutil.Amount(rawTxResult.Transaction.TxOut[changePosition].Value)
}

minRelayFee := rl.calcMinRelayFee(txSize)

if changeAmount < minRelayFee {
if hasChange && changeAmount < minRelayFee {
return nil, fmt.Errorf("the value of the utxo is not sufficient for relaying the tx. Require: %v. Have: %v", minRelayFee, changeAmount)
}

Expand All @@ -550,7 +564,7 @@ func (rl *Relayer) buildTxWithData(data []byte, firstTx *wire.MsgTx) (*types.Btc
txFee = minRelayFee
}
// ensuring the tx fee is not higher than the utxo value
if changeAmount < txFee {
if hasChange && changeAmount < txFee {
return nil, fmt.Errorf("the value of the utxo is not sufficient for paying the calculated fee of the tx. Calculated: %v. Have: %v", txFee, changeAmount)
}

Expand All @@ -568,18 +582,17 @@ func (rl *Relayer) buildTxWithData(data []byte, firstTx *wire.MsgTx) (*types.Btc

change := changeAmount - txFee

if change < dustThreshold {
if hasChange && change < dustThreshold {
return nil, fmt.Errorf("change amount is %v less then dust treshold %v", change, dustThreshold)
}

rl.logger.Debugf("Successfully composed a BTC tx: tx fee: %v, output value: %v, tx size: %v, hex: %v",
txFee, changeAmount, txSize, hex.EncodeToString(signedTxBytes.Bytes()))

return &types.BtcTxInfo{
Tx: tx,
ChangeAddress: changeAddr,
Size: txSize,
Fee: txFee,
Tx: tx,
Size: txSize,
Fee: txFee,
}, nil
}

Expand Down
9 changes: 4 additions & 5 deletions types/ckpt_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ type CheckpointInfo struct {

// BtcTxInfo stores information of a BTC tx as part of a checkpoint
type BtcTxInfo struct {
TxId *chainhash.Hash
Tx *wire.MsgTx
ChangeAddress btcutil.Address
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not using ChangeAddress, removed it

Size int64 // the size of the BTC tx
Fee btcutil.Amount // tx fee cost by the BTC tx
TxId *chainhash.Hash
Tx *wire.MsgTx
Size int64 // the size of the BTC tx
Fee btcutil.Amount // tx fee cost by the BTC tx
}
Loading