github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/orderer/configtx/configtx.go (about) 1 /* 2 * Copyright contributors to the Hyperledger Fabric Operator project 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at: 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package configtx 20 21 import ( 22 "path/filepath" 23 "time" 24 25 "github.com/hyperledger/fabric-protos-go/orderer/etcdraft" 26 "github.com/hyperledger/fabric/common/viperutil" 27 "github.com/pkg/errors" 28 "github.com/spf13/viper" 29 ) 30 31 // +k8s:openapi-gen=true 32 func GetGenesisDefaults() *TopLevel { 33 return &TopLevel{ 34 Profiles: map[string]*Profile{ 35 "Initial": &Profile{ 36 Orderer: &Orderer{ 37 Organizations: []*Organization{}, 38 OrdererType: "etcdraft", 39 Addresses: []string{}, 40 BatchTimeout: 2 * time.Second, 41 BatchSize: BatchSize{ 42 MaxMessageCount: 500, 43 AbsoluteMaxBytes: 10 * 1024 * 1024, 44 PreferredMaxBytes: 2 * 1024 * 1024, 45 }, 46 EtcdRaft: &etcdraft.ConfigMetadata{ 47 Consenters: []*etcdraft.Consenter{}, 48 Options: &etcdraft.Options{ 49 TickInterval: "500ms", 50 ElectionTick: 10, 51 HeartbeatTick: 1, 52 MaxInflightBlocks: 5, 53 SnapshotIntervalSize: 20 * 1024 * 1024, // 20 MB 54 }, 55 }, 56 Capabilities: map[string]bool{ 57 "V1_4_2": true, 58 }, 59 Policies: map[string]*Policy{ 60 "Readers": &Policy{ 61 Type: "ImplicitMeta", 62 Rule: "ANY Readers", 63 }, 64 "Writers": &Policy{ 65 Type: "ImplicitMeta", 66 Rule: "ANY Writers", 67 }, 68 "Admins": &Policy{ 69 Type: "ImplicitMeta", 70 Rule: "ANY Admins", 71 }, 72 "BlockValidation": &Policy{ 73 Type: "ImplicitMeta", 74 Rule: "ANY Writers", 75 }, 76 }, 77 }, 78 79 Consortiums: map[string]*Consortium{ 80 "SampleConsortium": &Consortium{}, 81 }, 82 Capabilities: map[string]bool{ 83 "V1_4_3": true, 84 }, 85 Policies: map[string]*Policy{ 86 "Readers": &Policy{ 87 Type: "ImplicitMeta", 88 Rule: "ANY Readers", 89 }, 90 "Writers": &Policy{ 91 Type: "ImplicitMeta", 92 Rule: "ANY Writers", 93 }, 94 "Admins": &Policy{ 95 Type: "ImplicitMeta", 96 Rule: "MAJORITY Admins", 97 }, 98 }, 99 }, 100 }, 101 } 102 } 103 104 func LoadTopLevelConfig(configFile string) (*TopLevel, error) { 105 config := viper.New() 106 configDir, err := filepath.Abs(filepath.Dir(configFile)) 107 if err != nil { 108 return nil, errors.Wrap(err, "error getting absolute path") 109 } 110 config.AddConfigPath(configDir) 111 config.SetConfigName("configtx") 112 113 err = config.ReadInConfig() 114 if err != nil { 115 return nil, errors.Wrap(err, "error reading configuration") 116 } 117 118 var uconf TopLevel 119 err = viperutil.EnhancedExactUnmarshal(config, &uconf) 120 if err != nil { 121 return nil, errors.Wrap(err, "error unmarshaling config into struct") 122 } 123 124 return &uconf, nil 125 } 126 127 type ConfigTx struct { 128 Config *TopLevel 129 } 130 131 func New() *ConfigTx { 132 c := &ConfigTx{ 133 Config: GetGenesisDefaults(), 134 } 135 136 return c 137 } 138 139 func (c *ConfigTx) GetProfile(name string) (*Profile, error) { 140 p, found := c.Config.Profiles[name] 141 if !found { 142 return nil, errors.Errorf("profile '%s' does not exist", name) 143 } 144 145 err := c.CompleteProfileInitialization(p) 146 if err != nil { 147 return nil, err 148 } 149 150 return p, nil 151 } 152 153 func (c *ConfigTx) CompleteProfileInitialization(p *Profile) error { 154 if p.Orderer != nil { 155 return c.CompleteOrdererInitialization(p.Orderer) 156 } 157 158 return nil 159 } 160 161 func (c *ConfigTx) CompleteOrdererInitialization(ord *Orderer) error { 162 // Additional, consensus type-dependent initialization goes here 163 // Also using this to panic on unknown orderer type. 164 switch ord.OrdererType { 165 case ConsensusTypeSolo: 166 // nothing to be done here 167 case ConsensusTypeKafka: 168 // nothing to be done here 169 case ConsensusTypeEtcdRaft: 170 if _, err := time.ParseDuration(ord.EtcdRaft.Options.TickInterval); err != nil { 171 return errors.Errorf("Etcdraft TickInterval (%s) must be in time duration format", ord.EtcdRaft.Options.TickInterval) 172 } 173 174 // validate the specified members for Options 175 if ord.EtcdRaft.Options.ElectionTick <= ord.EtcdRaft.Options.HeartbeatTick { 176 return errors.Errorf("election tick must be greater than heartbeat tick") 177 } 178 179 for _, c := range ord.EtcdRaft.GetConsenters() { 180 if c.Host == "" { 181 return errors.Errorf("consenter info in %s configuration did not specify host", ConsensusTypeEtcdRaft) 182 } 183 if c.Port == 0 { 184 return errors.Errorf("consenter info in %s configuration did not specify port", ConsensusTypeEtcdRaft) 185 } 186 if c.ClientTlsCert == nil { 187 return errors.Errorf("consenter info in %s configuration did not specify client TLS cert", ConsensusTypeEtcdRaft) 188 } 189 if c.ServerTlsCert == nil { 190 return errors.Errorf("consenter info in %s configuration did not specify server TLS cert", ConsensusTypeEtcdRaft) 191 } 192 } 193 default: 194 return errors.Errorf("unknown orderer type: %s", ord.OrdererType) 195 } 196 197 return nil 198 }