Skip to content

Commit

Permalink
feat: query latest block with missing blocks (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Jan 14, 2024
1 parent 1a4675f commit e409f51
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 35 deletions.
6 changes: 1 addition & 5 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ min-signed-per-window = 0.05
# You can set it to something smaller if your websocket connection is lagging.
# Defaults to 30.
blocks = 30
# Internval to fetch latest block from chain.
# Setting it to 0 would disable automatic blocks fetching, which is not desired.
# Defaults to 120.
latest-block = 120
# Interval to fetch slashing params from chain. Set to 0 to disable fetching slashing params from chain
# and use local blocks-window and min-signed-per-window.
# Defaults to 300.
Expand All @@ -95,7 +91,7 @@ validators-list = 1000
# How many signing infos to query at once.
signing-infos = 1000

# You can specify multiple chain. Each chain should have its own set of reporters
# You can specify multiple chain. Each chain should have its own set of reporters,
# and they should not overlap.
[[chains]]
name = "sentinel"
Expand Down
48 changes: 19 additions & 29 deletions pkg/app_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,13 @@ func (a *AppManager) UpdateValidators(height int64) []error {
}

func (a *AppManager) PopulateInBackground() {
a.PopulateLatestBlock()
a.PopulateSlashingParams()

// Start populating blocks in background
go a.PopulateBlocks()

// Setting timers
go a.SyncBlocks()
go a.SyncLatestBlock()
go a.SyncSlashingParams()
go a.SyncTrim()
}
Expand All @@ -328,22 +326,6 @@ func (a *AppManager) SyncBlocks() {
}
}

func (a *AppManager) SyncLatestBlock() {
if a.Config.Intervals.Blocks == 0 {
a.Logger.Info().Msg("Latest block continuous population is disabled.")
return
}

latestBlockTimer := time.NewTicker(a.Config.Intervals.LatestBlock * time.Second)

for {
select {
case <-latestBlockTimer.C:
a.PopulateLatestBlock()
}
}
}

func (a *AppManager) SyncSlashingParams() {
if a.Config.Intervals.SlashingParams == 0 {
a.Logger.Info().Msg("Slashing params continuous population is disabled.")
Expand Down Expand Up @@ -380,16 +362,28 @@ func (a *AppManager) SyncTrim() {
}
}

func (a *AppManager) PopulateLatestBlock() {
func (a *AppManager) PopulateBlocks() {
if a.IsPopulatingBlocks {
a.Logger.Info().Msg("AppManager is populating blocks already, not populating again")
return
}

a.IsPopulatingBlocks = true

// Populating latest block
a.Logger.Info().Msg("Populating latest block...")

blockRaw, err := a.RPCManager.GetBlock(0)
if err != nil {
a.Logger.Error().Err(err).Msg("Error querying for last block")
a.IsPopulatingBlocks = false
return
}

block, err := blockRaw.Result.Block.ToBlock()
if err != nil {
a.Logger.Warn().Msg("Error parsing block")
a.IsPopulatingBlocks = false
return
}

Expand All @@ -399,6 +393,7 @@ func (a *AppManager) PopulateLatestBlock() {
Int64("last_height", lastStateHeight).
Int64("height", block.Height).
Msg("Got older block when populating latest height, not proceeding further.")
a.IsPopulatingBlocks = false
return
}

Expand All @@ -411,6 +406,7 @@ func (a *AppManager) PopulateLatestBlock() {
a.Logger.Error().
Err(err).
Msg("Error updating historical validators")
a.IsPopulatingBlocks = false
return
}

Expand All @@ -420,25 +416,19 @@ func (a *AppManager) PopulateLatestBlock() {
a.Logger.Error().
Err(err).
Msg("Error inserting last block")
a.IsPopulatingBlocks = false
return
}
}

func (a *AppManager) PopulateBlocks() {
if a.IsPopulatingBlocks {
a.Logger.Info().Msg("AppManager is populating blocks already, not populating again")
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.")
a.IsPopulatingBlocks = false
return
}

a.Logger.Info().Msg("Populating blocks...")

a.IsPopulatingBlocks = true

missingBlocks := a.StateManager.GetMissingBlocksSinceLatest(a.Config.StoreBlocks)
if len(missingBlocks) == 0 {
a.Logger.Info().
Expand Down
1 change: 0 additions & 1 deletion pkg/config/intervals.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import "time"

type IntervalsConfig struct {
Blocks time.Duration `default:"30" toml:"blocks"`
LatestBlock time.Duration `default:"120" toml:"latest_block"`
Trim time.Duration `default:"300" toml:"trim"`
SlashingParams time.Duration `default:"300" toml:"slashing_params"`
}

0 comments on commit e409f51

Please sign in to comment.