From 3afbc13fc8c80ece076900113bbbbd15c3f0a1ef Mon Sep 17 00:00:00 2001 From: Jason Yellick Date: Thu, 9 Feb 2017 12:25:07 -0500 Subject: [PATCH] [FAB-2149] Change policies to use ConfigGroup https://jira.hyperledger.org/browse/FAB-2149 This CR migrates the policy framework off of ConfigItem and on to ConfigGroup (as ConfigItem is deprecated and pending removal). Change-Id: Ief9ed46422275e2c48f84700e275e3fd764aea6f Signed-off-by: Jason Yellick --- common/cauthdsl/policy_util.go | 12 +++--- common/configtx/template.go | 36 +++++++++++++++++- common/configtx/test/orderer.template | Bin 342 -> 278 bytes .../bootstrap/provisional/provisional.go | 16 +++++--- .../bootstrap/provisional/template_test.go | 34 ----------------- orderer/multichain/manager_test.go | 5 +-- .../broadcast_config/newchain.go | 5 +-- 7 files changed, 55 insertions(+), 53 deletions(-) delete mode 100644 orderer/common/bootstrap/provisional/template_test.go diff --git a/common/cauthdsl/policy_util.go b/common/cauthdsl/policy_util.go index 5a7a1bd2955..83ae1bb12c7 100644 --- a/common/cauthdsl/policy_util.go +++ b/common/cauthdsl/policy_util.go @@ -22,13 +22,13 @@ import ( ) // TemplatePolicy creates a headerless configuration item representing a policy for a given key -func TemplatePolicy(key string, sigPolicyEnv *cb.SignaturePolicyEnvelope) *cb.ConfigItem { - return &cb.ConfigItem{ - Type: cb.ConfigItem_POLICY, - Key: key, - Value: utils.MarshalOrPanic(&cb.Policy{ +func TemplatePolicy(key string, sigPolicyEnv *cb.SignaturePolicyEnvelope) *cb.ConfigGroup { + configGroup := cb.NewConfigGroup() + configGroup.Policies[key] = &cb.ConfigPolicy{ + Policy: &cb.Policy{ Type: int32(cb.Policy_SIGNATURE), Policy: utils.MarshalOrPanic(sigPolicyEnv), - }), + }, } + return configGroup } diff --git a/common/configtx/template.go b/common/configtx/template.go index 165c97ebecd..ac6f51f4d86 100644 --- a/common/configtx/template.go +++ b/common/configtx/template.go @@ -44,6 +44,40 @@ type Template interface { Envelope(chainID string) (*cb.ConfigEnvelope, error) } +type simpleTemplateNext struct { + configGroup *cb.ConfigGroup +} + +// NewSimpleTemplateNext creates a Template using the supplied ConfigGroup +func NewSimpleTemplateNext(configGroups ...*cb.ConfigGroup) Template { + sts := make([]Template, len(configGroups)) + for i, group := range configGroups { + sts[i] = &simpleTemplateNext{ + configGroup: group, + } + } + return NewCompositeTemplate(sts...) +} + +// Envelope returns a ConfigEnvelopes for the given chainID +func (st *simpleTemplateNext) Envelope(chainID string) (*cb.ConfigEnvelope, error) { + config, err := proto.Marshal(&cb.ConfigNext{ + Header: &cb.ChannelHeader{ + ChannelId: chainID, + Type: int32(cb.HeaderType_CONFIGURATION_ITEM), + }, + Channel: st.configGroup, + }) + + if err != nil { + return nil, err + } + + return &cb.ConfigEnvelope{ + Config: config, + }, nil +} + type simpleTemplate struct { items []*cb.ConfigItem } @@ -153,8 +187,6 @@ func copyGroup(source *cb.ConfigGroup, target *cb.ConfigGroup) error { // Items returns a set of ConfigEnvelopes for the given chainID, and errors only on marshaling errors func (ct *compositeTemplate) Envelope(chainID string) (*cb.ConfigEnvelope, error) { channel := cb.NewConfigGroup() - channel.Groups[ApplicationGroup] = cb.NewConfigGroup() - channel.Groups[OrdererGroup] = cb.NewConfigGroup() for i := range ct.templates { configEnv, err := ct.templates[i].Envelope(chainID) diff --git a/common/configtx/test/orderer.template b/common/configtx/test/orderer.template index 18f3f701439580ed9d6780a805434b853d25fdd5..9abd4fdef65da4c58093c12eb4bad67bd717dd2a 100644 GIT binary patch delta 7 Ocmcb{G>vJ47$X1+AObi5 delta 72 zcmbQnbd70)7^4H1qLQ#*YPoZMURq|lXGv;qKz>eUa-|j*2cr<15Q`8K2P2oX62D_| Ua%w?|V@?iKngb{eloVnB0LJqXg#Z8m diff --git a/orderer/common/bootstrap/provisional/provisional.go b/orderer/common/bootstrap/provisional/provisional.go index 83a6b58f034..66816d76983 100644 --- a/orderer/common/bootstrap/provisional/provisional.go +++ b/orderer/common/bootstrap/provisional/provisional.go @@ -34,8 +34,8 @@ import ( type Generator interface { bootstrap.Helper - // TemplateItems returns a set of config items which can be used to initialize a template - TemplateItems() []*cb.ConfigItem + // ChannelTemplate returns a template which can be used to help initialize a channel + ChannelTemplate() configtx.Template } const ( @@ -61,6 +61,7 @@ var DefaultChainCreationPolicyNames = []string{AcceptAllPolicyKey} type bootstrapper struct { minimalItems []*cb.ConfigItem + minimalGroups []*cb.ConfigGroup systemChainItems []*cb.ConfigItem } @@ -83,7 +84,9 @@ func New(conf *config.TopLevel) Generator { configtxorderer.TemplateBatchTimeout(conf.Genesis.BatchTimeout.String()), configtxorderer.TemplateIngressPolicyNames([]string{AcceptAllPolicyKey}), configtxorderer.TemplateEgressPolicyNames([]string{AcceptAllPolicyKey}), + }, + minimalGroups: []*cb.ConfigGroup{ // Policies cauthdsl.TemplatePolicy(configtx.NewConfigItemPolicyKey, cauthdsl.RejectAllPolicy), cauthdsl.TemplatePolicy(AcceptAllPolicyKey, cauthdsl.AcceptAllPolicy), @@ -105,15 +108,18 @@ func New(conf *config.TopLevel) Generator { return bs } -func (bs *bootstrapper) TemplateItems() []*cb.ConfigItem { - return bs.minimalItems +func (bs *bootstrapper) ChannelTemplate() configtx.Template { + return configtx.NewCompositeTemplate( + configtx.NewSimpleTemplate(bs.minimalItems...), + configtx.NewSimpleTemplateNext(bs.minimalGroups...), + ) } func (bs *bootstrapper) GenesisBlock() *cb.Block { block, err := genesis.NewFactoryImpl( configtx.NewCompositeTemplate( - configtx.NewSimpleTemplate(bs.minimalItems...), configtx.NewSimpleTemplate(bs.systemChainItems...), + bs.ChannelTemplate(), ), ).Block(TestChainID) diff --git a/orderer/common/bootstrap/provisional/template_test.go b/orderer/common/bootstrap/provisional/template_test.go deleted file mode 100644 index aab2c9259ad..00000000000 --- a/orderer/common/bootstrap/provisional/template_test.go +++ /dev/null @@ -1,34 +0,0 @@ -/* -Copyright IBM Corp. 2017 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 ( - "testing" - - configtxtest "github.com/hyperledger/fabric/common/configtx/test" - "github.com/hyperledger/fabric/orderer/localconfig" -) - -func TestUpdateTemplate(t *testing.T) { - conf := config.Load() - - generator := New(conf) - - templateItems := generator.TemplateItems() - - configtxtest.WriteTemplate(configtxtest.OrdererTemplateName, templateItems...) -} diff --git a/orderer/multichain/manager_test.go b/orderer/multichain/manager_test.go index 6da4dbc9e00..14c6f14fc6a 100644 --- a/orderer/multichain/manager_test.go +++ b/orderer/multichain/manager_test.go @@ -226,14 +226,13 @@ func TestNewChain(t *testing.T) { manager := NewManagerImpl(lf, consenters, &mockCryptoHelper{}) generator := provisional.New(conf) - items := generator.TemplateItems() - simpleTemplate := configtx.NewSimpleTemplate(items...) + channelTemplate := generator.ChannelTemplate() signer, err := msp.NewNoopMsp().GetDefaultSigningIdentity() assert.NoError(t, err) newChainID := "TestNewChain" - newChainMessage, err := configtx.MakeChainCreationTransaction(provisional.AcceptAllPolicyKey, newChainID, signer, simpleTemplate) + newChainMessage, err := configtx.MakeChainCreationTransaction(provisional.AcceptAllPolicyKey, newChainID, signer, channelTemplate) if err != nil { t.Fatalf("Error producing config transaction: %s", err) } diff --git a/orderer/sample_clients/broadcast_config/newchain.go b/orderer/sample_clients/broadcast_config/newchain.go index 53214f8563a..b40004f4b37 100644 --- a/orderer/sample_clients/broadcast_config/newchain.go +++ b/orderer/sample_clients/broadcast_config/newchain.go @@ -26,15 +26,14 @@ import ( func newChainRequest(consensusType, creationPolicy, newChannelId string) *cb.Envelope { conf.Genesis.OrdererType = consensusType generator := provisional.New(conf) - items := generator.TemplateItems() - simpleTemplate := configtx.NewSimpleTemplate(items...) + channelTemplate := generator.ChannelTemplate() signer, err := msp.NewNoopMsp().GetDefaultSigningIdentity() if err != nil { panic(err) } - env, err := configtx.MakeChainCreationTransaction(creationPolicy, newChannelId, signer, simpleTemplate) + env, err := configtx.MakeChainCreationTransaction(creationPolicy, newChannelId, signer, channelTemplate) if err != nil { panic(err) }