forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
157 lines (138 loc) · 5.18 KB
/
util.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package tests
import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/cauthdsl"
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/common/crypto"
"github.com/hyperledger/fabric/common/flogging"
mmsp "github.com/hyperledger/fabric/common/mocks/msp"
lutils "github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/ledger/rwset"
protopeer "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protoutil"
)
var logger = flogging.MustGetLogger("test2")
// collConf helps writing tests with less verbose code by specifying coll configuration
// in a simple struct in place of 'common.CollectionConfigPackage'. (the test heplers' apis
// use 'collConf' as parameters and return values and transform back and forth to/from proto
// message internally (using func 'convertToCollConfigProtoBytes' and 'convertFromCollConfigProto')
type collConf struct {
name string
btl uint64
members []string
}
type txAndPvtdata struct {
Txid string
Envelope *common.Envelope
Pvtws *rwset.TxPvtReadWriteSet
}
func convertToCollConfigProtoBytes(collConfs []*collConf) ([]byte, error) {
var protoConfArray []*common.CollectionConfig
for _, c := range collConfs {
protoConf := &common.CollectionConfig{
Payload: &common.CollectionConfig_StaticCollectionConfig{
StaticCollectionConfig: &common.StaticCollectionConfig{
Name: c.name,
BlockToLive: c.btl,
MemberOrgsPolicy: convertToMemberOrgsPolicy(c.members),
},
},
}
protoConfArray = append(protoConfArray, protoConf)
}
return proto.Marshal(&common.CollectionConfigPackage{Config: protoConfArray})
}
func convertToMemberOrgsPolicy(members []string) *common.CollectionPolicyConfig {
var data [][]byte
for _, member := range members {
data = append(data, []byte(member))
}
return &common.CollectionPolicyConfig{
Payload: &common.CollectionPolicyConfig_SignaturePolicy{
SignaturePolicy: cauthdsl.Envelope(cauthdsl.Or(cauthdsl.SignedBy(0), cauthdsl.SignedBy(1)), data),
},
}
}
func convertFromMemberOrgsPolicy(policy *common.CollectionPolicyConfig) []string {
ids := policy.GetSignaturePolicy().Identities
var members []string
for _, id := range ids {
members = append(members, string(id.Principal))
}
return members
}
func convertFromCollConfigProto(collConfPkg *common.CollectionConfigPackage) []*collConf {
var collConfs []*collConf
protoConfArray := collConfPkg.Config
for _, protoConf := range protoConfArray {
p := protoConf.GetStaticCollectionConfig()
collConfs = append(collConfs,
&collConf{
name: p.Name,
btl: p.BlockToLive,
members: convertFromMemberOrgsPolicy(p.MemberOrgsPolicy),
},
)
}
return collConfs
}
func constructTransaction(txid string, simulationResults []byte) (*common.Envelope, error) {
channelid := "dummyChannel"
ccid := &protopeer.ChaincodeID{
Name: "dummyCC",
Version: "dummyVer",
}
txenv, _, err := constructUnsignedTxEnv(channelid, ccid, &protopeer.Response{Status: 200}, simulationResults, txid, nil, nil)
return txenv, err
}
// constructUnsignedTxEnv creates a Transaction envelope from given inputs
func constructUnsignedTxEnv(chainID string, ccid *protopeer.ChaincodeID, response *protopeer.Response, simulationResults []byte, txid string, events []byte, visibility []byte) (*common.Envelope, string, error) {
mspLcl := mmsp.NewNoopMsp()
sigId, _ := mspLcl.GetDefaultSigningIdentity()
ss, err := sigId.Serialize()
if err != nil {
return nil, "", err
}
var prop *protopeer.Proposal
if txid == "" {
// if txid is not set, then we need to generate one while creating the proposal message
prop, txid, err = protoutil.CreateChaincodeProposal(common.HeaderType_ENDORSER_TRANSACTION, chainID, &protopeer.ChaincodeInvocationSpec{ChaincodeSpec: &protopeer.ChaincodeSpec{ChaincodeId: ccid}}, ss)
} else {
// if txid is set, we should not generate a txid instead reuse the given txid
nonce, err := crypto.GetRandomNonce()
if err != nil {
return nil, "", err
}
prop, txid, err = protoutil.CreateChaincodeProposalWithTxIDNonceAndTransient(txid, common.HeaderType_ENDORSER_TRANSACTION, chainID, &protopeer.ChaincodeInvocationSpec{ChaincodeSpec: &protopeer.ChaincodeSpec{ChaincodeId: ccid}}, nonce, ss, nil)
}
if err != nil {
return nil, "", err
}
presp, err := protoutil.CreateProposalResponse(prop.Header, prop.Payload, response, simulationResults, nil, ccid, nil, sigId)
if err != nil {
return nil, "", err
}
env, err := protoutil.CreateSignedTx(prop, sigId, presp)
if err != nil {
return nil, "", err
}
return env, txid, nil
}
func constructTestGenesisBlock(channelid string) (*common.Block, error) {
blk, err := configtxtest.MakeGenesisBlock(channelid)
if err != nil {
return nil, err
}
setBlockFlagsToValid(blk)
return blk, nil
}
func setBlockFlagsToValid(block *common.Block) {
protoutil.InitBlockMetadata(block)
block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] =
lutils.NewTxValidationFlagsSetValue(len(block.Data.Data), protopeer.TxValidationCode_VALID)
}