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

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package snapshot
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  
    13  	"github.com/hechain20/hechain/bccsp"
    14  	pb "github.com/hyperledger/fabric-protos-go/peer"
    15  	"github.com/pkg/errors"
    16  	"github.com/spf13/cobra"
    17  )
    18  
    19  // cancelRequestCmd returns the cobra command for snapshot cancelrequest command
    20  func cancelRequestCmd(cl *client, cryptoProvider bccsp.BCCSP) *cobra.Command {
    21  	snapshotCancelRequestCmd := &cobra.Command{
    22  		Use:   "cancelrequest",
    23  		Short: "Cancel a request for a snapshot at the specified block.",
    24  		Long:  "Cancel a request for a snapshot at the specified block.",
    25  		RunE: func(cmd *cobra.Command, args []string) error {
    26  			return cancelRequest(cmd, cl, cryptoProvider)
    27  		},
    28  	}
    29  
    30  	flagList := []string{
    31  		"channelID",
    32  		"blockNumber",
    33  		"peerAddress",
    34  		"tlsRootCertFile",
    35  	}
    36  	attachFlags(snapshotCancelRequestCmd, flagList)
    37  
    38  	return snapshotCancelRequestCmd
    39  }
    40  
    41  func cancelRequest(cmd *cobra.Command, cl *client, cryptoProvider bccsp.BCCSP) error {
    42  	if err := validateCancelRequest(); err != nil {
    43  		return err
    44  	}
    45  
    46  	// Parsing of the command line is done so silence cmd usage
    47  	cmd.SilenceUsage = true
    48  
    49  	// create a client if not provided
    50  	if cl == nil {
    51  		var err error
    52  		cl, err = newClient(cryptoProvider)
    53  		if err != nil {
    54  			return err
    55  		}
    56  	}
    57  
    58  	signatureHdr, err := createSignatureHeader(cl.signer)
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	request := &pb.SnapshotRequest{
    64  		SignatureHeader: signatureHdr,
    65  		ChannelId:       channelID,
    66  		BlockNumber:     blockNumber,
    67  	}
    68  	signedRequest, err := signSnapshotRequest(cl.signer, request)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	_, err = cl.snapshotClient.Cancel(context.Background(), signedRequest)
    74  	if err != nil {
    75  		return errors.WithMessage(err, "failed to cancel the request")
    76  	}
    77  
    78  	fmt.Fprint(cl.writer, "Snapshot request cancelled successfully\n")
    79  	return nil
    80  }
    81  
    82  func validateCancelRequest() error {
    83  	if channelID == "" {
    84  		return errors.New("the required parameter 'channelID' is empty. Rerun the command with -c flag")
    85  	}
    86  	if blockNumber == 0 {
    87  		return errors.New("the required parameter 'blockNumber' is empty or set to 0. Rerun the command with -b flag")
    88  	}
    89  	return nil
    90  }