Skip to content

Commit

Permalink
feat: add snapshots intervals (#56)
Browse files Browse the repository at this point in the history
* feat: add snapshots-interval

* chore: add snapshot-interval to params command

* chore: add test for snapshotManager.GetNewerHeight

* chore: changed log verbosity
  • Loading branch information
freak12techno authored Apr 21, 2024
1 parent fa976f6 commit fa5d1d4
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 5 deletions.
5 changes: 5 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 13 additions & 3 deletions pkg/app_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.")
Expand Down
1 change: 1 addition & 0 deletions pkg/config/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
8 changes: 8 additions & 0 deletions pkg/reporters/discord/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pkg/reporters/telegram/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/snapshot/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/snapshot/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down
3 changes: 2 additions & 1 deletion templates/discord/Params.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 . }})
Expand Down
3 changes: 2 additions & 1 deletion templates/telegram/Params.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
Top {{ .GetConsumerRequiredValidators }} are required to sign blocks.
{{- else -}}
The chain is a sovereign chain.
{{ end }}
{{- end }}

<strong>App config</strong>
Interval between sending/generating reports: {{ .FormatSnapshotInterval }}
Missed blocks thresholds:
{{ range .Config.MissedBlocksGroups -}}
{{ .EmojiEnd }} {{ .Start }} - {{ .End }} ({{ $render.FormatGroupPercent . }})
Expand Down

0 comments on commit fa5d1d4

Please sign in to comment.