forked from hyperledger/fabric-sdk-go
/
lscc.go
97 lines (81 loc) · 3.02 KB
/
lscc.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package resmgmt
import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/txn"
"github.com/pkg/errors"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
protos_utils "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/utils"
)
const (
lscc = "lscc"
lsccDeploy = "deploy"
lsccUpgrade = "upgrade"
escc = "escc"
vscc = "vscc"
)
// chaincodeProposalType reflects transitions in the chaincode lifecycle
type chaincodeProposalType int
// Define chaincode proposal types
const (
InstantiateChaincode chaincodeProposalType = iota
UpgradeChaincode
)
// chaincodeDeployRequest holds parameters for creating an instantiate or upgrade chaincode proposal.
type chaincodeDeployRequest struct {
Name string
Path string
Version string
Args [][]byte
Policy *common.SignaturePolicyEnvelope
CollConfig []*common.CollectionConfig
}
// createChaincodeDeployProposal creates an instantiate or upgrade chaincode proposal.
func createChaincodeDeployProposal(txh fab.TransactionHeader, deploy chaincodeProposalType, channelID string, chaincode chaincodeDeployRequest) (*fab.TransactionProposal, error) {
// Generate arguments for deploy (channel, marshaled CCDS, marshaled chaincode policy, marshaled collection policy)
args := [][]byte{}
args = append(args, []byte(channelID))
ccds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{
Type: pb.ChaincodeSpec_GOLANG, ChaincodeId: &pb.ChaincodeID{Name: chaincode.Name, Path: chaincode.Path, Version: chaincode.Version},
Input: &pb.ChaincodeInput{Args: chaincode.Args}}}
ccdsBytes, err := protos_utils.Marshal(ccds)
if err != nil {
return nil, errors.WithMessage(err, "marshal of chaincode deployment spec failed")
}
args = append(args, ccdsBytes)
chaincodePolicyBytes, err := protos_utils.Marshal(chaincode.Policy)
if err != nil {
return nil, errors.WithMessage(err, "marshal of chaincode policy failed")
}
args = append(args, chaincodePolicyBytes)
args = append(args, []byte(escc))
args = append(args, []byte(vscc))
if chaincode.CollConfig != nil {
collConfigBytes, err := proto.Marshal(&common.CollectionConfigPackage{Config: chaincode.CollConfig})
if err != nil {
return nil, errors.WithMessage(err, "marshal of collection policy failed")
}
args = append(args, collConfigBytes)
}
// Fcn is deploy or upgrade
fcn := ""
switch deploy {
case InstantiateChaincode:
fcn = lsccDeploy
case UpgradeChaincode:
fcn = lsccUpgrade
default:
return nil, errors.New("chaincode deployment type unknown")
}
cir := fab.ChaincodeInvokeRequest{
ChaincodeID: lscc,
Fcn: fcn,
Args: args,
}
return txn.CreateChaincodeInvokeProposal(txh, cir)
}