Skip to content

Commit

Permalink
cache genesis rpc response (front page does not need any rpc calls now)
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Sep 8, 2023
1 parent 603fe4f commit 52bfd04
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
9 changes: 9 additions & 0 deletions indexer/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type indexerCache struct {
epochStatsMap map[uint64][]*EpochStats
lastValidatorsEpoch int64
lastValidatorsResp *rpctypes.StandardV1StateValidatorsResponse
genesisResp *rpctypes.StandardV1GenesisResponse
validatorLoadingLimiter chan int
}

Expand Down Expand Up @@ -79,6 +80,14 @@ func (cache *indexerCache) setFinalizedHead(epoch int64, root []byte) {
}
}

func (cache *indexerCache) setGenesis(genesis *rpctypes.StandardV1GenesisResponse) {
cache.cacheMutex.Lock()
defer cache.cacheMutex.Unlock()
if cache.genesisResp == nil {
cache.genesisResp = genesis
}
}

func (cache *indexerCache) getFinalizedHead() (int64, []byte) {
cache.cacheMutex.RLock()
defer cache.cacheMutex.RUnlock()
Expand Down
1 change: 1 addition & 0 deletions indexer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (client *IndexerClient) checkIndexerClient() error {
if genesis.Data.GenesisForkVersion.String() != utils.Config.Chain.Config.GenesisForkVersion {
return fmt.Errorf("genesis fork version from RPC does not match the genesis fork version explorer configuration")
}
client.indexerCache.setGenesis(genesis)

// check syncronization state
syncStatus, err := client.rpcClient.GetNodeSyncing()
Expand Down
4 changes: 4 additions & 0 deletions indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ func (indexer *Indexer) GetRpcClient(archive bool, head []byte) *rpc.BeaconClien
return readyClient.rpcClient
}

func (indexer *Indexer) GetCachedGenesis() *rpctypes.StandardV1GenesisResponse {
return indexer.indexerCache.genesisResp
}

func (indexer *Indexer) GetFinalizedEpoch() (int64, []byte) {
return indexer.indexerCache.getFinalizedHead()
}
Expand Down
2 changes: 1 addition & 1 deletion services/beaconservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (bs *BeaconService) GetCachedEpochStats(epoch uint64) *indexer.EpochStats {
}

func (bs *BeaconService) GetGenesis() (*rpctypes.StandardV1GenesisResponse, error) {
return bs.indexer.GetRpcClient(false, nil).GetGenesis()
return bs.indexer.GetCachedGenesis(), nil
}

func (bs *BeaconService) GetSlotDetailsByBlockroot(blockroot []byte) (*rpctypes.CombinedBlockResponse, error) {
Expand Down

0 comments on commit 52bfd04

Please sign in to comment.