From 7063203edfe2f52baad845b4458fcdc064403dcc Mon Sep 17 00:00:00 2001 From: Lazar Date: Fri, 18 Oct 2024 11:02:42 +0200 Subject: [PATCH 1/3] limit go routines --- .../stakingeventwatcher/stakingeventwatcher.go | 11 ++++++++++- .../stakingeventwatcher/tracked_delegations.go | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go b/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go index 331ff720..75657831 100644 --- a/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go +++ b/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go @@ -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 { @@ -539,6 +539,10 @@ func (sew *StakingEventWatcher) checkBtcForStakingTx() error { } for _, del := range delegations { + if del.ActivationInProgress { + continue + } + txHash := del.StakingTx.TxHash() details, status, err := sew.btcClient.TxDetails(&txHash, del.StakingTx.TxOut[del.StakingOutputIdx].PkScript) if err != nil { @@ -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", stakingTxHash) + } + _ = retry.Do(func() error { verified, err := sew.babylonNodeAdapter.IsDelegationVerified(stakingTxHash) if err != nil { @@ -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 }, diff --git a/btcstaking-tracker/stakingeventwatcher/tracked_delegations.go b/btcstaking-tracker/stakingeventwatcher/tracked_delegations.go index d5ec235c..7bbcecf3 100644 --- a/btcstaking-tracker/stakingeventwatcher/tracked_delegations.go +++ b/btcstaking-tracker/stakingeventwatcher/tracked_delegations.go @@ -13,6 +13,7 @@ type TrackedDelegation struct { StakingOutputIdx uint32 UnbondingOutput *wire.TxOut DelegationStartHeight uint32 + ActivationInProgress bool } type TrackedDelegations struct { @@ -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 +} From 789922a81e861fda3816df9bac26f203bb930b02 Mon Sep 17 00:00:00 2001 From: Lazar Date: Fri, 18 Oct 2024 11:08:40 +0200 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 6 ++++++ .../stakingeventwatcher/stakingeventwatcher.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa4032e2..80a5d10e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go b/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go index 75657831..39a0096c 100644 --- a/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go +++ b/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go @@ -576,7 +576,7 @@ func (sew *StakingEventWatcher) activateBtcDelegation( defer cancel() if err := sew.pendingTracker.UpdateActivation(stakingTxHash, true); err != nil { - sew.logger.Debugf("skipping tx %s is not in pending tracker", stakingTxHash) + sew.logger.Debugf("skipping tx %s is not in pending tracker, err: %v", stakingTxHash, err) } _ = retry.Do(func() error { From add9a62d177ada7d1a10db2408da8986803ffb2b Mon Sep 17 00:00:00 2001 From: Lazar Date: Fri, 18 Oct 2024 11:46:15 +0200 Subject: [PATCH 3/3] more logs --- btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go b/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go index 39a0096c..85b709da 100644 --- a/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go +++ b/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go @@ -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)