From 9455c047ce027050ae42fd9f358f207f4a7562ed Mon Sep 17 00:00:00 2001 From: Evan Han Date: Mon, 5 Sep 2022 19:15:38 +0800 Subject: [PATCH] Merge PR: check watchdb version when start node (#2502) --- x/evm/watcher/db.go | 22 ++++++++++++++++++++++ x/evm/watcher/watcher.go | 2 ++ 2 files changed, 24 insertions(+) diff --git a/x/evm/watcher/db.go b/x/evm/watcher/db.go index 1e19880a82..8ba0c1becd 100644 --- a/x/evm/watcher/db.go +++ b/x/evm/watcher/db.go @@ -1,7 +1,9 @@ package watcher import ( + "io/ioutil" "log" + "os" "path/filepath" "sync" @@ -47,9 +49,29 @@ func initDb() dbm.DB { backend = string(dbm.GoLevelDBBackend) } + versionPath := filepath.Join(dbPath, WatchDBName+".db", "VERSION") + if !checkVersion(versionPath) { + os.RemoveAll(filepath.Join(dbPath, WatchDBName+".db")) + } + db := dbm.NewDB(WatchDBName, dbm.BackendType(backend), dbPath) + writeVersion(versionPath) + return db + return dbm.NewDB(WatchDBName, dbm.BackendType(backend), dbPath) } +func checkVersion(versionPath string) bool { + content, err := ioutil.ReadFile(versionPath) + if err != nil || string(content) != version { + return false + } + return true +} + +func writeVersion(versionPath string) { + ioutil.WriteFile(versionPath, []byte(version), 0666) +} + func (w WatchStore) Set(key []byte, value []byte) { err := w.db.Set(key, value) if err != nil { diff --git a/x/evm/watcher/watcher.go b/x/evm/watcher/watcher.go index 6de2329b3a..56b268c8fd 100644 --- a/x/evm/watcher/watcher.go +++ b/x/evm/watcher/watcher.go @@ -22,6 +22,8 @@ import ( "github.com/spf13/viper" ) +const version = "v1" + var itjs = jsoniter.ConfigCompatibleWithStandardLibrary type Watcher struct {