Skip to content

Commit

Permalink
pr comments per: @KonradStaniec
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Oct 23, 2024
1 parent ae53d68 commit 94eb4e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package stakingeventwatcher

import (
"bytes"
"context"
"fmt"

Expand Down Expand Up @@ -116,20 +115,19 @@ func (bca *BabylonClientAdapter) ReportUnbonding(
inclusionProof *btcstakingtypes.InclusionProof) error {
signer := bca.babylonClient.MustGetAddr()

var stakeSpendingTxBuf bytes.Buffer
if err := stakeSpendingTx.Serialize(&stakeSpendingTxBuf); err != nil {
stakeSpendingBytes, err := bbn.SerializeBTCTx(stakeSpendingTx)
if err != nil {
return err
}

msg := btcstakingtypes.MsgBTCUndelegate{
Signer: signer,
StakingTxHash: stakingTxHash.String(),
StakeSpendingTx: stakeSpendingTxBuf.Bytes(),
StakeSpendingTx: stakeSpendingBytes,
StakeSpendingTxInclusionProof: inclusionProof,
}

resp, err := bca.babylonClient.ReliablySendMsg(ctx, &msg, []*errors.Error{}, []*errors.Error{})

if err != nil && resp != nil {
return fmt.Errorf("msg MsgBTCUndelegate failed exeuction with code %d and error %w", resp.Code, err)
}
Expand All @@ -149,7 +147,7 @@ func (bca *BabylonClientAdapter) BtcClientTipHeight() (uint32, error) {
return 0, fmt.Errorf("failed to retrieve btc light client tip from babyln: %w", err)
}

return uint32(resp.Header.Height), nil
return resp.Header.Height, nil
}

// ActivateDelegation provides inclusion proof to activate delegation
Expand Down
9 changes: 7 additions & 2 deletions btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,19 @@ func (sew *StakingEventWatcher) watchForSpend(spendEvent *notifier.SpendEvent, t
// As we only care about unbonding transactions, we do not need to take additional actions.
// We start polling babylon for delegation to stop being active, and then delete it from unbondingTracker.
sew.logger.Debugf("Spending tx %s for staking tx %s is not unbonding tx. Info: %v", spendingTxHash, delegationId, err)
proof, err := sew.waitForStakeSpendInclusionProof(quitCtx, spendingTx)
if err != nil {
sew.logger.Errorf("unbonding tx %s for staking tx %s proof not built", spendingTxHash, delegationId)
return
}
sew.reportUnbondingToBabylon(quitCtx, delegationId, spendingTx, proof)
sew.waitForDelegationToStopBeingActive(quitCtx, delegationId)
} else {
sew.metrics.DetectedUnbondingTransactionsCounter.Inc()
// We found valid unbonding tx. We need to try to report it to babylon.
// We stop reporting if delegation is no longer active or we succeed.
proof, err := sew.waitForStakeSpendInclusionProof(quitCtx, spendingTx)
if err != nil {
// todo(lazar): log and report a metric, push to removalChan
sew.logger.Errorf("unbonding tx %s for staking tx %s proof not built", spendingTxHash, delegationId)
return
}
Expand All @@ -464,7 +469,7 @@ func (sew *StakingEventWatcher) watchForSpend(spendEvent *notifier.SpendEvent, t
func (sew *StakingEventWatcher) buildSpendingTxProof(spendingTx *wire.MsgTx) (*btcstakingtypes.InclusionProof, error) {
txHash := spendingTx.TxHash()
if len(spendingTx.TxOut) == 0 {
return nil, fmt.Errorf("stake spending tx has no outputs")
panic(fmt.Errorf("stake spending tx has no outputs %s", spendingTx.TxHash().String())) // this is a software error
}
details, status, err := sew.btcClient.TxDetails(&txHash, spendingTx.TxOut[0].PkScript)
if err != nil {
Expand Down

0 comments on commit 94eb4e0

Please sign in to comment.