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(swe): prevent stale reads #96

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

* [#94](https://github.com/babylonlabs-io/vigilante/pull/94) adds gosec and fixes gosec issues
* [#96](https://github.com/babylonlabs-io/vigilante/pull/96) fixes potential stale data read

## v0.15.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,7 @@ func (sew *StakingEventWatcher) handlerVerifiedDelegations() {
// checkBtcForStakingTx gets a snapshot of current Delegations in cache
// checks if staking tx is in BTC, generates a proof and invokes sending of MsgAddBTCDelegationInclusionProof
func (sew *StakingEventWatcher) checkBtcForStakingTx() error {
delegations := sew.pendingTracker.GetDelegations()
if delegations == nil {
return nil
}

for _, del := range delegations {
for del := range sew.pendingTracker.DelegationsIter() {
if del.ActivationInProgress {
continue
}
Expand Down
15 changes: 15 additions & 0 deletions btcstaking-tracker/stakingeventwatcher/tracked_delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stakingeventwatcher

import (
"fmt"
"iter"
"sync"

"github.com/btcsuite/btcd/chaincfg/chainhash"
Expand Down Expand Up @@ -58,6 +59,20 @@ func (td *TrackedDelegations) GetDelegations() []*TrackedDelegation {
return delegations
}

func (td *TrackedDelegations) DelegationsIter() iter.Seq[*TrackedDelegation] {
return func(yield func(*TrackedDelegation) bool) {
td.mu.RLock()
defer td.mu.RUnlock()

// we lock for the entirety of the iteration
for _, v := range td.mapping {
if !yield(v) {
return
}
}
}
}

func (td *TrackedDelegations) AddDelegation(
StakingTx *wire.MsgTx,
StakingOutputIdx uint32,
Expand Down
Loading