Skip to content

Commit

Permalink
made cache requests none blocking, so the explorer gets useable with …
Browse files Browse the repository at this point in the history
…bigger public testnets / mainnet
  • Loading branch information
pk910 committed Aug 31, 2023
1 parent abe43f3 commit 0b09f43
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion indexer/cache_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions indexer/epoch_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,15 @@ 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 {
headSlot, headRoot := indexer.GetCanonicalHead()
headEpoch := utils.EpochOfSlot(headSlot)

epochStats := indexer.getCachedEpochStats(epoch, headRoot)
if epochStats == nil {
if epochStats == nil || !epochStats.IsReady() {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions indexer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions indexer/votes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{},
Expand Down

0 comments on commit 0b09f43

Please sign in to comment.