Skip to content

Commit

Permalink
limit go routines
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Oct 18, 2024
1 parent d8508f7 commit 7063203
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go
Original file line number Diff line number Diff line change
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 {
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", stakingTxHash)
}

_ = 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
}

0 comments on commit 7063203

Please sign in to comment.