Skip to content

Commit

Permalink
feat: added populators class
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Apr 23, 2024
1 parent fa5d1d4 commit 6a94a9b
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 140 deletions.
165 changes: 25 additions & 140 deletions pkg/app_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -14,7 +15,6 @@ import (
"main/pkg/tendermint"
"main/pkg/types"
"main/pkg/utils"
"strconv"
"sync"
"time"

Expand All @@ -30,6 +30,7 @@ type AppManager struct {
SnapshotManager *snapshotPkg.Manager
WebsocketManager *tendermint.WebsocketManager
MetricsManager *metrics.Manager
Populators []*populatorsPkg.Wrapper
Reporters []reportersPkg.Reporter
IsPopulatingBlocks bool

Expand Down Expand Up @@ -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,
Expand All @@ -69,6 +88,7 @@ func NewAppManager(
WebsocketManager: websocketManager,
MetricsManager: metricsManager,
Reporters: reporters,
Populators: populators,
IsPopulatingBlocks: false,
}
}
Expand Down Expand Up @@ -101,6 +121,10 @@ func (a *AppManager) Start() {
}
}

for _, populator := range a.Populators {
go populator.Start()
}

go a.ListenForEvents()
go a.PopulateInBackground()

Expand Down Expand Up @@ -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 {
Expand All @@ -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() {
Expand All @@ -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")
Expand Down
78 changes: 78 additions & 0 deletions pkg/populators/populator.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
80 changes: 80 additions & 0 deletions pkg/populators/slashing_params_populator.go
Original file line number Diff line number Diff line change
@@ -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"
}
Loading

0 comments on commit 6a94a9b

Please sign in to comment.