-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(sew): ADR-029 generalized unbonding and babylon v0.13.0 #87
Changes from 7 commits
e882600
8835d63
8775b06
752dc39
4cbe4de
91dad8a
ae53d68
94eb4e0
09c65f1
8be947f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -374,7 +374,8 @@ func (sew *StakingEventWatcher) waitForDelegationToStopBeingActive( | |
func (sew *StakingEventWatcher) reportUnbondingToBabylon( | ||
ctx context.Context, | ||
stakingTxHash chainhash.Hash, | ||
unbondingSignature *schnorr.Signature, | ||
stakeSpendingTx *wire.MsgTx, | ||
proof *btcstakingtypes.InclusionProof, | ||
) { | ||
_ = retry.Do(func() error { | ||
active, err := sew.babylonNodeAdapter.IsDelegationActive(stakingTxHash) | ||
|
@@ -394,7 +395,7 @@ func (sew *StakingEventWatcher) reportUnbondingToBabylon( | |
return nil | ||
} | ||
|
||
if err = sew.babylonNodeAdapter.ReportUnbonding(ctx, stakingTxHash, unbondingSignature); err != nil { | ||
if err = sew.babylonNodeAdapter.ReportUnbonding(ctx, stakingTxHash, stakeSpendingTx, proof); err != nil { | ||
sew.metrics.FailedReportedUnbondingTransactions.Inc() | ||
return fmt.Errorf("error reporting unbonding tx %s to babylon: %v", stakingTxHash, err) | ||
} | ||
|
@@ -426,7 +427,7 @@ func (sew *StakingEventWatcher) watchForSpend(spendEvent *notifier.SpendEvent, t | |
return | ||
} | ||
|
||
schnorrSignature, err := tryParseStakerSignatureFromSpentTx(spendingTx, td) | ||
_, err := tryParseStakerSignatureFromSpentTx(spendingTx, td) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this function actually checks whether stake spending transaction is unbonding transaction registered to Babylon. So imo we can leave it but we should send inclusion proof through So the only difference between error case and non err case is metrics which we are incrementing. |
||
delegationId := td.StakingTx.TxHash() | ||
spendingTxHash := spendingTx.TxHash() | ||
|
||
|
@@ -442,8 +443,14 @@ func (sew *StakingEventWatcher) watchForSpend(spendEvent *notifier.SpendEvent, t | |
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 | ||
} | ||
sew.logger.Debugf("found unbonding tx %s for staking tx %s", spendingTxHash, delegationId) | ||
sew.reportUnbondingToBabylon(quitCtx, delegationId, schnorrSignature) | ||
sew.reportUnbondingToBabylon(quitCtx, delegationId, spendingTx, proof) | ||
sew.logger.Debugf("unbonding tx %s for staking tx %s reported to babylon", spendingTxHash, delegationId) | ||
} | ||
|
||
|
@@ -454,6 +461,65 @@ 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tbh this should not happen as we are handling tx after we detected it was included in mempool/chain. Maybe panic here or have some error which we do not retry ? (imo this means our assumptions are broken so we should do something drastic) |
||
} | ||
details, status, err := sew.btcClient.TxDetails(&txHash, spendingTx.TxOut[0].PkScript) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if status != btcclient.TxInChain { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder whether we need this check i.e whether |
||
return nil, nil | ||
} | ||
|
||
btcTxs := types.GetWrappedTxs(details.Block) | ||
ib := types.NewIndexedBlock(int32(details.BlockHeight), &details.Block.Header, btcTxs) | ||
|
||
proof, err := ib.GenSPVProof(int(details.TxIndex)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return btcstakingtypes.NewInclusionProofFromSpvProof(proof), nil | ||
} | ||
|
||
// waitForStakeSpendInclusionProof polls btc until stake spend tx has inclusion proof built | ||
func (sew *StakingEventWatcher) waitForStakeSpendInclusionProof( | ||
ctx context.Context, | ||
spendingTx *wire.MsgTx, | ||
) (*btcstakingtypes.InclusionProof, error) { | ||
var ( | ||
proof *btcstakingtypes.InclusionProof | ||
err error | ||
) | ||
_ = retry.Do(func() error { | ||
proof, err = sew.buildSpendingTxProof(spendingTx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if proof == nil { | ||
return fmt.Errorf("proof not yet built") | ||
} | ||
|
||
return nil | ||
}, | ||
retry.Context(ctx), | ||
retryForever, | ||
fixedDelyTypeWithJitter, | ||
retry.MaxDelay(sew.cfg.CheckDelegationActiveInterval), | ||
retry.MaxJitter(sew.cfg.RetryJitter), | ||
retry.OnRetry(func(n uint, err error) { | ||
sew.logger.Debugf("retrying checking if stake spending tx is in chain %s. Attempt: %d. Err: %v", spendingTx.TxHash(), n, err) | ||
}), | ||
) | ||
|
||
return proof, nil | ||
} | ||
|
||
func (sew *StakingEventWatcher) handleUnbondedDelegations() { | ||
defer sew.wg.Done() | ||
for { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on
bbn
(bbn "github.com/babylonlabs-io/babylon/types"
)we have this helper -bbn.SerializeBTCTx(