github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/tx/endorser/endorsertx_suite_test.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package endorsertx_test
     8  
     9  import (
    10  	"math/rand"
    11  	"testing"
    12  
    13  	txpkg "github.com/hechain20/hechain/pkg/tx"
    14  	"github.com/hechain20/hechain/protoutil"
    15  	"github.com/hyperledger/fabric-protos-go/common"
    16  	"github.com/hyperledger/fabric-protos-go/peer"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  func randomLowerAlphaString(size int) string {
    22  	letters := []rune("abcdefghijklmnopqrstuvwxyz")
    23  	output := make([]rune, size)
    24  	for i := range output {
    25  		output[i] = letters[rand.Intn(len(letters))]
    26  	}
    27  	return string(output)
    28  }
    29  
    30  func genTxEnvelope(
    31  	hdrExt []byte,
    32  	payloadData []byte,
    33  	prpExt []byte,
    34  	prp []byte,
    35  	chHeader *common.ChannelHeader,
    36  	sigHeader *common.SignatureHeader,
    37  ) *txpkg.Envelope {
    38  	var hdrExtBytes []byte
    39  	if hdrExt != nil {
    40  		hdrExtBytes = hdrExt
    41  	} else {
    42  		hdrExtBytes = protoutil.MarshalOrPanic(
    43  			&peer.ChaincodeHeaderExtension{
    44  				ChaincodeId: &peer.ChaincodeID{
    45  					Name: "my-called-cc",
    46  				},
    47  			},
    48  		)
    49  	}
    50  
    51  	chHeader.Extension = hdrExtBytes
    52  
    53  	var extBytes []byte
    54  	if prpExt != nil {
    55  		extBytes = prpExt
    56  	} else {
    57  		extBytes = protoutil.MarshalOrPanic(&peer.ChaincodeAction{
    58  			Results: []byte("results"),
    59  			Response: &peer.Response{
    60  				Status: 200,
    61  			},
    62  			Events: []byte("events"),
    63  		})
    64  	}
    65  
    66  	var prpBytes []byte
    67  	if prp != nil {
    68  		prpBytes = prp
    69  	} else {
    70  		prpBytes = protoutil.MarshalOrPanic(&peer.ProposalResponsePayload{
    71  			Extension:    extBytes,
    72  			ProposalHash: []byte("phash"),
    73  		})
    74  	}
    75  
    76  	ccEndAct := &peer.ChaincodeEndorsedAction{
    77  		ProposalResponsePayload: prpBytes,
    78  		Endorsements: []*peer.Endorsement{
    79  			{
    80  				Endorser:  []byte("endorser"),
    81  				Signature: []byte("signature"),
    82  			},
    83  		},
    84  	}
    85  
    86  	ccActP := &peer.ChaincodeActionPayload{
    87  		Action: ccEndAct,
    88  	}
    89  
    90  	tx := &peer.Transaction{
    91  		Actions: []*peer.TransactionAction{
    92  			{
    93  				Payload: protoutil.MarshalOrPanic(ccActP),
    94  			},
    95  		},
    96  	}
    97  
    98  	var txenvPayloadDataBytes []byte
    99  	if payloadData != nil {
   100  		txenvPayloadDataBytes = payloadData
   101  	} else {
   102  		txenvPayloadDataBytes = protoutil.MarshalOrPanic(tx)
   103  	}
   104  
   105  	return &txpkg.Envelope{
   106  		ChannelHeader:   chHeader,
   107  		SignatureHeader: sigHeader,
   108  		Data:            txenvPayloadDataBytes,
   109  	}
   110  }
   111  
   112  func TestEndorserTx(t *testing.T) {
   113  	RegisterFailHandler(Fail)
   114  	RunSpecs(t, "EndorserTx Suite")
   115  }