-
Notifications
You must be signed in to change notification settings - Fork 0
/
channelinfo_provider.go
181 lines (160 loc) · 5.25 KB
/
channelinfo_provider.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
/*
Copyright hechain. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package kvledger
import (
"github.com/hechain20/hechain/bccsp/factory"
"github.com/hechain20/hechain/common/channelconfig"
commonledger "github.com/hechain20/hechain/common/ledger"
"github.com/hechain20/hechain/common/ledger/blkstorage"
"github.com/hechain20/hechain/core/ledger"
"github.com/hechain20/hechain/core/ledger/kvledger/txmgmt/statedb"
"github.com/hechain20/hechain/protoutil"
cb "github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/ledger/queryresult"
"github.com/pkg/errors"
)
type channelInfoProvider struct {
channelName string
blockStore *blkstorage.BlockStore
ledger.DeployedChaincodeInfoProvider
}
// NamespacesAndCollections returns namespaces and their collections for the channel.
func (p *channelInfoProvider) NamespacesAndCollections(vdb statedb.VersionedDB) (map[string][]string, error) {
mspIDs, err := p.getAllMSPIDs()
if err != nil {
return nil, err
}
implicitCollNames := make([]string, len(mspIDs))
for i, mspID := range mspIDs {
implicitCollNames[i] = p.GenerateImplicitCollectionForOrg(mspID).Name
}
chaincodesInfo, err := p.AllChaincodesInfo(p.channelName, &simpleQueryExecutor{vdb})
if err != nil {
return nil, err
}
retNamespaces := map[string][]string{}
// iterate each chaincode, add implicit collections and explicit collections
for _, ccInfo := range chaincodesInfo {
ccName := ccInfo.Name
retNamespaces[ccName] = []string{}
retNamespaces[ccName] = append(retNamespaces[ccName], implicitCollNames...)
if ccInfo.ExplicitCollectionConfigPkg == nil {
continue
}
for _, config := range ccInfo.ExplicitCollectionConfigPkg.Config {
collConfig := config.GetStaticCollectionConfig()
if collConfig != nil {
retNamespaces[ccName] = append(retNamespaces[ccName], collConfig.Name)
}
}
}
// add lifecycle management namespaces with implicit collections (not applicable to legacy lifecycle)
for _, ns := range p.Namespaces() {
retNamespaces[ns] = []string{}
if ns == "lscc" {
continue
}
retNamespaces[ns] = append(retNamespaces[ns], implicitCollNames...)
}
// add namespace ""
retNamespaces[""] = []string{}
return retNamespaces, nil
}
// getAllMSPIDs retrieves the MSPIDs of application organizations in all the channel configurations,
// including current and previous channel configurations.
func (p *channelInfoProvider) getAllMSPIDs() ([]string, error) {
blockchainInfo, err := p.blockStore.GetBlockchainInfo()
if err != nil {
return nil, err
}
if blockchainInfo.Height == 0 {
return nil, nil
}
// Iterate over the config blocks to get all the channel configs, extract MSPIDs and add to mspidsMap
mspidsMap := map[string]struct{}{}
blockNum := blockchainInfo.Height - 1
for {
configBlock, err := p.mostRecentConfigBlockAsOf(blockNum)
if err != nil {
return nil, err
}
mspids, err := channelconfig.ExtractMSPIDsForApplicationOrgs(configBlock, factory.GetDefault())
if err != nil {
return nil, err
}
for _, mspid := range mspids {
if _, ok := mspidsMap[mspid]; !ok {
mspidsMap[mspid] = struct{}{}
}
}
if configBlock.Header.Number == 0 {
break
}
blockNum = configBlock.Header.Number - 1
}
mspids := make([]string, 0, len(mspidsMap))
for mspid := range mspidsMap {
mspids = append(mspids, mspid)
}
return mspids, nil
}
// mostRecentConfigBlockAsOf fetches the most recent config block at or below the blockNum
func (p *channelInfoProvider) mostRecentConfigBlockAsOf(blockNum uint64) (*cb.Block, error) {
block, err := p.blockStore.RetrieveBlockByNumber(blockNum)
if err != nil {
return nil, err
}
configBlockNum, err := protoutil.GetLastConfigIndexFromBlock(block)
if err != nil {
return nil, err
}
return p.blockStore.RetrieveBlockByNumber(configBlockNum)
}
// simpleQueryExecutor implements ledger.SimpleQueryExecutor interface
type simpleQueryExecutor struct {
statedb.VersionedDB
}
func (sqe *simpleQueryExecutor) GetState(ns string, key string) ([]byte, error) {
versionedValue, err := sqe.VersionedDB.GetState(ns, key)
if err != nil {
return nil, err
}
var value []byte
if versionedValue != nil {
value = versionedValue.Value
}
return value, nil
}
func (sqe *simpleQueryExecutor) GetStateRangeScanIterator(ns string, startKey string, endKey string) (commonledger.ResultsIterator, error) {
dbItr, err := sqe.VersionedDB.GetStateRangeScanIterator(ns, startKey, endKey)
if err != nil {
return nil, err
}
itr := &resultsItr{ns: ns, dbItr: dbItr}
return itr, nil
}
// GetPrivateDataHash is not implemented and should not be called
func (sqe *simpleQueryExecutor) GetPrivateDataHash(namespace, collection, key string) ([]byte, error) {
return nil, errors.New("not implemented yet")
}
type resultsItr struct {
ns string
dbItr statedb.ResultsIterator
}
// Next implements method in interface ledger.ResultsIterator
func (itr *resultsItr) Next() (commonledger.QueryResult, error) {
versionedKV, err := itr.dbItr.Next()
if err != nil {
return nil, err
}
if versionedKV == nil {
return nil, nil
}
return &queryresult.KV{Namespace: versionedKV.Namespace, Key: versionedKV.Key, Value: versionedKV.Value}, nil
}
// Close implements method in interface ledger.ResultsIterator
func (itr *resultsItr) Close() {
itr.dbItr.Close()
}