github.com/kaituanwang/hyperledger@v2.0.1+incompatible/core/ledger/kvledger/reset.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 SPDX-License-Identifier: Apache-2.0 4 */ 5 6 package kvledger 7 8 import ( 9 "github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage" 10 "github.com/hyperledger/fabric/common/ledger/util/leveldbhelper" 11 "github.com/pkg/errors" 12 ) 13 14 // ResetAllKVLedgers resets all ledger to the genesis block. 15 func ResetAllKVLedgers(rootFSPath string) error { 16 fileLockPath := fileLockPath(rootFSPath) 17 fileLock := leveldbhelper.NewFileLock(fileLockPath) 18 if err := fileLock.Lock(); err != nil { 19 return errors.Wrap(err, "as another peer node command is executing,"+ 20 " wait for that command to complete its execution or terminate it before retrying") 21 } 22 defer fileLock.Unlock() 23 24 logger.Info("Resetting all channel ledgers to genesis block") 25 logger.Infof("Ledger data folder from config = [%s]", rootFSPath) 26 if err := dropDBs(rootFSPath); err != nil { 27 return err 28 } 29 if err := resetBlockStorage(rootFSPath); err != nil { 30 return err 31 } 32 logger.Info("All channel ledgers have been successfully reset to the genesis block") 33 return nil 34 } 35 36 // LoadPreResetHeight returns the prereset height for the specified ledgers. 37 func LoadPreResetHeight(rootFSPath string, ledgerIDs []string) (map[string]uint64, error) { 38 blockstorePath := BlockStorePath(rootFSPath) 39 logger.Infof("Loading prereset height from path [%s]", blockstorePath) 40 return fsblkstorage.LoadPreResetHeight(blockstorePath, ledgerIDs) 41 } 42 43 // ClearPreResetHeight removes the prereset height recorded in the file system for the specified ledgers. 44 func ClearPreResetHeight(rootFSPath string, ledgerIDs []string) error { 45 blockstorePath := BlockStorePath(rootFSPath) 46 logger.Infof("Clearing off prereset height files from path [%s] for ledgerIDs [%#v]", blockstorePath, ledgerIDs) 47 return fsblkstorage.ClearPreResetHeight(blockstorePath, ledgerIDs) 48 } 49 50 func resetBlockStorage(rootFSPath string) error { 51 blockstorePath := BlockStorePath(rootFSPath) 52 logger.Infof("Resetting BlockStore to genesis block at location [%s]", blockstorePath) 53 return fsblkstorage.ResetBlockStore(blockstorePath) 54 }