github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/rollback.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  // 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  	ledgerIDs, err := blkstorage.GetLedgersBootstrappedFromSnapshot(blockstorePath)
    27  	if err != nil {
    28  		return errors.WithMessage(err, "error while checking if any ledger has been bootstrapped from snapshot")
    29  	}
    30  	if len(ledgerIDs) > 0 {
    31  		return errors.Errorf("cannot rollback any channel because the peer contains channel(s) %s that were bootstrapped from snapshot", ledgerIDs)
    32  	}
    33  
    34  	if err := blkstorage.ValidateRollbackParams(blockstorePath, ledgerID, blockNum); err != nil {
    35  		return err
    36  	}
    37  
    38  	logger.Infof("Dropping databases")
    39  	if err := dropDBs(rootFSPath); err != nil {
    40  		return err
    41  	}
    42  
    43  	logger.Info("Rolling back ledger store")
    44  	indexConfig := &blkstorage.IndexConfig{AttrsToIndex: attrsToIndex}
    45  	if err := blkstorage.Rollback(blockstorePath, ledgerID, blockNum, indexConfig); err != nil {
    46  		return err
    47  	}
    48  	logger.Infof("The channel [%s] has been successfully rolled back to the block number [%d]", ledgerID, blockNum)
    49  	return nil
    50  }