github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/core/ledger/kvledger/rollback.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package kvledger 8 9 import ( 10 "github.com/hyperledger/fabric/common/ledger/util/leveldbhelper" 11 "github.com/hyperledger/fabric/core/ledger/ledgerstorage" 12 "github.com/pkg/errors" 13 ) 14 15 // RollbackKVLedger rollbacks a ledger to a specified block number 16 func RollbackKVLedger(rootFSPath, ledgerID string, blockNum uint64) error { 17 fileLockPath := fileLockPath(rootFSPath) 18 fileLock := leveldbhelper.NewFileLock(fileLockPath) 19 if err := fileLock.Lock(); err != nil { 20 return errors.Wrap(err, "as another peer node command is executing,"+ 21 " wait for that command to complete its execution or terminate it before retrying") 22 } 23 defer fileLock.Unlock() 24 25 blockstorePath := BlockStorePath(rootFSPath) 26 if err := ledgerstorage.ValidateRollbackParams(blockstorePath, ledgerID, blockNum); err != nil { 27 return err 28 } 29 30 logger.Infof("Dropping databases") 31 if err := dropDBs(rootFSPath); err != nil { 32 return err 33 } 34 35 logger.Info("Rolling back ledger store") 36 if err := ledgerstorage.Rollback(blockstorePath, ledgerID, blockNum); err != nil { 37 return err 38 } 39 logger.Infof("The channel [%s] has been successfully rolled back to the block number [%d]", ledgerID, blockNum) 40 return nil 41 }