github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/orderer/sample_clients/broadcast_timestamp/client.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"time"
    23  
    24  	"github.com/golang/protobuf/proto"
    25  	"github.com/hyperledger/fabric/common/configtx/tool/provisional"
    26  	"github.com/hyperledger/fabric/orderer/localconfig"
    27  	cb "github.com/hyperledger/fabric/protos/common"
    28  	ab "github.com/hyperledger/fabric/protos/orderer"
    29  	"github.com/hyperledger/fabric/protos/utils"
    30  	"golang.org/x/net/context"
    31  	"google.golang.org/grpc"
    32  )
    33  
    34  type broadcastClient struct {
    35  	client  ab.AtomicBroadcast_BroadcastClient
    36  	chainID string
    37  }
    38  
    39  // newBroadcastClient creates a simple instance of the broadcastClient interface
    40  func newBroadcastClient(client ab.AtomicBroadcast_BroadcastClient, chainID string) *broadcastClient {
    41  	return &broadcastClient{client: client, chainID: chainID}
    42  }
    43  
    44  func (s *broadcastClient) broadcast(transaction []byte) error {
    45  	payload, err := proto.Marshal(&cb.Payload{
    46  		Header: &cb.Header{
    47  			ChannelHeader: utils.MarshalOrPanic(&cb.ChannelHeader{
    48  				ChannelId: s.chainID,
    49  			}),
    50  			SignatureHeader: utils.MarshalOrPanic(&cb.SignatureHeader{}),
    51  		},
    52  		Data: transaction,
    53  	})
    54  	if err != nil {
    55  		panic(err)
    56  	}
    57  	return s.client.Send(&cb.Envelope{Payload: payload})
    58  }
    59  
    60  func (s *broadcastClient) getAck() error {
    61  	msg, err := s.client.Recv()
    62  	if err != nil {
    63  		return err
    64  	}
    65  	if msg.Status != cb.Status_SUCCESS {
    66  		return fmt.Errorf("Got unexpected status: %v", msg.Status)
    67  	}
    68  	return nil
    69  }
    70  
    71  func main() {
    72  	config := config.Load()
    73  
    74  	var chainID string
    75  	var serverAddr string
    76  	var messages uint64
    77  
    78  	flag.StringVar(&serverAddr, "server", fmt.Sprintf("%s:%d", config.General.ListenAddress, config.General.ListenPort), "The RPC server to connect to.")
    79  	flag.StringVar(&chainID, "chainID", provisional.TestChainID, "The chain ID to broadcast to.")
    80  	flag.Uint64Var(&messages, "messages", 1, "The number of messages to broadcast.")
    81  	flag.Parse()
    82  
    83  	conn, err := grpc.Dial(serverAddr, grpc.WithInsecure())
    84  	defer func() {
    85  		_ = conn.Close()
    86  	}()
    87  	if err != nil {
    88  		fmt.Println("Error connecting:", err)
    89  		return
    90  	}
    91  	client, err := ab.NewAtomicBroadcastClient(conn).Broadcast(context.TODO())
    92  	if err != nil {
    93  		fmt.Println("Error connecting:", err)
    94  		return
    95  	}
    96  
    97  	s := newBroadcastClient(client, chainID)
    98  	for i := uint64(0); i < messages; i++ {
    99  		s.broadcast([]byte(fmt.Sprintf("Testing %v", time.Now())))
   100  		err = s.getAck()
   101  	}
   102  	if err != nil {
   103  		fmt.Printf("\nError: %v\n", err)
   104  	}
   105  }