@@ -19,78 +19,42 @@ package core
19
19
import (
20
20
"errors"
21
21
"fmt"
22
- "strings"
23
22
24
23
"github.com/op/go-logging"
25
24
"github.com/spf13/viper"
26
25
"golang.org/x/net/context"
27
26
28
- "encoding/asn1"
29
- "encoding/base64"
30
- "sync"
31
-
32
27
"github.com/hyperledger/fabric/core/chaincode"
33
28
"github.com/hyperledger/fabric/core/chaincode/platforms"
34
29
"github.com/hyperledger/fabric/core/container"
35
- crypto "github.com/hyperledger/fabric/core/crypto"
36
- "github.com/hyperledger/fabric/core/peer"
37
- "github.com/hyperledger/fabric/core/util"
38
30
pb "github.com/hyperledger/fabric/protos/peer"
39
31
)
40
32
41
33
var devopsLogger = logging .MustGetLogger ("devops" )
42
34
43
35
// NewDevopsServer creates and returns a new Devops server instance.
44
- func NewDevopsServer (coord peer. MessageHandlerCoordinator ) * Devops {
36
+ func NewDevopsServer () * Devops {
45
37
d := new (Devops )
46
- d .coord = coord
47
- d .isSecurityEnabled = viper .GetBool ("security.enabled" )
48
- d .bindingMap = & bindingMap {m : make (map [string ]crypto.TransactionHandler )}
49
38
return d
50
39
}
51
40
52
- // bindingMap Used to store map of binding to TransactionHandler
53
- type bindingMap struct {
54
- sync.RWMutex
55
- m map [string ]crypto.TransactionHandler
56
- }
57
-
58
41
// Devops implementation of Devops services
59
42
type Devops struct {
60
- coord peer.MessageHandlerCoordinator
61
- isSecurityEnabled bool
62
- bindingMap * bindingMap
63
- }
64
-
65
- func (b * bindingMap ) getKeyFromBinding (binding []byte ) string {
66
- return base64 .StdEncoding .EncodeToString (binding )
67
- }
68
-
69
- func (b * bindingMap ) addBinding (bindingToAdd []byte , txHandler crypto.TransactionHandler ) {
70
- b .Lock ()
71
- defer b .Unlock ()
72
- key := b .getKeyFromBinding (bindingToAdd )
73
- b .m [key ] = txHandler
74
43
}
75
44
76
- func (b * bindingMap ) getTxHandlerForBinding (binding []byte ) (crypto.TransactionHandler , error ) {
77
- b .Lock ()
78
- defer b .Unlock ()
79
- key := b .getKeyFromBinding (binding )
80
- txHandler , ok := b .m [key ]
81
- if ok != true {
82
- // TXhandler not found by key, return error
83
- return nil , fmt .Errorf ("Transaction handler not found for binding key = %s" , key )
45
+ // checkSpec to see if chaincode resides within current package capture for language.
46
+ func checkSpec (spec * pb.ChaincodeSpec ) error {
47
+ // Don't allow nil value
48
+ if spec == nil {
49
+ return errors .New ("Expected chaincode specification, nil received" )
84
50
}
85
- return txHandler , nil
86
- }
87
-
88
- // Login establishes the security context with the Devops service
89
- func (d * Devops ) Login (ctx context.Context , secret * pb.Secret ) (* pb.Response , error ) {
90
51
91
- return & pb.Response {Status : pb .Response_FAILURE , Msg : []byte ("login command has been removed" )}, nil
52
+ platform , err := platforms .Find (spec .Type )
53
+ if err != nil {
54
+ return fmt .Errorf ("Failed to determine platform type: %s" , err )
55
+ }
92
56
93
- // TODO: Handle timeout and expiration
57
+ return platform . ValidateSpec ( spec )
94
58
}
95
59
96
60
// Build builds the supplied chaincode image
@@ -99,7 +63,7 @@ func (*Devops) Build(context context.Context, spec *pb.ChaincodeSpec) (*pb.Chain
99
63
var codePackageBytes []byte
100
64
if mode != chaincode .DevModeUserRunsChaincode {
101
65
devopsLogger .Debugf ("Received build request for chaincode spec: %v" , spec )
102
- if err := CheckSpec (spec ); err != nil {
66
+ if err := checkSpec (spec ); err != nil {
103
67
return nil , err
104
68
}
105
69
@@ -117,201 +81,3 @@ func (*Devops) Build(context context.Context, spec *pb.ChaincodeSpec) (*pb.Chain
117
81
chaincodeDeploymentSpec := & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec , CodePackage : codePackageBytes }
118
82
return chaincodeDeploymentSpec , nil
119
83
}
120
-
121
- // GetChaincodeBytes get chaincode deployment spec given the chaincode spec
122
- func GetChaincodeBytes (context context.Context , spec * pb.ChaincodeSpec ) (* pb.ChaincodeDeploymentSpec , error ) {
123
- mode := viper .GetString ("chaincode.mode" )
124
- var codePackageBytes []byte
125
- if mode != chaincode .DevModeUserRunsChaincode {
126
- devopsLogger .Debugf ("Received build request for chaincode spec: %v" , spec )
127
- var err error
128
- if err = CheckSpec (spec ); err != nil {
129
- return nil , err
130
- }
131
-
132
- codePackageBytes , err = container .GetChaincodePackageBytes (spec )
133
- if err != nil {
134
- err = fmt .Errorf ("Error getting chaincode package bytes: %s" , err )
135
- devopsLogger .Error (fmt .Sprintf ("%s" , err ))
136
- return nil , err
137
- }
138
- }
139
- chaincodeDeploymentSpec := & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec , CodePackage : codePackageBytes }
140
- return chaincodeDeploymentSpec , nil
141
- }
142
-
143
- // Deploy deploys the supplied chaincode image to the validators through a transaction
144
- func (d * Devops ) Deploy (ctx context.Context , spec * pb.ChaincodeSpec ) (* pb.ChaincodeDeploymentSpec , error ) {
145
- // get the deployment spec
146
- chaincodeDeploymentSpec , err := GetChaincodeBytes (ctx , spec )
147
-
148
- if err != nil {
149
- devopsLogger .Error (fmt .Sprintf ("Error deploying chaincode spec: %v\n \n error: %s" , spec , err ))
150
- return nil , err
151
- }
152
-
153
- // Now create the Transactions message and send to Peer.
154
-
155
- transID := chaincodeDeploymentSpec .ChaincodeSpec .ChaincodeID .Name
156
-
157
- var tx * pb.Transaction
158
-
159
- if devopsLogger .IsEnabledFor (logging .DEBUG ) {
160
- devopsLogger .Debugf ("Creating deployment transaction (%s)" , transID )
161
- }
162
- tx , err = pb .NewChaincodeDeployTransaction (chaincodeDeploymentSpec , transID )
163
- if err != nil {
164
- return nil , fmt .Errorf ("Error deploying chaincode: %s " , err )
165
- }
166
-
167
- if devopsLogger .IsEnabledFor (logging .DEBUG ) {
168
- devopsLogger .Debugf ("Sending deploy transaction (%s) to validator" , tx .Txid )
169
- }
170
- resp := d .coord .ExecuteTransaction (tx )
171
- if resp .Status == pb .Response_FAILURE {
172
- err = errors .New (string (resp .Msg ))
173
- }
174
-
175
- return chaincodeDeploymentSpec , err
176
- }
177
-
178
- func (d * Devops ) invokeOrQuery (ctx context.Context , chaincodeInvocationSpec * pb.ChaincodeInvocationSpec , attributes []string , invoke bool ) (* pb.Response , error ) {
179
-
180
- if chaincodeInvocationSpec .ChaincodeSpec .ChaincodeID .Name == "" {
181
- return nil , errors .New ("name not given for invoke/query" )
182
- }
183
-
184
- // Now create the Transactions message and send to Peer.
185
- var customIDgenAlg = strings .ToLower (chaincodeInvocationSpec .IdGenerationAlg )
186
- var id string
187
- var generr error
188
- if invoke {
189
- if customIDgenAlg != "" {
190
- ctorbytes , merr := asn1 .Marshal (* chaincodeInvocationSpec .ChaincodeSpec .CtorMsg )
191
- if merr != nil {
192
- return nil , fmt .Errorf ("Error marshalling constructor: %s" , merr )
193
- }
194
- id , generr = util .GenerateIDWithAlg (customIDgenAlg , ctorbytes )
195
- if generr != nil {
196
- return nil , generr
197
- }
198
- } else {
199
- id = util .GenerateUUID ()
200
- }
201
- } else {
202
- id = util .GenerateUUID ()
203
- }
204
- devopsLogger .Infof ("Transaction ID: %v" , id )
205
- var transaction * pb.Transaction
206
- var err error
207
- var sec crypto.Client
208
-
209
- transaction , err = d .createExecTx (chaincodeInvocationSpec , attributes , id , invoke , sec )
210
- if err != nil {
211
- return nil , err
212
- }
213
- devopsLogger .Debugf ("Sending invocation transaction (%s) to validator" , transaction .Txid )
214
- resp := d .coord .ExecuteTransaction (transaction )
215
- if resp .Status == pb .Response_FAILURE {
216
- err = errors .New (string (resp .Msg ))
217
- } else {
218
- if ! invoke && nil != sec && viper .GetBool ("security.privacy" ) {
219
- if resp .Msg , err = sec .DecryptQueryResult (transaction , resp .Msg ); nil != err {
220
- devopsLogger .Errorf ("Failed decrypting query transaction result %s" , string (resp .Msg [:]))
221
- //resp = &pb.Response{Status: pb.Response_FAILURE, Msg: []byte(err.Error())}
222
- }
223
- }
224
- }
225
- return resp , err
226
- }
227
-
228
- func (d * Devops ) createExecTx (spec * pb.ChaincodeInvocationSpec , attributes []string , uuid string , invokeTx bool , sec crypto.Client ) (* pb.Transaction , error ) {
229
- var tx * pb.Transaction
230
- var err error
231
-
232
- //TODO What should we do with the attributes
233
- if nil != sec {
234
- if devopsLogger .IsEnabledFor (logging .DEBUG ) {
235
- devopsLogger .Debugf ("Creating secure invocation transaction %s" , uuid )
236
- }
237
- if invokeTx {
238
- tx , err = sec .NewChaincodeExecute (spec , uuid , attributes ... )
239
- } else {
240
- tx , err = sec .NewChaincodeQuery (spec , uuid , attributes ... )
241
- }
242
- if nil != err {
243
- return nil , err
244
- }
245
- } else {
246
- if devopsLogger .IsEnabledFor (logging .DEBUG ) {
247
- devopsLogger .Debugf ("Creating invocation transaction (%s)" , uuid )
248
- }
249
- var t pb.Transaction_Type
250
- if invokeTx {
251
- t = pb .Transaction_CHAINCODE_INVOKE
252
- } else {
253
- t = pb .Transaction_CHAINCODE_QUERY
254
- }
255
- tx , err = pb .NewChaincodeExecute (spec , uuid , t )
256
- if nil != err {
257
- return nil , err
258
- }
259
- }
260
- return tx , nil
261
- }
262
-
263
- // Invoke performs the supplied invocation on the specified chaincode through a transaction
264
- func (d * Devops ) Invoke (ctx context.Context , chaincodeInvocationSpec * pb.ChaincodeInvocationSpec ) (* pb.Response , error ) {
265
- return d .invokeOrQuery (ctx , chaincodeInvocationSpec , chaincodeInvocationSpec .ChaincodeSpec .Attributes , true )
266
- }
267
-
268
- // Query performs the supplied query on the specified chaincode through a transaction
269
- func (d * Devops ) Query (ctx context.Context , chaincodeInvocationSpec * pb.ChaincodeInvocationSpec ) (* pb.Response , error ) {
270
- return d .invokeOrQuery (ctx , chaincodeInvocationSpec , chaincodeInvocationSpec .ChaincodeSpec .Attributes , false )
271
- }
272
-
273
- // CheckSpec to see if chaincode resides within current package capture for language.
274
- func CheckSpec (spec * pb.ChaincodeSpec ) error {
275
- // Don't allow nil value
276
- if spec == nil {
277
- return errors .New ("Expected chaincode specification, nil received" )
278
- }
279
-
280
- platform , err := platforms .Find (spec .Type )
281
- if err != nil {
282
- return fmt .Errorf ("Failed to determine platform type: %s" , err )
283
- }
284
-
285
- return platform .ValidateSpec (spec )
286
- }
287
-
288
- // EXP_GetApplicationTCert retrieves an application TCert for the supplied user
289
- func (d * Devops ) EXP_GetApplicationTCert (ctx context.Context , secret * pb.Secret ) (* pb.Response , error ) {
290
-
291
- devopsLogger .Warning ("GetApplicationTCert no longer supported" )
292
- return & pb.Response {Status : pb .Response_FAILURE , Msg : []byte ("no longer supported" )}, nil
293
- // TODO: Handle timeout and expiration
294
- }
295
-
296
- // EXP_PrepareForTx prepares a binding/TXHandler pair to be used in subsequent TX
297
- func (d * Devops ) EXP_PrepareForTx (ctx context.Context , secret * pb.Secret ) (* pb.Response , error ) {
298
-
299
- devopsLogger .Warning ("PrepareForTx no longer supported" )
300
- return & pb.Response {Status : pb .Response_FAILURE , Msg : []byte ("no longer supported" )}, nil
301
- // TODO: Handle timeout and expiration
302
- }
303
-
304
- // EXP_ProduceSigma produces a sigma as []byte and returns in response
305
- func (d * Devops ) EXP_ProduceSigma (ctx context.Context , sigmaInput * pb.SigmaInput ) (* pb.Response , error ) {
306
-
307
- devopsLogger .Warning ("ProduceSigma no longer supported" )
308
- return & pb.Response {Status : pb .Response_FAILURE , Msg : []byte ("no longer supported" )}, nil
309
-
310
- }
311
-
312
- // EXP_ExecuteWithBinding executes a transaction with a specific binding/TXHandler
313
- func (d * Devops ) EXP_ExecuteWithBinding (ctx context.Context , executeWithBinding * pb.ExecuteWithBinding ) (* pb.Response , error ) {
314
-
315
- devopsLogger .Warning ("ExecuteWithBinding no longer supported" )
316
- return & pb.Response {Status : pb .Response_FAILURE , Msg : []byte ("no longer supported" )}, nil
317
- }
0 commit comments