Skip to content

Commit

Permalink
manually add change output
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Oct 14, 2024
1 parent db1164f commit fa2cc74
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/vigilante/cmd/submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func GetSubmitterCmd() *cobra.Command {
cfg.Common.MaxRetryTimes,
submitterMetrics,
dbBackend,
cfg.BTC.WalletName,
)
if err != nil {
panic(fmt.Errorf("failed to create vigilante submitter: %w", err))
Expand Down
1 change: 1 addition & 0 deletions e2etest/monitor_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestMonitorBootstrap(t *testing.T) {
tm.Config.Common.MaxRetryTimes,
metrics.NewSubmitterMetrics(),
testutil.MakeTestBackend(t),
tm.Config.BTC.WalletName,
)

vigilantSubmitter.Start()
Expand Down
2 changes: 2 additions & 0 deletions e2etest/submitter_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func TestSubmitterSubmission(t *testing.T) {
tm.Config.Common.MaxRetryTimes,
metrics.NewSubmitterMetrics(),
testutil.MakeTestBackend(t),
tm.Config.BTC.WalletName,
)

vigilantSubmitter.Start()
Expand Down Expand Up @@ -144,6 +145,7 @@ func TestSubmitterSubmissionReplace(t *testing.T) {
tm.Config.Common.MaxRetryTimes,
metrics.NewSubmitterMetrics(),
testutil.MakeTestBackend(t),
tm.Config.BTC.WalletName,
)

vigilantSubmitter.Start()
Expand Down
2 changes: 1 addition & 1 deletion submitter/relayer/change_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestGetChangeAddress(t *testing.T) {
cfg := config.DefaultSubmitterConfig()
logger, err := config.NewRootLogger("auto", "debug")
require.NoError(t, err)
testRelayer := relayer.New(wallet, []byte("bbnt"), btctxformatter.CurrentVersion, submitterAddr,
testRelayer := relayer.New(wallet, btcConfig.WalletName, []byte("bbnt"), btctxformatter.CurrentVersion, submitterAddr,
submitterMetrics.RelayerMetrics, nil, &cfg, logger, testutil.MakeTestBackend(t))

// 1. only SegWit Bech32 addresses
Expand Down
24 changes: 19 additions & 5 deletions submitter/relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ type Relayer struct {
metrics *metrics.RelayerMetrics
config *config.SubmitterConfig
logger *zap.SugaredLogger
walletName string
}

func New(
wallet btcclient.BTCWallet,
walletName string,
tag btctxformatter.BabylonTag,
version btctxformatter.FormatVersion,
submitterAddress sdk.AccAddress,
Expand All @@ -74,6 +76,7 @@ func New(
return &Relayer{
Estimator: est,
BTCWallet: wallet,
walletName: walletName,
store: subStore,
tag: tag,
version: version,
Expand Down Expand Up @@ -487,9 +490,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)

isFirstTx := firstTx != nil
isSecondTx := firstTx != nil

if isFirstTx {
if isSecondTx {
txID := firstTx.TxHash()
outPoint := wire.NewOutPoint(&txID, changePosition)
txIn := wire.NewTxIn(outPoint, nil, nil)
Expand Down Expand Up @@ -518,9 +521,20 @@ func (rl *Relayer) buildTxWithData(data []byte, firstTx *wire.MsgTx) (*types.Btc

// 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())
// let's manually add a change output with 546 satoshis
if !isSecondTx && !hasChange {
changeAddr, err := rl.BTCWallet.GetRawChangeAddress(rl.walletName)
if err != nil {
return nil, fmt.Errorf("err getting raw change address %w", err)
}

changePkScript, err := txscript.PayToAddrScript(changeAddr)
if err != nil {
return nil, fmt.Errorf("failed to create script for change address: %s err %w", changeAddr, err)
}

changeOutput := wire.NewTxOut(546, changePkScript)
rawTxResult.Transaction.AddTxOut(changeOutput)
}

rl.logger.Debugf("Building a BTC tx using %s with data %x", rawTxResult.Transaction.TxID(), data)
Expand Down
2 changes: 2 additions & 0 deletions submitter/submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func New(
retrySleepTime, maxRetrySleepTime time.Duration, maxRetryTimes uint,
submitterMetrics *metrics.SubmitterMetrics,
db kvdb.Backend,
walletName string,
) (*Submitter, error) {
logger := parentLogger.With(zap.String("module", "submitter"))
var (
Expand Down Expand Up @@ -80,6 +81,7 @@ func New(

r := relayer.New(
btcWallet,
walletName,
checkpointTag,
btctxformatter.CurrentVersion,
submitterAddr,
Expand Down

0 comments on commit fa2cc74

Please sign in to comment.