Skip to content

Commit 3aa6009

Browse files
wlahtimastersingh24
authored andcommitted
FAB-11211 Utilize old and new cc pkg in cclifecycle
This CR enables the cclifecycle to utilize both the old ChaincodeDeploymentSpec package as well as the new ChaincodeInstallPackage by adding a function to list installed chaincodes to the persistence package provider. It first searches both the persistence store as well as the ccprovider. FAB-11211 #done Change-Id: I7869f0fa10f884daec1adb5616e2cd11edfb07d9 Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
1 parent a9e8cd7 commit 3aa6009

File tree

11 files changed

+596
-212
lines changed

11 files changed

+596
-212
lines changed

core/cclifecycle/util.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,72 +7,20 @@ SPDX-License-Identifier: Apache-2.0
77
package cc
88

99
import (
10-
"os"
11-
"strings"
12-
1310
"github.com/golang/protobuf/proto"
1411
"github.com/hyperledger/fabric/common/chaincode"
1512
"github.com/hyperledger/fabric/core/common/ccprovider"
1613
"github.com/hyperledger/fabric/core/common/privdata"
1714
"github.com/pkg/errors"
1815
)
1916

20-
// DirEnumerator enumerates directories
21-
type DirEnumerator func(string) ([]os.FileInfo, error)
22-
23-
// ChaincodeExtractor extracts chaincode from a given path
24-
type ChaincodeExtractor func(ccname string, ccversion string, path string) (ccprovider.CCPackage, error)
25-
2617
var (
2718
// AcceptAll returns a predicate that accepts all Metadata
2819
AcceptAll ChaincodePredicate = func(cc chaincode.Metadata) bool {
2920
return true
3021
}
3122
)
3223

33-
// InstalledCCs retrieves the installed chaincodes
34-
func InstalledCCs(dir string, ls DirEnumerator, ccFromPath ChaincodeExtractor) ([]chaincode.InstalledChaincode, error) {
35-
var chaincodes []chaincode.InstalledChaincode
36-
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
37-
return nil, nil
38-
}
39-
files, err := ls(dir)
40-
if err != nil {
41-
return nil, errors.Wrapf(err, "failed reading directory %s", dir)
42-
}
43-
44-
for _, f := range files {
45-
// Skip directories, we're only interested in normal files
46-
if f.IsDir() {
47-
continue
48-
}
49-
// A chaincode file name is of the type "name.version"
50-
// We're only interested in the name.
51-
// Skip files that don't adhere to the file naming convention of "A.B"
52-
i := strings.Index(f.Name(), ".")
53-
if i == -1 {
54-
Logger.Info("Skipping", f.Name(), "because of missing separator '.'")
55-
continue
56-
}
57-
ccName := f.Name()[:i] // Everything before the separator
58-
ccVersion := f.Name()[i+1:] // Everything after the separator
59-
60-
ccPackage, err := ccFromPath(ccName, ccVersion, dir)
61-
if err != nil {
62-
Logger.Warning("Failed obtaining chaincode information about", ccName, ccVersion, ":", err)
63-
return nil, errors.Wrapf(err, "failed obtaining information about %s, version %s", ccName, ccVersion)
64-
}
65-
66-
chaincodes = append(chaincodes, chaincode.InstalledChaincode{
67-
Name: ccName,
68-
Version: ccVersion,
69-
Id: ccPackage.GetId(),
70-
})
71-
}
72-
Logger.Debug("Returning", chaincodes)
73-
return chaincodes, nil
74-
}
75-
7624
// ChaincodePredicate accepts or rejects chaincode based on its metadata
7725
type ChaincodePredicate func(cc chaincode.Metadata) bool
7826

core/cclifecycle/util_test.go

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -8,147 +8,19 @@ package cc_test
88

99
import (
1010
"bytes"
11-
"fmt"
12-
"io/ioutil"
13-
"math/rand"
14-
"os"
15-
"path"
1611
"testing"
17-
"time"
1812

1913
"github.com/golang/protobuf/proto"
2014
"github.com/hyperledger/fabric/common/chaincode"
2115
"github.com/hyperledger/fabric/core/cclifecycle"
2216
"github.com/hyperledger/fabric/core/cclifecycle/mocks"
2317
"github.com/hyperledger/fabric/core/common/ccprovider"
2418
"github.com/hyperledger/fabric/protos/common"
25-
"github.com/hyperledger/fabric/protos/peer"
2619
"github.com/pkg/errors"
2720
"github.com/stretchr/testify/assert"
2821
"github.com/stretchr/testify/mock"
2922
)
3023

31-
func TestInstalledCCs(t *testing.T) {
32-
tmpDir := setupDirectoryStructure(t)
33-
defer func() {
34-
os.RemoveAll(tmpDir)
35-
}()
36-
testCases := []struct {
37-
name string
38-
directory string
39-
expected []chaincode.InstalledChaincode
40-
errorContains string
41-
ls cc.DirEnumerator
42-
extractCCFromPath cc.ChaincodeExtractor
43-
}{
44-
{
45-
name: "None empty directory",
46-
ls: ioutil.ReadDir,
47-
extractCCFromPath: ccprovider.LoadPackage,
48-
expected: []chaincode.InstalledChaincode{
49-
{
50-
Name: "example02",
51-
Version: "1.0",
52-
Id: []byte{45, 186, 93, 188, 51, 158, 115, 22, 174, 162, 104, 63, 175, 131, 156, 27, 123, 30, 226, 49, 61, 183, 146, 17, 37, 136, 17, 141, 240, 102, 170, 53},
53-
},
54-
{
55-
Name: "example04",
56-
Version: "1",
57-
Id: []byte{45, 186, 93, 188, 51, 158, 115, 22, 174, 162, 104, 63, 175, 131, 156, 27, 123, 30, 226, 49, 61, 183, 146, 17, 37, 136, 17, 141, 240, 102, 170, 53},
58-
},
59-
},
60-
directory: "nonempty",
61-
},
62-
{
63-
name: "None present directory",
64-
ls: ioutil.ReadDir,
65-
extractCCFromPath: ccprovider.LoadPackage,
66-
expected: nil,
67-
directory: "notexistent",
68-
},
69-
{
70-
name: "Empty directory",
71-
ls: ioutil.ReadDir,
72-
extractCCFromPath: ccprovider.LoadPackage,
73-
expected: nil,
74-
directory: "empty",
75-
},
76-
{
77-
name: "No permission to open directory",
78-
ls: func(_ string) ([]os.FileInfo, error) {
79-
return nil, errors.New("permission denied")
80-
},
81-
extractCCFromPath: ccprovider.LoadPackage,
82-
expected: nil,
83-
directory: "nopermission",
84-
errorContains: "permission denied",
85-
},
86-
{
87-
name: "No permission on chaincode files",
88-
ls: ioutil.ReadDir,
89-
extractCCFromPath: func(_ string, _ string, _ string) (ccprovider.CCPackage, error) {
90-
return nil, errors.New("permission denied")
91-
},
92-
expected: nil,
93-
directory: "nopermissionforfiles",
94-
errorContains: "permission denied",
95-
},
96-
}
97-
_ = testCases
98-
99-
for _, test := range testCases {
100-
test := test
101-
t.Run(test.name, func(t *testing.T) {
102-
res, err := cc.InstalledCCs(path.Join(tmpDir, test.directory), test.ls, test.extractCCFromPath)
103-
assert.Equal(t, test.expected, res)
104-
if test.errorContains == "" {
105-
assert.NoError(t, err)
106-
} else {
107-
assert.Contains(t, err.Error(), test.errorContains)
108-
}
109-
})
110-
}
111-
112-
}
113-
114-
func setupDirectoryStructure(t *testing.T) string {
115-
files := []string{
116-
"example02.1.0", // Version contains the delimiter '.' is a valid case
117-
"example03", // No version specified
118-
"example04.1", // Version doesn't contain the '.' delimiter
119-
}
120-
rand.Seed(time.Now().UnixNano())
121-
tmp := path.Join(os.TempDir(), fmt.Sprintf("%d", rand.Int()))
122-
assert.NoError(t, os.Mkdir(tmp, 0755))
123-
dir := path.Join(tmp, "empty")
124-
assert.NoError(t, os.Mkdir(dir, 0755))
125-
dir = path.Join(tmp, "nonempty")
126-
assert.NoError(t, os.Mkdir(dir, 0755))
127-
dir = path.Join(tmp, "nopermission")
128-
assert.NoError(t, os.Mkdir(dir, 0755))
129-
dir = path.Join(tmp, "nopermissionforfiles")
130-
assert.NoError(t, os.Mkdir(dir, 0755))
131-
noPermissionFile := path.Join(tmp, "nopermissionforfiles", "nopermission.1")
132-
_, err := os.Create(noPermissionFile)
133-
assert.NoError(t, err)
134-
dir = path.Join(tmp, "nonempty")
135-
assert.NoError(t, os.Mkdir(path.Join(tmp, "nonempty", "directory"), 0755))
136-
for _, f := range files {
137-
file, err := os.Create(path.Join(dir, f))
138-
assert.NoError(t, err)
139-
cds := &peer.ChaincodeDeploymentSpec{
140-
ChaincodeSpec: &peer.ChaincodeSpec{
141-
ChaincodeId: &peer.ChaincodeID{},
142-
},
143-
}
144-
b, _ := proto.Marshal(cds)
145-
file.Write(b)
146-
file.Close()
147-
}
148-
149-
return tmp
150-
}
151-
15224
func TestChaincodeInspection(t *testing.T) {
15325
acceptAll := func(cc chaincode.Metadata) bool {
15426
return true

core/chaincode/persistence/mock/legacy_package_provider.go

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)