Skip to content

Commit

Permalink
chore: address pr comment and simplified the stking tx migration
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Nov 27, 2024
1 parent cce90a9 commit 9f1f0ab
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 56 deletions.
2 changes: 2 additions & 0 deletions itest/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,8 @@ func TestStakeFromPhase1(t *testing.T) {

resFundRawStkTx, err := rpcBtc.FundRawTransaction(&tx, btcjson.FundRawTransactionOpts{
FeeRate: btcjson.Float64(0.02),
// by setting the ChangePosition to 1 we make sure that the staking output will be at index 0
ChangePosition: btcjson.Int(1),
}, btcjson.Bool(false))
require.NoError(t, err)
require.NotNil(t, resFundRawStkTx)
Expand Down
134 changes: 78 additions & 56 deletions staker/stakerapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1329,52 +1329,72 @@ func (app *App) handlePreApprovalCmd(
stakingTx *wire.MsgTx,
stakingOutputIdx uint32,
) (*chainhash.Hash, error) {
btcTxHash, _, err := app.handleSendDelegationRequest(
cmd.stakerAddress,
cmd.stakingTime,
cmd.requiredDepthOnBtcChain,
cmd.fpBtcPks,
cmd.pop,
stakingTx,
stakingOutputIdx,
nil,
)
return btcTxHash, err
}

func (app *App) handleSendDelegationRequest(
stakerAddress btcutil.Address,
stakingTime uint16,
requiredDepthOnBtcChain uint32,
fpBtcPks []*secp256k1.PublicKey,
pop *cl.BabylonPop,
stakingTx *wire.MsgTx,
stakingOutputIdx uint32,
inclusionInfo *inclusionInfo,
) (btcTxHash *chainhash.Hash, btcDelTxHash string, err error) {
// just to pass to buildAndSendDelegation
fakeStoredTx, err := stakerdb.CreateTrackedTransaction(
stakingTx,
stakingOutputIdx,
cmd.stakingTime,
cmd.fpBtcPks,
babylonPopToDBPop(cmd.pop),
cmd.stakerAddress,
stakingTime,
fpBtcPks,
babylonPopToDBPop(pop),
stakerAddress,
)

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

stakingTxHash := stakingTx.TxHash()

req := newSendDelegationRequest(&stakingTxHash, nil, cmd.requiredDepthOnBtcChain)
_, delegationData, err := app.buildAndSendDelegation(
req := newSendDelegationRequest(&stakingTxHash, inclusionInfo, requiredDepthOnBtcChain)
resp, delegationData, err := app.buildAndSendDelegation(
req,
cmd.stakerAddress,
stakerAddress,
fakeStoredTx,
)

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

err = app.txTracker.AddTransactionSentToBabylon(
stakingTx,
stakingOutputIdx,
cmd.stakingTime,
cmd.fpBtcPks,
babylonPopToDBPop(cmd.pop),
cmd.stakerAddress,
stakingTime,
fpBtcPks,
babylonPopToDBPop(pop),
stakerAddress,
delegationData.Ud.UnbondingTransaction,
delegationData.Ud.UnbondingTxUnbondingTime,
)

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

app.wg.Add(1)
go app.checkForUnbondingTxSignaturesOnBabylon(&stakingTxHash)

return &stakingTxHash, nil
return &stakingTxHash, resp.TxHash, nil
}

func (app *App) handlePostApprovalCmd(
Expand Down Expand Up @@ -1518,60 +1538,49 @@ func (app *App) handleStakingCommands() {

case cmd := <-app.migrateStakingCmd:
stkTxHash := cmd.notifierTx.Tx.TxHash()
stakingParams, err := app.babylonClient.Params()
stkParams, err := app.babylonClient.Params()
if err != nil {
cmd.errChan <- err
continue
}

bestBlockHeight := app.currentBestBlockHeight.Load()
if err := app.waitForStakingTransactionConfirmation(
&stkTxHash,
cmd.parsedStakingTx.StakingOutput.PkScript,
stakingParams.ConfirmationTimeBlocks,
bestBlockHeight,
); err != nil {
// check confirmation is deep enough
if err := checkConfirmationDepth(bestBlockHeight, cmd.notifierTx.BlockHeight, stkParams.ConfirmationTimeBlocks); err != nil {
cmd.errChan <- err
continue
}

if err := app.txTracker.AddTransactionSentToBTC(
cmd.notifierTx.Tx,
uint32(cmd.parsedStakingTx.StakingOutputIdx),
_, btcDelTxHash, err := app.handleSendDelegationRequest(
cmd.stakerAddr,
cmd.parsedStakingTx.OpReturnData.StakingTime,
stkParams.ConfirmationTimeBlocks,
[]*btcec.PublicKey{cmd.parsedStakingTx.OpReturnData.FinalityProviderPublicKey.PubKey},
babylonPopToDBPop(cmd.pop),
cmd.stakerAddr,
); err != nil {
app.logger.WithError(err).Info("err to set tx as sent on BTC")
cmd.errChan <- err
continue
}

go func() {
// eventually tx is send to babylon and notifies the cmd request
storedTx, err := app.waitForTrackedTransactionState(stkTxHash, proto.TransactionState_SENT_TO_BABYLON, time.Second, 20)
if err != nil {
utils.PushOrQuit(
cmd.errChan,
err,
app.quit,
)
app.logger.WithFields(logrus.Fields{
"stakingTxHash": stkTxHash,
}).Debugf("BTC delegation waited for too long to become active, check the status manually")
return
}

cmd.pop,
cmd.notifierTx.Tx,
uint32(cmd.parsedStakingTx.StakingOutputIdx),
app.newBtcInclusionInfo(cmd.notifierTx),
)
if err != nil {
utils.PushOrQuit(
cmd.successChanTxHash,
storedTx.BtcDelegationTxHash,
cmd.errChan,
err,
app.quit,
)
app.logger.WithFields(logrus.Fields{
"consumerBtcDelegationTxHash": storedTx.BtcDelegationTxHash,
}).Debugf("Sending BTC delegation was a success")
}()
"stakingTxHash": stkTxHash,
}).Debugf("BTC delegation waited for too long to become active, check the status manually")
return
}

utils.PushOrQuit(
cmd.successChanTxHash,
btcDelTxHash,
app.quit,
)
app.logger.WithFields(logrus.Fields{
"consumerBtcDelegationTxHash": btcDelTxHash,
}).Debugf("Sending BTC delegation was a success")
case <-app.quit:
return
}
Expand Down Expand Up @@ -2343,3 +2352,16 @@ func (app *App) unlockAndCreatePop(stakerAddress btcutil.Address) (*cl.BabylonPo
stakerAddress,
)
}

func checkConfirmationDepth(tipBlockHeight, txInclusionBlockHeight, confirmationTimeBlocks uint32) error {
if txInclusionBlockHeight >= tipBlockHeight {
return fmt.Errorf("inclusion block height: %d should be lower than current tip: %d", txInclusionBlockHeight, tipBlockHeight)
}
if (tipBlockHeight - txInclusionBlockHeight) < confirmationTimeBlocks {
return fmt.Errorf(
"BTC tx not deep enough, current tip: %d, tx inclusion height: %d, confirmations needed: %d",
tipBlockHeight, txInclusionBlockHeight, confirmationTimeBlocks,
)
}
return nil
}

0 comments on commit 9f1f0ab

Please sign in to comment.