forked from hyperledger/fabric
/
provisional.go
250 lines (208 loc) · 8.65 KB
/
provisional.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package provisional
import (
"fmt"
"os"
"path/filepath"
"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/config"
configvaluesmsp "github.com/hyperledger/fabric/common/config/msp"
"github.com/hyperledger/fabric/common/configtx"
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
"github.com/hyperledger/fabric/common/genesis"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/orderer/common/bootstrap"
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"
pb "github.com/hyperledger/fabric/protos/peer"
logging "github.com/op/go-logging"
)
var logger = logging.MustGetLogger("common/configtx/tool/provisional")
// Generator can either create an orderer genesis block or config template
type Generator interface {
bootstrap.Helper
// ChannelTemplate returns a template which can be used to help initialize a channel
ChannelTemplate() configtx.Template
GenesisBlockForChannel(channelID string) *cb.Block
}
const (
// ConsensusTypeSolo identifies the solo consensus implementation.
ConsensusTypeSolo = "solo"
// ConsensusTypeKafka identifies the Kafka-based consensus implementation.
ConsensusTypeKafka = "kafka"
// ConsensusTypeSbft identifies the SBFT consensus implementation.
ConsensusTypeSbft = "sbft"
// TestChainID is the default value of ChainID. It is used by all testing
// networks. It it necessary to set and export this variable so that test
// clients can connect without being rejected for targetting a chain which
// does not exist.
TestChainID = "testchainid"
// AcceptAllPolicyKey is the key of the AcceptAllPolicy.
AcceptAllPolicyKey = "AcceptAllPolicy"
// BlockValidationPolicyKey
BlockValidationPolicyKey = "BlockValidation"
)
func resolveMSPDir(path string) string {
if path == "" || path[0] == os.PathSeparator {
return path
}
// Look for MSP dir first in current path, then in ORDERER_CFG_PATH, then PEER_CFG_PATH, and finally in GOPATH
searchPath := []string{
".",
os.Getenv("ORDERER_CFG_PATH"),
os.Getenv("PEER_CFG_PATH"),
}
for _, p := range filepath.SplitList(os.Getenv("GOPATH")) {
searchPath = append(searchPath, filepath.Join(p, "src/github.com/hyperledger/fabric/common/configtx/tool/"))
}
for _, baseDir := range searchPath {
logger.Infof("Checking for MSPDir at: %s", baseDir)
fqPath := filepath.Join(baseDir, path)
if _, err := os.Stat(fqPath); err != nil {
// The mspdir does not exist
continue
}
return fqPath
}
logger.Panicf("Unable to resolve a path for MSPDir: %s", path)
// Unreachable
return ""
}
// DefaultChainCreationPolicyNames is the default value of ChainCreatorsKey.
var DefaultChainCreationPolicyNames = []string{AcceptAllPolicyKey}
type bootstrapper struct {
channelGroups []*cb.ConfigGroup
ordererGroups []*cb.ConfigGroup
applicationGroups []*cb.ConfigGroup
ordererSystemChannelGroups []*cb.ConfigGroup
}
// New returns a new provisional bootstrap helper.
func New(conf *genesisconfig.Profile) Generator {
bs := &bootstrapper{
channelGroups: []*cb.ConfigGroup{
// Chain Config Types
config.DefaultHashingAlgorithm(),
config.DefaultBlockDataHashingStructure(),
config.TemplateOrdererAddresses(conf.Orderer.Addresses), // TODO, move to conf.Channel when it exists
// Default policies
policies.TemplateImplicitMetaAnyPolicy([]string{}, configvaluesmsp.ReadersPolicyKey),
policies.TemplateImplicitMetaAnyPolicy([]string{}, configvaluesmsp.WritersPolicyKey),
policies.TemplateImplicitMetaMajorityPolicy([]string{}, configvaluesmsp.AdminsPolicyKey),
// Temporary AcceptAllPolicy XXX, remove
cauthdsl.TemplatePolicy(AcceptAllPolicyKey, cauthdsl.AcceptAllPolicy),
},
}
if conf.Orderer != nil {
bs.ordererGroups = []*cb.ConfigGroup{
// Orderer Config Types
config.TemplateConsensusType(conf.Orderer.OrdererType),
config.TemplateBatchSize(&ab.BatchSize{
MaxMessageCount: conf.Orderer.BatchSize.MaxMessageCount,
AbsoluteMaxBytes: conf.Orderer.BatchSize.AbsoluteMaxBytes,
PreferredMaxBytes: conf.Orderer.BatchSize.PreferredMaxBytes,
}),
config.TemplateBatchTimeout(conf.Orderer.BatchTimeout.String()),
config.TemplateChannelRestrictions(conf.Orderer.MaxChannels),
// Initialize the default Reader/Writer/Admins orderer policies, as well as block validation policy
policies.TemplateImplicitMetaPolicyWithSubPolicy([]string{config.OrdererGroupKey}, BlockValidationPolicyKey, configvaluesmsp.WritersPolicyKey, cb.ImplicitMetaPolicy_ANY),
policies.TemplateImplicitMetaAnyPolicy([]string{config.OrdererGroupKey}, configvaluesmsp.ReadersPolicyKey),
policies.TemplateImplicitMetaAnyPolicy([]string{config.OrdererGroupKey}, configvaluesmsp.WritersPolicyKey),
policies.TemplateImplicitMetaMajorityPolicy([]string{config.OrdererGroupKey}, configvaluesmsp.AdminsPolicyKey),
}
for _, org := range conf.Orderer.Organizations {
mspConfig, err := msp.GetVerifyingMspConfig(resolveMSPDir(org.MSPDir), org.BCCSP, org.ID)
if err != nil {
logger.Panicf("Error loading MSP configuration for org %s: %s", org.Name, err)
}
bs.ordererGroups = append(bs.ordererGroups, configvaluesmsp.TemplateGroupMSP([]string{config.OrdererGroupKey, org.Name}, mspConfig))
}
switch conf.Orderer.OrdererType {
case ConsensusTypeSolo, ConsensusTypeSbft:
case ConsensusTypeKafka:
bs.ordererGroups = append(bs.ordererGroups, config.TemplateKafkaBrokers(conf.Orderer.Kafka.Brokers))
default:
panic(fmt.Errorf("Wrong consenter type value given: %s", conf.Orderer.OrdererType))
}
bs.ordererSystemChannelGroups = []*cb.ConfigGroup{
// Policies
config.TemplateChainCreationPolicyNames(DefaultChainCreationPolicyNames),
}
}
if conf.Application != nil {
bs.applicationGroups = []*cb.ConfigGroup{
// Initialize the default Reader/Writer/Admins application policies
policies.TemplateImplicitMetaAnyPolicy([]string{config.ApplicationGroupKey}, configvaluesmsp.ReadersPolicyKey),
policies.TemplateImplicitMetaAnyPolicy([]string{config.ApplicationGroupKey}, configvaluesmsp.WritersPolicyKey),
policies.TemplateImplicitMetaMajorityPolicy([]string{config.ApplicationGroupKey}, configvaluesmsp.AdminsPolicyKey),
}
for _, org := range conf.Application.Organizations {
mspConfig, err := msp.GetVerifyingMspConfig(resolveMSPDir(org.MSPDir), org.BCCSP, org.ID)
if err != nil {
logger.Panicf("Error loading MSP configuration for org %s: %s", org.Name, err)
}
bs.applicationGroups = append(bs.applicationGroups, configvaluesmsp.TemplateGroupMSP([]string{config.ApplicationGroupKey, org.Name}, mspConfig))
var anchorProtos []*pb.AnchorPeer
for _, anchorPeer := range org.AnchorPeers {
anchorProtos = append(anchorProtos, &pb.AnchorPeer{
Host: anchorPeer.Host,
Port: int32(anchorPeer.Port),
})
}
bs.applicationGroups = append(bs.applicationGroups, config.TemplateAnchorPeers(org.Name, anchorProtos))
}
}
return bs
}
func (bs *bootstrapper) ChannelTemplate() configtx.Template {
return configtx.NewModPolicySettingTemplate(
configvaluesmsp.AdminsPolicyKey,
configtx.NewCompositeTemplate(
configtx.NewSimpleTemplate(bs.channelGroups...),
configtx.NewSimpleTemplate(bs.ordererGroups...),
configtx.NewSimpleTemplate(bs.applicationGroups...),
),
)
}
// XXX deprecate and remove
func (bs *bootstrapper) GenesisBlock() *cb.Block {
block, err := genesis.NewFactoryImpl(
configtx.NewModPolicySettingTemplate(
configvaluesmsp.AdminsPolicyKey,
configtx.NewCompositeTemplate(
configtx.NewSimpleTemplate(bs.ordererSystemChannelGroups...),
bs.ChannelTemplate(),
),
),
).Block(TestChainID)
if err != nil {
panic(err)
}
return block
}
func (bs *bootstrapper) GenesisBlockForChannel(channelID string) *cb.Block {
block, err := genesis.NewFactoryImpl(
configtx.NewModPolicySettingTemplate(
configvaluesmsp.AdminsPolicyKey,
configtx.NewCompositeTemplate(
configtx.NewSimpleTemplate(bs.ordererSystemChannelGroups...),
bs.ChannelTemplate(),
),
),
).Block(channelID)
if err != nil {
panic(err)
}
return block
}