-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
collection_val.go
81 lines (70 loc) · 2.33 KB
/
collection_val.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package lockbasedtxmgr
import (
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/protos/common"
)
// collNameValidator validates the presence of a collection in a namespace
// This is expected to be instantiated in the context of a simulator/queryexecutor
type collNameValidator struct {
ccInfoProvider ledger.DeployedChaincodeInfoProvider
queryExecutor *lockBasedQueryExecutor
cache collConfigCache
}
func newCollNameValidator(ccInfoProvider ledger.DeployedChaincodeInfoProvider, qe *lockBasedQueryExecutor) *collNameValidator {
return &collNameValidator{ccInfoProvider, qe, make(collConfigCache)}
}
func (v *collNameValidator) validateCollName(ns, coll string) error {
if !v.cache.isPopulatedFor(ns) {
conf, err := v.retrieveCollConfigFromStateDB(ns)
if err != nil {
return err
}
v.cache.populate(ns, conf)
}
if !v.cache.containsCollName(ns, coll) {
return &ledger.InvalidCollNameError{
Ns: ns,
Coll: coll,
}
}
return nil
}
func (v *collNameValidator) retrieveCollConfigFromStateDB(ns string) (*common.CollectionConfigPackage, error) {
logger.Debugf("retrieveCollConfigFromStateDB() begin - ns=[%s]", ns)
ccInfo, err := v.ccInfoProvider.ChaincodeInfo(ns, v.queryExecutor)
if err != nil {
return nil, err
}
if ccInfo == nil || ccInfo.CollectionConfigPkg == nil {
return nil, &ledger.CollConfigNotDefinedError{Ns: ns}
}
confPkg := ccInfo.CollectionConfigPkg
logger.Debugf("retrieveCollConfigFromStateDB() successfully retrieved - ns=[%s], confPkg=[%s]", ns, confPkg)
return confPkg, nil
}
type collConfigCache map[collConfigkey]bool
type collConfigkey struct {
ns, coll string
}
func (c collConfigCache) populate(ns string, pkg *common.CollectionConfigPackage) {
// an entry with an empty collection name to indicate that the cache is populated for the namespace 'ns'
// see function 'isPopulatedFor'
c[collConfigkey{ns, ""}] = true
for _, config := range pkg.Config {
sConfig := config.GetStaticCollectionConfig()
if sConfig == nil {
continue
}
c[collConfigkey{ns, sConfig.Name}] = true
}
}
func (c collConfigCache) isPopulatedFor(ns string) bool {
return c[collConfigkey{ns, ""}]
}
func (c collConfigCache) containsCollName(ns, coll string) bool {
return c[collConfigkey{ns, coll}]
}