/
txtestutils.go
114 lines (94 loc) · 3.93 KB
/
txtestutils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package testutils
import (
"fmt"
"os"
mmsp "github.com/hyperledger/fabric/common/mocks/msp"
"github.com/hyperledger/fabric/msp"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
putils "github.com/hyperledger/fabric/protos/utils"
)
var (
signer msp.SigningIdentity
)
func init() {
var err error
// setup the MSP manager so that we can sign/verify
err = msptesttools.LoadMSPSetupForTesting()
if err != nil {
fmt.Printf("Could not load msp config, err %s", err)
os.Exit(-1)
return
}
signer, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
if err != nil {
os.Exit(-1)
fmt.Printf("Could not initialize msp/signer")
return
}
}
// ConstractBytesProposalResponsePayload constructs a ProposalResponsePayload byte for tests with a default signer.
func ConstractBytesProposalResponsePayload(chainID string, ccid *pb.ChaincodeID, pResponse *pb.Response, simulationResults []byte) ([]byte, error) {
ss, err := signer.Serialize()
if err != nil {
return nil, err
}
prop, _, err := putils.CreateChaincodeProposal(common.HeaderType_ENDORSER_TRANSACTION, chainID, &pb.ChaincodeInvocationSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: ccid}}, ss)
if err != nil {
return nil, err
}
presp, err := putils.CreateProposalResponse(prop.Header, prop.Payload, pResponse, simulationResults, nil, ccid, nil, signer)
if err != nil {
return nil, err
}
return presp.Payload, nil
}
// ConstructSingedTxEnvWithDefaultSigner constructs a transaction envelop for tests with a default signer.
// This method helps other modules to construct a transaction with supplied parameters
func ConstructSingedTxEnvWithDefaultSigner(chainID string, ccid *pb.ChaincodeID, response *pb.Response, simulationResults []byte, events []byte, visibility []byte) (*common.Envelope, string, error) {
return ConstructSingedTxEnv(chainID, ccid, response, simulationResults, events, visibility, signer)
}
// ConstructSingedTxEnv constructs a transaction envelop for tests
func ConstructSingedTxEnv(chainID string, ccid *pb.ChaincodeID, pResponse *pb.Response, simulationResults []byte, events []byte, visibility []byte, signer msp.SigningIdentity) (*common.Envelope, string, error) {
ss, err := signer.Serialize()
if err != nil {
return nil, "", err
}
prop, txid, err := putils.CreateChaincodeProposal(common.HeaderType_ENDORSER_TRANSACTION, chainID, &pb.ChaincodeInvocationSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: ccid}}, ss)
if err != nil {
return nil, "", err
}
presp, err := putils.CreateProposalResponse(prop.Header, prop.Payload, pResponse, simulationResults, nil, ccid, nil, signer)
if err != nil {
return nil, "", err
}
env, err := putils.CreateSignedTx(prop, signer, presp)
if err != nil {
return nil, "", err
}
return env, txid, nil
}
var mspLcl msp.MSP
var sigId msp.SigningIdentity
// ConstructUnsingedTxEnv creates a Transaction envelope from given inputs
func ConstructUnsingedTxEnv(chainID string, ccid *pb.ChaincodeID, response *pb.Response, simulationResults []byte, events []byte, visibility []byte) (*common.Envelope, string, error) {
if mspLcl == nil {
mspLcl = mmsp.NewNoopMsp()
sigId, _ = mspLcl.GetDefaultSigningIdentity()
}
return ConstructSingedTxEnv(chainID, ccid, response, simulationResults, events, visibility, sigId)
}