Skip to content

Commit

Permalink
Fix src overwrite (#1215)
Browse files Browse the repository at this point in the history
This change includes 2 changes:

- ensure that the rewritten of statedb_info.json has a correct block height
- refactor ReadStateDbInfo such that only statedb path is required.
  • Loading branch information
wsodsong authored Oct 31, 2024
1 parent 9b96504 commit 7bddcf6
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 25 deletions.
8 changes: 7 additions & 1 deletion executor/extension/primer/primer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ func (p *stateDbPrimer[T]) PreRun(_ executor.State[T], ctx *executor.Context) (e
// is used to determine block from which the priming starts
var primingStartBlock uint64
if p.cfg.IsExistingStateDb {
stateDbInfo, err := utils.ReadStateDbInfo(filepath.Join(p.cfg.StateDbSrc, utils.PathToDbInfo))
var stateDbInfo utils.StateDbInfo
var err error
if p.cfg.ShadowDb {
stateDbInfo, err = utils.ReadStateDbInfo(filepath.Join(p.cfg.StateDbSrc, utils.PathToPrimaryStateDb))
} else {
stateDbInfo, err = utils.ReadStateDbInfo(p.cfg.StateDbSrc)
}
if err != nil {
return fmt.Errorf("cannot read state db info; %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions executor/extension/statedb/archive_db_block_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *archiveBlockChecker[T]) PreRun(executor.State[T], *executor.Context) er
var archiveLastBlock uint64

if c.cfg.ShadowDb {
primeDbInfo, err := utils.ReadStateDbInfo(filepath.Join(c.cfg.StateDbSrc, utils.PathToPrimaryStateDb, utils.PathToDbInfo))
primeDbInfo, err := utils.ReadStateDbInfo(filepath.Join(c.cfg.StateDbSrc, utils.PathToPrimaryStateDb))
if err != nil {
return fmt.Errorf("cannot read state db info for primary db; %v", err)
}
Expand All @@ -56,7 +56,7 @@ func (c *archiveBlockChecker[T]) PreRun(executor.State[T], *executor.Context) er
return fmt.Errorf("prime state db %v does not contain archive", filepath.Join(c.cfg.StateDbSrc, utils.PathToPrimaryStateDb))
}

shadowDbInfo, err := utils.ReadStateDbInfo(filepath.Join(c.cfg.StateDbSrc, utils.PathToShadowStateDb, utils.PathToDbInfo))
shadowDbInfo, err := utils.ReadStateDbInfo(filepath.Join(c.cfg.StateDbSrc, utils.PathToShadowStateDb))
if err != nil {
return fmt.Errorf("cannot read state db info for shadow db; %v", err)
}
Expand All @@ -68,7 +68,7 @@ func (c *archiveBlockChecker[T]) PreRun(executor.State[T], *executor.Context) er
archiveLastBlock = umath.Min(shadowDbInfo.Block, primeDbInfo.Block)

} else {
stateDbInfo, err := utils.ReadStateDbInfo(filepath.Join(c.cfg.StateDbSrc, utils.PathToDbInfo))
stateDbInfo, err := utils.ReadStateDbInfo(c.cfg.StateDbSrc)
if err != nil {
return fmt.Errorf("cannot read state db info; %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions executor/extension/statedb/live_db_block_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func (c *liveDbBlockChecker[T]) PreRun(executor.State[T], *executor.Context) err
)

if c.cfg.ShadowDb {
primeDbInfo, err = utils.ReadStateDbInfo(filepath.Join(c.cfg.StateDbSrc, utils.PathToPrimaryStateDb, utils.PathToDbInfo))
primeDbInfo, err = utils.ReadStateDbInfo(filepath.Join(c.cfg.StateDbSrc, utils.PathToPrimaryStateDb))
if err != nil {
return fmt.Errorf("cannot read state db info for primary db; %v", err)
}

shadowDbInfo, err = utils.ReadStateDbInfo(filepath.Join(c.cfg.StateDbSrc, utils.PathToShadowStateDb, utils.PathToDbInfo))
shadowDbInfo, err = utils.ReadStateDbInfo(filepath.Join(c.cfg.StateDbSrc, utils.PathToShadowStateDb))
if err != nil {
return fmt.Errorf("cannot read state db info for shadow db; %v", err)
}
Expand All @@ -70,7 +70,7 @@ func (c *liveDbBlockChecker[T]) PreRun(executor.State[T], *executor.Context) err
lastBlock = primeDbInfo.Block

} else {
primeDbInfo, err = utils.ReadStateDbInfo(filepath.Join(c.cfg.StateDbSrc, utils.PathToDbInfo))
primeDbInfo, err = utils.ReadStateDbInfo(c.cfg.StateDbSrc)
if err != nil {
return fmt.Errorf("cannot read state db info; %v", err)
}
Expand Down
17 changes: 15 additions & 2 deletions executor/extension/statedb/state_db_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,23 @@ func (m *stateDbManager[T]) PreRun(_ executor.State[T], ctx *executor.Context) e
m.log.Warningf("--keep-db is not used. Directory %v with DB will be removed at the end of this run.", ctx.StateDbPath)
}

if err := utils.WriteStateDbInfo(ctx.StateDbPath, m.cfg, 0, gc.Hash{}, false); err != nil {
return fmt.Errorf("failed to create state-db info file; %v", err)
// Set state-db info to incomplete state at the beginning of the run.
// If state-db info exists, read block number and hash from it.
var blockNum uint64
var blockHash gc.Hash
if m.cfg.IsExistingStateDb {
dbinfo, err := utils.ReadStateDbInfo(ctx.StateDbPath)
if err != nil {
return fmt.Errorf("failed to read state-db info file; %v", err)
}
m.log.Infof("Resuming from block %v", blockNum)
blockNum = dbinfo.Block
}

// Mark state-db info with incomplete state.
if err := utils.WriteStateDbInfo(ctx.StateDbPath, m.cfg, blockNum, blockHash, false); err != nil {
return fmt.Errorf("failed to create state-db info file; %v", err)
}
return nil
}

Expand Down
14 changes: 7 additions & 7 deletions executor/extension/statedb/state_db_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestStateDbManager_StateDbInfoExistenceAndReadable(t *testing.T) {
t.Fatalf("failed to find %v of stateDbInfo; %v", utils.PathToDbInfo, err)
}

_, err = utils.ReadStateDbInfo(filename)
_, err = utils.ReadStateDbInfo(expectedPath)
if err != nil {
t.Fatal("failed to read statedb_info.json")
}
Expand Down Expand Up @@ -223,15 +223,15 @@ func TestStateDbManager_OverrideArchiveMode(t *testing.T) {
}

expectedName := fmt.Sprintf("state_db_%v_%v_%v", cfg.DbImpl, cfg.DbVariant, state.Block)
DbPath := filepath.Join(cfg.DbTmp, expectedName)
dbPath := filepath.Join(cfg.DbTmp, expectedName)

filename := filepath.Join(DbPath, utils.PathToDbInfo)
filename := filepath.Join(dbPath, utils.PathToDbInfo)

if _, err := os.Stat(filename); err != nil {
t.Fatalf("failed to find %v of stateDbInfo; %v", utils.PathToDbInfo, err)
}

stateDbInfo, err := utils.ReadStateDbInfo(filename)
stateDbInfo, err := utils.ReadStateDbInfo(dbPath)
if err != nil {
t.Fatal("failed to read statedb_info.json")
}
Expand All @@ -257,7 +257,7 @@ func TestStateDbManager_OverrideArchiveMode(t *testing.T) {
cfg.DbImpl = "carmen"
cfg.ChainID = utils.MainnetChainID
cfg.ArchiveMode = true
cfg.StateDbSrc = DbPath
cfg.StateDbSrc = dbPath
if test.readOnlyTool {
cfg.SetStateDbSrcReadOnly()
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func TestStateDbManager_OverrideArchiveVariant(t *testing.T) {
t.Fatalf("failed to find %v of stateDbInfo; %v", utils.PathToDbInfo, err)
}

stateDbInfo, err := utils.ReadStateDbInfo(filename)
stateDbInfo, err := utils.ReadStateDbInfo(dbPath)
if err != nil {
t.Fatal("failed to read statedb_info.json")
}
Expand Down Expand Up @@ -710,7 +710,7 @@ func TestStateDbManager_PreRunCreatesZeroStateDbInfo(t *testing.T) {
t.Fatalf("failed to create stateDb; %v", err)
}

info, err := utils.ReadStateDbInfo(filepath.Join(ctx.StateDbPath, utils.PathToDbInfo))
info, err := utils.ReadStateDbInfo(ctx.StateDbPath)
if err != nil {
t.Fatalf("cannot read state-db info: %v", err)
}
Expand Down
10 changes: 4 additions & 6 deletions utils/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@ func useExistingStateDB(cfg *Config) (state.StateDB, string, error) {
cfg.PathToStateDb = filepath.Join(cfg.PathToStateDb, PathToPrimaryStateDb)
}

stateDbInfoFile := filepath.Join(cfg.PathToStateDb, PathToDbInfo)
stateDbInfo, err = ReadStateDbInfo(stateDbInfoFile)
stateDbInfo, err = ReadStateDbInfo(cfg.PathToStateDb)
if err != nil {
return nil, "", fmt.Errorf("cannot read StateDb cfg file '%v'; %v", stateDbInfoFile, err)
return nil, "", fmt.Errorf("cannot read StateDb cfg file in '%v'; %v", cfg.PathToStateDb, err)
}

// If the state db is in read-only mode, set archive config as in statedb_info
Expand Down Expand Up @@ -143,10 +142,9 @@ func useExistingStateDB(cfg *Config) (state.StateDB, string, error) {
)

shadowDbPath = filepath.Join(cfg.StateDbSrc, PathToShadowStateDb)
shadowDbInfoFile := filepath.Join(shadowDbPath, PathToDbInfo)
shadowDbInfo, err = ReadStateDbInfo(shadowDbInfoFile)
shadowDbInfo, err = ReadStateDbInfo(shadowDbPath)
if err != nil {
return nil, "", fmt.Errorf("cannot read ShadowDb cfg file '%v'; %v", shadowDbInfoFile, err)
return nil, "", fmt.Errorf("cannot read ShadowDb info file in '%v'; %v", shadowDbPath, err)
}

cfg.ShadowImpl = shadowDbInfo.Impl
Expand Down
3 changes: 2 additions & 1 deletion utils/statedb_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ func WriteStateDbInfo(directory string, cfg *Config, block uint64, root common.H
}

// ReadStateDbInfo reads meta file of loaded stateDB
func ReadStateDbInfo(filename string) (StateDbInfo, error) {
func ReadStateDbInfo(dbpath string) (StateDbInfo, error) {
var dbinfo StateDbInfo
filename := filepath.Join(dbpath, PathToDbInfo)
file, err := os.ReadFile(filename)
if err != nil {
return dbinfo, fmt.Errorf("failed to read %v; %v", filename, err)
Expand Down
3 changes: 1 addition & 2 deletions utils/statedb_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ func TestStatedbInfo_WriteReadStateDbInfo(t *testing.T) {
}

// Getting the DB info file path and call for reading from it
dbInfoFile := filepath.Join(cfg.StateDbSrc, PathToDbInfo)
dbInfo, err := ReadStateDbInfo(dbInfoFile)
dbInfo, err := ReadStateDbInfo(cfg.StateDbSrc)
if err != nil {
t.Fatalf("failed to read from DB info json file: %v", err)
}
Expand Down

0 comments on commit 7bddcf6

Please sign in to comment.