github.com/iotexproject/iotex-core@v1.14.1-rc1/tools/readtip/main.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 // This is a recovery tool that recovers a corrupted or missing state database. 7 // To use, run "make recover" 8 package main 9 10 import ( 11 "context" 12 "flag" 13 "fmt" 14 "os" 15 16 "go.uber.org/zap" 17 18 "github.com/iotexproject/iotex-core/config" 19 "github.com/iotexproject/iotex-core/db" 20 "github.com/iotexproject/iotex-core/pkg/log" 21 "github.com/iotexproject/iotex-core/pkg/util/byteutil" 22 "github.com/iotexproject/iotex-core/state/factory" 23 ) 24 25 var ( 26 // _stateDBPath is the path of state db 27 _stateDBPath string 28 // overwritePath is the path to the config file which overwrite default values 29 _overwritePath string 30 // secretPath is the path to the config file store secret values 31 _secretPath string 32 ) 33 34 func init() { 35 flag.StringVar(&_stateDBPath, "state-db-path", "", "State DB path") 36 flag.StringVar(&_overwritePath, "config-path", "", "Config path") 37 flag.StringVar(&_secretPath, "secret-path", "", "Secret path") 38 flag.Usage = func() { 39 _, _ = fmt.Fprintf(os.Stderr, "usage: readtip -config-path=[string]\n") 40 flag.PrintDefaults() 41 os.Exit(2) 42 } 43 flag.Parse() 44 } 45 46 func readStateDBPath() string { 47 if _stateDBPath != "" { 48 return _stateDBPath 49 } 50 cfg, err := config.New([]string{_overwritePath, _secretPath}, []string{}) 51 if err != nil { 52 log.S().Panic("failed to new config.", zap.Error(err)) 53 } 54 return cfg.Chain.TrieDBPath 55 } 56 57 func main() { 58 cfg := db.DefaultConfig 59 cfg.ReadOnly = true 60 store, err := db.CreateKVStore(cfg, readStateDBPath()) 61 if err != nil { 62 log.S().Panic("failed to load state db", zap.Error(err)) 63 } 64 if err := store.Start(context.Background()); err != nil { 65 log.S().Panic("failed to start db", zap.Error(err)) 66 } 67 defer func() { 68 if err := store.Stop(context.Background()); err != nil { 69 log.S().Panic("failed to stop db", zap.Error(err)) 70 } 71 }() 72 h, err := store.Get(factory.AccountKVNamespace, []byte(factory.CurrentHeightKey)) 73 if err != nil { 74 log.S().Panic("failed to read state db", zap.Error(err)) 75 } 76 fmt.Println(byteutil.BytesToUint64(h)) 77 }