-
Notifications
You must be signed in to change notification settings - Fork 1
/
channel.go
205 lines (165 loc) · 6.34 KB
/
channel.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package channelconfig
import (
"fmt"
"math"
"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/common/capabilities"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/pkg/errors"
)
// Channel config keys
const (
// ConsortiumKey is the key for the cb.ConfigValue for the Consortium message
ConsortiumKey = "Consortium"
// HashingAlgorithmKey is the cb.ConfigItem type key name for the HashingAlgorithm message
HashingAlgorithmKey = "HashingAlgorithm"
// BlockDataHashingStructureKey is the cb.ConfigItem type key name for the BlockDataHashingStructure message
BlockDataHashingStructureKey = "BlockDataHashingStructure"
// OrdererAddressesKey is the cb.ConfigItem type key name for the OrdererAddresses message
OrdererAddressesKey = "OrdererAddresses"
// GroupKey is the name of the channel group
ChannelGroupKey = "Channel"
// CapabilitiesKey is the name of the key which refers to capabilities, it appears at the channel,
// application, and orderer levels and this constant is used for all three.
CapabilitiesKey = "Capabilities"
)
// ChannelValues gives read only access to the channel configuration
type ChannelValues interface {
// HashingAlgorithm returns the default algorithm to be used when hashing
// such as computing block hashes, and CreationPolicy digests
HashingAlgorithm() func(input []byte) []byte
// BlockDataHashingStructureWidth returns the width to use when constructing the
// Merkle tree to compute the BlockData hash
BlockDataHashingStructureWidth() uint32
// OrdererAddresses returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver
OrdererAddresses() []string
}
// ChannelProtos is where the proposed configuration is unmarshaled into
type ChannelProtos struct {
HashingAlgorithm *cb.HashingAlgorithm
BlockDataHashingStructure *cb.BlockDataHashingStructure
OrdererAddresses *cb.OrdererAddresses
Consortium *cb.Consortium
Capabilities *cb.Capabilities
}
// ChannelConfig stores the channel configuration
type ChannelConfig struct {
protos *ChannelProtos
hashingAlgorithm func(input []byte) []byte
mspManager msp.MSPManager
appConfig *ApplicationConfig
ordererConfig *OrdererConfig
consortiumsConfig *ConsortiumsConfig
}
// NewChannelConfig creates a new ChannelConfig
func NewChannelConfig(channelGroup *cb.ConfigGroup) (*ChannelConfig, error) {
cc := &ChannelConfig{
protos: &ChannelProtos{},
}
if err := DeserializeProtoValuesFromGroup(channelGroup, cc.protos); err != nil {
return nil, errors.Wrap(err, "failed to deserialize values")
}
if err := cc.Validate(); err != nil {
return nil, err
}
capabilities := cc.Capabilities()
mspConfigHandler := NewMSPConfigHandler(capabilities.MSPVersion())
var err error
for groupName, group := range channelGroup.Groups {
switch groupName {
case ApplicationGroupKey:
cc.appConfig, err = NewApplicationConfig(group, mspConfigHandler)
case OrdererGroupKey:
cc.ordererConfig, err = NewOrdererConfig(group, mspConfigHandler)
case ConsortiumsGroupKey:
cc.consortiumsConfig, err = NewConsortiumsConfig(group, mspConfigHandler)
default:
return nil, fmt.Errorf("Disallowed channel group: %s", group)
}
if err != nil {
return nil, errors.Wrapf(err, "could not create channel %s sub-group config", groupName)
}
}
if cc.mspManager, err = mspConfigHandler.CreateMSPManager(); err != nil {
return nil, err
}
return cc, nil
}
// MSPManager returns the MSP manager for this config
func (cc *ChannelConfig) MSPManager() msp.MSPManager {
return cc.mspManager
}
// OrdererConfig returns the orderer config associated with this channel
func (cc *ChannelConfig) OrdererConfig() *OrdererConfig {
return cc.ordererConfig
}
// ApplicationConfig returns the application config associated with this channel
func (cc *ChannelConfig) ApplicationConfig() *ApplicationConfig {
return cc.appConfig
}
// ConsortiumsConfig returns the consortium config associated with this channel if it exists
func (cc *ChannelConfig) ConsortiumsConfig() *ConsortiumsConfig {
return cc.consortiumsConfig
}
// HashingAlgorithm returns a function pointer to the chain hashing algorihtm
func (cc *ChannelConfig) HashingAlgorithm() func(input []byte) []byte {
return cc.hashingAlgorithm
}
// BlockDataHashingStructure returns the width to use when forming the block data hashing structure
func (cc *ChannelConfig) BlockDataHashingStructureWidth() uint32 {
return cc.protos.BlockDataHashingStructure.Width
}
// OrdererAddresses returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver
func (cc *ChannelConfig) OrdererAddresses() []string {
return cc.protos.OrdererAddresses.Addresses
}
// ConsortiumName returns the name of the consortium this channel was created under
func (cc *ChannelConfig) ConsortiumName() string {
return cc.protos.Consortium.Name
}
// Capabilities returns information about the available capabilities for this channel
func (cc *ChannelConfig) Capabilities() ChannelCapabilities {
return capabilities.NewChannelProvider(cc.protos.Capabilities.Capabilities)
}
// Validate inspects the generated configuration protos and ensures that the values are correct
func (cc *ChannelConfig) Validate() error {
for _, validator := range []func() error{
cc.validateHashingAlgorithm,
cc.validateBlockDataHashingStructure,
cc.validateOrdererAddresses,
} {
if err := validator(); err != nil {
return err
}
}
return nil
}
func (cc *ChannelConfig) validateHashingAlgorithm() error {
switch cc.protos.HashingAlgorithm.Name {
case bccsp.SHA256:
cc.hashingAlgorithm = util.ComputeSHA256
case bccsp.SHA3_256:
cc.hashingAlgorithm = util.ComputeSHA3256
default:
return fmt.Errorf("Unknown hashing algorithm type: %s", cc.protos.HashingAlgorithm.Name)
}
return nil
}
func (cc *ChannelConfig) validateBlockDataHashingStructure() error {
if cc.protos.BlockDataHashingStructure.Width != math.MaxUint32 {
return fmt.Errorf("BlockDataHashStructure width only supported at MaxUint32 in this version")
}
return nil
}
func (cc *ChannelConfig) validateOrdererAddresses() error {
if len(cc.protos.OrdererAddresses.Addresses) == 0 {
return fmt.Errorf("Must set some OrdererAddresses")
}
return nil
}