github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/peer/node/unjoin.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package node
     8  
     9  import (
    10  	"path/filepath"
    11  
    12  	coreconfig "github.com/hechain20/hechain/core/config"
    13  	"github.com/hechain20/hechain/core/ledger/kvledger"
    14  	"github.com/hechain20/hechain/core/transientstore"
    15  	"github.com/hechain20/hechain/internal/peer/common"
    16  	"github.com/pkg/errors"
    17  	"github.com/spf13/cobra"
    18  )
    19  
    20  func unjoinCmd() *cobra.Command {
    21  	var channelID string
    22  
    23  	cmd := &cobra.Command{
    24  		Use:   "unjoin",
    25  		Short: "Unjoin the peer from a channel.",
    26  		Long:  "Unjoin the peer from a channel.  When the command is executed, the peer must be offline.",
    27  		RunE: func(cmd *cobra.Command, args []string) error {
    28  			if channelID == common.UndefinedParamValue {
    29  				return errors.New("Must supply channel ID")
    30  			}
    31  
    32  			if err := unjoinChannel(channelID); err != nil {
    33  				return err
    34  			}
    35  
    36  			return nil
    37  		},
    38  	}
    39  	flags := cmd.Flags()
    40  	flags.StringVarP(&channelID, "channelID", "c", common.UndefinedParamValue, "Channel to unjoin.")
    41  
    42  	return cmd
    43  }
    44  
    45  // unjoin the peer from a channel.
    46  func unjoinChannel(channelID string) error {
    47  	// transient storage must be scrubbed prior to removing the kvledger for the channel.  Once the
    48  	// kvledger storage has been removed, a subsequent ledger removal will return a "no such ledger" error.
    49  	// By removing the transient storage prior to deleting the ledger, a crash may be recovered by re-running
    50  	// the peer unjoin.
    51  	transientStoragePath := filepath.Join(coreconfig.GetPath("peer.fileSystemPath"), "transientstore")
    52  	if err := transientstore.Drop(transientStoragePath, channelID); err != nil {
    53  		return err
    54  	}
    55  
    56  	config := ledgerConfig()
    57  	if err := kvledger.UnjoinChannel(config, channelID); err != nil {
    58  		return err
    59  	}
    60  
    61  	return nil
    62  }