forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 1
/
btlpolicy.go
93 lines (80 loc) · 2.73 KB
/
btlpolicy.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package pvtdatapolicy
import (
"math"
"sync"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/core/common/privdata"
)
var defaultBTL uint64 = math.MaxUint64
// BTLPolicy BlockToLive policy for the pvt data
type BTLPolicy interface {
// GetBTL returns BlockToLive for a given namespace and collection
GetBTL(ns string, coll string) (uint64, error)
// GetExpiringBlock returns the block number by which the pvtdata for given namespace,collection, and committingBlock should expire
GetExpiringBlock(namesapce string, collection string, committingBlock uint64) (uint64, error)
}
// LSCCBasedBTLPolicy implements interface BTLPolicy.
// This implementation loads the BTL policy from lscc namespace which is populated
// with the collection configuration during chaincode initialization
type LSCCBasedBTLPolicy struct {
collInfoProvider collectionInfoProvider
cache map[btlkey]uint64
lock sync.Mutex
}
type btlkey struct {
ns string
coll string
}
// ConstructBTLPolicy constructs an instance of LSCCBasedBTLPolicy
func ConstructBTLPolicy(collInfoProvider collectionInfoProvider) BTLPolicy {
return &LSCCBasedBTLPolicy{
collInfoProvider: collInfoProvider,
cache: make(map[btlkey]uint64),
}
}
// GetBTL implements corresponding function in interface `BTLPolicyMgr`
func (p *LSCCBasedBTLPolicy) GetBTL(namesapce string, collection string) (uint64, error) {
var btl uint64
var ok bool
key := btlkey{namesapce, collection}
p.lock.Lock()
defer p.lock.Unlock()
btl, ok = p.cache[key]
if !ok {
collConfig, err := p.collInfoProvider.CollectionInfo(namesapce, collection)
if err != nil {
return 0, err
}
if collConfig == nil {
return 0, privdata.NoSuchCollectionError{Namespace: namesapce, Collection: collection}
}
btlConfigured := collConfig.BlockToLive
if btlConfigured > 0 {
btl = uint64(btlConfigured)
} else {
btl = defaultBTL
}
p.cache[key] = btl
}
return btl, nil
}
// GetExpiringBlock implements function from the interface `BTLPolicy`
func (p *LSCCBasedBTLPolicy) GetExpiringBlock(namesapce string, collection string, committingBlock uint64) (uint64, error) {
btl, err := p.GetBTL(namesapce, collection)
if err != nil {
return 0, err
}
expiryBlk := committingBlock + btl + uint64(1)
if expiryBlk <= committingBlock { // committingBlk + btl overflows uint64-max
expiryBlk = math.MaxUint64
}
return expiryBlk, nil
}
type collectionInfoProvider interface {
CollectionInfo(chaincodeName, collectionName string) (*peer.StaticCollectionConfig, error)
}
//go:generate counterfeiter -o mock/coll_info_provider.go -fake-name CollectionInfoProvider . collectionInfoProvider