diff --git a/CHANGELOG.md b/CHANGELOG.md index 54ce638..fd032b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,8 @@ 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 +* [#98](https://github.com/babylonlabs-io/vigilante/pull/98) fixes golangci configuration ## v0.15.0 diff --git a/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go b/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go index a98d17e..b33fb81 100644 --- a/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go +++ b/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go @@ -575,12 +575,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() { - delegations := sew.pendingTracker.GetDelegations() - if delegations == nil { - return - } - - for _, del := range delegations { + for del := range sew.pendingTracker.DelegationsIter() { if del.ActivationInProgress { continue } diff --git a/btcstaking-tracker/stakingeventwatcher/tracked_delegations.go b/btcstaking-tracker/stakingeventwatcher/tracked_delegations.go index 1cf5eee..aad1f09 100644 --- a/btcstaking-tracker/stakingeventwatcher/tracked_delegations.go +++ b/btcstaking-tracker/stakingeventwatcher/tracked_delegations.go @@ -2,6 +2,7 @@ package stakingeventwatcher import ( "fmt" + "iter" "sync" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -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,