From fa5d1d4d69554d1d4d8fad76c43e6bd21e72593e Mon Sep 17 00:00:00 2001 From: Sergey <83376337+freak12techno@users.noreply.github.com> Date: Sun, 21 Apr 2024 15:14:15 +0300 Subject: [PATCH] feat: add snapshots intervals (#56) * feat: add snapshots-interval * chore: add snapshot-interval to params command * chore: add test for snapshotManager.GetNewerHeight * chore: changed log verbosity --- config.example.toml | 5 +++++ pkg/app_manager.go | 16 +++++++++++++--- pkg/config/chain.go | 1 + pkg/reporters/discord/types.go | 8 ++++++++ pkg/reporters/telegram/types.go | 8 ++++++++ pkg/snapshot/manager.go | 4 ++++ pkg/snapshot/manager_test.go | 1 + templates/discord/Params.md | 3 ++- templates/telegram/Params.html | 3 ++- 9 files changed, 44 insertions(+), 5 deletions(-) diff --git a/config.example.toml b/config.example.toml index 84e2de8..8c62577 100644 --- a/config.example.toml +++ b/config.example.toml @@ -86,6 +86,11 @@ emoji-start = ["🟡", "🟡", "🟠", "🟠", "🔴", "🔴"] # to another). # Defaults to ["🟢", "🟡", "🟡", "🟡", "🟡", "🟠", "🟠", "🟠", "🟠"] emoji-end = ["🟢", "🟡", "🟡", "🟠", "🟠", "🟠"] +# Minimal interval between two snapshots to be reported. +# For example, if a snapshot was generated at block 10, and snapshot-interval is 5, +# then the next snapshot would be done on block 15 or later (if there were errors processing it/fetching datat). +# Defaults to 1, so every block. +snapshots-interval = 10 # Periodical intervals check params. You can omit this completely, or some fields inside and the default # ones will be used. [chains.intervals] diff --git a/pkg/app_manager.go b/pkg/app_manager.go index 1034654..c1d39a2 100644 --- a/pkg/app_manager.go +++ b/pkg/app_manager.go @@ -169,6 +169,18 @@ func (a *AppManager) ProcessSnapshot(block *types.Block) { a.snapshotMutex.Lock() defer a.snapshotMutex.Unlock() + if a.SnapshotManager.HasNewerSnapshot() { + if newerHeight := a.SnapshotManager.GetNewerHeight(); block.Height-newerHeight < a.Config.SnapshotsInterval { + a.Logger.Debug(). + Int64("older_height", newerHeight). + Int64("height", block.Height). + Int64("diff", block.Height-newerHeight). + Int64("snapshot_interval", a.Config.SnapshotsInterval). + Msg("Trying to generate a snapshot between two blocks which are too close, skipping.") + return + } + } + totalBlocksCount := a.StateManager.GetBlocksCountSinceLatest(a.Config.StoreBlocks) a.Logger.Info(). Int64("count", totalBlocksCount). @@ -450,7 +462,7 @@ func (a *AppManager) PopulateBlocks() { a.IsPopulatingBlocks = true // Populating latest block - a.Logger.Info().Msg("Populating latest block...") + a.Logger.Info().Msg("Populating blocks...") blockRaw, err := a.DataManager.GetBlock(0) if err != nil { @@ -500,8 +512,6 @@ func (a *AppManager) PopulateBlocks() { return } - a.Logger.Info().Msg("Populating latest block...") - // Populating blocks if a.StateManager.GetLastBlockHeight() == 0 { a.Logger.Warn().Msg("Latest block is not set, cannot populate blocks.") diff --git a/pkg/config/chain.go b/pkg/config/chain.go index 8bd9768..e6d5bfa 100644 --- a/pkg/config/chain.go +++ b/pkg/config/chain.go @@ -23,6 +23,7 @@ type ChainConfig struct { BlocksWindow int64 `default:"10000" toml:"blocks-window"` MinSignedPerWindow float64 `default:"0.05" toml:"min-signed-per-window"` QueryEachSigningInfo null.Bool `default:"false" toml:"query-each-signing-info"` + SnapshotsInterval int64 `default:"1" toml:"snapshots-interval"` Pagination ChainPagination `toml:"pagination"` Intervals IntervalsConfig `toml:"intervals"` diff --git a/pkg/reporters/discord/types.go b/pkg/reporters/discord/types.go index f731e9e..8d06fce 100644 --- a/pkg/reporters/discord/types.go +++ b/pkg/reporters/discord/types.go @@ -70,6 +70,14 @@ func (r paramsRender) GetConsumerRequiredValidators() int { return len(r.Validators) - r.ConsumerOptOutValidators } +func (r paramsRender) FormatSnapshotInterval() string { + if r.Config.SnapshotsInterval == 1 { + return "every block" + } + + return fmt.Sprintf("every %d blocks", r.Config.SnapshotsInterval) +} + type notifierEntry struct { Link types.Link Notifiers []*types.Notifier diff --git a/pkg/reporters/telegram/types.go b/pkg/reporters/telegram/types.go index 844d049..4d1b5e5 100644 --- a/pkg/reporters/telegram/types.go +++ b/pkg/reporters/telegram/types.go @@ -69,6 +69,14 @@ func (r paramsRender) GetConsumerRequiredValidators() int { return len(r.Validators) - r.ConsumerOptOutValidators } +func (r paramsRender) FormatSnapshotInterval() string { + if r.Config.SnapshotsInterval == 1 { + return "every block" + } + + return fmt.Sprintf("every %d blocks", r.Config.SnapshotsInterval) +} + func (r paramsRender) FormatSoftOptOut() string { return fmt.Sprintf("%.2f", r.Config.ConsumerSoftOptOut*100) } diff --git a/pkg/snapshot/manager.go b/pkg/snapshot/manager.go index 0ed69a4..b657188 100644 --- a/pkg/snapshot/manager.go +++ b/pkg/snapshot/manager.go @@ -50,6 +50,10 @@ func (m *Manager) GetOlderHeight() int64 { return m.olderSnapshot.Height } +func (m *Manager) GetNewerHeight() int64 { + return m.newerSnapshot.Height +} + func (m *Manager) GetReport() (*types.Report, error) { return m.newerSnapshot.Snapshot.GetReport(m.olderSnapshot.Snapshot, m.config) } diff --git a/pkg/snapshot/manager_test.go b/pkg/snapshot/manager_test.go index f21088d..c1bc376 100644 --- a/pkg/snapshot/manager_test.go +++ b/pkg/snapshot/manager_test.go @@ -50,6 +50,7 @@ func TestManagerCommitNewSnapshot(t *testing.T) { assert.True(t, manager.HasNewerSnapshot(), "Should not have older snapshot!") assert.Equal(t, int64(10), manager.GetOlderHeight(), "Height mismatch!") + assert.Equal(t, int64(20), manager.GetNewerHeight(), "Height mismatch!") report, err := manager.GetReport() require.NoError(t, err, "Error should not be presented!") diff --git a/templates/discord/Params.md b/templates/discord/Params.md index 85611f7..c2e6d11 100644 --- a/templates/discord/Params.md +++ b/templates/discord/Params.md @@ -14,9 +14,10 @@ Soft opt-out percent is at {{ .FormatSoftOptOut }}%. Top {{ .GetConsumerRequiredValidators }} are required to sign blocks. {{- else -}} The chain is a sovereign chain. -{{ end }} +{{- end }} **App config** +Interval between sending/generating reports: {{ .FormatSnapshotInterval }} Missed blocks thresholds: {{ range .Config.MissedBlocksGroups -}} {{ .EmojiEnd }} {{ .Start }} - {{ .End }} ({{ $render.FormatGroupPercent . }}) diff --git a/templates/telegram/Params.html b/templates/telegram/Params.html index d361df9..4ab072a 100644 --- a/templates/telegram/Params.html +++ b/templates/telegram/Params.html @@ -15,9 +15,10 @@ Top {{ .GetConsumerRequiredValidators }} are required to sign blocks. {{- else -}} The chain is a sovereign chain. -{{ end }} +{{- end }} App config +Interval between sending/generating reports: {{ .FormatSnapshotInterval }} Missed blocks thresholds: {{ range .Config.MissedBlocksGroups -}} {{ .EmojiEnd }} {{ .Start }} - {{ .End }} ({{ $render.FormatGroupPercent . }})