From 27de64d4a67bb9484c46af93beebd72528b1c16d Mon Sep 17 00:00:00 2001 From: Lazar Date: Wed, 6 Nov 2024 13:54:39 +0100 Subject: [PATCH] use iter for stale reads --- .../stakingeventwatcher/stakingeventwatcher.go | 7 +------ .../stakingeventwatcher/tracked_delegations.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go b/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go index 6eb90dee..95c131be 100644 --- a/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go +++ b/btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go @@ -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 } diff --git a/btcstaking-tracker/stakingeventwatcher/tracked_delegations.go b/btcstaking-tracker/stakingeventwatcher/tracked_delegations.go index 7bbcecf3..5d2ddce6 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,