@@ -19,78 +19,42 @@ package core
1919import (
2020 "errors"
2121 "fmt"
22- "strings"
2322
2423 "github.com/op/go-logging"
2524 "github.com/spf13/viper"
2625 "golang.org/x/net/context"
2726
28- "encoding/asn1"
29- "encoding/base64"
30- "sync"
31-
3227 "github.com/hyperledger/fabric/core/chaincode"
3328 "github.com/hyperledger/fabric/core/chaincode/platforms"
3429 "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"
3830 pb "github.com/hyperledger/fabric/protos/peer"
3931)
4032
4133var devopsLogger = logging .MustGetLogger ("devops" )
4234
4335// NewDevopsServer creates and returns a new Devops server instance.
44- func NewDevopsServer (coord peer. MessageHandlerCoordinator ) * Devops {
36+ func NewDevopsServer () * Devops {
4537 d := new (Devops )
46- d .coord = coord
47- d .isSecurityEnabled = viper .GetBool ("security.enabled" )
48- d .bindingMap = & bindingMap {m : make (map [string ]crypto.TransactionHandler )}
4938 return d
5039}
5140
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-
5841// Devops implementation of Devops services
5942type 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
7443}
7544
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" )
8450 }
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 ) {
9051
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+ }
9256
93- // TODO: Handle timeout and expiration
57+ return platform . ValidateSpec ( spec )
9458}
9559
9660// Build builds the supplied chaincode image
@@ -99,7 +63,7 @@ func (*Devops) Build(context context.Context, spec *pb.ChaincodeSpec) (*pb.Chain
9963 var codePackageBytes []byte
10064 if mode != chaincode .DevModeUserRunsChaincode {
10165 devopsLogger .Debugf ("Received build request for chaincode spec: %v" , spec )
102- if err := CheckSpec (spec ); err != nil {
66+ if err := checkSpec (spec ); err != nil {
10367 return nil , err
10468 }
10569
@@ -117,201 +81,3 @@ func (*Devops) Build(context context.Context, spec *pb.ChaincodeSpec) (*pb.Chain
11781 chaincodeDeploymentSpec := & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec , CodePackage : codePackageBytes }
11882 return chaincodeDeploymentSpec , nil
11983}
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