forked from hyperledger/fabric-sdk-go
/
cachekey.go
116 lines (93 loc) · 2.34 KB
/
cachekey.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chpvdr
import (
"crypto/sha256"
"strconv"
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
)
// ctxtCacheKey is a lazy cache key for the context cache
type ctxtCacheKey struct {
key string
context fab.ClientContext
}
// newCtxtCacheKey returns a new cacheKey
func newCtxtCacheKey(ctx fab.ClientContext) (*ctxtCacheKey, error) {
identity, err := ctx.Serialize()
if err != nil {
return nil, err
}
h := sha256.New()
if _, err := h.Write(identity); err != nil {
return nil, err
}
hash := h.Sum(nil)
return &ctxtCacheKey{
key: string(hash),
context: ctx,
}, nil
}
// String returns the key as a string
func (k *ctxtCacheKey) String() string {
return k.key
}
// cacheKey holds a key for the provider cache
type cacheKey struct {
channelConfig fab.ChannelCfg
}
// newCacheKey returns a new cacheKey
func newCacheKey(chConfig fab.ChannelCfg) *cacheKey {
return &cacheKey{
channelConfig: chConfig,
}
}
// String returns the key as a string
func (k *cacheKey) String() string {
return k.channelConfig.ID()
}
// eventCacheKey holds a key for the provider cache
type eventCacheKey struct {
key string
channelConfig fab.ChannelCfg
opts []options.Opt
}
// newEventCacheKey returns a new eventCacheKey
func newEventCacheKey(chConfig fab.ChannelCfg, opts ...options.Opt) (*eventCacheKey, error) {
params := defaultParams()
options.Apply(params, opts)
h := sha256.New()
if _, err := h.Write([]byte(params.getOptKey())); err != nil {
return nil, err
}
hash := h.Sum([]byte(chConfig.ID()))
return &eventCacheKey{
channelConfig: chConfig,
key: string(hash),
opts: opts,
}, nil
}
// Opts returns the options to use for creating events service
func (k *eventCacheKey) Opts() []options.Opt {
return k.opts
}
// String returns the key as a string
func (k *eventCacheKey) String() string {
return k.key
}
type params struct {
permitBlockEvents bool
}
func defaultParams() *params {
return ¶ms{}
}
func (p *params) PermitBlockEvents() {
p.permitBlockEvents = true
}
func (p *params) getOptKey() string {
// Construct opts portion
optKey := "blockEvents:" + strconv.FormatBool(p.permitBlockEvents)
return optKey
}