Skip to content

Commit

Permalink
[FAB-2149] Change policies to use ConfigGroup
Browse files Browse the repository at this point in the history
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 <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Feb 12, 2017
1 parent a052b61 commit 3afbc13
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 53 deletions.
12 changes: 6 additions & 6 deletions common/cauthdsl/policy_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
36 changes: 34 additions & 2 deletions common/configtx/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
Binary file modified common/configtx/test/orderer.template
Binary file not shown.
16 changes: 11 additions & 5 deletions orderer/common/bootstrap/provisional/provisional.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -61,6 +61,7 @@ var DefaultChainCreationPolicyNames = []string{AcceptAllPolicyKey}

type bootstrapper struct {
minimalItems []*cb.ConfigItem
minimalGroups []*cb.ConfigGroup
systemChainItems []*cb.ConfigItem
}

Expand All @@ -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),
Expand All @@ -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)

Expand Down
34 changes: 0 additions & 34 deletions orderer/common/bootstrap/provisional/template_test.go

This file was deleted.

5 changes: 2 additions & 3 deletions orderer/multichain/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions orderer/sample_clients/broadcast_config/newchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 3afbc13

Please sign in to comment.