Skip to content

Commit

Permalink
kwild: streamline db is-initialized check
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed May 21, 2024
1 parent 8176ce1 commit 07025b0
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions cmd/kwild/server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func buildDB(d *coreDependencies, closer *closeFuncs) *pg.DB {
// If yes, restore the database from the snapshot
restoreDB(d)

db, err := d.dbOpener(d.ctx, d.cfg.AppCfg.DBName, 11)
db, err := d.dbOpener(d.ctx, d.cfg.AppCfg.DBName, 24)
if err != nil {
failBuild(err, "kwild database open failed")
}
Expand All @@ -428,10 +428,10 @@ func buildDB(d *coreDependencies, closer *closeFuncs) *pg.DB {
// DB restoration from snapshot is skipped in the following scenarios:
// - If the DB is already initialized (i.e this is not a new node)
// - If the genesis apphash is not specified
// - If statesync is enabled. Statesync will take care of rapildly syncing the database
// - If statesync is enabled. Statesync will take care of syncing the database
// to the network state using statesync snapshots.
func restoreDB(d *coreDependencies) {
if isDbInitialized(d) || d.genesisCfg.DataAppHash == nil || d.cfg.ChainCfg.StateSync.Enable {
if d.cfg.ChainCfg.StateSync.Enable || len(d.genesisCfg.DataAppHash) == 0 || isDbInitialized(d) {
return
}

Expand Down Expand Up @@ -476,24 +476,18 @@ func restoreDB(d *coreDependencies) {

// isDbInitialized checks if the database is already initialized.
func isDbInitialized(d *coreDependencies) bool {
db, err := d.dbOpener(d.ctx, d.cfg.AppCfg.DBName, 11)
db, err := d.poolOpener(d.ctx, d.cfg.AppCfg.DBName, 3)
if err != nil {
failBuild(err, "kwild database open failed")
}
defer db.Close()

// Check if the database is empty or initialized previously
// If the database is empty, we need to restore the database from the snapshot
initTx, err := db.BeginTx(d.ctx)
if err != nil {
failBuild(err, "could not start app initialization DB transaction")
}
defer initTx.Rollback(d.ctx)

_, err = voting.GetValidators(d.ctx, initTx)
vals, _ := voting.GetValidators(d.ctx, db)
// ERROR: relation "kwild_voting.voters" does not exist
// assumption that error is due to the missing table and schema.
return err == nil
return len(vals) > 0
}

func buildEngine(d *coreDependencies, db *pg.DB) *execution.GlobalContext {
Expand Down

0 comments on commit 07025b0

Please sign in to comment.