/
collection_val.go
86 lines (74 loc) · 2.39 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
82
83
84
85
86
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package lockbasedtxmgr
import (
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/core/ledger"
)
// 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 {
ledgerID string
ccInfoProvider ledger.DeployedChaincodeInfoProvider
queryExecutor *lockBasedQueryExecutor
cache collConfigCache
noop bool
}
func newCollNameValidator(ledgerID string, ccInfoProvider ledger.DeployedChaincodeInfoProvider, qe *lockBasedQueryExecutor, noop bool) *collNameValidator {
return &collNameValidator{ledgerID, ccInfoProvider, qe, make(collConfigCache), noop}
}
func (v *collNameValidator) validateCollName(ns, coll string) error {
if v.noop {
return nil
}
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) (*peer.CollectionConfigPackage, error) {
logger.Debugf("retrieveCollConfigFromStateDB() begin - ns=[%s]", ns)
confPkg, err := v.ccInfoProvider.AllCollectionsConfigPkg(v.ledgerID, ns, v.queryExecutor)
if err != nil {
return nil, err
}
if confPkg == nil {
return nil, &ledger.CollConfigNotDefinedError{Ns: ns}
}
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 *peer.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}]
}