From 7063203edfe2f52baad845b4458fcdc064403dcc Mon Sep 17 00:00:00 2001 From: Lazar Date: Fri, 18 Oct 2024 11:02:42 +0200 Subject: [PATCH] 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 331ff72..7565783 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 d5ec235..7bbcecf 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 +}