github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/protos/common/configuration.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package common 18 19 import ( 20 "fmt" 21 22 "github.com/hyperledger/fabric/protos/msp" 23 24 "github.com/golang/protobuf/proto" 25 ) 26 27 type DynamicConfigGroupFactory interface { 28 DynamicConfigGroup(cg *ConfigGroup) proto.Message 29 } 30 31 // ChannelGroupMap is a slightly hacky way to break the dependency cycle which would 32 // be created if the protos/common package imported the protos/orderer or protos/peer 33 // packages. These packages instead register a DynamicConfigGroupFactory with the 34 // ChannelGroupMap when they are loaded, creating a runtime linkage between the two 35 var ChannelGroupMap = map[string]DynamicConfigGroupFactory{ 36 "Consortiums": DynamicConsortiumsGroupFactory{}, 37 } 38 39 type DynamicChannelGroup struct { 40 *ConfigGroup 41 } 42 43 func (dcg *DynamicChannelGroup) DynamicMapFieldProto(name string, key string, base proto.Message) (proto.Message, error) { 44 switch name { 45 case "groups": 46 cg, ok := base.(*ConfigGroup) 47 if !ok { 48 return nil, fmt.Errorf("ConfigGroup groups can only contain ConfigGroup messages") 49 } 50 51 dcgf, ok := ChannelGroupMap[key] 52 if !ok { 53 return nil, fmt.Errorf("unknown channel ConfigGroup sub-group: %s", key) 54 } 55 return dcgf.DynamicConfigGroup(cg), nil 56 case "values": 57 cv, ok := base.(*ConfigValue) 58 if !ok { 59 return nil, fmt.Errorf("ConfigGroup values can only contain ConfigValue messages") 60 } 61 return &DynamicChannelConfigValue{ 62 ConfigValue: cv, 63 name: key, 64 }, nil 65 default: 66 return nil, fmt.Errorf("ConfigGroup does not have a dynamic field: %s", name) 67 } 68 } 69 70 type DynamicChannelConfigValue struct { 71 *ConfigValue 72 name string 73 } 74 75 func (dccv *DynamicChannelConfigValue) VariablyOpaqueFieldProto(name string) (proto.Message, error) { 76 if name != dccv.VariablyOpaqueFields()[0] { 77 return nil, fmt.Errorf("not a marshaled field: %s", name) 78 } 79 switch dccv.name { 80 case "HashingAlgorithm": 81 return &HashingAlgorithm{}, nil 82 case "BlockDataHashingStructure": 83 return &BlockDataHashingStructure{}, nil 84 case "OrdererAddresses": 85 return &OrdererAddresses{}, nil 86 case "Consortium": 87 return &Consortium{}, nil 88 default: 89 return nil, fmt.Errorf("unknown Channel ConfigValue name: %s", dccv.name) 90 } 91 } 92 93 func (dccv *DynamicChannelConfigValue) Underlying() proto.Message { 94 return dccv.ConfigValue 95 } 96 97 type DynamicConsortiumsGroupFactory struct{} 98 99 func (dogf DynamicConsortiumsGroupFactory) DynamicConfigGroup(cg *ConfigGroup) proto.Message { 100 return &DynamicConsortiumsGroup{ 101 ConfigGroup: cg, 102 } 103 } 104 105 type DynamicConsortiumsGroup struct { 106 *ConfigGroup 107 } 108 109 func (dcg *DynamicConsortiumsGroup) DynamicMapFieldProto(name string, key string, base proto.Message) (proto.Message, error) { 110 switch name { 111 case "groups": 112 cg, ok := base.(*ConfigGroup) 113 if !ok { 114 return nil, fmt.Errorf("ConfigGroup groups can only contain ConfigGroup messages") 115 } 116 117 return &DynamicConsortiumGroup{ 118 ConfigGroup: cg, 119 }, nil 120 case "values": 121 return nil, fmt.Errorf("Consortiums currently support no config values") 122 default: 123 return nil, fmt.Errorf("ConfigGroup does not have a dynamic field: %s", name) 124 } 125 } 126 127 func (dcg *DynamicConsortiumsGroup) Underlying() proto.Message { 128 return dcg.ConfigGroup 129 } 130 131 type DynamicConsortiumGroup struct { 132 *ConfigGroup 133 } 134 135 func (dcg *DynamicConsortiumGroup) DynamicMapFieldProto(name string, key string, base proto.Message) (proto.Message, error) { 136 switch name { 137 case "groups": 138 cg, ok := base.(*ConfigGroup) 139 if !ok { 140 return nil, fmt.Errorf("ConfigGroup groups can only contain ConfigGroup messages") 141 } 142 return &DynamicConsortiumOrgGroup{ 143 ConfigGroup: cg, 144 }, nil 145 case "values": 146 cv, ok := base.(*ConfigValue) 147 if !ok { 148 return nil, fmt.Errorf("ConfigGroup values can only contain ConfigValue messages") 149 } 150 151 return &DynamicConsortiumConfigValue{ 152 ConfigValue: cv, 153 name: key, 154 }, nil 155 default: 156 return nil, fmt.Errorf("not a dynamic orderer map field: %s", name) 157 } 158 } 159 160 func (dcg *DynamicConsortiumGroup) Underlying() proto.Message { 161 return dcg.ConfigGroup 162 } 163 164 type DynamicConsortiumConfigValue struct { 165 *ConfigValue 166 name string 167 } 168 169 func (dccv *DynamicConsortiumConfigValue) VariablyOpaqueFieldProto(name string) (proto.Message, error) { 170 if name != dccv.VariablyOpaqueFields()[0] { 171 return nil, fmt.Errorf("not a marshaled field: %s", name) 172 } 173 switch dccv.name { 174 case "ChannelCreationPolicy": 175 return &Policy{}, nil 176 default: 177 return nil, fmt.Errorf("unknown Consortium ConfigValue name: %s", dccv.name) 178 } 179 } 180 181 type DynamicConsortiumOrgGroup struct { 182 *ConfigGroup 183 } 184 185 func (dcg *DynamicConsortiumOrgGroup) DynamicMapFieldProto(name string, key string, base proto.Message) (proto.Message, error) { 186 switch name { 187 case "groups": 188 return nil, fmt.Errorf("ConsortiumOrg groups do not support sub groups") 189 case "values": 190 cv, ok := base.(*ConfigValue) 191 if !ok { 192 return nil, fmt.Errorf("ConfigGroup values can only contain ConfigValue messages") 193 } 194 195 return &DynamicConsortiumOrgConfigValue{ 196 ConfigValue: cv, 197 name: key, 198 }, nil 199 default: 200 return nil, fmt.Errorf("not a dynamic orderer map field: %s", name) 201 } 202 } 203 204 type DynamicConsortiumOrgConfigValue struct { 205 *ConfigValue 206 name string 207 } 208 209 func (dcocv *DynamicConsortiumOrgConfigValue) VariablyOpaqueFieldProto(name string) (proto.Message, error) { 210 if name != dcocv.VariablyOpaqueFields()[0] { 211 return nil, fmt.Errorf("not a marshaled field: %s", name) 212 } 213 switch dcocv.name { 214 case "MSP": 215 return &msp.MSPConfig{}, nil 216 default: 217 return nil, fmt.Errorf("unknown Consortium Org ConfigValue name: %s", dcocv.name) 218 } 219 }