github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/peer/snapshot/submitrequest.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  // submitRequestCmd returns the cobra command for snapshot submitrequest command
    20  func submitRequestCmd(cl *client, cryptoProvider bccsp.BCCSP) *cobra.Command {
    21  	snapshotSubmitRequestCmd := &cobra.Command{
    22  		Use:   "submitrequest",
    23  		Short: "Submit a request for a snapshot at the specified block.",
    24  		Long:  "Submit a request for a snapshot at the specified block. When the blockNumber parameter is set to 0 or not provided, it will submit a request for the last committed block.",
    25  		RunE: func(cmd *cobra.Command, args []string) error {
    26  			return submitRequest(cmd, cl, cryptoProvider)
    27  		},
    28  	}
    29  
    30  	flagList := []string{
    31  		"channelID",
    32  		"blockNumber",
    33  		"peerAddress",
    34  		"tlsRootCertFile",
    35  	}
    36  	attachFlags(snapshotSubmitRequestCmd, flagList)
    37  
    38  	return snapshotSubmitRequestCmd
    39  }
    40  
    41  func submitRequest(cmd *cobra.Command, cl *client, cryptoProvider bccsp.BCCSP) error {
    42  	if err := validateSubmitRequest(); 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.Generate(context.Background(), signedRequest)
    74  	if err != nil {
    75  		return errors.WithMessage(err, "failed to submit the request")
    76  	}
    77  
    78  	fmt.Fprint(cl.writer, "Snapshot request submitted successfully\n")
    79  	return nil
    80  }
    81  
    82  func validateSubmitRequest() error {
    83  	if channelID == "" {
    84  		return errors.New("the required parameter 'channelID' is empty. Rerun the command with -c flag")
    85  	}
    86  	return nil
    87  }