-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathchaincode.go
311 lines (272 loc) · 7.49 KB
/
chaincode.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
Copyright: Cognition Foundry. All Rights Reserved.
License: Apache License Version 2.0
*/
package gohfc
import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/common"
"path/filepath"
"strings"
"os"
"io"
"bytes"
"compress/gzip"
"archive/tar"
"path"
"github.com/golang/protobuf/ptypes/timestamp"
"time"
"fmt"
)
type ChainCodeType int32
const (
ChaincodeSpec_UNDEFINED ChainCodeType = 0
ChaincodeSpec_GOLANG ChainCodeType = 1
ChaincodeSpec_NODE ChainCodeType = 2
ChaincodeSpec_CAR ChainCodeType = 3
ChaincodeSpec_JAVA ChainCodeType = 4
)
// ChainCode the fields necessary to execute operation over chaincode.
type ChainCode struct {
ChannelId string
Name string
Version string
Type ChainCodeType
Args []string
ArgBytes []byte
TransientMap map[string][]byte
rawArgs [][]byte
}
func (c *ChainCode) toChainCodeArgs() ([][]byte) {
if len(c.rawArgs) > 0 {
return c.rawArgs
}
args := make([][]byte, len(c.Args))
for i, arg := range c.Args {
args[i] = []byte(arg)
}
if len(c.ArgBytes) > 0 {
args = append(args, c.ArgBytes)
}
return args
}
// InstallRequest holds fields needed to install chaincode
type InstallRequest struct {
ChannelId string
ChainCodeName string
ChainCodeVersion string
ChainCodeType ChainCodeType
Namespace string
SrcPath string
Libraries []ChaincodeLibrary
}
type CollectionConfig struct {
Name string
RequiredPeersCount int32
MaximumPeersCount int32
Organizations []string
}
type ChaincodeLibrary struct {
Namespace string
SrcPath string
}
// ChainCodesResponse is the result of queering installed and instantiated chaincodes
type ChainCodesResponse struct {
PeerName string
Error error
ChainCodes []*peer.ChaincodeInfo
}
// createInstallProposal read chaincode from provided source and namespace, pack it and generate install proposal
// transaction. Transaction is not send from this func
func createInstallProposal(identity Identity, req *InstallRequest) (*transactionProposal, error) {
var packageBytes []byte
var err error
switch req.ChainCodeType {
case ChaincodeSpec_GOLANG:
packageBytes, err = packGolangCC(req.Namespace, req.SrcPath, req.Libraries)
if err != nil {
return nil, err
}
default:
return nil, ErrUnsupportedChaincodeType
}
now := time.Now()
depSpec, err := proto.Marshal(&peer.ChaincodeDeploymentSpec{
ChaincodeSpec: &peer.ChaincodeSpec{
ChaincodeId: &peer.ChaincodeID{Name: req.ChainCodeName, Path: req.Namespace, Version: req.ChainCodeVersion},
Type: peer.ChaincodeSpec_Type(req.ChainCodeType),
},
CodePackage: packageBytes,
EffectiveDate: ×tamp.Timestamp{Seconds: int64(now.Second()), Nanos: int32(now.Nanosecond())},
})
if err != nil {
return nil, err
}
spec, err := chainCodeInvocationSpec(ChainCode{Type: req.ChainCodeType,
Name: LSCC,
Args: []string{"install"},
ArgBytes: depSpec,
})
creator, err := marshalProtoIdentity(identity)
if err != nil {
return nil, err
}
txId, err := newTransactionId(creator)
if err != nil {
return nil, err
}
ccHdrExt := &peer.ChaincodeHeaderExtension{ChaincodeId: &peer.ChaincodeID{Name: LSCC}}
channelHeaderBytes, err := channelHeader(common.HeaderType_ENDORSER_TRANSACTION, txId, req.ChannelId, 0, ccHdrExt)
if err != nil {
return nil, err
}
ccPropPayloadBytes, err := proto.Marshal(&peer.ChaincodeProposalPayload{
Input: spec,
TransientMap: nil,
})
if err != nil {
return nil, err
}
sigHeader, err := signatureHeader(creator, txId)
if err != nil {
return nil, err
}
header := header(sigHeader, channelHeaderBytes)
hdrBytes, err := proto.Marshal(header)
if err != nil {
return nil, err
}
proposal, err := proposal(hdrBytes, ccPropPayloadBytes)
if err != nil {
return nil, err
}
return &transactionProposal{proposal: proposal, transactionId: txId.TransactionId}, nil
}
// createInstantiateProposal creates instantiate proposal transaction for already installed chaincode.
// transaction is not send from this func
func createInstantiateProposal(identity Identity, req *ChainCode, operation string, collectionConfig []byte) (*transactionProposal, error) {
if operation != "deploy" && operation != "upgrade" {
return nil, fmt.Errorf("install proposall accept only 'deploy' and 'upgrade' operations")
}
depSpec, err := proto.Marshal(&peer.ChaincodeDeploymentSpec{
ChaincodeSpec: &peer.ChaincodeSpec{
ChaincodeId: &peer.ChaincodeID{Name: req.Name, Version: req.Version},
Type: peer.ChaincodeSpec_Type(req.Type),
Input: &peer.ChaincodeInput{Args: req.toChainCodeArgs()},
},
})
if err != nil {
return nil, err
}
policy, err := defaultPolicy(identity.MspId)
if err != nil {
return nil, err
}
marshPolicy, err := proto.Marshal(policy)
if err != nil {
return nil, err
}
args := [][]byte{
[]byte(operation),
[]byte(req.ChannelId),
depSpec,
marshPolicy,
[]byte("escc"),
[]byte("vscc"),
}
if len(collectionConfig) > 0 {
args = append(args, collectionConfig)
}
spec, err := chainCodeInvocationSpec(ChainCode{
Type: req.Type,
Name: LSCC,
rawArgs: args,
})
creator, err := marshalProtoIdentity(identity)
if err != nil {
return nil, err
}
txId, err := newTransactionId(creator)
if err != nil {
return nil, err
}
headerExtension := &peer.ChaincodeHeaderExtension{ChaincodeId: &peer.ChaincodeID{Name: LSCC}}
channelHeaderBytes, err := channelHeader(common.HeaderType_ENDORSER_TRANSACTION, txId, req.ChannelId, 0, headerExtension)
if err != nil {
return nil, err
}
payloadBytes, err := proto.Marshal(&peer.ChaincodeProposalPayload{Input: spec, TransientMap: req.TransientMap})
if err != nil {
return nil, err
}
signatureHeader, err := signatureHeader(creator, txId)
if err != nil {
return nil, err
}
headerBytes, err := proto.Marshal(header(signatureHeader, channelHeaderBytes))
if err != nil {
return nil, err
}
proposal, err := proposal(headerBytes, payloadBytes)
if err != nil {
return nil, err
}
return &transactionProposal{proposal: proposal, transactionId: txId.TransactionId}, nil
}
// packGolangCC read provided src expecting Golang source code, repackage it in provided namespace, and compress it
func packGolangCC(namespace, source string, libs []ChaincodeLibrary) ([]byte, error) {
twBuf := new(bytes.Buffer)
tw := tar.NewWriter(twBuf)
var gzBuf bytes.Buffer
zw := gzip.NewWriter(&gzBuf)
concatLibs := append(libs, ChaincodeLibrary{SrcPath: source, Namespace: namespace})
for _, s := range concatLibs {
_, err := os.Stat(s.SrcPath)
if err != nil {
return nil, err
}
baseDir := path.Join("/src", s.Namespace)
err = filepath.Walk(s.SrcPath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
header, err := tar.FileInfoHeader(info, "")
if err != nil {
return err
}
header.Mode = 0100000
if baseDir != "" {
header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, s.SrcPath))
}
if header.Name == baseDir {
return nil
}
if err := tw.WriteHeader(header); err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(tw, file)
return err
})
if err != nil {
tw.Close()
return nil, err
}
}
_, err := zw.Write(twBuf.Bytes())
if err != nil {
return nil, err
}
tw.Close()
zw.Close()
return gzBuf.Bytes(), nil
}