github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/reset.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/blkstorage/fsblkstorage"
    11  	"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  // ResetAllKVLedgers resets all ledger to the genesis block.
    16  func ResetAllKVLedgers(rootFSPath string) 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  	logger.Info("Resetting all channel ledgers to genesis block")
    26  	logger.Infof("Ledger data folder from config = [%s]", rootFSPath)
    27  	if err := dropDBs(rootFSPath); err != nil {
    28  		return err
    29  	}
    30  	if err := resetBlockStorage(rootFSPath); err != nil {
    31  		return err
    32  	}
    33  	logger.Info("All channel ledgers have been successfully reset to the genesis block")
    34  	return nil
    35  }
    36  
    37  // LoadPreResetHeight returns the prereset height for the specified ledgers.
    38  func LoadPreResetHeight(rootFSPath string, ledgerIDs []string) (map[string]uint64, error) {
    39  	blockstorePath := BlockStorePath(rootFSPath)
    40  	logger.Infof("Loading prereset height from path [%s]", blockstorePath)
    41  	return fsblkstorage.LoadPreResetHeight(blockstorePath, ledgerIDs)
    42  }
    43  
    44  // ClearPreResetHeight removes the prereset height recorded in the file system for the specified ledgers.
    45  func ClearPreResetHeight(rootFSPath string, ledgerIDs []string) error {
    46  	blockstorePath := BlockStorePath(rootFSPath)
    47  	logger.Infof("Clearing off prereset height files from path [%s] for ledgerIDs [%#v]", blockstorePath, ledgerIDs)
    48  	return fsblkstorage.ClearPreResetHeight(blockstorePath, ledgerIDs)
    49  }
    50  
    51  func resetBlockStorage(rootFSPath string) error {
    52  	blockstorePath := BlockStorePath(rootFSPath)
    53  	logger.Infof("Resetting BlockStore to genesis block at location [%s]", blockstorePath)
    54  	return fsblkstorage.ResetBlockStore(blockstorePath)
    55  }