Skip to content

Commit 5fe1df7

Browse files
committed
[FAB-2295] Minor fixes to configtx template
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 <[email protected]>
1 parent a5714ce commit 5fe1df7

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

common/configtx/template.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,34 @@ import (
3030
)
3131

3232
const (
33+
// CreationPolicyKey defines the config key used in the channel
34+
// config, under which the creation policy is defined.
3335
CreationPolicyKey = "CreationPolicy"
3436
msgVersion = int32(0)
3537
epoch = 0
3638

39+
// ApplicationGroup identifies the `groups` map key in the channel
40+
// config, under which the application-related config group is set.
3741
ApplicationGroup = "Application"
38-
OrdererGroup = "Orderer"
39-
MSPKey = "MSP"
42+
// OrdererGroup identifies the `groups` map key in the channel
43+
// config, under which the orderer-related config group is set.
44+
OrdererGroup = "Orderer"
45+
// MSPKey identifies the config key in the channel config,
46+
// under which the application-related config group is set.
47+
MSPKey = "MSP"
4048
)
4149

4250
// Template can be used to faciliate creation of config transactions
4351
type Template interface {
44-
// Items returns a set of ConfigUpdateEnvelopes for the given chainID
52+
// Envelope returns a ConfigUpdateEnvelope for the given chainID
4553
Envelope(chainID string) (*cb.ConfigUpdateEnvelope, error)
4654
}
4755

4856
type simpleTemplate struct {
4957
configGroup *cb.ConfigGroup
5058
}
5159

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

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

86-
// NewSimpleTemplate creates a Template using the source Templates
94+
// NewCompositeTemplate creates a Template using the source Templates
8795
func NewCompositeTemplate(templates ...Template) Template {
8896
return &compositeTemplate{templates: templates}
8997
}
@@ -119,7 +127,7 @@ func copyGroup(source *cb.ConfigGroup, target *cb.ConfigGroup) error {
119127
return nil
120128
}
121129

122-
// Envelope returns the ConfigUpdateEnvelope for the given chainID
130+
// Envelope returns a ConfigUpdateEnvelope for the given chainID
123131
func (ct *compositeTemplate) Envelope(chainID string) (*cb.ConfigUpdateEnvelope, error) {
124132
channel := cb.NewConfigGroup()
125133

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

155-
// NewChainCreationTemplate takes a CreationPolicy and a Template to produce a Template which outputs an appropriately
156-
// constructed list of ConfigUpdateEnvelope. Note, using this Template in
157-
// a CompositeTemplate will invalidate the CreationPolicy
163+
// NewChainCreationTemplate takes a CreationPolicy and a Template to produce a
164+
// Template which outputs an appropriately constructed list of ConfigUpdateEnvelopes.
158165
func NewChainCreationTemplate(creationPolicy string, template Template) Template {
159166
result := cb.NewConfigGroup()
160167
result.Groups[configtxorderer.GroupKey] = cb.NewConfigGroup()

common/configtx/template_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import (
2020
"fmt"
2121
"testing"
2222

23+
"github.com/golang/protobuf/proto"
2324
configtxorderer "github.com/hyperledger/fabric/common/configtx/handlers/orderer"
2425
cb "github.com/hyperledger/fabric/protos/common"
26+
ab "github.com/hyperledger/fabric/protos/orderer"
2527

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

105-
_, ok := configNext.WriteSet.Groups[configtxorderer.GroupKey].Values[CreationPolicyKey]
107+
configValue, ok := configNext.WriteSet.Groups[configtxorderer.GroupKey].Values[CreationPolicyKey]
106108
assert.True(t, ok, "Did not find creation policy")
109+
110+
creationPolicyMessage := new(ab.CreationPolicy)
111+
if err := proto.Unmarshal(configValue.Value, creationPolicyMessage); err != nil {
112+
t.Fatal("Should not have errored:", err)
113+
}
114+
assert.Equal(t, creationPolicy, creationPolicyMessage.Policy, "Policy names don't match")
107115
}

0 commit comments

Comments
 (0)