Skip to content

Commit

Permalink
[FAB-2295] Minor fixes to configtx template
Browse files Browse the repository at this point in the history
1. Remove confusing comment on `NewChainCreationTemplate`.
2. Add comments to exported variables.
3. Extend test so that it checks policy name

Change-Id: Ica061bfcf82662f223ebdaa1dbe62423bc819dbe
Signed-off-by: Kostas Christidis <kostas@christidis.io>
  • Loading branch information
kchristidis committed Feb 16, 2017
1 parent a5714ce commit 5fe1df7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
27 changes: 17 additions & 10 deletions common/configtx/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,34 @@ import (
)

const (
// CreationPolicyKey defines the config key used in the channel
// config, under which the creation policy is defined.
CreationPolicyKey = "CreationPolicy"
msgVersion = int32(0)
epoch = 0

// ApplicationGroup identifies the `groups` map key in the channel
// config, under which the application-related config group is set.
ApplicationGroup = "Application"
OrdererGroup = "Orderer"
MSPKey = "MSP"
// OrdererGroup identifies the `groups` map key in the channel
// config, under which the orderer-related config group is set.
OrdererGroup = "Orderer"
// MSPKey identifies the config key in the channel config,
// under which the application-related config group is set.
MSPKey = "MSP"
)

// Template can be used to faciliate creation of config transactions
type Template interface {
// Items returns a set of ConfigUpdateEnvelopes for the given chainID
// Envelope returns a ConfigUpdateEnvelope for the given chainID
Envelope(chainID string) (*cb.ConfigUpdateEnvelope, error)
}

type simpleTemplate struct {
configGroup *cb.ConfigGroup
}

// NewSimpleTemplate creates a Template using the supplied ConfigGroup
// NewSimpleTemplate creates a Template using the supplied ConfigGroups
func NewSimpleTemplate(configGroups ...*cb.ConfigGroup) Template {
sts := make([]Template, len(configGroups))
for i, group := range configGroups {
Expand All @@ -60,7 +68,7 @@ func NewSimpleTemplate(configGroups ...*cb.ConfigGroup) Template {
return NewCompositeTemplate(sts...)
}

// Envelope returns a ConfigUpdateEnvelopes for the given chainID
// Envelope returns a ConfigUpdateEnvelope for the given chainID
func (st *simpleTemplate) Envelope(chainID string) (*cb.ConfigUpdateEnvelope, error) {
config, err := proto.Marshal(&cb.ConfigUpdate{
Header: &cb.ChannelHeader{
Expand All @@ -83,7 +91,7 @@ type compositeTemplate struct {
templates []Template
}

// NewSimpleTemplate creates a Template using the source Templates
// NewCompositeTemplate creates a Template using the source Templates
func NewCompositeTemplate(templates ...Template) Template {
return &compositeTemplate{templates: templates}
}
Expand Down Expand Up @@ -119,7 +127,7 @@ func copyGroup(source *cb.ConfigGroup, target *cb.ConfigGroup) error {
return nil
}

// Envelope returns the ConfigUpdateEnvelope for the given chainID
// Envelope returns a ConfigUpdateEnvelope for the given chainID
func (ct *compositeTemplate) Envelope(chainID string) (*cb.ConfigUpdateEnvelope, error) {
channel := cb.NewConfigGroup()

Expand Down Expand Up @@ -152,9 +160,8 @@ func (ct *compositeTemplate) Envelope(chainID string) (*cb.ConfigUpdateEnvelope,
return &cb.ConfigUpdateEnvelope{ConfigUpdate: marshaledConfig}, nil
}

// NewChainCreationTemplate takes a CreationPolicy and a Template to produce a Template which outputs an appropriately
// constructed list of ConfigUpdateEnvelope. Note, using this Template in
// a CompositeTemplate will invalidate the CreationPolicy
// NewChainCreationTemplate takes a CreationPolicy and a Template to produce a
// Template which outputs an appropriately constructed list of ConfigUpdateEnvelopes.
func NewChainCreationTemplate(creationPolicy string, template Template) Template {
result := cb.NewConfigGroup()
result.Groups[configtxorderer.GroupKey] = cb.NewConfigGroup()
Expand Down
10 changes: 9 additions & 1 deletion common/configtx/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"fmt"
"testing"

"github.com/golang/protobuf/proto"
configtxorderer "github.com/hyperledger/fabric/common/configtx/handlers/orderer"
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -102,6 +104,12 @@ func TestNewChainTemplate(t *testing.T) {
assert.True(t, ok, "Expected to find %d but did not", i)
}

_, ok := configNext.WriteSet.Groups[configtxorderer.GroupKey].Values[CreationPolicyKey]
configValue, ok := configNext.WriteSet.Groups[configtxorderer.GroupKey].Values[CreationPolicyKey]
assert.True(t, ok, "Did not find creation policy")

creationPolicyMessage := new(ab.CreationPolicy)
if err := proto.Unmarshal(configValue.Value, creationPolicyMessage); err != nil {
t.Fatal("Should not have errored:", err)
}
assert.Equal(t, creationPolicy, creationPolicyMessage.Policy, "Policy names don't match")
}

0 comments on commit 5fe1df7

Please sign in to comment.