forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mgr.go
242 lines (211 loc) · 8.15 KB
/
mgr.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package confighistory
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/ledger/rwset/kvrwset"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/ledger"
"github.com/pkg/errors"
)
var logger = flogging.MustGetLogger("confighistory")
const (
collectionConfigNamespace = "lscc" // lscc namespace was introduced in version 1.2 and we continue to use this in order to be compatible with existing data
)
// Mgr should be registered as a state listener. The state listener builds the history and retriever helps in querying the history
type Mgr interface {
ledger.StateListener
GetRetriever(ledgerID string, ledgerInfoRetriever LedgerInfoRetriever) ledger.ConfigHistoryRetriever
Close()
}
type mgr struct {
ccInfoProvider ledger.DeployedChaincodeInfoProvider
dbProvider *dbProvider
}
// NewMgr constructs an instance that implements interface `Mgr`
func NewMgr(dbPath string, ccInfoProvider ledger.DeployedChaincodeInfoProvider) (Mgr, error) {
p, err := newDBProvider(dbPath)
if err != nil {
return nil, err
}
return &mgr{ccInfoProvider, p}, nil
}
func (m *mgr) Initialize(ledgerID string, qe ledger.SimpleQueryExecutor) error {
// Noop
return nil
}
// InterestedInNamespaces implements function from the interface ledger.StateListener
func (m *mgr) InterestedInNamespaces() []string {
return m.ccInfoProvider.Namespaces()
}
// StateCommitDone implements function from the interface ledger.StateListener
func (m *mgr) StateCommitDone(ledgerID string) {
// Noop
}
// HandleStateUpdates implements function from the interface ledger.StateListener
// In this implementation, the latest collection config package is retrieved via
// ledger.DeployedChaincodeInfoProvider and is persisted as a separate entry in a separate db.
// The composite key for the entry is a tuple of <blockNum, namespace, key>
func (m *mgr) HandleStateUpdates(trigger *ledger.StateUpdateTrigger) error {
updatedCCs, err := m.ccInfoProvider.UpdatedChaincodes(extractPublicUpdates(trigger.StateUpdates))
if err != nil {
return err
}
// updated chaincodes can be empty if the invocation to this function is triggered
// because of state updates that contains only chaincode approval transaction output
if len(updatedCCs) == 0 {
return nil
}
updatedCollConfigs := map[string]*peer.CollectionConfigPackage{}
for _, cc := range updatedCCs {
ccInfo, err := m.ccInfoProvider.ChaincodeInfo(trigger.LedgerID, cc.Name, trigger.PostCommitQueryExecutor)
if err != nil {
return err
}
// DeployedChaincodeInfoProvider implementation in new lifecycle return an empty 'CollectionConfigPackage'
// (instead of a nil) to indicate the absence of collection config, so check for both conditions
if ccInfo.ExplicitCollectionConfigPkg == nil || len(ccInfo.ExplicitCollectionConfigPkg.Config) == 0 {
continue
}
updatedCollConfigs[ccInfo.Name] = ccInfo.ExplicitCollectionConfigPkg
}
if len(updatedCollConfigs) == 0 {
return nil
}
batch, err := prepareDBBatch(updatedCollConfigs, trigger.CommittingBlockNum)
if err != nil {
return err
}
dbHandle := m.dbProvider.getDB(trigger.LedgerID)
return dbHandle.writeBatch(batch, true)
}
// GetRetriever returns an implementation of `ledger.ConfigHistoryRetriever` for the given ledger id.
func (m *mgr) GetRetriever(ledgerID string, ledgerInfoRetriever LedgerInfoRetriever) ledger.ConfigHistoryRetriever {
return &retriever{
ledgerInfoRetriever: ledgerInfoRetriever,
ledgerID: ledgerID,
deployedCCInfoProvider: m.ccInfoProvider,
dbHandle: m.dbProvider.getDB(ledgerID),
}
}
// Close implements the function in the interface 'Mgr'
func (m *mgr) Close() {
m.dbProvider.Close()
}
type retriever struct {
ledgerInfoRetriever LedgerInfoRetriever
ledgerID string
deployedCCInfoProvider ledger.DeployedChaincodeInfoProvider
dbHandle *db
}
// MostRecentCollectionConfigBelow implements function from the interface ledger.ConfigHistoryRetriever
func (r *retriever) MostRecentCollectionConfigBelow(blockNum uint64, chaincodeName string) (*ledger.CollectionConfigInfo, error) {
compositeKV, err := r.dbHandle.mostRecentEntryBelow(blockNum, collectionConfigNamespace, constructCollectionConfigKey(chaincodeName))
if err != nil {
return nil, err
}
implicitColls, err := r.getImplicitCollection(chaincodeName)
if err != nil {
return nil, err
}
return constructCollectionConfigInfo(compositeKV, implicitColls)
}
// CollectionConfigAt implements function from the interface ledger.ConfigHistoryRetriever
func (r *retriever) CollectionConfigAt(blockNum uint64, chaincodeName string) (*ledger.CollectionConfigInfo, error) {
info, err := r.ledgerInfoRetriever.GetBlockchainInfo()
if err != nil {
return nil, err
}
maxCommittedBlockNum := info.Height - 1
if maxCommittedBlockNum < blockNum {
return nil, &ledger.ErrCollectionConfigNotYetAvailable{MaxBlockNumCommitted: maxCommittedBlockNum,
Msg: fmt.Sprintf("The maximum block number committed [%d] is less than the requested block number [%d]", maxCommittedBlockNum, blockNum)}
}
compositeKV, err := r.dbHandle.entryAt(blockNum, collectionConfigNamespace, constructCollectionConfigKey(chaincodeName))
if err != nil {
return nil, err
}
implicitColls, err := r.getImplicitCollection(chaincodeName)
if err != nil {
return nil, err
}
return constructCollectionConfigInfo(compositeKV, implicitColls)
}
func (r *retriever) getImplicitCollection(chaincodeName string) ([]*peer.StaticCollectionConfig, error) {
qe, err := r.ledgerInfoRetriever.NewQueryExecutor()
if err != nil {
return nil, err
}
defer qe.Done()
return r.deployedCCInfoProvider.ImplicitCollections(r.ledgerID, chaincodeName, qe)
}
func prepareDBBatch(chaincodeCollConfigs map[string]*peer.CollectionConfigPackage, committingBlockNum uint64) (*batch, error) {
batch := newBatch()
for ccName, collConfig := range chaincodeCollConfigs {
key := constructCollectionConfigKey(ccName)
var configBytes []byte
var err error
if configBytes, err = proto.Marshal(collConfig); err != nil {
return nil, errors.WithStack(err)
}
batch.add(collectionConfigNamespace, key, committingBlockNum, configBytes)
}
return batch, nil
}
func compositeKVToCollectionConfig(compositeKV *compositeKV) (*ledger.CollectionConfigInfo, error) {
conf := &peer.CollectionConfigPackage{}
if err := proto.Unmarshal(compositeKV.value, conf); err != nil {
return nil, errors.Wrap(err, "error unmarshalling compositeKV to collection config")
}
return &ledger.CollectionConfigInfo{
CollectionConfig: conf,
CommittingBlockNum: compositeKV.blockNum,
}, nil
}
func constructCollectionConfigKey(chaincodeName string) string {
return chaincodeName + "~collection" // collection config key as in version 1.2 and we continue to use this in order to be compatible with existing data
}
func extractPublicUpdates(stateUpdates ledger.StateUpdates) map[string][]*kvrwset.KVWrite {
m := map[string][]*kvrwset.KVWrite{}
for ns, updates := range stateUpdates {
m[ns] = updates.PublicUpdates
}
return m
}
func constructCollectionConfigInfo(
compositeKV *compositeKV,
implicitColls []*peer.StaticCollectionConfig,
) (*ledger.CollectionConfigInfo, error) {
var collConf *ledger.CollectionConfigInfo
var err error
if compositeKV == nil && len(implicitColls) == 0 {
return nil, nil
}
collConf = &ledger.CollectionConfigInfo{
CollectionConfig: &peer.CollectionConfigPackage{},
}
if compositeKV != nil {
if collConf, err = compositeKVToCollectionConfig(compositeKV); err != nil {
return nil, err
}
}
for _, implicitColl := range implicitColls {
cc := &peer.CollectionConfig{}
cc.Payload = &peer.CollectionConfig_StaticCollectionConfig{StaticCollectionConfig: implicitColl}
collConf.CollectionConfig.Config = append(
collConf.CollectionConfig.Config,
cc,
)
}
return collConf, nil
}
// LedgerInfoRetriever retrieves the relevant info from ledger
type LedgerInfoRetriever interface {
GetBlockchainInfo() (*common.BlockchainInfo, error)
NewQueryExecutor() (ledger.QueryExecutor, error)
}