Skip to content

Commit 91155fb

Browse files
author
Jason Yellick
committed
[FAB-5686] Update protolator for resource config
With the addition of the new protos to support the resource config comes the need for additional logic in the configtxlator/protolator to support inspecting the new config type. This CR adds that logic so that these new types may be inspected. Change-Id: I91f4de3e4b260a16ddcb3531e8bb761e5f3efbbc Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent 7e5e955 commit 91155fb

File tree

2 files changed

+105
-6
lines changed

2 files changed

+105
-6
lines changed

protos/common/configtx.go

+41-6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ func (cs *ConfigSignature) StaticallyOpaqueFieldProto(name string) (proto.Messag
5252
return &SignatureHeader{}, nil
5353
}
5454

55+
// DynamicConfigTypes allows for other packages outside of the common package
56+
// to register dynamic config types
57+
var DynamicConfigTypes = map[ConfigType]func(cg *ConfigGroup) proto.Message{
58+
ConfigType_CHANNEL: func(cg *ConfigGroup) proto.Message {
59+
return &DynamicChannelGroup{
60+
ConfigGroup: cg,
61+
}
62+
},
63+
}
64+
5565
func (c *Config) DynamicFields() []string {
5666
return []string{"channel_group"}
5767
}
@@ -66,9 +76,32 @@ func (c *Config) DynamicFieldProto(name string, base proto.Message) (proto.Messa
6676
return nil, fmt.Errorf("Config must embed a config group as its dynamic field")
6777
}
6878

69-
return &DynamicChannelGroup{
70-
ConfigGroup: cg,
71-
}, nil
79+
dm, ok := DynamicConfigTypes[ConfigType(c.Type)]
80+
if !ok {
81+
return nil, fmt.Errorf("Unknown config type: %d", c.Type)
82+
}
83+
return dm(cg), nil
84+
}
85+
86+
// ConfigUpdateIsolatedDataTypes allows other proto packages to register types for the
87+
// the isolated_data field. This is necessary to break import cycles.
88+
var ConfigUpdateIsolatedDataTypes = map[string]func(string) proto.Message{}
89+
90+
func (c *ConfigUpdate) StaticallyOpaqueMapFields() []string {
91+
return []string{"isolated_data"}
92+
}
93+
94+
func (c *ConfigUpdate) StaticallyOpaqueMapFieldProto(name string, key string) (proto.Message, error) {
95+
if name != c.StaticallyOpaqueMapFields()[0] {
96+
return nil, fmt.Errorf("Not a statically opaque map field: %s", name)
97+
}
98+
99+
mf, ok := ConfigUpdateIsolatedDataTypes[key]
100+
if !ok {
101+
return nil, fmt.Errorf("Unknown map key: %s", key)
102+
}
103+
104+
return mf(key), nil
72105
}
73106

74107
func (c *ConfigUpdate) DynamicFields() []string {
@@ -85,9 +118,11 @@ func (c *ConfigUpdate) DynamicFieldProto(name string, base proto.Message) (proto
85118
return nil, fmt.Errorf("Expected base to be *ConfigGroup, got %T", base)
86119
}
87120

88-
return &DynamicChannelGroup{
89-
ConfigGroup: cg,
90-
}, nil
121+
dm, ok := DynamicConfigTypes[ConfigType(c.Type)]
122+
if !ok {
123+
return nil, fmt.Errorf("Unknown config type: %d", c.Type)
124+
}
125+
return dm(cg), nil
91126
}
92127

93128
func (cv *ConfigValue) VariablyOpaqueFields() []string {

protos/peer/resources.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package peer
8+
9+
import (
10+
"fmt"
11+
12+
"github.com/hyperledger/fabric/protos/common"
13+
14+
"github.com/golang/protobuf/proto"
15+
)
16+
17+
const RSCCSeedDataKey = "rscc_seed_data"
18+
19+
func init() {
20+
common.DynamicConfigTypes[common.ConfigType_RESOURCE] = func(cg *common.ConfigGroup) proto.Message {
21+
return &DynamicResourceConfigGroup{
22+
ConfigGroup: cg,
23+
}
24+
}
25+
26+
common.ConfigUpdateIsolatedDataTypes[RSCCSeedDataKey] = func(key string) proto.Message {
27+
return &common.Config{}
28+
}
29+
}
30+
31+
type DynamicResourceConfigGroup struct {
32+
*common.ConfigGroup
33+
}
34+
35+
func (drcg *DynamicResourceConfigGroup) DynamicMapFields() []string {
36+
return []string{"values"}
37+
}
38+
39+
func (drcg *DynamicResourceConfigGroup) DynamicMapFieldProto(name string, key string, base proto.Message) (proto.Message, error) {
40+
switch name {
41+
case "values":
42+
cv, ok := base.(*common.ConfigValue)
43+
if !ok {
44+
return nil, fmt.Errorf("ConfigGroup values can only contain ConfigValue messages")
45+
}
46+
return &DynamicResourceConfigValue{
47+
ConfigValue: cv,
48+
}, nil
49+
default:
50+
return nil, fmt.Errorf("ConfigGroup does not have a dynamic field: %s", name)
51+
}
52+
}
53+
54+
type DynamicResourceConfigValue struct {
55+
*common.ConfigValue
56+
}
57+
58+
func (drcv *DynamicResourceConfigValue) VariablyOpaqueFieldProto(name string) (proto.Message, error) {
59+
if name != drcv.VariablyOpaqueFields()[0] {
60+
return nil, fmt.Errorf("Not a marshaled field: %s", name)
61+
}
62+
63+
return &Resource{}, nil
64+
}

0 commit comments

Comments
 (0)