-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa5d1d4
commit 6a94a9b
Showing
5 changed files
with
285 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
Oops, something went wrong.