Skip to content

Commit 151a9a6

Browse files
committed
Converge deployment spec validation
We currently had two competing deployment-spec import utilities. One only performed bare minimum validation while the other utilized the platform driver for full validation. Let's consolidate them. Change-Id: I7d43139c3e5928b2af229cb51ece9e49d0159f13 Signed-off-by: Gregory Haskins <[email protected]>
1 parent 709d87b commit 151a9a6

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

core/scc/lccc/lccc.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,6 @@ func (lccc *LifeCycleSysCC) getChaincode(stub shim.ChaincodeStubInterface, ccnam
251251
return nil, nil, NotFoundErr(ccname)
252252
}
253253

254-
//getChaincodeDeploymentSpec returns a ChaincodeDeploymentSpec given args
255-
func (lccc *LifeCycleSysCC) getChaincodeDeploymentSpec(code []byte) (*pb.ChaincodeDeploymentSpec, error) {
256-
cds := &pb.ChaincodeDeploymentSpec{}
257-
258-
err := proto.Unmarshal(code, cds)
259-
if err != nil {
260-
return nil, InvalidDeploymentSpecErr(err.Error())
261-
}
262-
263-
return cds, nil
264-
}
265-
266254
// getChaincodes returns all chaincodes instantiated on this LCCC's channel
267255
func (lccc *LifeCycleSysCC) getChaincodes(stub shim.ChaincodeStubInterface) pb.Response {
268256
// get all rows from LCCC
@@ -343,7 +331,7 @@ func (lccc *LifeCycleSysCC) isValidChaincodeName(chaincodename string) bool {
343331

344332
//this implements "install" Invoke transaction
345333
func (lccc *LifeCycleSysCC) executeInstall(stub shim.ChaincodeStubInterface, depSpec []byte) error {
346-
cds, err := lccc.getChaincodeDeploymentSpec(depSpec)
334+
cds, err := utils.GetChaincodeDeploymentSpec(depSpec)
347335

348336
if err != nil {
349337
return err
@@ -366,7 +354,7 @@ func (lccc *LifeCycleSysCC) executeInstall(stub shim.ChaincodeStubInterface, dep
366354

367355
//this implements "deploy" Invoke transaction
368356
func (lccc *LifeCycleSysCC) executeDeploy(stub shim.ChaincodeStubInterface, chainname string, depSpec []byte, policy []byte, escc []byte, vscc []byte) error {
369-
cds, err := lccc.getChaincodeDeploymentSpec(depSpec)
357+
cds, err := utils.GetChaincodeDeploymentSpec(depSpec)
370358

371359
if err != nil {
372360
return err
@@ -420,7 +408,7 @@ func (lccc *LifeCycleSysCC) getUpgradeVersion(cd *ccprovider.ChaincodeData, cds
420408

421409
//this implements "upgrade" Invoke transaction
422410
func (lccc *LifeCycleSysCC) executeUpgrade(stub shim.ChaincodeStubInterface, chainName string, depSpec []byte, policy []byte, escc []byte, vscc []byte) ([]byte, error) {
423-
cds, err := lccc.getChaincodeDeploymentSpec(depSpec)
411+
cds, err := utils.GetChaincodeDeploymentSpec(depSpec)
424412
if err != nil {
425413
return nil, err
426414
}

core/scc/lccc/lccc_test.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ import (
2626
"github.com/hyperledger/fabric/core/common/ccprovider"
2727
"github.com/hyperledger/fabric/core/common/sysccprovider"
2828
//"github.com/hyperledger/fabric/core/container"
29+
"archive/tar"
30+
"bytes"
31+
"compress/gzip"
32+
33+
"github.com/hyperledger/fabric/core/container/util"
2934
pb "github.com/hyperledger/fabric/protos/peer"
3035
)
3136

@@ -56,9 +61,19 @@ func register(stub *shim.MockStub, ccname string) error {
5661
func constructDeploymentSpec(name string, path string, version string, initArgs [][]byte, createFS bool) (*pb.ChaincodeDeploymentSpec, error) {
5762
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: name, Path: path, Version: version}, Input: &pb.ChaincodeInput{Args: initArgs}}
5863

59-
codePackageBytes := []byte(name + path + version)
64+
codePackageBytes := bytes.NewBuffer(nil)
65+
gz := gzip.NewWriter(codePackageBytes)
66+
tw := tar.NewWriter(gz)
67+
68+
err := util.WriteBytesToPackage("src/garbage.go", []byte(name+path+version), tw)
69+
if err != nil {
70+
return nil, err
71+
}
72+
73+
tw.Close()
74+
gz.Close()
6075

61-
chaincodeDeploymentSpec := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec, CodePackage: codePackageBytes}
76+
chaincodeDeploymentSpec := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec, CodePackage: codePackageBytes.Bytes()}
6277

6378
if createFS {
6479
err := ccprovider.PutChaincodeIntoFS(chaincodeDeploymentSpec)
@@ -182,9 +197,8 @@ func TestInvalidCodeDeploy(t *testing.T) {
182197
baddepspec := []byte("bad deploy spec")
183198
args := [][]byte{[]byte(DEPLOY), []byte("test"), baddepspec}
184199
res := stub.MockInvoke("1", args)
185-
expectErr := InvalidDeploymentSpecErr("unexpected EOF")
186-
if string(res.Message) != expectErr.Error() {
187-
t.Logf("get result: %+v", res)
200+
if res.Status == shim.OK {
201+
t.Logf("Expected failure")
188202
t.FailNow()
189203
}
190204
}

0 commit comments

Comments
 (0)