diff --git a/common/chainconfig/chainconfig_test.go b/common/chainconfig/chainconfig_test.go index c25d75236df..8a0a2bf6651 100644 --- a/common/chainconfig/chainconfig_test.go +++ b/common/chainconfig/chainconfig_test.go @@ -21,7 +21,6 @@ import ( "testing" cb "github.com/hyperledger/fabric/protos/common" - "github.com/hyperledger/fabric/protos/utils" logging "github.com/op/go-logging" ) @@ -30,6 +29,14 @@ func init() { logging.SetLevel(logging.DEBUG, "") } +func makeInvalidConfigItem(key string) *cb.ConfigurationItem { + return &cb.ConfigurationItem{ + Type: cb.ConfigurationItem_Chain, + Key: key, + Value: []byte("Garbage Data"), + } +} + func TestDoubleBegin(t *testing.T) { defer func() { if err := recover(); err == nil { @@ -63,22 +70,10 @@ func TestRollback(t *testing.T) { } func TestHashingAlgorithm(t *testing.T) { - invalidMessage := - &cb.ConfigurationItem{ - Type: cb.ConfigurationItem_Chain, - Key: HashingAlgorithmKey, - Value: []byte("Garbage Data"), - } - invalidAlgorithm := &cb.ConfigurationItem{ - Type: cb.ConfigurationItem_Chain, - Key: HashingAlgorithmKey, - Value: utils.MarshalOrPanic(&cb.HashingAlgorithm{Name: "MD5"}), - } - validAlgorithm := &cb.ConfigurationItem{ - Type: cb.ConfigurationItem_Chain, - Key: HashingAlgorithmKey, - Value: utils.MarshalOrPanic(&cb.HashingAlgorithm{Name: SHA3Shake256}), - } + invalidMessage := makeInvalidConfigItem(HashingAlgorithmKey) + invalidAlgorithm := TemplateHashingAlgorithm("MD5") + validAlgorithm := DefaultHashingAlgorithm() + m := NewDescriptorImpl() m.BeginConfig() @@ -105,23 +100,10 @@ func TestHashingAlgorithm(t *testing.T) { } func TestBlockDataHashingStructure(t *testing.T) { - expectedWidth := uint32(7) - invalidMessage := - &cb.ConfigurationItem{ - Type: cb.ConfigurationItem_Chain, - Key: BlockDataHashingStructureKey, - Value: []byte("Garbage Data"), - } - invalidWidth := &cb.ConfigurationItem{ - Type: cb.ConfigurationItem_Chain, - Key: BlockDataHashingStructureKey, - Value: utils.MarshalOrPanic(&cb.BlockDataHashingStructure{Width: 0}), - } - validWidth := &cb.ConfigurationItem{ - Type: cb.ConfigurationItem_Chain, - Key: BlockDataHashingStructureKey, - Value: utils.MarshalOrPanic(&cb.BlockDataHashingStructure{Width: expectedWidth}), - } + invalidMessage := makeInvalidConfigItem(BlockDataHashingStructureKey) + invalidWidth := TemplateBlockDataHashingStructure(0) + validWidth := DefaultBlockDataHashingStructure() + m := NewDescriptorImpl() m.BeginConfig() @@ -142,23 +124,14 @@ func TestBlockDataHashingStructure(t *testing.T) { m.CommitConfig() - if newWidth := m.BlockDataHashingStructureWidth(); newWidth != expectedWidth { - t.Fatalf("Unexpected width, got %d expected %d", newWidth, expectedWidth) + if newWidth := m.BlockDataHashingStructureWidth(); newWidth != defaultBlockDataHashingStructureWidth { + t.Fatalf("Unexpected width, got %d expected %d", newWidth, defaultBlockDataHashingStructureWidth) } } func TestOrdererAddresses(t *testing.T) { - expectedResult := []string{"foo", "bar:1234"} - invalidMessage := &cb.ConfigurationItem{ - Type: cb.ConfigurationItem_Chain, - Key: BlockDataHashingStructureKey, - Value: []byte("Garbage Data"), - } - validMessage := &cb.ConfigurationItem{ - Type: cb.ConfigurationItem_Chain, - Key: OrdererAddressesKey, - Value: utils.MarshalOrPanic(&cb.OrdererAddresses{Addresses: expectedResult}), - } + invalidMessage := makeInvalidConfigItem(OrdererAddressesKey) + validMessage := DefaultOrdererAddresses() m := NewDescriptorImpl() m.BeginConfig() @@ -174,7 +147,7 @@ func TestOrdererAddresses(t *testing.T) { m.CommitConfig() - if newAddrs := m.OrdererAddresses(); !reflect.DeepEqual(newAddrs, expectedResult) { - t.Fatalf("Unexpected width, got %s expected %s", newAddrs, expectedResult) + if newAddrs := m.OrdererAddresses(); !reflect.DeepEqual(newAddrs, defaultOrdererAddresses) { + t.Fatalf("Unexpected width, got %s expected %s", newAddrs, defaultOrdererAddresses) } } diff --git a/common/chainconfig/chainconfig_util.go b/common/chainconfig/chainconfig_util.go new file mode 100644 index 00000000000..029349cb7a3 --- /dev/null +++ b/common/chainconfig/chainconfig_util.go @@ -0,0 +1,73 @@ +/* +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 chainconfig + +import ( + "math" + + cb "github.com/hyperledger/fabric/protos/common" + "github.com/hyperledger/fabric/protos/utils" +) + +const defaultHashingAlgorithm = SHA3Shake256 + +// TemplateHashingAlgorithm creates a headerless configuration item representing the hashing algorithm +func TemplateHashingAlgorithm(name string) *cb.ConfigurationItem { + return &cb.ConfigurationItem{ + Type: cb.ConfigurationItem_Chain, + Key: HashingAlgorithmKey, + Value: utils.MarshalOrPanic(&cb.HashingAlgorithm{Name: name}), + } + +} + +// DefaultHashingAlgorithm creates a headerless configuration item for the default hashing algorithm +func DefaultHashingAlgorithm() *cb.ConfigurationItem { + return TemplateHashingAlgorithm(defaultHashingAlgorithm) +} + +const defaultBlockDataHashingStructureWidth = math.MaxUint32 + +// TemplateBlockDataHashingStructure creates a headerless configuration item representing the block data hashing structure +func TemplateBlockDataHashingStructure(width uint32) *cb.ConfigurationItem { + return &cb.ConfigurationItem{ + Type: cb.ConfigurationItem_Chain, + Key: BlockDataHashingStructureKey, + Value: utils.MarshalOrPanic(&cb.BlockDataHashingStructure{Width: width}), + } +} + +// DefaultBlockDatahashingStructure creates a headerless configuration item for the default block data hashing structure +func DefaultBlockDataHashingStructure() *cb.ConfigurationItem { + return TemplateBlockDataHashingStructure(defaultBlockDataHashingStructureWidth) +} + +var defaultOrdererAddresses = []string{"127.0.0.1:7050"} + +// TemplateOrdererAddressess creates a headerless configuration item representing the orderer addresses +func TemplateOrdererAddresses(addresses []string) *cb.ConfigurationItem { + return &cb.ConfigurationItem{ + Type: cb.ConfigurationItem_Chain, + Key: OrdererAddressesKey, + Value: utils.MarshalOrPanic(&cb.OrdererAddresses{Addresses: addresses}), + } +} + +// DefaultOrdererAddresses creates a headerless configuration item for the default orderer addresses +func DefaultOrdererAddresses() *cb.ConfigurationItem { + return TemplateOrdererAddresses(defaultOrdererAddresses) +}