github.com/hyperledger-labs/bdls@v2.1.1+incompatible/integration/raft/client.go (about)

     1  /*
     2  Copyright IBM Corp All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package raft
     8  
     9  import (
    10  	"github.com/hyperledger/fabric-protos-go/common"
    11  	"github.com/hyperledger/fabric-protos-go/orderer"
    12  	"github.com/hyperledger/fabric/cmd/common/signer"
    13  	"github.com/hyperledger/fabric/integration/nwo"
    14  	"github.com/hyperledger/fabric/protoutil"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  func FetchBlock(n *nwo.Network, o *nwo.Orderer, seq uint64, channel string) *common.Block {
    19  	denv := CreateDeliverEnvelope(n, o, seq, channel)
    20  	Expect(denv).NotTo(BeNil())
    21  
    22  	var blk *common.Block
    23  	Eventually(func() error {
    24  		var err error
    25  		blk, err = nwo.Deliver(n, o, denv)
    26  		return err
    27  	}, n.EventuallyTimeout).ShouldNot(HaveOccurred())
    28  
    29  	return blk
    30  }
    31  
    32  func CreateBroadcastEnvelope(n *nwo.Network, signer interface{}, channel string, data []byte) *common.Envelope {
    33  	env, err := protoutil.CreateSignedEnvelope(
    34  		common.HeaderType_MESSAGE,
    35  		channel,
    36  		nil,
    37  		&common.Envelope{Payload: data},
    38  		0,
    39  		0,
    40  	)
    41  	Expect(err).NotTo(HaveOccurred())
    42  
    43  	return signAsAdmin(n, signer, env)
    44  }
    45  
    46  // CreateDeliverEnvelope creates a deliver env to seek for specified block.
    47  func CreateDeliverEnvelope(n *nwo.Network, entity interface{}, blkNum uint64, channel string) *common.Envelope {
    48  	specified := &orderer.SeekPosition{
    49  		Type: &orderer.SeekPosition_Specified{
    50  			Specified: &orderer.SeekSpecified{Number: blkNum},
    51  		},
    52  	}
    53  	env, err := protoutil.CreateSignedEnvelope(
    54  		common.HeaderType_DELIVER_SEEK_INFO,
    55  		channel,
    56  		nil,
    57  		&orderer.SeekInfo{
    58  			Start:    specified,
    59  			Stop:     specified,
    60  			Behavior: orderer.SeekInfo_BLOCK_UNTIL_READY,
    61  		},
    62  		0,
    63  		0,
    64  	)
    65  	Expect(err).NotTo(HaveOccurred())
    66  
    67  	return signAsAdmin(n, entity, env)
    68  }
    69  
    70  func signAsAdmin(n *nwo.Network, entity interface{}, env *common.Envelope) *common.Envelope {
    71  	var conf signer.Config
    72  	switch t := entity.(type) {
    73  	case *nwo.Peer:
    74  		conf = signer.Config{
    75  			MSPID:        n.Organization(t.Organization).MSPID,
    76  			IdentityPath: n.PeerUserCert(t, "Admin"),
    77  			KeyPath:      n.PeerUserKey(t, "Admin"),
    78  		}
    79  	case *nwo.Orderer:
    80  		conf = signer.Config{
    81  			MSPID:        n.Organization(t.Organization).MSPID,
    82  			IdentityPath: n.OrdererUserCert(t, "Admin"),
    83  			KeyPath:      n.OrdererUserKey(t, "Admin"),
    84  		}
    85  	default:
    86  		panic("unsupported signing entity type")
    87  	}
    88  
    89  	signer, err := signer.NewSigner(conf)
    90  	Expect(err).NotTo(HaveOccurred())
    91  
    92  	payload, err := protoutil.UnmarshalPayload(env.Payload)
    93  	Expect(err).NotTo(HaveOccurred())
    94  	Expect(payload.Header).NotTo(BeNil())
    95  	Expect(payload.Header.ChannelHeader).NotTo(BeNil())
    96  
    97  	nonce, err := protoutil.CreateNonce()
    98  	Expect(err).NotTo(HaveOccurred())
    99  	sighdr := &common.SignatureHeader{
   100  		Creator: signer.Creator,
   101  		Nonce:   nonce,
   102  	}
   103  	payload.Header.SignatureHeader = protoutil.MarshalOrPanic(sighdr)
   104  	payloadBytes := protoutil.MarshalOrPanic(payload)
   105  
   106  	sig, err := signer.Sign(payloadBytes)
   107  	Expect(err).NotTo(HaveOccurred())
   108  
   109  	return &common.Envelope{Payload: payloadBytes, Signature: sig}
   110  }