Skip to content

Commit

Permalink
[FAB-1699] Add chain config item utility methods
Browse files Browse the repository at this point in the history
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 <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Jan 19, 2017
1 parent aa8e51b commit c5d05d7
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 49 deletions.
71 changes: 22 additions & 49 deletions common/chainconfig/chainconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down Expand Up @@ -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()

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

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

Expand All @@ -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)
}
}
73 changes: 73 additions & 0 deletions common/chainconfig/chainconfig_util.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit c5d05d7

Please sign in to comment.