Skip to content

Commit

Permalink
feat: report snapshot after populating blocks (#35)
Browse files Browse the repository at this point in the history
* feat: report snapshot after populating blocks

* feat: update validators after fetching latest block

* feat: do not process older snapshots
  • Loading branch information
freak12techno committed Jan 22, 2024
1 parent e409f51 commit 5897656
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions pkg/app_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type AppManager struct {
Reporters []reportersPkg.Reporter
IsPopulatingBlocks bool

mutex sync.Mutex
mutex sync.Mutex
snapshotMutex sync.Mutex
}

func NewAppManager(
Expand Down Expand Up @@ -127,7 +128,7 @@ func (a *AppManager) ProcessEvent(emittable types.WebsocketEmittable) {
latestHeight := a.StateManager.GetLastBlockHeight()

if latestHeight > block.Height {
a.Logger.Info().
a.Logger.Warn().
Int64("last_height", latestHeight).
Int64("height", block.Height).
Msg("Trying to generate a report for a block that was processed before")
Expand Down Expand Up @@ -158,6 +159,13 @@ func (a *AppManager) ProcessEvent(emittable types.WebsocketEmittable) {
Msg("Error inserting new block")
}

a.ProcessSnapshot(block)
}

func (a *AppManager) ProcessSnapshot(block *types.Block) {
a.snapshotMutex.Lock()
defer a.snapshotMutex.Unlock()

totalBlocksCount := a.StateManager.GetBlocksCountSinceLatest(a.Config.StoreBlocks)
a.Logger.Info().
Int64("count", totalBlocksCount).
Expand Down Expand Up @@ -210,6 +218,14 @@ func (a *AppManager) ProcessEvent(emittable types.WebsocketEmittable) {
}

olderHeight := a.SnapshotManager.GetOlderHeight()
if olderHeight >= block.Height {
a.Logger.Warn().
Int64("older_height", olderHeight).
Int64("height", block.Height).
Msg("Trying to generate the snapshot for the older height, skipping.")
return
}

a.Logger.Info().
Int64("older_height", olderHeight).
Int64("height", block.Height).
Expand Down Expand Up @@ -399,6 +415,7 @@ func (a *AppManager) PopulateBlocks() {

a.Logger.Info().
Int64("height", block.Height).
Time("time", block.Time).
Msg("Last block height")

validators, err := a.RPCManager.GetActiveSetAtBlock(block.Height)
Expand Down Expand Up @@ -501,4 +518,23 @@ func (a *AppManager) PopulateBlocks() {
}

a.IsPopulatingBlocks = false

latestHeight := a.StateManager.GetLastBlockHeight()

if latestHeight > block.Height {
a.Logger.Warn().
Int64("last_height", latestHeight).
Int64("height", block.Height).
Msg("Trying to generate a report for a block that was processed before")
return
}

if errs := a.UpdateValidators(latestHeight - 1); len(errs) > 0 {
a.Logger.Error().
Errs("errors", errs).
Msg("Error updating validators")
return
}

a.ProcessSnapshot(block)
}

0 comments on commit 5897656

Please sign in to comment.