From 0b8691fea4c5a83cd9ca005f13858bb017b920fe Mon Sep 17 00:00:00 2001 From: ciripel Date: Wed, 19 Jun 2024 18:30:36 +0300 Subject: [PATCH] save StorageConfiguration in Store --- core/engine.go | 6 +++--- store/store.go | 12 +++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/core/engine.go b/core/engine.go index e4204b0..0f5d1e9 100644 --- a/core/engine.go +++ b/core/engine.go @@ -43,7 +43,7 @@ func NewEngine(ctx context.Context, config *configuration.Runtime) (*Engine, err logger: slog.Default(), // TODO: replace with custom logger } - go result.fetchAutomatically(config) + go result.fetchAutomatically() return result, nil } @@ -156,7 +156,7 @@ func (e *Engine) GetLastFetchedCycle() (int64, error) { return e.store.GetLastFetchedCycle() } -func (e *Engine) fetchAutomatically(config *configuration.Runtime) { +func (e *Engine) fetchAutomatically() { go func() { for { select { @@ -194,7 +194,7 @@ func (e *Engine) fetchAutomatically(config *configuration.Runtime) { if err = e.FetchCycleDelegationStates(e.ctx, cycle, nil); err != nil { e.logger.Error("failed to fetch cycle delegation states", "cycle", cycle, "error", err.Error()) } - if err = e.store.PruneDelegationState(cycle, config); err != nil { + if err = e.store.PruneDelegationState(cycle); err != nil { e.logger.Error("failed to prune cycles out", "error", err.Error()) } } diff --git a/store/store.go b/store/store.go index cc42312..558c2f1 100644 --- a/store/store.go +++ b/store/store.go @@ -14,7 +14,8 @@ import ( ) type Store struct { - db *gorm.DB + db *gorm.DB + config configuration.StorageConfiguration } func NewStore(config *configuration.Runtime) (*Store, error) { @@ -38,7 +39,8 @@ func NewStore(config *configuration.Runtime) (*Store, error) { } db.AutoMigrate(&StoredDelegationState{}) return &Store{ - db: db, + db: db, + config: config.Storage, }, nil } @@ -66,12 +68,12 @@ func (s *Store) StoreDelegationState(state *StoredDelegationState) error { return nil } -func (s *Store) PruneDelegationState(cycle int64, config *configuration.Runtime) error { - if config.Storage.Mode != constants.Rolling { +func (s *Store) PruneDelegationState(cycle int64) error { + if s.config.Mode != constants.Rolling { return nil } - prunedCycle := cycle - int64(config.Storage.StoredCycles) + prunedCycle := cycle - int64(s.config.StoredCycles) state := &StoredDelegationState{} slog.Debug("pruning delegation states smaller than", "cycle", prunedCycle) return s.db.Model(&StoredDelegationState{}).Where("cycle < ?", prunedCycle).Delete(state).Error