Skip to content

Commit c5d05d7

Browse files
author
Jason Yellick
committed
[FAB-1699] Add chain config item utility methods
https://jira.hyperledger.org/browse/FAB-1699 Add helpers for encoding configuration items for the new chain config types. Change-Id: Ie943b0c48691a875c36a47bbf441f33064553371 Signed-off-by: Jason Yellick <[email protected]>
1 parent aa8e51b commit c5d05d7

File tree

2 files changed

+95
-49
lines changed

2 files changed

+95
-49
lines changed

common/chainconfig/chainconfig_test.go

+22-49
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"testing"
2222

2323
cb "github.com/hyperledger/fabric/protos/common"
24-
"github.com/hyperledger/fabric/protos/utils"
2524

2625
logging "github.com/op/go-logging"
2726
)
@@ -30,6 +29,14 @@ func init() {
3029
logging.SetLevel(logging.DEBUG, "")
3130
}
3231

32+
func makeInvalidConfigItem(key string) *cb.ConfigurationItem {
33+
return &cb.ConfigurationItem{
34+
Type: cb.ConfigurationItem_Chain,
35+
Key: key,
36+
Value: []byte("Garbage Data"),
37+
}
38+
}
39+
3340
func TestDoubleBegin(t *testing.T) {
3441
defer func() {
3542
if err := recover(); err == nil {
@@ -63,22 +70,10 @@ func TestRollback(t *testing.T) {
6370
}
6471

6572
func TestHashingAlgorithm(t *testing.T) {
66-
invalidMessage :=
67-
&cb.ConfigurationItem{
68-
Type: cb.ConfigurationItem_Chain,
69-
Key: HashingAlgorithmKey,
70-
Value: []byte("Garbage Data"),
71-
}
72-
invalidAlgorithm := &cb.ConfigurationItem{
73-
Type: cb.ConfigurationItem_Chain,
74-
Key: HashingAlgorithmKey,
75-
Value: utils.MarshalOrPanic(&cb.HashingAlgorithm{Name: "MD5"}),
76-
}
77-
validAlgorithm := &cb.ConfigurationItem{
78-
Type: cb.ConfigurationItem_Chain,
79-
Key: HashingAlgorithmKey,
80-
Value: utils.MarshalOrPanic(&cb.HashingAlgorithm{Name: SHA3Shake256}),
81-
}
73+
invalidMessage := makeInvalidConfigItem(HashingAlgorithmKey)
74+
invalidAlgorithm := TemplateHashingAlgorithm("MD5")
75+
validAlgorithm := DefaultHashingAlgorithm()
76+
8277
m := NewDescriptorImpl()
8378
m.BeginConfig()
8479

@@ -105,23 +100,10 @@ func TestHashingAlgorithm(t *testing.T) {
105100
}
106101

107102
func TestBlockDataHashingStructure(t *testing.T) {
108-
expectedWidth := uint32(7)
109-
invalidMessage :=
110-
&cb.ConfigurationItem{
111-
Type: cb.ConfigurationItem_Chain,
112-
Key: BlockDataHashingStructureKey,
113-
Value: []byte("Garbage Data"),
114-
}
115-
invalidWidth := &cb.ConfigurationItem{
116-
Type: cb.ConfigurationItem_Chain,
117-
Key: BlockDataHashingStructureKey,
118-
Value: utils.MarshalOrPanic(&cb.BlockDataHashingStructure{Width: 0}),
119-
}
120-
validWidth := &cb.ConfigurationItem{
121-
Type: cb.ConfigurationItem_Chain,
122-
Key: BlockDataHashingStructureKey,
123-
Value: utils.MarshalOrPanic(&cb.BlockDataHashingStructure{Width: expectedWidth}),
124-
}
103+
invalidMessage := makeInvalidConfigItem(BlockDataHashingStructureKey)
104+
invalidWidth := TemplateBlockDataHashingStructure(0)
105+
validWidth := DefaultBlockDataHashingStructure()
106+
125107
m := NewDescriptorImpl()
126108
m.BeginConfig()
127109

@@ -142,23 +124,14 @@ func TestBlockDataHashingStructure(t *testing.T) {
142124

143125
m.CommitConfig()
144126

145-
if newWidth := m.BlockDataHashingStructureWidth(); newWidth != expectedWidth {
146-
t.Fatalf("Unexpected width, got %d expected %d", newWidth, expectedWidth)
127+
if newWidth := m.BlockDataHashingStructureWidth(); newWidth != defaultBlockDataHashingStructureWidth {
128+
t.Fatalf("Unexpected width, got %d expected %d", newWidth, defaultBlockDataHashingStructureWidth)
147129
}
148130
}
149131

150132
func TestOrdererAddresses(t *testing.T) {
151-
expectedResult := []string{"foo", "bar:1234"}
152-
invalidMessage := &cb.ConfigurationItem{
153-
Type: cb.ConfigurationItem_Chain,
154-
Key: BlockDataHashingStructureKey,
155-
Value: []byte("Garbage Data"),
156-
}
157-
validMessage := &cb.ConfigurationItem{
158-
Type: cb.ConfigurationItem_Chain,
159-
Key: OrdererAddressesKey,
160-
Value: utils.MarshalOrPanic(&cb.OrdererAddresses{Addresses: expectedResult}),
161-
}
133+
invalidMessage := makeInvalidConfigItem(OrdererAddressesKey)
134+
validMessage := DefaultOrdererAddresses()
162135
m := NewDescriptorImpl()
163136
m.BeginConfig()
164137

@@ -174,7 +147,7 @@ func TestOrdererAddresses(t *testing.T) {
174147

175148
m.CommitConfig()
176149

177-
if newAddrs := m.OrdererAddresses(); !reflect.DeepEqual(newAddrs, expectedResult) {
178-
t.Fatalf("Unexpected width, got %s expected %s", newAddrs, expectedResult)
150+
if newAddrs := m.OrdererAddresses(); !reflect.DeepEqual(newAddrs, defaultOrdererAddresses) {
151+
t.Fatalf("Unexpected width, got %s expected %s", newAddrs, defaultOrdererAddresses)
179152
}
180153
}
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package chainconfig
18+
19+
import (
20+
"math"
21+
22+
cb "github.com/hyperledger/fabric/protos/common"
23+
"github.com/hyperledger/fabric/protos/utils"
24+
)
25+
26+
const defaultHashingAlgorithm = SHA3Shake256
27+
28+
// TemplateHashingAlgorithm creates a headerless configuration item representing the hashing algorithm
29+
func TemplateHashingAlgorithm(name string) *cb.ConfigurationItem {
30+
return &cb.ConfigurationItem{
31+
Type: cb.ConfigurationItem_Chain,
32+
Key: HashingAlgorithmKey,
33+
Value: utils.MarshalOrPanic(&cb.HashingAlgorithm{Name: name}),
34+
}
35+
36+
}
37+
38+
// DefaultHashingAlgorithm creates a headerless configuration item for the default hashing algorithm
39+
func DefaultHashingAlgorithm() *cb.ConfigurationItem {
40+
return TemplateHashingAlgorithm(defaultHashingAlgorithm)
41+
}
42+
43+
const defaultBlockDataHashingStructureWidth = math.MaxUint32
44+
45+
// TemplateBlockDataHashingStructure creates a headerless configuration item representing the block data hashing structure
46+
func TemplateBlockDataHashingStructure(width uint32) *cb.ConfigurationItem {
47+
return &cb.ConfigurationItem{
48+
Type: cb.ConfigurationItem_Chain,
49+
Key: BlockDataHashingStructureKey,
50+
Value: utils.MarshalOrPanic(&cb.BlockDataHashingStructure{Width: width}),
51+
}
52+
}
53+
54+
// DefaultBlockDatahashingStructure creates a headerless configuration item for the default block data hashing structure
55+
func DefaultBlockDataHashingStructure() *cb.ConfigurationItem {
56+
return TemplateBlockDataHashingStructure(defaultBlockDataHashingStructureWidth)
57+
}
58+
59+
var defaultOrdererAddresses = []string{"127.0.0.1:7050"}
60+
61+
// TemplateOrdererAddressess creates a headerless configuration item representing the orderer addresses
62+
func TemplateOrdererAddresses(addresses []string) *cb.ConfigurationItem {
63+
return &cb.ConfigurationItem{
64+
Type: cb.ConfigurationItem_Chain,
65+
Key: OrdererAddressesKey,
66+
Value: utils.MarshalOrPanic(&cb.OrdererAddresses{Addresses: addresses}),
67+
}
68+
}
69+
70+
// DefaultOrdererAddresses creates a headerless configuration item for the default orderer addresses
71+
func DefaultOrdererAddresses() *cb.ConfigurationItem {
72+
return TemplateOrdererAddresses(defaultOrdererAddresses)
73+
}

0 commit comments

Comments
 (0)