From 0b09f43107a7c0e8660eafb1cdc9848855500e8a Mon Sep 17 00:00:00 2001 From: pk910 Date: Thu, 31 Aug 2023 23:04:32 +0200 Subject: [PATCH] made cache requests none blocking, so the explorer gets useable with bigger public testnets / mainnet --- indexer/cache_logic.go | 2 +- indexer/epoch_stats.go | 16 ++++++++++++++++ indexer/indexer.go | 4 ++-- indexer/synchronizer.go | 4 ++-- indexer/votes.go | 8 +++++--- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/indexer/cache_logic.go b/indexer/cache_logic.go index 7de5e05f..af1b8122 100644 --- a/indexer/cache_logic.go +++ b/indexer/cache_logic.go @@ -147,7 +147,7 @@ func (cache *indexerCache) processFinalizedEpoch(epoch uint64) error { } // calculate votes - epochVotes := aggregateEpochVotes(canonicalMap, epoch, epochStats, epochTarget, false) + epochVotes := aggregateEpochVotes(canonicalMap, epoch, epochStats, epochTarget, false, true) if epochStats.validatorStats != nil { logger.Infof("epoch %v stats: %v validators (%v)", epoch, epochStats.validatorStats.ValidatorCount, epochStats.validatorStats.EligibleAmount) diff --git a/indexer/epoch_stats.go b/indexer/epoch_stats.go index 883eff9f..e20a19ac 100644 --- a/indexer/epoch_stats.go +++ b/indexer/epoch_stats.go @@ -96,6 +96,22 @@ func (cache *indexerCache) removeEpochStats(epochStats *EpochStats) { } } +func (epochStats *EpochStats) IsReady() bool { + if !epochStats.dutiesMutex.TryRLock() { + return false + } + epochStats.dutiesMutex.RUnlock() + return true +} + +func (epochStats *EpochStats) IsValidatorsReady() bool { + if !epochStats.validatorsMutex.TryRLock() { + return false + } + epochStats.validatorsMutex.RUnlock() + return true +} + func (epochStats *EpochStats) GetDependentStateRef() string { epochStats.dutiesMutex.RLock() defer epochStats.dutiesMutex.RUnlock() diff --git a/indexer/indexer.go b/indexer/indexer.go index ab0e8f21..ccdd5f8b 100644 --- a/indexer/indexer.go +++ b/indexer/indexer.go @@ -439,7 +439,7 @@ func (indexer *Indexer) getEpochVotes(epoch uint64, epochStats *EpochStats) *Epo } // calculate votes - return aggregateEpochVotes(canonicalMap, epoch, epochStats, epochTarget, false) + return aggregateEpochVotes(canonicalMap, epoch, epochStats, epochTarget, false, false) } func (indexer *Indexer) BuildLiveEpoch(epoch uint64) *dbtypes.Epoch { @@ -447,7 +447,7 @@ func (indexer *Indexer) BuildLiveEpoch(epoch uint64) *dbtypes.Epoch { headEpoch := utils.EpochOfSlot(headSlot) epochStats := indexer.getCachedEpochStats(epoch, headRoot) - if epochStats == nil { + if epochStats == nil || !epochStats.IsReady() { return nil } diff --git a/indexer/synchronizer.go b/indexer/synchronizer.go index b560e9a5..fb42496c 100644 --- a/indexer/synchronizer.go +++ b/indexer/synchronizer.go @@ -240,9 +240,9 @@ func (sync *synchronizerState) syncEpoch(syncEpoch uint64, lastTry bool, skipCli targetRoot = firstBlock.header.Message.ParentRoot } } - epochVotes := aggregateEpochVotes(sync.cachedBlocks, syncEpoch, epochStats, targetRoot, false) + epochVotes := aggregateEpochVotes(sync.cachedBlocks, syncEpoch, epochStats, targetRoot, false, true) if epochVotes.currentEpoch.targetVoteAmount < 10000 { - epochVotes = aggregateEpochVotes(sync.cachedBlocks, syncEpoch, epochStats, targetRoot, false) + epochVotes = aggregateEpochVotes(sync.cachedBlocks, syncEpoch, epochStats, targetRoot, false, true) } // save blocks diff --git a/indexer/votes.go b/indexer/votes.go index fde48931..4ab65466 100644 --- a/indexer/votes.go +++ b/indexer/votes.go @@ -22,7 +22,7 @@ type EpochVotes struct { ActivityMap map[uint64]bool } -func aggregateEpochVotes(blockMap map[uint64]*CacheBlock, epoch uint64, epochStats *EpochStats, targetRoot []byte, currentOnly bool) *EpochVotes { +func aggregateEpochVotes(blockMap map[uint64]*CacheBlock, epoch uint64, epochStats *EpochStats, targetRoot []byte, currentOnly bool, awaitValidaotStats bool) *EpochVotes { firstSlot := epoch * utils.Config.Chain.Config.SlotsPerEpoch lastSlot := firstSlot + utils.Config.Chain.Config.SlotsPerEpoch - 1 if !currentOnly { @@ -33,8 +33,10 @@ func aggregateEpochVotes(blockMap map[uint64]*CacheBlock, epoch uint64, epochSta // avait all lazy loaded data is available epochStats.dutiesMutex.RLock() defer epochStats.dutiesMutex.RUnlock() - epochStats.validatorsMutex.RLock() - defer epochStats.validatorsMutex.RUnlock() + if awaitValidaotStats { + epochStats.validatorsMutex.RLock() + defer epochStats.validatorsMutex.RUnlock() + } votes := EpochVotes{ ActivityMap: map[uint64]bool{},