mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-10-14 00:59:33 +00:00
Add version file to database (#1678)
* Add version file to database * Remove redundant code * Check for version before opening the database, create version file after * Create version file before opening the database
This commit is contained in:
parent
15811b0bcb
commit
acdc59b565
25
app/app.go
25
app/app.go
@ -85,12 +85,6 @@ func (app *kaspadApp) main(startedChan chan<- struct{}) error {
|
|||||||
profiling.Start(app.cfg.Profile, log)
|
profiling.Start(app.cfg.Profile, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform upgrades to kaspad as new versions require it.
|
|
||||||
if err := doUpgrades(); err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return now if an interrupt signal was triggered.
|
// Return now if an interrupt signal was triggered.
|
||||||
if signal.InterruptRequested(interrupt) {
|
if signal.InterruptRequested(interrupt) {
|
||||||
return nil
|
return nil
|
||||||
@ -163,12 +157,6 @@ func (app *kaspadApp) main(startedChan chan<- struct{}) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// doUpgrades performs upgrades to kaspad as new versions require it.
|
|
||||||
// currently it's a placeholder we got from kaspad upstream, that does nothing
|
|
||||||
func doUpgrades() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// dbPath returns the path to the block database given a database type.
|
// dbPath returns the path to the block database given a database type.
|
||||||
func databasePath(cfg *config.Config) string {
|
func databasePath(cfg *config.Config) string {
|
||||||
return filepath.Join(cfg.AppDir, "data")
|
return filepath.Join(cfg.AppDir, "data")
|
||||||
@ -181,6 +169,17 @@ func removeDatabase(cfg *config.Config) error {
|
|||||||
|
|
||||||
func openDB(cfg *config.Config) (database.Database, error) {
|
func openDB(cfg *config.Config) (database.Database, error) {
|
||||||
dbPath := databasePath(cfg)
|
dbPath := databasePath(cfg)
|
||||||
|
|
||||||
|
err := checkDatabaseVersion(dbPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
log.Infof("Loading database from '%s'", dbPath)
|
log.Infof("Loading database from '%s'", dbPath)
|
||||||
return ldb.NewLevelDB(dbPath, leveldbCacheSizeMiB)
|
db, err := ldb.NewLevelDB(dbPath, leveldbCacheSizeMiB)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return db, nil
|
||||||
}
|
}
|
||||||
|
57
app/db_version.go
Normal file
57
app/db_version.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
const currentDatabaseVersion = 1
|
||||||
|
|
||||||
|
func checkDatabaseVersion(dbPath string) (err error) {
|
||||||
|
versionFileName := versionFilePath(dbPath)
|
||||||
|
|
||||||
|
versionBytes, err := os.ReadFile(versionFileName)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) { // If version file doesn't exist, we assume that the database is new
|
||||||
|
return createDatabaseVersionFile(dbPath, versionFileName)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
databaseVersion, err := strconv.Atoi(string(versionBytes))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if databaseVersion != currentDatabaseVersion {
|
||||||
|
// TODO: Once there's more then one database version, it might make sense to add upgrade logic at this point
|
||||||
|
return errors.Errorf("Invalid database version %d. Expected version: %d", databaseVersion, currentDatabaseVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createDatabaseVersionFile(dbPath string, versionFileName string) error {
|
||||||
|
err := os.MkdirAll(dbPath, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
versionFile, err := os.Create(versionFileName)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
defer versionFile.Close()
|
||||||
|
|
||||||
|
versionString := strconv.Itoa(currentDatabaseVersion)
|
||||||
|
_, err = versionFile.Write([]byte(versionString))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func versionFilePath(dbPath string) string {
|
||||||
|
dbVersionFileName := path.Join(dbPath, "version")
|
||||||
|
return dbVersionFileName
|
||||||
|
}
|
@ -9,7 +9,9 @@ import (
|
|||||||
// AddBlock submits the given block to be added to the
|
// AddBlock submits the given block to be added to the
|
||||||
// current virtual. This process may result in a new virtual block
|
// current virtual. This process may result in a new virtual block
|
||||||
// getting created
|
// getting created
|
||||||
func (csm *consensusStateManager) AddBlock(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (*externalapi.SelectedChainPath, externalapi.UTXODiff, error) {
|
func (csm *consensusStateManager) AddBlock(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (
|
||||||
|
*externalapi.SelectedChainPath, externalapi.UTXODiff, error) {
|
||||||
|
|
||||||
onEnd := logger.LogAndMeasureExecutionTime(log, "csm.AddBlock")
|
onEnd := logger.LogAndMeasureExecutionTime(log, "csm.AddBlock")
|
||||||
defer onEnd()
|
defer onEnd()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user