Skip to content

Commit

Permalink
various small improvements & performance fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Sep 14, 2023
1 parent e0d198a commit 70dcd3e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
8 changes: 8 additions & 0 deletions indexer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ func (client *IndexerClient) pollLatestBlocks() error {
if err != nil {
return err
}
err = client.ensureEpochStats(utils.EpochOfSlot(currentBlock.Slot), currentBlock.Root)
if err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -502,6 +506,10 @@ func (client *IndexerClient) processBlockEvent(evt *rpctypes.StandardV1StreamedB
if err != nil {
return err
}
err = client.ensureEpochStats(utils.EpochOfSlot(currentBlock.Slot), currentBlock.Root)
if err != nil {
return err
}
client.setHeadBlock(evt.Block, uint64(evt.Slot))
return nil
}
Expand Down
1 change: 1 addition & 0 deletions indexer/epoch_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (client *IndexerClient) ensureEpochStats(epoch uint64, head []byte) error {
proposerRsp, err = client.rpcClient.GetProposerDuties(epoch)
if err != nil {
logger.WithField("client", client.clientName).Warnf("could not load proposer duties for epoch %v: %v", epoch, err)
return fmt.Errorf("could not find proposer duties for epoch %v: %v", epoch, err)
}
if proposerRsp == nil {
return fmt.Errorf("could not find proposer duties for epoch %v", epoch)
Expand Down
4 changes: 4 additions & 0 deletions indexer/votes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indexer
import (
"bytes"
"fmt"
"time"

"github.com/pk910/light-beaconchain-explorer/utils"
)
Expand All @@ -23,6 +24,8 @@ type EpochVotes struct {
}

func aggregateEpochVotes(blockMap map[uint64]*CacheBlock, epoch uint64, epochStats *EpochStats, targetRoot []byte, currentOnly bool, awaitDutiesLoaded bool) *EpochVotes {
t1 := time.Now()

firstSlot := epoch * utils.Config.Chain.Config.SlotsPerEpoch
lastSlot := firstSlot + utils.Config.Chain.Config.SlotsPerEpoch - 1
if !currentOnly {
Expand Down Expand Up @@ -104,5 +107,6 @@ func aggregateEpochVotes(blockMap map[uint64]*CacheBlock, epoch uint64, epochSta
}
}

logger.Debugf("aggregated epoch %v votes in %v\n", epoch, time.Since(t1))
return &votes
}
13 changes: 10 additions & 3 deletions services/beaconservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,19 @@ func (bs *BeaconService) GetProposerAssignments(firstEpoch uint64, lastEpoch uin
for epochIdx := int64(firstEpoch); epochIdx >= int64(idxMinEpoch) && epochIdx >= int64(lastEpoch); epochIdx-- {
epoch = uint64(epochIdx)
epochStats := bs.indexer.GetCachedEpochStats(epoch)

if epochStats != nil {
synchronizedEpochs[epoch] = true
for slot, vidx := range epochStats.GetProposerAssignments() {
proposerAssignments[slot] = vidx
proposers := epochStats.TryGetProposerAssignments()
if proposers != nil {
for slot, vidx := range proposers {
proposerAssignments[slot] = vidx
}
} else {
epochStats = nil
}
} else {
}
if epochStats == nil {
for idx := uint64(0); idx < utils.Config.Chain.Config.SlotsPerEpoch; idx++ {
proposerAssignments[(epoch*utils.Config.Chain.Config.SlotsPerEpoch)+idx] = math.MaxInt64
}
Expand Down
2 changes: 2 additions & 0 deletions services/frontendcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func (fc *FrontendCacheService) SetFrontendCache(pageKey string, value interface
}

func (fc *FrontendCacheService) ProcessCachedPage(pageKey string, caching bool, returnValue interface{}, buildFn func(pageCall *FrontendCacheProcessingPage) interface{}) interface{} {
//fmt.Printf("page call %v (goid: %v)\n", pageKey, utils.Goid())

fc.processingMutex.Lock()
processingPage := fc.processingDict[pageKey]
if processingPage != nil {
Expand Down
32 changes: 32 additions & 0 deletions utils/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package utils

import (
"bytes"
"runtime"
"strconv"
)

var (
goroutinePrefix = []byte("goroutine ")
)

// This is terrible, slow, and should never be used.
func Goid() int {
buf := make([]byte, 32)
n := runtime.Stack(buf, false)
buf = buf[:n]
// goroutine 1 [running]: ...

buf, ok := bytes.CutPrefix(buf, goroutinePrefix)
if !ok {
return 0
}

i := bytes.IndexByte(buf, ' ')
if i < 0 {
return 0
}

res, _ := strconv.Atoi(string(buf[:i]))
return res
}

0 comments on commit 70dcd3e

Please sign in to comment.