github.com/true-sqn/fabric@v2.1.1+incompatible/internal/peer/channel/fetch.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package channel
     8  
     9  import (
    10  	"fmt"
    11  	"io/ioutil"
    12  	"strconv"
    13  	"strings"
    14  
    15  	"github.com/golang/protobuf/proto"
    16  	cb "github.com/hyperledger/fabric-protos-go/common"
    17  	"github.com/hyperledger/fabric/internal/peer/common"
    18  	"github.com/hyperledger/fabric/protoutil"
    19  	"github.com/spf13/cobra"
    20  )
    21  
    22  func fetchCmd(cf *ChannelCmdFactory) *cobra.Command {
    23  	fetchCmd := &cobra.Command{
    24  		Use:   "fetch <newest|oldest|config|(number)> [outputfile]",
    25  		Short: "Fetch a block",
    26  		Long:  "Fetch a specified block, writing it to a file.",
    27  		RunE: func(cmd *cobra.Command, args []string) error {
    28  			return fetch(cmd, args, cf)
    29  		},
    30  	}
    31  	flagList := []string{
    32  		"channelID",
    33  		"bestEffort",
    34  	}
    35  	attachFlags(fetchCmd, flagList)
    36  
    37  	return fetchCmd
    38  }
    39  
    40  func fetch(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
    41  	if len(args) == 0 {
    42  		return fmt.Errorf("fetch target required, oldest, newest, config, or a number")
    43  	}
    44  	if len(args) > 2 {
    45  		return fmt.Errorf("trailing args detected")
    46  	}
    47  	// Parsing of the command line is done so silence cmd usage
    48  	cmd.SilenceUsage = true
    49  
    50  	// default to fetching from orderer
    51  	ordererRequired := OrdererRequired
    52  	peerDeliverRequired := PeerDeliverNotRequired
    53  	if len(strings.Split(common.OrderingEndpoint, ":")) != 2 {
    54  		// if no orderer endpoint supplied, connect to peer's deliver service
    55  		ordererRequired = OrdererNotRequired
    56  		peerDeliverRequired = PeerDeliverRequired
    57  	}
    58  	var err error
    59  	if cf == nil {
    60  		cf, err = InitCmdFactory(EndorserNotRequired, peerDeliverRequired, ordererRequired)
    61  		if err != nil {
    62  			return err
    63  		}
    64  	}
    65  
    66  	var block *cb.Block
    67  
    68  	switch args[0] {
    69  	case "oldest":
    70  		block, err = cf.DeliverClient.GetOldestBlock()
    71  	case "newest":
    72  		block, err = cf.DeliverClient.GetNewestBlock()
    73  	case "config":
    74  		iBlock, err2 := cf.DeliverClient.GetNewestBlock()
    75  		if err2 != nil {
    76  			return err2
    77  		}
    78  		lc, err2 := protoutil.GetLastConfigIndexFromBlock(iBlock)
    79  		if err2 != nil {
    80  			return err2
    81  		}
    82  		logger.Infof("Retrieving last config block: %d", lc)
    83  		block, err = cf.DeliverClient.GetSpecifiedBlock(lc)
    84  	default:
    85  		num, err2 := strconv.Atoi(args[0])
    86  		if err2 != nil {
    87  			return fmt.Errorf("fetch target illegal: %s", args[0])
    88  		}
    89  		block, err = cf.DeliverClient.GetSpecifiedBlock(uint64(num))
    90  	}
    91  
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	b, err := proto.Marshal(block)
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	var file string
   102  	if len(args) == 1 {
   103  		file = channelID + "_" + args[0] + ".block"
   104  	} else {
   105  		file = args[1]
   106  	}
   107  
   108  	if err = ioutil.WriteFile(file, b, 0644); err != nil {
   109  		return err
   110  	}
   111  
   112  	return nil
   113  }