Skip to content

Commit

Permalink
refactor: update cache in slot exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
Monika-Bitfly committed Mar 7, 2025
1 parent 8e7402d commit a2cf5a8
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions backend/pkg/exporter/modules/slot_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"sync"
"time"

"github.com/gobitfly/beaconchain/pkg/commons/cache"
"github.com/gobitfly/beaconchain/pkg/commons/log"
"github.com/gobitfly/beaconchain/pkg/commons/rpc"
"github.com/gobitfly/beaconchain/pkg/commons/services"
Expand All @@ -35,18 +34,20 @@ type slotExporter struct {
ModuleContext
Client rpc.Client
db edb.SlotExporterRepository
cache edb.ExporterCache
FirstRun bool
latestEpoch uint64
latestSlot uint64
finalizedEpoch uint64
latestProposed uint64
}

func NewSlotExporter(moduleContext ModuleContext, client rpc.Client, db edb.SlotExporterRepository) ModuleInterface {
func NewSlotExporter(moduleContext ModuleContext, client rpc.Client, cache edb.ExporterCache, db edb.SlotExporterRepository) ModuleInterface {
return &slotExporter{
ModuleContext: moduleContext,
Client: client,
db: db,
cache: cache,
FirstRun: true,
latestEpoch: 0,
latestSlot: 0,
Expand All @@ -67,26 +68,46 @@ func (e *slotExporter) OnHead(_ *constypes.StandardEventHeadResponse) (err error
// cache handling
defer func() {
if err == nil {
if e.latestEpoch > 0 && cache.LatestEpoch.Get() < e.latestEpoch {
err := cache.LatestEpoch.Set(e.latestEpoch)
latestEpoch, err := e.cache.GetLatestEpoch()
if err != nil {
log.Error(err, "error retrieving latestEpoch from cache", 0)
}

if e.latestEpoch > 0 && latestEpoch < e.latestEpoch {
err := e.cache.SetLatestEpoch(e.latestEpoch)
if err != nil {
log.Error(err, "error setting latestEpoch in cache", 0)
}
}
if e.latestSlot > 0 && cache.LatestSlot.Get() < e.latestSlot {
err := cache.LatestSlot.Set(e.latestSlot)

latestSlot, err := e.cache.GetLatestSlot()
if err != nil {
log.Error(err, "error retrieving latestSlot from cache", 0)
}
if e.latestSlot > 0 && latestSlot < e.latestSlot {
err := e.cache.SetLatestSlot(e.latestSlot)
if err != nil {
log.Error(err, "error setting latestSlot in cache", 0)
}
}
if e.finalizedEpoch > 0 && cache.LatestFinalizedEpoch.Get() < e.finalizedEpoch {
err := cache.LatestFinalizedEpoch.Set(e.finalizedEpoch)

latestFinalizedEpoch, err := e.cache.GetLatestFinalizedEpoch()
if err != nil {
log.Error(err, "error retrieving latestFinalizedEpoch from cache", 0)
}
if e.finalizedEpoch > 0 && latestFinalizedEpoch < e.finalizedEpoch {
err := e.cache.SetLatestFinalizedEpoch(e.finalizedEpoch)
if err != nil {
log.Error(err, "error setting latestFinalizedEpoch in cache", 0)
}
}
if e.latestProposed > 0 && cache.LatestProposedSlot.Get() < e.latestProposed {
err := cache.LatestProposedSlot.Set(e.latestProposed)

latestProposedSlot, err := e.cache.GetLatestProposedSlot()
if err != nil {
log.Error(err, "error retrieving latestProposedSlot from cache", 0)
}
if e.latestProposed > 0 && latestProposedSlot < e.latestProposed {
err := e.cache.SetLatestProposedSlot(e.latestProposed)
if err != nil {
log.Error(err, "error setting latestProposedSlot in cache", 0)
}
Expand Down

0 comments on commit a2cf5a8

Please sign in to comment.