github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/reset.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package kvledger
     8  
     9  import (
    10  	"github.com/hechain20/hechain/common/ledger/blkstorage"
    11  	"github.com/hechain20/hechain/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  	blockstorePath := BlockStorePath(rootFSPath)
    26  	ledgerIDs, err := blkstorage.GetLedgersBootstrappedFromSnapshot(blockstorePath)
    27  	if err != nil {
    28  		return err
    29  	}
    30  	if len(ledgerIDs) > 0 {
    31  		return errors.Errorf("cannot reset channels because the peer contains channel(s) %s that were bootstrapped from snapshot", ledgerIDs)
    32  	}
    33  
    34  	logger.Info("Resetting all channel ledgers to genesis block")
    35  	logger.Infof("Ledger data folder from config = [%s]", rootFSPath)
    36  	if err := dropDBs(rootFSPath); err != nil {
    37  		return err
    38  	}
    39  	if err := resetBlockStorage(rootFSPath); err != nil {
    40  		return err
    41  	}
    42  	logger.Info("All channel ledgers have been successfully reset to the genesis block")
    43  	return nil
    44  }
    45  
    46  // LoadPreResetHeight returns the prereset height for the specified ledgers.
    47  func LoadPreResetHeight(rootFSPath string, ledgerIDs []string) (map[string]uint64, error) {
    48  	blockstorePath := BlockStorePath(rootFSPath)
    49  	logger.Infof("Loading prereset height from path [%s]", blockstorePath)
    50  	return blkstorage.LoadPreResetHeight(blockstorePath, ledgerIDs)
    51  }
    52  
    53  // ClearPreResetHeight removes the prereset height recorded in the file system for the specified ledgers.
    54  func ClearPreResetHeight(rootFSPath string, ledgerIDs []string) error {
    55  	blockstorePath := BlockStorePath(rootFSPath)
    56  	logger.Infof("Clearing off prereset height files from path [%s] for ledgerIDs [%#v]", blockstorePath, ledgerIDs)
    57  	return blkstorage.ClearPreResetHeight(blockstorePath, ledgerIDs)
    58  }
    59  
    60  func resetBlockStorage(rootFSPath string) error {
    61  	blockstorePath := BlockStorePath(rootFSPath)
    62  	logger.Infof("Resetting BlockStore to genesis block at location [%s]", blockstorePath)
    63  	return blkstorage.ResetBlockStore(blockstorePath)
    64  }