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

chore: add semaphore to rate limit activations #136

Merged
merged 3 commits into from
Dec 11, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
## Unreleased

### Bug Fixes

* [#138](https://github.com/babylonlabs-io/vigilante/pull/138) fix: panic in SendCheckpointToBTC

### Improvements

* [#136](https://github.com/babylonlabs-io/vigilante/pull/136) rate limit activations

## v0.18.0

### Improvements
Expand Down
18 changes: 15 additions & 3 deletions btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"golang.org/x/sync/semaphore"
"sync"
"sync/atomic"
"time"
Expand All @@ -26,8 +27,9 @@ import (
)

var (
fixedDelyTypeWithJitter = retry.DelayType(retry.CombineDelay(retry.FixedDelay, retry.RandomDelay))
retryForever = retry.Attempts(0)
fixedDelyTypeWithJitter = retry.DelayType(retry.CombineDelay(retry.FixedDelay, retry.RandomDelay))
retryForever = retry.Attempts(0)
maxConcurrentActivations = int64(1000)
)

func (sew *StakingEventWatcher) quitContext() (context.Context, func()) {
Expand Down Expand Up @@ -83,6 +85,7 @@ type StakingEventWatcher struct {
unbondingDelegationChan chan *newDelegation
unbondingRemovalChan chan *delegationInactive
currentBestBlockHeight atomic.Uint32
activationLimiter *semaphore.Weighted
}

func NewStakingEventWatcher(
Expand All @@ -106,6 +109,7 @@ func NewStakingEventWatcher(
inProgressTracker: NewTrackedDelegations(),
unbondingDelegationChan: make(chan *newDelegation),
unbondingRemovalChan: make(chan *delegationInactive),
activationLimiter: semaphore.NewWeighted(maxConcurrentActivations), // todo(lazar): this should be in config
}
}

Expand Down Expand Up @@ -613,6 +617,11 @@ func (sew *StakingEventWatcher) checkBtcForStakingTx() {
continue
}

if err := sew.activationLimiter.Acquire(context.Background(), 1); err != nil {
sew.logger.Warnf("error acquiring a activation semaphore %s", err)
continue
}

if _, err = sew.inProgressTracker.AddDelegation(
del.StakingTx,
del.StakingOutputIdx,
Expand All @@ -624,7 +633,10 @@ func (sew *StakingEventWatcher) checkBtcForStakingTx() {
continue
}

go sew.activateBtcDelegation(txHash, proof, details.Block.BlockHash(), params.ConfirmationTimeBlocks)
go func() {
defer sew.activationLimiter.Release(1)
sew.activateBtcDelegation(txHash, proof, details.Block.BlockHash(), params.ConfirmationTimeBlocks)
}()
}
}

Expand Down
Loading