Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add daily refetch module #454

Merged
merged 18 commits into from
Aug 26, 2022
Merged
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#### Staking Module
- ([\#443](https://github.com/forbole/bdjuno/pull/443)) Remove tombstone status from staking module(already stored in slashing module)

#### Daily refetch
- ([\#454](https://github.com/forbole/bdjuno/pull/454)) Added `daily refetch` module to refetch missing blocks every day


## Version v3.2.0
### Changes
#### Mint module
Expand Down
8 changes: 8 additions & 0 deletions database/daily_refetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package database

// GetTotalBlocks implements database.Database
func (db *Db) GetTotalBlocks() (int64, error) {
var blockCount int64
err := db.Sql.QueryRow(`SELECT count(*) FROM block;`).Scan(&blockCount)
return blockCount, err
}
74 changes: 74 additions & 0 deletions modules/daily_refetch/handle_periodic_operations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package daily_refetch

import (
"fmt"
"time"

"github.com/forbole/juno/v3/parser"
"github.com/forbole/juno/v3/types/config"

"github.com/go-co-op/gocron"
"github.com/rs/zerolog/log"

parsecmdtypes "github.com/forbole/juno/v3/cmd/parse/types"
)

func (m *Module) RegisterPeriodicOperations(scheduler *gocron.Scheduler) error {
log.Debug().Str("module", "daily refetch").Msg("setting up periodic tasks")

// Setup a cron job to run every midnight
if _, err := scheduler.Every(1).Day().At("00:00").Do(func() {
m.refetchMissingBlocks()
}); err != nil {
return fmt.Errorf("error while setting up daily refetch periodic operation: %s", err)
}

return nil
}

// refetchMissingBlocks checks for missing blocks from one day ago and refetches them
func (m *Module) refetchMissingBlocks() error {
log.Trace().Str("module", "daily refetch").Str("refetching", "blocks").
Msg("refetching missing blocks")

latestBlock, err := m.node.LatestHeight()
if err != nil {
return fmt.Errorf("error while getting latest block: %s", err)
}

blockCount, err := m.database.GetTotalBlocks()
if err != nil {
return fmt.Errorf("error while getting block count: %s", err)
}

if blockCount >= latestBlock {
return nil
}

parseCtx, err := parsecmdtypes.GetParserContext(config.Cfg, parsecmdtypes.NewConfig())
if err != nil {
return err
}

workerCtx := parser.NewContext(parseCtx.EncodingConfig, parseCtx.Node, parseCtx.Database, parseCtx.Logger, parseCtx.Modules)
worker := parser.NewWorker(workerCtx, nil, 0)

blockHeightDayAgo, err := m.database.GetBlockHeightTimeDayAgo(time.Now())
if err != nil {
return fmt.Errorf("error while getting block height from a day ago: %s", err)
}

var startHeight int64 = blockHeightDayAgo.Height

log.Info().Int64("start height", startHeight).Int64("end height", latestBlock).
Msg("getting missing blocks and transactions from a day ago")
for k := startHeight; k <= latestBlock; k++ {
err = worker.ProcessIfNotExists(k)
if err != nil {
return fmt.Errorf("error while re-fetching block %d: %s", k, err)
}
}

return nil

}
35 changes: 35 additions & 0 deletions modules/daily_refetch/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package daily_refetch

import (
"github.com/forbole/juno/v3/node"

bdjunodb "github.com/forbole/bdjuno/v3/database"

"github.com/forbole/juno/v3/modules"
)

var (
_ modules.Module = &Module{}
_ modules.PeriodicOperationsModule = &Module{}
)

type Module struct {
node node.Node
database *bdjunodb.Db
}

// NewModule builds a new Module instance
func NewModule(
node node.Node,
database *bdjunodb.Db,
) *Module {
return &Module{
node: node,
database: database,
}
}

// Name implements modules.Module
func (m *Module) Name() string {
return "daily refetch"
}
3 changes: 3 additions & 0 deletions modules/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/forbole/bdjuno/v3/modules/distribution"
"github.com/forbole/bdjuno/v3/modules/feegrant"

dailyrefetch "github.com/forbole/bdjuno/v3/modules/daily_refetch"
"github.com/forbole/bdjuno/v3/modules/gov"
"github.com/forbole/bdjuno/v3/modules/mint"
"github.com/forbole/bdjuno/v3/modules/modules"
Expand Down Expand Up @@ -75,6 +76,7 @@ func (r *Registrar) BuildModules(ctx registrar.Context) jmodules.Modules {
authModule := auth.NewModule(r.parser, cdc, db)
bankModule := bank.NewModule(r.parser, sources.BankSource, cdc, db)
consensusModule := consensus.NewModule(db)
dailyRefetchModule := dailyrefetch.NewModule(ctx.Proxy, db)
distrModule := distribution.NewModule(sources.DistrSource, cdc, db)
feegrantModule := feegrant.NewModule(cdc, db)
mintModule := mint.NewModule(sources.MintSource, cdc, db)
Expand All @@ -91,6 +93,7 @@ func (r *Registrar) BuildModules(ctx registrar.Context) jmodules.Modules {
authModule,
bankModule,
consensusModule,
dailyRefetchModule,
distrModule,
feegrantModule,
govModule,
Expand Down