github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/peer/snapshot/listpending.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  // listPendingCmd returns the cobra command for snapshot listpending command
    20  func listPendingCmd(cl *client, cryptoProvider bccsp.BCCSP) *cobra.Command {
    21  	snapshotGenerateRequestCmd := &cobra.Command{
    22  		Use:   "listpending",
    23  		Short: "List pending requests for snapshots.",
    24  		Long:  "List pending requests for snapshots.",
    25  		RunE: func(cmd *cobra.Command, args []string) error {
    26  			return listPending(cmd, cl, cryptoProvider)
    27  		},
    28  	}
    29  	flagList := []string{
    30  		"channelID",
    31  		"peerAddress",
    32  		"tlsRootCertFile",
    33  	}
    34  	attachFlags(snapshotGenerateRequestCmd, flagList)
    35  
    36  	return snapshotGenerateRequestCmd
    37  }
    38  
    39  func listPending(cmd *cobra.Command, cl *client, cryptoProvider bccsp.BCCSP) error {
    40  	if err := validateListPending(); err != nil {
    41  		return err
    42  	}
    43  
    44  	// Parsing of the command line is done so silence cmd usage
    45  	cmd.SilenceUsage = true
    46  
    47  	// create a client if not provided
    48  	if cl == nil {
    49  		var err error
    50  		cl, err = newClient(cryptoProvider)
    51  		if err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	signatureHdr, err := createSignatureHeader(cl.signer)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	query := &pb.SnapshotQuery{
    62  		SignatureHeader: signatureHdr,
    63  		ChannelId:       channelID,
    64  	}
    65  	signedRequest, err := signSnapshotRequest(cl.signer, query)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	resp, err := cl.snapshotClient.QueryPendings(context.Background(), signedRequest)
    71  	if err != nil {
    72  		return errors.WithMessage(err, "failed to list pending requests")
    73  	}
    74  
    75  	fmt.Fprintf(cl.writer, "Successfully got pending snapshot requests: %v\n", resp.BlockNumbers)
    76  	return nil
    77  }
    78  
    79  func validateListPending() error {
    80  	if channelID == "" {
    81  		return errors.New("the required parameter 'channelID' is empty. Rerun the command with -c flag")
    82  	}
    83  	return nil
    84  }