Skip to content
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

fix(sew): limit go routine spawn for activating delegation #84

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

### Bug Fixes

* [#84](https://github.com/babylonlabs-io/vigilante/pull/84) fix spawning more go routines than needed when activating
delegations, add more logging


## v0.13.0

### Improvements
Expand Down
13 changes: 11 additions & 2 deletions btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (sew *StakingEventWatcher) checkBabylonDelegations(status btcstakingtypes.B
return fmt.Errorf("error fetching active delegations from babylon: %v", err)
}

sew.logger.Debugf("fetched %d delegations from babylon", len(delegations))
sew.logger.Debugf("fetched %d delegations from babylon by status %s", len(delegations), status)

for _, delegation := range delegations {
addDel(delegation)
Expand Down Expand Up @@ -484,7 +484,7 @@ func (sew *StakingEventWatcher) handleUnbondedDelegations() {
spendEv, err := sew.btcNotifier.RegisterSpendNtfn(
&stakingOutpoint,
activeDel.stakingTx.TxOut[activeDel.stakingOutputIdx].PkScript,
uint32(activeDel.delegationStartHeight),
activeDel.delegationStartHeight,
)

if err != nil {
Expand Down Expand Up @@ -539,6 +539,10 @@ func (sew *StakingEventWatcher) checkBtcForStakingTx() error {
}

for _, del := range delegations {
if del.ActivationInProgress {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better if we didn't get all the tracked delegations at once, because it could happen that we load the delegations to iterate, after that someone updates the del.ActivationInProgress value and this iterator would not know about it

If we are okay with this scenario the code looks good to me

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree with what you say. Tho I do think there's not a high chance for this to happen. What approach would you recommend, first thing that come to my mind was using Iterators

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, like an iterator would work or a function which gets the next del that is not activated

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will create an issue to improve this, not a high priority, worst case is we retry

continue
}

txHash := del.StakingTx.TxHash()
details, status, err := sew.btcClient.TxDetails(&txHash, del.StakingTx.TxOut[del.StakingOutputIdx].PkScript)
if err != nil {
Expand Down Expand Up @@ -571,6 +575,10 @@ func (sew *StakingEventWatcher) activateBtcDelegation(
ctx, cancel := sew.quitContext()
defer cancel()

if err := sew.pendingTracker.UpdateActivation(stakingTxHash, true); err != nil {
sew.logger.Debugf("skipping tx %s is not in pending tracker, err: %v", stakingTxHash, err)
}

_ = retry.Do(func() error {
verified, err := sew.babylonNodeAdapter.IsDelegationVerified(stakingTxHash)
if err != nil {
Expand All @@ -590,6 +598,7 @@ func (sew *StakingEventWatcher) activateBtcDelegation(
sew.metrics.ReportedActivateDelegationsCounter.Inc()

sew.pendingTracker.RemoveDelegation(stakingTxHash)
sew.logger.Debugf("staking tx activated %s", stakingTxHash.String())

return nil
},
Expand Down
16 changes: 16 additions & 0 deletions btcstaking-tracker/stakingeventwatcher/tracked_delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type TrackedDelegation struct {
StakingOutputIdx uint32
UnbondingOutput *wire.TxOut
DelegationStartHeight uint32
ActivationInProgress bool
}

type TrackedDelegations struct {
Expand Down Expand Up @@ -118,3 +119,18 @@ func (td *TrackedDelegations) HasDelegationChanged(
// The delegation exists but hasn't changed
return false, true
}

func (td *TrackedDelegations) UpdateActivation(tx chainhash.Hash, inProgress bool) error {
td.mu.Lock()
defer td.mu.Unlock()

delegation, ok := td.mapping[tx]

if !ok {
return fmt.Errorf("delegation with tx hash %s not found", tx.String())
}

delegation.ActivationInProgress = inProgress

return nil
}
Loading