Skip to content

Commit

Permalink
first tx handle and ignore for 2nd
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Oct 14, 2024
1 parent ee17d0f commit 3a74e9f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
52 changes: 30 additions & 22 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

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

if len(rawTxResult.Transaction.TxOut) <= changePosition {
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())
}

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 @@ -554,7 +563,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 @@ -572,18 +581,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
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
}

0 comments on commit 3a74e9f

Please sign in to comment.