Skip to content

Commit

Permalink
cleanup, use changePosition
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Aug 16, 2024
1 parent 37d6109 commit a0d6c40
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 54 deletions.
8 changes: 1 addition & 7 deletions e2etest/submitter_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package e2etest

import (
"github.com/babylonlabs-io/vigilante/submitter/relayer"
"math/rand"
"testing"
"time"
Expand Down Expand Up @@ -175,12 +174,7 @@ func TestSubmitterSubmissionReplace(t *testing.T) {
// 2. outputs with different values
// 3. different signatures
require.Equal(t, sendTransactions[1].MsgTx().TxIn[0].PreviousOutPoint, resendTx2.MsgTx().TxIn[0].PreviousOutPoint)

resendTxOutIdx, err := relayer.IndexOfChangeAddr(resendTx2.MsgTx().TxOut)
require.NoError(t, err)
ogTxOutIdx, err := relayer.IndexOfChangeAddr(sendTransactions[1].MsgTx().TxOut)
require.NoError(t, err)
require.Less(t, resendTx2.MsgTx().TxOut[resendTxOutIdx].Value, sendTransactions[1].MsgTx().TxOut[ogTxOutIdx].Value)
require.Less(t, resendTx2.MsgTx().TxOut[1].Value, sendTransactions[1].MsgTx().TxOut[1].Value)
require.NotEqual(t, sendTransactions[1].MsgTx().TxIn[0].Witness[0], resendTx2.MsgTx().TxIn[0].Witness[0])

// mine a block with those replacement transactions just to be sure they execute correctly
Expand Down
9 changes: 3 additions & 6 deletions e2etest/test_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,14 @@ func StartManager(t *testing.T, numMatureOutputsInWallet uint32) *TestManager {
return true
}, eventuallyWaitTimeOut, eventuallyPollTime)

//walletPrivKey, err := testRpcClient.DumpPrivKey(minerAddressDecoded)
//require.NoError(t, err)

return &TestManager{
TestRpcClient: testRpcClient,
BabylonHandler: bh,
BabylonClient: babylonClient,
BitcoindHandler: btcHandler,
BTCClient: btcClient,
Config: cfg,
//WalletPrivKey: walletPrivKey.PrivKey,
WalletPrivKey: walletPrivKey,
WalletPrivKey: walletPrivKey,
}
}

Expand Down Expand Up @@ -245,14 +241,15 @@ func importPrivateKey(btcHandler *BitcoindTestHandler) (*btcec.PrivateKey, error
return nil, err
}

// "combo" allows us to import a key and handle multiple types of btc scripts with a single descriptor command.
descriptor := fmt.Sprintf("combo(%s)", wif.String())

// Create the JSON descriptor object.
descJSON, err := json.Marshal([]map[string]interface{}{
{
"desc": descriptor,
"active": true,
"timestamp": "now",
"timestamp": "now", // tells Bitcoind to start scanning from the current blockchain height
"label": "test key",
},
})
Expand Down
29 changes: 7 additions & 22 deletions submitter/relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,7 @@ func (rl *Relayer) resendSecondTxOfCheckpointToBTC(tx2 *types.BtcTxInfo, bumpedF
bumpedFee = balance
}

idx, err := IndexOfChangeAddr(tx2.Tx.TxOut)
if err != nil {
return nil, err
}

tx2.Tx.TxOut[idx].Value = int64(balance - bumpedFee)
tx2.Tx.TxOut[1].Value = int64(balance - bumpedFee)

// resign the tx as the output is changed
tx, err := rl.signTx(tx2.Tx)
Expand Down Expand Up @@ -273,7 +268,6 @@ func (rl *Relayer) convertCkptToTwoTxAndSubmit(ckpt *ckpttypes.RawCheckpointResp

// this is to wait for btcwallet to update utxo database so that
// the tx that tx1 consumes will not appear in the next unspent txs lit
// todo: Lazar chech if this needed?
time.Sleep(1 * time.Second)

rl.logger.Infof("Sent two txs to BTC for checkpointing epoch %v, first txid: %s, second txid: %s",
Expand Down Expand Up @@ -323,16 +317,11 @@ func (rl *Relayer) ChainTwoTxAndSend(
return nil, nil, fmt.Errorf("failed to send tx1 to BTC: %w", err)
}

changeIdx, err := IndexOfChangeAddr(tx1.Tx.TxOut)
if err != nil {
return nil, nil, err
}

changeUtxo := &types.UTXO{
TxID: tx1.TxId,
Vout: 1,
ScriptPK: tx1.Tx.TxOut[changeIdx].PkScript,
Amount: btcutil.Amount(tx1.Tx.TxOut[changeIdx].Value),
ScriptPK: tx1.Tx.TxOut[1].PkScript,
Amount: btcutil.Amount(tx1.Tx.TxOut[1].Value),
Addr: tx1.ChangeAddress,
}

Expand Down Expand Up @@ -395,21 +384,17 @@ func (rl *Relayer) buildTxWithData(
tx.AddTxOut(wire.NewTxOut(0, dataScript))

feeRate := btcutil.Amount(rl.getFeeRate()).ToBTC()
changePosition := 1
rawTxResult, err := rl.BTCWallet.FundRawTransaction(tx, btcjson.FundRawTransactionOpts{
FeeRate: &feeRate,
//EstimateMode: &btcjson.EstimateModeConservative,
FeeRate: &feeRate,
ChangePosition: &changePosition,
}, nil)
if err != nil {
return nil, err
}

idxTxOut, err := IndexOfChangeAddr(rawTxResult.Transaction.TxOut)
if err != nil {
return nil, err
}

_, addresses, _, err := txscript.ExtractPkScriptAddrs(
rawTxResult.Transaction.TxOut[idxTxOut].PkScript,
rawTxResult.Transaction.TxOut[1].PkScript,
rl.GetNetParams(),
)

Expand Down
19 changes: 0 additions & 19 deletions submitter/relayer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package relayer

import (
"bytes"
"errors"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/mempool"
"github.com/btcsuite/btcd/wire"
Expand All @@ -22,20 +20,3 @@ func calculateTxVirtualSize(tx *wire.MsgTx) (int64, error) {

return mempool.GetTxVirtualSize(btcTx), nil
}

// IndexOfChangeAddr returns the index of the first transaction output with a non-zero value,
// indicating a change address, or an error if none is found.
func IndexOfChangeAddr(outs []*wire.TxOut) (uint, error) {
if outs == nil {
return 0, errors.New("nil outs param")
}

for index, out := range outs {
// A change address output must have a non-zero value.
if out.Value != 0 {
return uint(index), nil
}
}

return 0, errors.New("no TxOut with change addr found")
}

0 comments on commit a0d6c40

Please sign in to comment.