From dafdaabad86c13e2fd95828fe2799ac7d9beb7bb Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 18 Apr 2024 02:06:15 +0300 Subject: [PATCH 1/4] feat: add snapshots-interval --- config.example.toml | 5 +++++ pkg/app_manager.go | 16 +++++++++++++--- pkg/config/chain.go | 1 + pkg/snapshot/manager.go | 4 ++++ 4 files changed, 23 insertions(+), 3 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..b13e0c6 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.Warn(). + 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/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) } From d9d7ca82143a560586df11f72e1c6de4b8bbcbba Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 18 Apr 2024 02:27:26 +0300 Subject: [PATCH 2/4] chore: add snapshot-interval to params command --- pkg/reporters/discord/types.go | 8 ++++++++ pkg/reporters/telegram/types.go | 8 ++++++++ templates/discord/Params.md | 3 ++- templates/telegram/Params.html | 3 ++- 4 files changed, 20 insertions(+), 2 deletions(-) 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/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 . }}) From 457a999fc048d7ed0623705c195a2573ba63a00a Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 18 Apr 2024 02:32:49 +0300 Subject: [PATCH 3/4] chore: add test for snapshotManager.GetNewerHeight --- pkg/snapshot/manager_test.go | 1 + 1 file changed, 1 insertion(+) 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!") From dafd1b797fb4247a7d13e4bab791929430033cae Mon Sep 17 00:00:00 2001 From: Sergey Date: Sun, 21 Apr 2024 04:54:02 +0300 Subject: [PATCH 4/4] chore: changed log verbosity --- pkg/app_manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/app_manager.go b/pkg/app_manager.go index b13e0c6..c1d39a2 100644 --- a/pkg/app_manager.go +++ b/pkg/app_manager.go @@ -171,7 +171,7 @@ func (a *AppManager) ProcessSnapshot(block *types.Block) { if a.SnapshotManager.HasNewerSnapshot() { if newerHeight := a.SnapshotManager.GetNewerHeight(); block.Height-newerHeight < a.Config.SnapshotsInterval { - a.Logger.Warn(). + a.Logger.Debug(). Int64("older_height", newerHeight). Int64("height", block.Height). Int64("diff", block.Height-newerHeight).