diff --git a/pkg/app_manager.go b/pkg/app_manager.go index c1d39a2..62cdc82 100644 --- a/pkg/app_manager.go +++ b/pkg/app_manager.go @@ -6,6 +6,7 @@ import ( dataPkg "main/pkg/data" databasePkg "main/pkg/database" "main/pkg/metrics" + populatorsPkg "main/pkg/populators" reportersPkg "main/pkg/reporters" "main/pkg/reporters/discord" "main/pkg/reporters/telegram" @@ -14,7 +15,6 @@ import ( "main/pkg/tendermint" "main/pkg/types" "main/pkg/utils" - "strconv" "sync" "time" @@ -30,6 +30,7 @@ type AppManager struct { SnapshotManager *snapshotPkg.Manager WebsocketManager *tendermint.WebsocketManager MetricsManager *metrics.Manager + Populators []*populatorsPkg.Wrapper Reporters []reportersPkg.Reporter IsPopulatingBlocks bool @@ -59,6 +60,24 @@ func NewAppManager( discord.NewReporter(config, version, managerLogger, stateManager, metricsManager, snapshotManager), } + populators := []*populatorsPkg.Wrapper{ + populatorsPkg.NewWrapper( + populatorsPkg.NewSlashingParamsPopulator(config, dataManager, stateManager, metricsManager, managerLogger), + config.Intervals.SlashingParams*time.Second, + managerLogger, + ), + populatorsPkg.NewWrapper( + populatorsPkg.NewSoftOptOutThresholdPopulator(config, dataManager, stateManager, metricsManager, managerLogger), + config.Intervals.SoftOptOutThreshold*time.Second, + managerLogger, + ), + populatorsPkg.NewWrapper( + populatorsPkg.NewTrimDatabasePopulator(stateManager), + config.Intervals.Trim*time.Second, + managerLogger, + ), + } + return &AppManager{ Logger: managerLogger, Config: config, @@ -69,6 +88,7 @@ func NewAppManager( WebsocketManager: websocketManager, MetricsManager: metricsManager, Reporters: reporters, + Populators: populators, IsPopulatingBlocks: false, } } @@ -101,6 +121,10 @@ func (a *AppManager) Start() { } } + for _, populator := range a.Populators { + go populator.Start() + } + go a.ListenForEvents() go a.PopulateInBackground() @@ -281,82 +305,6 @@ func (a *AppManager) ProcessSnapshot(block *types.Block) { } } -func (a *AppManager) PopulateSlashingParams() { - if a.Config.Intervals.SlashingParams == 0 { - return - } - - params, err := a.DataManager.GetSlashingParams(a.StateManager.GetLastBlockHeight() - 1) - if err != nil { - a.Logger.Warn(). - Err(err). - Msg("Error updating slashing params") - - return - } - - minSignedPerWindow, err := params.Params.MinSignedPerWindow.Float64() - if err != nil { - a.Logger.Warn(). - Err(err). - Msg("Got malformed slashing params from node") - return - } - - a.Config.BlocksWindow = params.Params.SignedBlocksWindow - a.Config.MinSignedPerWindow = minSignedPerWindow - - a.Logger.Info(). - Int64("blocks_window", a.Config.BlocksWindow). - Float64("min_signed_per_window", a.Config.MinSignedPerWindow). - Msg("Got slashing params") - - a.MetricsManager.LogSlashingParams( - a.Config.Name, - a.Config.BlocksWindow, - a.Config.MinSignedPerWindow, - a.Config.StoreBlocks, - ) - a.Config.RecalculateMissedBlocksGroups() -} - -func (a *AppManager) PopulateSoftOptOutThreshold() { - if !a.Config.IsConsumer.Bool { - return - } - - if a.Config.Intervals.SlashingParams == 0 { - return - } - - params, err := a.DataManager.GetConsumerSoftOutOutThreshold(a.StateManager.GetLastBlockHeight() - 1) - if err != nil { - a.Logger.Warn(). - Err(err). - Msg("Error updating soft out-out threshold") - - return - } - - thresholdAsString := params.Param.Value[1 : len(params.Param.Value)-1] - threshold, err := strconv.ParseFloat(thresholdAsString, 64) - if err != nil { - a.Logger.Warn(). - Err(err). - Msg("Error parsing soft out-out threshold") - - return - } - - a.Config.ConsumerSoftOptOut = threshold - - a.Logger.Info(). - Float64("threshold", threshold). - Msg("Got soft out-out threshold") - - a.MetricsManager.LogConsumerSoftOutThreshold(a.Config.Name, threshold) -} - func (a *AppManager) UpdateValidators(height int64) []error { validators, errs := a.DataManager.GetValidators(height) if len(errs) > 0 { @@ -368,17 +316,11 @@ func (a *AppManager) UpdateValidators(height int64) []error { } func (a *AppManager) PopulateInBackground() { - a.PopulateSlashingParams() - a.PopulateSoftOptOutThreshold() - // Start populating blocks in background go a.PopulateBlocks() // Setting timers go a.SyncBlocks() - go a.SyncSlashingParams() - go a.SyncSoftOptOutThreshold() - go a.SyncTrim() } func (a *AppManager) SyncBlocks() { @@ -396,63 +338,6 @@ func (a *AppManager) SyncBlocks() { } } -func (a *AppManager) SyncSlashingParams() { - if a.Config.Intervals.SlashingParams == 0 { - a.Logger.Info().Msg("Slashing params continuous population is disabled.") - return - } - - slashingParamsTimer := time.NewTicker(a.Config.Intervals.SlashingParams * time.Second) - - for { - select { - case <-slashingParamsTimer.C: - a.PopulateSlashingParams() - } - } -} - -func (a *AppManager) SyncSoftOptOutThreshold() { - if !a.Config.IsConsumer.Bool { - a.Logger.Debug(). - Msg("Chain is not a consumer chain, soft opt-out threshold fetching disabled.") - } - - if a.Config.Intervals.SoftOptOutThreshold == 0 { - a.Logger.Info().Msg("Soft opt-out threshold continuous population is disabled.") - return - } - - softOptOutTimer := time.NewTicker(a.Config.Intervals.SoftOptOutThreshold * time.Second) - - for { - select { - case <-softOptOutTimer.C: - a.PopulateSoftOptOutThreshold() - } - } -} - -func (a *AppManager) SyncTrim() { - if a.Config.Intervals.Trim == 0 { - a.Logger.Info().Msg("Trim continuous population is disabled.") - return - } - - trimTimer := time.NewTicker(a.Config.Intervals.Trim * time.Second) - - for { - select { - case <-trimTimer.C: - { - if err := a.StateManager.TrimBlocks(); err != nil { - a.Logger.Error().Err(err).Msg("Error trimming blocks") - } - } - } - } -} - func (a *AppManager) PopulateBlocks() { if a.IsPopulatingBlocks { a.Logger.Info().Msg("AppManager is populating blocks already, not populating again") diff --git a/pkg/populators/populator.go b/pkg/populators/populator.go new file mode 100644 index 0000000..2e1b3f5 --- /dev/null +++ b/pkg/populators/populator.go @@ -0,0 +1,78 @@ +package populators + +import ( + "time" + + "github.com/rs/zerolog" +) + +type Populator interface { + Populate() error + Name() string + Enabled() bool +} + +type Wrapper struct { + Populator Populator + Duration time.Duration + Logger zerolog.Logger +} + +func NewWrapper( + populator Populator, + duration time.Duration, + logger zerolog.Logger, +) *Wrapper { + return &Wrapper{ + Duration: duration, + Populator: populator, + Logger: logger.With(). + Str("component", "populator_wrapper"). + Logger(), + } +} + +func (w *Wrapper) Start() { + if w.Duration == 0 { + w.Logger.Info(). + Str("name", w.Populator.Name()). + Msg("Populator is disabled in config") + return + } + + if !w.Populator.Enabled() { + w.Logger.Info(). + Str("name", w.Populator.Name()). + Msg("Populator is disabled") + return + } + + w.Logger.Info(). + Str("name", w.Populator.Name()). + Dur("interval", w.Duration). + Msg("Populator is enabled") + + w.TryPopulate() + + timer := time.NewTicker(w.Duration) + + for { + select { + case <-timer.C: + w.TryPopulate() + } + } +} + +func (w *Wrapper) TryPopulate() { + if err := w.Populator.Populate(); err != nil { + w.Logger.Error(). + Err(err). + Str("name", w.Populator.Name()). + Msg("Got error when populating data") + } else { + w.Logger.Debug(). + Str("name", w.Populator.Name()). + Msg("Population finished successfully") + } +} diff --git a/pkg/populators/slashing_params_populator.go b/pkg/populators/slashing_params_populator.go new file mode 100644 index 0000000..1082478 --- /dev/null +++ b/pkg/populators/slashing_params_populator.go @@ -0,0 +1,80 @@ +package populators + +import ( + configPkg "main/pkg/config" + "main/pkg/data" + "main/pkg/metrics" + "main/pkg/state" + + "github.com/rs/zerolog" +) + +type SlashingParamsPopulator struct { + Config *configPkg.ChainConfig + DataManager *data.Manager + StateManager *state.Manager + MetricsManager *metrics.Manager + Logger zerolog.Logger +} + +func NewSlashingParamsPopulator( + config *configPkg.ChainConfig, + dataManager *data.Manager, + stateManager *state.Manager, + metricsManager *metrics.Manager, + logger zerolog.Logger, +) *SlashingParamsPopulator { + return &SlashingParamsPopulator{ + Config: config, + DataManager: dataManager, + StateManager: stateManager, + MetricsManager: metricsManager, + Logger: logger.With(). + Str("component", "slashing_params_populator"). + Logger(), + } +} +func (p *SlashingParamsPopulator) Populate() error { + params, err := p.DataManager.GetSlashingParams(p.StateManager.GetLastBlockHeight() - 1) + if err != nil { + p.Logger.Warn(). + Err(err). + Msg("Error updating slashing params") + + return err + } + + minSignedPerWindow, err := params.Params.MinSignedPerWindow.Float64() + if err != nil { + p.Logger.Warn(). + Err(err). + Msg("Got malformed slashing params from node") + return err + } + + p.Config.BlocksWindow = params.Params.SignedBlocksWindow + p.Config.MinSignedPerWindow = minSignedPerWindow + + p.Logger.Info(). + Int64("blocks_window", p.Config.BlocksWindow). + Float64("min_signed_per_window", p.Config.MinSignedPerWindow). + Msg("Got slashing params") + + p.MetricsManager.LogSlashingParams( + p.Config.Name, + p.Config.BlocksWindow, + p.Config.MinSignedPerWindow, + p.Config.StoreBlocks, + ) + p.Config.RecalculateMissedBlocksGroups() + + return nil +} + +func (p *SlashingParamsPopulator) Enabled() bool { + return true +} + +func (p *SlashingParamsPopulator) Name() string { + return "slashing-params-populator" +} diff --git a/pkg/populators/soft_opt_out_threshold_populator.go b/pkg/populators/soft_opt_out_threshold_populator.go new file mode 100644 index 0000000..5cabc5a --- /dev/null +++ b/pkg/populators/soft_opt_out_threshold_populator.go @@ -0,0 +1,74 @@ +package populators + +import ( + configPkg "main/pkg/config" + "main/pkg/data" + "main/pkg/metrics" + "main/pkg/state" + "strconv" + + "github.com/rs/zerolog" +) + +type SoftOptOutThresholdPopulator struct { + Config *configPkg.ChainConfig + DataManager *data.Manager + StateManager *state.Manager + MetricsManager *metrics.Manager + Logger zerolog.Logger +} + +func NewSoftOptOutThresholdPopulator( + config *configPkg.ChainConfig, + dataManager *data.Manager, + stateManager *state.Manager, + metricsManager *metrics.Manager, + logger zerolog.Logger, +) *SoftOptOutThresholdPopulator { + return &SoftOptOutThresholdPopulator{ + Config: config, + DataManager: dataManager, + StateManager: stateManager, + MetricsManager: metricsManager, + Logger: logger.With(). + Str("component", "soft_opt_out_threshold_populator"). + Logger(), + } +} +func (p *SoftOptOutThresholdPopulator) Populate() error { + params, err := p.DataManager.GetConsumerSoftOutOutThreshold(p.StateManager.GetLastBlockHeight() - 1) + if err != nil { + p.Logger.Warn(). + Err(err). + Msg("Error updating soft out-out threshold") + + return err + } + + thresholdAsString := params.Param.Value[1 : len(params.Param.Value)-1] + threshold, err := strconv.ParseFloat(thresholdAsString, 64) + if err != nil { + p.Logger.Warn(). + Err(err). + Msg("Error parsing soft out-out threshold") + + return err + } + + p.Config.ConsumerSoftOptOut = threshold + + p.Logger.Info(). + Float64("threshold", threshold). + Msg("Got soft out-out threshold") + + p.MetricsManager.LogConsumerSoftOutThreshold(p.Config.Name, threshold) + return nil +} + +func (p *SoftOptOutThresholdPopulator) Enabled() bool { + return p.Config.IsConsumer.Bool +} + +func (p *SoftOptOutThresholdPopulator) Name() string { + return "soft-opt-out-threshold-populator" +} diff --git a/pkg/populators/trim_database_populator.go b/pkg/populators/trim_database_populator.go new file mode 100644 index 0000000..fa0fbf9 --- /dev/null +++ b/pkg/populators/trim_database_populator.go @@ -0,0 +1,28 @@ +package populators + +import ( + "main/pkg/state" +) + +type TrimDatabasePopulator struct { + StateManager *state.Manager +} + +func NewTrimDatabasePopulator( + stateManager *state.Manager, +) *TrimDatabasePopulator { + return &TrimDatabasePopulator{ + StateManager: stateManager, + } +} +func (p *TrimDatabasePopulator) Populate() error { + return p.StateManager.TrimBlocks() +} + +func (p *TrimDatabasePopulator) Enabled() bool { + return true +} + +func (p *TrimDatabasePopulator) Name() string { + return "trim-database-populator" +}