Skip to content

Commit a052b61

Browse files
author
Jason Yellick
committed
[FAB-2145] Migrate configtx.Handler to ConfigValue
https://jira.hyperledger.org/browse/FAB-2145 The ConfigItem proto is deprecated, as a step in removing it, the configtx.Handler interface needs to migrate to the new structure ConfigValue. This CR does that. Change-Id: I36f544913127421c99f955bcb721a2a581c0cc59 Signed-off-by: Jason Yellick <[email protected]>
1 parent 79aa652 commit a052b61

17 files changed

+166
-226
lines changed

common/cauthdsl/policy_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ func makePolicySource(policyResult bool) []byte {
5959

6060
func addPolicy(manager *policies.ManagerImpl, id string, policy []byte) {
6161
manager.BeginConfig()
62-
err := manager.ProposeConfig(&cb.ConfigItem{
63-
Type: cb.ConfigItem_POLICY,
64-
Key: id,
62+
err := manager.ProposeConfig(id, &cb.ConfigValue{
6563
Value: policy,
6664
})
6765
if err != nil {

common/configtx/api/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type OrdererConfig interface {
7676
// Handler provides a hook which allows other pieces of code to participate in config proposals
7777
type Handler interface {
7878
// ProposeConfig called when config is added to a proposal
79-
ProposeConfig(configItem *cb.ConfigItem) error
79+
ProposeConfig(key string, configValue *cb.ConfigValue) error
8080
}
8181

8282
// Manager provides a mechanism to query and update config

common/configtx/bytes_handler.go

-68
This file was deleted.

common/configtx/handlers/application/sharedconfig.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,19 @@ func (di *SharedConfigImpl) CommitConfig() {
110110
}
111111

112112
// ProposeConfig is used to add new config to the config proposal
113-
func (di *SharedConfigImpl) ProposeConfig(configItem *cb.ConfigItem) error {
114-
switch configItem.Key {
113+
func (di *SharedConfigImpl) ProposeConfig(key string, configValue *cb.ConfigValue) error {
114+
switch key {
115115
case AnchorPeersKey:
116116
anchorPeers := &pb.AnchorPeers{}
117-
if err := proto.Unmarshal(configItem.Value, anchorPeers); err != nil {
118-
return fmt.Errorf("Unmarshaling error for %s: %s", configItem.Key, err)
117+
if err := proto.Unmarshal(configValue.Value, anchorPeers); err != nil {
118+
return fmt.Errorf("Unmarshaling error for %s: %s", key, err)
119119
}
120120
if logger.IsEnabledFor(logging.DEBUG) {
121-
logger.Debugf("Setting %s to %v", configItem.Key, anchorPeers.AnchorPeers)
121+
logger.Debugf("Setting %s to %v", key, anchorPeers.AnchorPeers)
122122
}
123123
di.pendingConfig.anchorPeers = anchorPeers.AnchorPeers
124124
default:
125-
logger.Warningf("Uknown Peer config item with key %s", configItem.Key)
125+
logger.Warningf("Uknown Peer config item with key %s", key)
126126
}
127127
return nil
128128
}

common/configtx/handlers/application/sharedconfig_test.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ func init() {
3131
logging.SetLevel(logging.DEBUG, "")
3232
}
3333

34-
func makeInvalidConfigItem(key string) *cb.ConfigItem {
35-
return &cb.ConfigItem{
36-
Type: cb.ConfigItem_PEER,
37-
Key: key,
34+
func makeInvalidConfigValue() *cb.ConfigValue {
35+
return &cb.ConfigValue{
3836
Value: []byte("Garbage Data"),
3937
}
4038
}
4139

40+
func itemToValue(configItem *cb.ConfigItem) (string, *cb.ConfigValue) {
41+
return configItem.Key, &cb.ConfigValue{Value: configItem.Value}
42+
}
43+
4244
func TestInterface(t *testing.T) {
4345
_ = configtxapi.ApplicationConfig(NewSharedConfigImpl(nil))
4446
}
@@ -80,17 +82,17 @@ func TestAnchorPeers(t *testing.T) {
8082
&pb.AnchorPeer{Host: "foo", Port: 234, Cert: []byte("foocert")},
8183
&pb.AnchorPeer{Host: "bar", Port: 237, Cert: []byte("barcert")},
8284
}
83-
invalidMessage := makeInvalidConfigItem(AnchorPeersKey)
85+
invalidMessage := makeInvalidConfigValue()
8486
validMessage := TemplateAnchorPeers(endVal)
8587
m := NewSharedConfigImpl(nil)
8688
m.BeginConfig()
8789

88-
err := m.ProposeConfig(invalidMessage)
90+
err := m.ProposeConfig(AnchorPeersKey, invalidMessage)
8991
if err == nil {
9092
t.Fatalf("Should have failed on invalid message")
9193
}
9294

93-
err = m.ProposeConfig(validMessage)
95+
err = m.ProposeConfig(itemToValue(validMessage))
9496
if err != nil {
9597
t.Fatalf("Error applying valid config: %s", err)
9698
}

common/configtx/handlers/channel/sharedconfig.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ func (pm *SharedConfigImpl) CommitConfig() {
136136
}
137137

138138
// ProposeConfig is used to add new config to the config proposal
139-
func (pm *SharedConfigImpl) ProposeConfig(configItem *cb.ConfigItem) error {
140-
switch configItem.Key {
139+
func (pm *SharedConfigImpl) ProposeConfig(key string, configValue *cb.ConfigValue) error {
140+
switch key {
141141
case HashingAlgorithmKey:
142142
hashingAlgorithm := &cb.HashingAlgorithm{}
143-
if err := proto.Unmarshal(configItem.Value, hashingAlgorithm); err != nil {
143+
if err := proto.Unmarshal(configValue.Value, hashingAlgorithm); err != nil {
144144
return fmt.Errorf("Unmarshaling error for HashingAlgorithm: %s", err)
145145
}
146146
switch hashingAlgorithm.Name {
@@ -151,7 +151,7 @@ func (pm *SharedConfigImpl) ProposeConfig(configItem *cb.ConfigItem) error {
151151
}
152152
case BlockDataHashingStructureKey:
153153
blockDataHashingStructure := &cb.BlockDataHashingStructure{}
154-
if err := proto.Unmarshal(configItem.Value, blockDataHashingStructure); err != nil {
154+
if err := proto.Unmarshal(configValue.Value, blockDataHashingStructure); err != nil {
155155
return fmt.Errorf("Unmarshaling error for BlockDataHashingStructure: %s", err)
156156
}
157157

@@ -162,12 +162,12 @@ func (pm *SharedConfigImpl) ProposeConfig(configItem *cb.ConfigItem) error {
162162
pm.pendingConfig.blockDataHashingStructureWidth = blockDataHashingStructure.Width
163163
case OrdererAddressesKey:
164164
ordererAddresses := &cb.OrdererAddresses{}
165-
if err := proto.Unmarshal(configItem.Value, ordererAddresses); err != nil {
165+
if err := proto.Unmarshal(configValue.Value, ordererAddresses); err != nil {
166166
return fmt.Errorf("Unmarshaling error for HashingAlgorithm: %s", err)
167167
}
168168
pm.pendingConfig.ordererAddresses = ordererAddresses.Addresses
169169
default:
170-
logger.Warningf("Uknown Chain config item with key %s", configItem.Key)
170+
logger.Warningf("Uknown Chain config item with key %s", key)
171171
}
172172
return nil
173173
}

common/configtx/handlers/channel/sharedconfig_test.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ func init() {
3030
logging.SetLevel(logging.DEBUG, "")
3131
}
3232

33-
func makeInvalidConfigItem(key string) *cb.ConfigItem {
34-
return &cb.ConfigItem{
35-
Type: cb.ConfigItem_CHAIN,
36-
Key: key,
33+
// A temporary method while ConfigItem is being removed
34+
func itemToValue(configItem *cb.ConfigItem) (string, *cb.ConfigValue) {
35+
return configItem.Key, &cb.ConfigValue{Value: configItem.Value}
36+
}
37+
38+
func makeInvalidConfigItem() *cb.ConfigValue {
39+
return &cb.ConfigValue{
3740
Value: []byte("Garbage Data"),
3841
}
3942
}
@@ -75,24 +78,24 @@ func TestRollback(t *testing.T) {
7578
}
7679

7780
func TestHashingAlgorithm(t *testing.T) {
78-
invalidMessage := makeInvalidConfigItem(HashingAlgorithmKey)
81+
invalidMessage := makeInvalidConfigItem()
7982
invalidAlgorithm := TemplateHashingAlgorithm("MD5")
8083
validAlgorithm := DefaultHashingAlgorithm()
8184

8285
m := NewSharedConfigImpl(nil, nil)
8386
m.BeginConfig()
8487

85-
err := m.ProposeConfig(invalidMessage)
88+
err := m.ProposeConfig(HashingAlgorithmKey, invalidMessage)
8689
if err == nil {
8790
t.Fatalf("Should have failed on invalid message")
8891
}
8992

90-
err = m.ProposeConfig(invalidAlgorithm)
93+
err = m.ProposeConfig(itemToValue(invalidAlgorithm))
9194
if err == nil {
9295
t.Fatalf("Should have failed on invalid algorithm")
9396
}
9497

95-
err = m.ProposeConfig(validAlgorithm)
98+
err = m.ProposeConfig(itemToValue(validAlgorithm))
9699
if err != nil {
97100
t.Fatalf("Error applying valid config: %s", err)
98101
}
@@ -105,24 +108,24 @@ func TestHashingAlgorithm(t *testing.T) {
105108
}
106109

107110
func TestBlockDataHashingStructure(t *testing.T) {
108-
invalidMessage := makeInvalidConfigItem(BlockDataHashingStructureKey)
111+
invalidMessage := makeInvalidConfigItem()
109112
invalidWidth := TemplateBlockDataHashingStructure(0)
110113
validWidth := DefaultBlockDataHashingStructure()
111114

112115
m := NewSharedConfigImpl(nil, nil)
113116
m.BeginConfig()
114117

115-
err := m.ProposeConfig(invalidMessage)
118+
err := m.ProposeConfig(BlockDataHashingStructureKey, invalidMessage)
116119
if err == nil {
117120
t.Fatalf("Should have failed on invalid message")
118121
}
119122

120-
err = m.ProposeConfig(invalidWidth)
123+
err = m.ProposeConfig(itemToValue(invalidWidth))
121124
if err == nil {
122125
t.Fatalf("Should have failed on invalid width")
123126
}
124127

125-
err = m.ProposeConfig(validWidth)
128+
err = m.ProposeConfig(itemToValue(validWidth))
126129
if err != nil {
127130
t.Fatalf("Error applying valid config: %s", err)
128131
}
@@ -135,17 +138,17 @@ func TestBlockDataHashingStructure(t *testing.T) {
135138
}
136139

137140
func TestOrdererAddresses(t *testing.T) {
138-
invalidMessage := makeInvalidConfigItem(OrdererAddressesKey)
141+
invalidMessage := makeInvalidConfigItem()
139142
validMessage := DefaultOrdererAddresses()
140143
m := NewSharedConfigImpl(nil, nil)
141144
m.BeginConfig()
142145

143-
err := m.ProposeConfig(invalidMessage)
146+
err := m.ProposeConfig(OrdererAddressesKey, invalidMessage)
144147
if err == nil {
145148
t.Fatalf("Should have failed on invalid message")
146149
}
147150

148-
err = m.ProposeConfig(validMessage)
151+
err = m.ProposeConfig(itemToValue(validMessage))
149152
if err != nil {
150153
t.Fatalf("Error applying valid config: %s", err)
151154
}

common/configtx/handlers/msp/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ func (bh *MSPConfigHandler) CommitConfig() {
6060
}
6161

6262
// ProposeConfig called when config is added to a proposal
63-
func (bh *MSPConfigHandler) ProposeConfig(configItem *common.ConfigItem) error {
63+
func (bh *MSPConfigHandler) ProposeConfig(key string, configValue *common.ConfigValue) error {
6464
mspconfig := &mspprotos.MSPConfig{}
65-
err := proto.Unmarshal(configItem.Value, mspconfig)
65+
err := proto.Unmarshal(configValue.Value, mspconfig)
6666
if err != nil {
6767
return fmt.Errorf("Error unmarshalling msp config item, err %s", err)
6868
}

common/configtx/handlers/msp/config_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ func TestMSPConfigManager(t *testing.T) {
3232
confBytes, err := proto.Marshal(conf)
3333
assert.NoError(t, err)
3434

35-
ci := &common.ConfigItem{Key: "DEFAULT", Value: confBytes}
35+
ci := &common.ConfigValue{Value: confBytes}
3636

3737
// test success:
3838

3939
// begin/propose/commit
40+
key := "DEFAULT"
41+
4042
mspCH := &MSPConfigHandler{}
4143
mspCH.BeginConfig()
42-
err = mspCH.ProposeConfig(ci)
44+
err = mspCH.ProposeConfig(key, ci)
4345
assert.NoError(t, err)
4446
mspCH.CommitConfig()
4547

@@ -53,8 +55,8 @@ func TestMSPConfigManager(t *testing.T) {
5355
// test failure
5456
// begin/propose/commit
5557
mspCH.BeginConfig()
56-
err = mspCH.ProposeConfig(ci)
58+
err = mspCH.ProposeConfig(key, ci)
5759
assert.NoError(t, err)
58-
err = mspCH.ProposeConfig(&common.ConfigItem{Type: common.ConfigItem_MSP, Key: "DEFAULT", Value: []byte("BARF!")})
60+
err = mspCH.ProposeConfig(key, &common.ConfigValue{Value: []byte("BARF!")})
5961
assert.Error(t, err)
6062
}

common/configtx/handlers/orderer/sharedconfig.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ func (pm *ManagerImpl) CommitConfig() {
177177
}
178178

179179
// ProposeConfig is used to add new config to the config proposal
180-
func (pm *ManagerImpl) ProposeConfig(configItem *cb.ConfigItem) error {
181-
switch configItem.Key {
180+
func (pm *ManagerImpl) ProposeConfig(key string, configValue *cb.ConfigValue) error {
181+
switch key {
182182
case ConsensusTypeKey:
183183
consensusType := &ab.ConsensusType{}
184-
if err := proto.Unmarshal(configItem.Value, consensusType); err != nil {
184+
if err := proto.Unmarshal(configValue.Value, consensusType); err != nil {
185185
return fmt.Errorf("Unmarshaling error for ConsensusType: %s", err)
186186
}
187187
if pm.config.consensusType == "" {
@@ -194,7 +194,7 @@ func (pm *ManagerImpl) ProposeConfig(configItem *cb.ConfigItem) error {
194194
pm.pendingConfig.consensusType = consensusType.Type
195195
case BatchSizeKey:
196196
batchSize := &ab.BatchSize{}
197-
if err := proto.Unmarshal(configItem.Value, batchSize); err != nil {
197+
if err := proto.Unmarshal(configValue.Value, batchSize); err != nil {
198198
return fmt.Errorf("Unmarshaling error for BatchSize: %s", err)
199199
}
200200
if batchSize.MaxMessageCount == 0 {
@@ -214,7 +214,7 @@ func (pm *ManagerImpl) ProposeConfig(configItem *cb.ConfigItem) error {
214214
var timeoutValue time.Duration
215215
var err error
216216
batchTimeout := &ab.BatchTimeout{}
217-
if err = proto.Unmarshal(configItem.Value, batchTimeout); err != nil {
217+
if err = proto.Unmarshal(configValue.Value, batchTimeout); err != nil {
218218
return fmt.Errorf("Unmarshaling error for BatchTimeout: %s", err)
219219
}
220220
if timeoutValue, err = time.ParseDuration(batchTimeout.Timeout); err != nil {
@@ -226,7 +226,7 @@ func (pm *ManagerImpl) ProposeConfig(configItem *cb.ConfigItem) error {
226226
pm.pendingConfig.batchTimeout = timeoutValue
227227
case ChainCreationPolicyNamesKey:
228228
chainCreationPolicyNames := &ab.ChainCreationPolicyNames{}
229-
if err := proto.Unmarshal(configItem.Value, chainCreationPolicyNames); err != nil {
229+
if err := proto.Unmarshal(configValue.Value, chainCreationPolicyNames); err != nil {
230230
return fmt.Errorf("Unmarshaling error for ChainCreator: %s", err)
231231
}
232232
if chainCreationPolicyNames.Names == nil {
@@ -238,19 +238,19 @@ func (pm *ManagerImpl) ProposeConfig(configItem *cb.ConfigItem) error {
238238
}
239239
case IngressPolicyNamesKey:
240240
ingressPolicyNames := &ab.IngressPolicyNames{}
241-
if err := proto.Unmarshal(configItem.Value, ingressPolicyNames); err != nil {
241+
if err := proto.Unmarshal(configValue.Value, ingressPolicyNames); err != nil {
242242
return fmt.Errorf("Unmarshaling error for IngressPolicyNames: %s", err)
243243
}
244244
pm.pendingConfig.ingressPolicyNames = ingressPolicyNames.Names
245245
case EgressPolicyNamesKey:
246246
egressPolicyNames := &ab.EgressPolicyNames{}
247-
if err := proto.Unmarshal(configItem.Value, egressPolicyNames); err != nil {
247+
if err := proto.Unmarshal(configValue.Value, egressPolicyNames); err != nil {
248248
return fmt.Errorf("Unmarshaling error for EgressPolicyNames: %s", err)
249249
}
250250
pm.pendingConfig.egressPolicyNames = egressPolicyNames.Names
251251
case KafkaBrokersKey:
252252
kafkaBrokers := &ab.KafkaBrokers{}
253-
if err := proto.Unmarshal(configItem.Value, kafkaBrokers); err != nil {
253+
if err := proto.Unmarshal(configValue.Value, kafkaBrokers); err != nil {
254254
return fmt.Errorf("Unmarshaling error for KafkaBrokers: %s", err)
255255
}
256256
if len(kafkaBrokers.Brokers) == 0 {

0 commit comments

Comments
 (0)