Skip to content

Commit

Permalink
[FAB-2145] Migrate configtx.Handler to ConfigValue
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-2145

The ConfigItem proto is deprecated, as a step in removing it, the
configtx.Handler interface needs to migrate to the new structure
ConfigValue.  This CR does that.

Change-Id: I36f544913127421c99f955bcb721a2a581c0cc59
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Feb 12, 2017
1 parent 79aa652 commit a052b61
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 226 deletions.
4 changes: 1 addition & 3 deletions common/cauthdsl/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ func makePolicySource(policyResult bool) []byte {

func addPolicy(manager *policies.ManagerImpl, id string, policy []byte) {
manager.BeginConfig()
err := manager.ProposeConfig(&cb.ConfigItem{
Type: cb.ConfigItem_POLICY,
Key: id,
err := manager.ProposeConfig(id, &cb.ConfigValue{
Value: policy,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion common/configtx/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type OrdererConfig interface {
// Handler provides a hook which allows other pieces of code to participate in config proposals
type Handler interface {
// ProposeConfig called when config is added to a proposal
ProposeConfig(configItem *cb.ConfigItem) error
ProposeConfig(key string, configValue *cb.ConfigValue) error
}

// Manager provides a mechanism to query and update config
Expand Down
68 changes: 0 additions & 68 deletions common/configtx/bytes_handler.go

This file was deleted.

12 changes: 6 additions & 6 deletions common/configtx/handlers/application/sharedconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,19 @@ func (di *SharedConfigImpl) CommitConfig() {
}

// ProposeConfig is used to add new config to the config proposal
func (di *SharedConfigImpl) ProposeConfig(configItem *cb.ConfigItem) error {
switch configItem.Key {
func (di *SharedConfigImpl) ProposeConfig(key string, configValue *cb.ConfigValue) error {
switch key {
case AnchorPeersKey:
anchorPeers := &pb.AnchorPeers{}
if err := proto.Unmarshal(configItem.Value, anchorPeers); err != nil {
return fmt.Errorf("Unmarshaling error for %s: %s", configItem.Key, err)
if err := proto.Unmarshal(configValue.Value, anchorPeers); err != nil {
return fmt.Errorf("Unmarshaling error for %s: %s", key, err)
}
if logger.IsEnabledFor(logging.DEBUG) {
logger.Debugf("Setting %s to %v", configItem.Key, anchorPeers.AnchorPeers)
logger.Debugf("Setting %s to %v", key, anchorPeers.AnchorPeers)
}
di.pendingConfig.anchorPeers = anchorPeers.AnchorPeers
default:
logger.Warningf("Uknown Peer config item with key %s", configItem.Key)
logger.Warningf("Uknown Peer config item with key %s", key)
}
return nil
}
Expand Down
16 changes: 9 additions & 7 deletions common/configtx/handlers/application/sharedconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ func init() {
logging.SetLevel(logging.DEBUG, "")
}

func makeInvalidConfigItem(key string) *cb.ConfigItem {
return &cb.ConfigItem{
Type: cb.ConfigItem_PEER,
Key: key,
func makeInvalidConfigValue() *cb.ConfigValue {
return &cb.ConfigValue{
Value: []byte("Garbage Data"),
}
}

func itemToValue(configItem *cb.ConfigItem) (string, *cb.ConfigValue) {
return configItem.Key, &cb.ConfigValue{Value: configItem.Value}
}

func TestInterface(t *testing.T) {
_ = configtxapi.ApplicationConfig(NewSharedConfigImpl(nil))
}
Expand Down Expand Up @@ -80,17 +82,17 @@ func TestAnchorPeers(t *testing.T) {
&pb.AnchorPeer{Host: "foo", Port: 234, Cert: []byte("foocert")},
&pb.AnchorPeer{Host: "bar", Port: 237, Cert: []byte("barcert")},
}
invalidMessage := makeInvalidConfigItem(AnchorPeersKey)
invalidMessage := makeInvalidConfigValue()
validMessage := TemplateAnchorPeers(endVal)
m := NewSharedConfigImpl(nil)
m.BeginConfig()

err := m.ProposeConfig(invalidMessage)
err := m.ProposeConfig(AnchorPeersKey, invalidMessage)
if err == nil {
t.Fatalf("Should have failed on invalid message")
}

err = m.ProposeConfig(validMessage)
err = m.ProposeConfig(itemToValue(validMessage))
if err != nil {
t.Fatalf("Error applying valid config: %s", err)
}
Expand Down
12 changes: 6 additions & 6 deletions common/configtx/handlers/channel/sharedconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ func (pm *SharedConfigImpl) CommitConfig() {
}

// ProposeConfig is used to add new config to the config proposal
func (pm *SharedConfigImpl) ProposeConfig(configItem *cb.ConfigItem) error {
switch configItem.Key {
func (pm *SharedConfigImpl) ProposeConfig(key string, configValue *cb.ConfigValue) error {
switch key {
case HashingAlgorithmKey:
hashingAlgorithm := &cb.HashingAlgorithm{}
if err := proto.Unmarshal(configItem.Value, hashingAlgorithm); err != nil {
if err := proto.Unmarshal(configValue.Value, hashingAlgorithm); err != nil {
return fmt.Errorf("Unmarshaling error for HashingAlgorithm: %s", err)
}
switch hashingAlgorithm.Name {
Expand All @@ -151,7 +151,7 @@ func (pm *SharedConfigImpl) ProposeConfig(configItem *cb.ConfigItem) error {
}
case BlockDataHashingStructureKey:
blockDataHashingStructure := &cb.BlockDataHashingStructure{}
if err := proto.Unmarshal(configItem.Value, blockDataHashingStructure); err != nil {
if err := proto.Unmarshal(configValue.Value, blockDataHashingStructure); err != nil {
return fmt.Errorf("Unmarshaling error for BlockDataHashingStructure: %s", err)
}

Expand All @@ -162,12 +162,12 @@ func (pm *SharedConfigImpl) ProposeConfig(configItem *cb.ConfigItem) error {
pm.pendingConfig.blockDataHashingStructureWidth = blockDataHashingStructure.Width
case OrdererAddressesKey:
ordererAddresses := &cb.OrdererAddresses{}
if err := proto.Unmarshal(configItem.Value, ordererAddresses); err != nil {
if err := proto.Unmarshal(configValue.Value, ordererAddresses); err != nil {
return fmt.Errorf("Unmarshaling error for HashingAlgorithm: %s", err)
}
pm.pendingConfig.ordererAddresses = ordererAddresses.Addresses
default:
logger.Warningf("Uknown Chain config item with key %s", configItem.Key)
logger.Warningf("Uknown Chain config item with key %s", key)
}
return nil
}
Expand Down
33 changes: 18 additions & 15 deletions common/configtx/handlers/channel/sharedconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ func init() {
logging.SetLevel(logging.DEBUG, "")
}

func makeInvalidConfigItem(key string) *cb.ConfigItem {
return &cb.ConfigItem{
Type: cb.ConfigItem_CHAIN,
Key: key,
// A temporary method while ConfigItem is being removed
func itemToValue(configItem *cb.ConfigItem) (string, *cb.ConfigValue) {
return configItem.Key, &cb.ConfigValue{Value: configItem.Value}
}

func makeInvalidConfigItem() *cb.ConfigValue {
return &cb.ConfigValue{
Value: []byte("Garbage Data"),
}
}
Expand Down Expand Up @@ -75,24 +78,24 @@ func TestRollback(t *testing.T) {
}

func TestHashingAlgorithm(t *testing.T) {
invalidMessage := makeInvalidConfigItem(HashingAlgorithmKey)
invalidMessage := makeInvalidConfigItem()
invalidAlgorithm := TemplateHashingAlgorithm("MD5")
validAlgorithm := DefaultHashingAlgorithm()

m := NewSharedConfigImpl(nil, nil)
m.BeginConfig()

err := m.ProposeConfig(invalidMessage)
err := m.ProposeConfig(HashingAlgorithmKey, invalidMessage)
if err == nil {
t.Fatalf("Should have failed on invalid message")
}

err = m.ProposeConfig(invalidAlgorithm)
err = m.ProposeConfig(itemToValue(invalidAlgorithm))
if err == nil {
t.Fatalf("Should have failed on invalid algorithm")
}

err = m.ProposeConfig(validAlgorithm)
err = m.ProposeConfig(itemToValue(validAlgorithm))
if err != nil {
t.Fatalf("Error applying valid config: %s", err)
}
Expand All @@ -105,24 +108,24 @@ func TestHashingAlgorithm(t *testing.T) {
}

func TestBlockDataHashingStructure(t *testing.T) {
invalidMessage := makeInvalidConfigItem(BlockDataHashingStructureKey)
invalidMessage := makeInvalidConfigItem()
invalidWidth := TemplateBlockDataHashingStructure(0)
validWidth := DefaultBlockDataHashingStructure()

m := NewSharedConfigImpl(nil, nil)
m.BeginConfig()

err := m.ProposeConfig(invalidMessage)
err := m.ProposeConfig(BlockDataHashingStructureKey, invalidMessage)
if err == nil {
t.Fatalf("Should have failed on invalid message")
}

err = m.ProposeConfig(invalidWidth)
err = m.ProposeConfig(itemToValue(invalidWidth))
if err == nil {
t.Fatalf("Should have failed on invalid width")
}

err = m.ProposeConfig(validWidth)
err = m.ProposeConfig(itemToValue(validWidth))
if err != nil {
t.Fatalf("Error applying valid config: %s", err)
}
Expand All @@ -135,17 +138,17 @@ func TestBlockDataHashingStructure(t *testing.T) {
}

func TestOrdererAddresses(t *testing.T) {
invalidMessage := makeInvalidConfigItem(OrdererAddressesKey)
invalidMessage := makeInvalidConfigItem()
validMessage := DefaultOrdererAddresses()
m := NewSharedConfigImpl(nil, nil)
m.BeginConfig()

err := m.ProposeConfig(invalidMessage)
err := m.ProposeConfig(OrdererAddressesKey, invalidMessage)
if err == nil {
t.Fatalf("Should have failed on invalid message")
}

err = m.ProposeConfig(validMessage)
err = m.ProposeConfig(itemToValue(validMessage))
if err != nil {
t.Fatalf("Error applying valid config: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions common/configtx/handlers/msp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func (bh *MSPConfigHandler) CommitConfig() {
}

// ProposeConfig called when config is added to a proposal
func (bh *MSPConfigHandler) ProposeConfig(configItem *common.ConfigItem) error {
func (bh *MSPConfigHandler) ProposeConfig(key string, configValue *common.ConfigValue) error {
mspconfig := &mspprotos.MSPConfig{}
err := proto.Unmarshal(configItem.Value, mspconfig)
err := proto.Unmarshal(configValue.Value, mspconfig)
if err != nil {
return fmt.Errorf("Error unmarshalling msp config item, err %s", err)
}
Expand Down
10 changes: 6 additions & 4 deletions common/configtx/handlers/msp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ func TestMSPConfigManager(t *testing.T) {
confBytes, err := proto.Marshal(conf)
assert.NoError(t, err)

ci := &common.ConfigItem{Key: "DEFAULT", Value: confBytes}
ci := &common.ConfigValue{Value: confBytes}

// test success:

// begin/propose/commit
key := "DEFAULT"

mspCH := &MSPConfigHandler{}
mspCH.BeginConfig()
err = mspCH.ProposeConfig(ci)
err = mspCH.ProposeConfig(key, ci)
assert.NoError(t, err)
mspCH.CommitConfig()

Expand All @@ -53,8 +55,8 @@ func TestMSPConfigManager(t *testing.T) {
// test failure
// begin/propose/commit
mspCH.BeginConfig()
err = mspCH.ProposeConfig(ci)
err = mspCH.ProposeConfig(key, ci)
assert.NoError(t, err)
err = mspCH.ProposeConfig(&common.ConfigItem{Type: common.ConfigItem_MSP, Key: "DEFAULT", Value: []byte("BARF!")})
err = mspCH.ProposeConfig(key, &common.ConfigValue{Value: []byte("BARF!")})
assert.Error(t, err)
}
18 changes: 9 additions & 9 deletions common/configtx/handlers/orderer/sharedconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ func (pm *ManagerImpl) CommitConfig() {
}

// ProposeConfig is used to add new config to the config proposal
func (pm *ManagerImpl) ProposeConfig(configItem *cb.ConfigItem) error {
switch configItem.Key {
func (pm *ManagerImpl) ProposeConfig(key string, configValue *cb.ConfigValue) error {
switch key {
case ConsensusTypeKey:
consensusType := &ab.ConsensusType{}
if err := proto.Unmarshal(configItem.Value, consensusType); err != nil {
if err := proto.Unmarshal(configValue.Value, consensusType); err != nil {
return fmt.Errorf("Unmarshaling error for ConsensusType: %s", err)
}
if pm.config.consensusType == "" {
Expand All @@ -194,7 +194,7 @@ func (pm *ManagerImpl) ProposeConfig(configItem *cb.ConfigItem) error {
pm.pendingConfig.consensusType = consensusType.Type
case BatchSizeKey:
batchSize := &ab.BatchSize{}
if err := proto.Unmarshal(configItem.Value, batchSize); err != nil {
if err := proto.Unmarshal(configValue.Value, batchSize); err != nil {
return fmt.Errorf("Unmarshaling error for BatchSize: %s", err)
}
if batchSize.MaxMessageCount == 0 {
Expand All @@ -214,7 +214,7 @@ func (pm *ManagerImpl) ProposeConfig(configItem *cb.ConfigItem) error {
var timeoutValue time.Duration
var err error
batchTimeout := &ab.BatchTimeout{}
if err = proto.Unmarshal(configItem.Value, batchTimeout); err != nil {
if err = proto.Unmarshal(configValue.Value, batchTimeout); err != nil {
return fmt.Errorf("Unmarshaling error for BatchTimeout: %s", err)
}
if timeoutValue, err = time.ParseDuration(batchTimeout.Timeout); err != nil {
Expand All @@ -226,7 +226,7 @@ func (pm *ManagerImpl) ProposeConfig(configItem *cb.ConfigItem) error {
pm.pendingConfig.batchTimeout = timeoutValue
case ChainCreationPolicyNamesKey:
chainCreationPolicyNames := &ab.ChainCreationPolicyNames{}
if err := proto.Unmarshal(configItem.Value, chainCreationPolicyNames); err != nil {
if err := proto.Unmarshal(configValue.Value, chainCreationPolicyNames); err != nil {
return fmt.Errorf("Unmarshaling error for ChainCreator: %s", err)
}
if chainCreationPolicyNames.Names == nil {
Expand All @@ -238,19 +238,19 @@ func (pm *ManagerImpl) ProposeConfig(configItem *cb.ConfigItem) error {
}
case IngressPolicyNamesKey:
ingressPolicyNames := &ab.IngressPolicyNames{}
if err := proto.Unmarshal(configItem.Value, ingressPolicyNames); err != nil {
if err := proto.Unmarshal(configValue.Value, ingressPolicyNames); err != nil {
return fmt.Errorf("Unmarshaling error for IngressPolicyNames: %s", err)
}
pm.pendingConfig.ingressPolicyNames = ingressPolicyNames.Names
case EgressPolicyNamesKey:
egressPolicyNames := &ab.EgressPolicyNames{}
if err := proto.Unmarshal(configItem.Value, egressPolicyNames); err != nil {
if err := proto.Unmarshal(configValue.Value, egressPolicyNames); err != nil {
return fmt.Errorf("Unmarshaling error for EgressPolicyNames: %s", err)
}
pm.pendingConfig.egressPolicyNames = egressPolicyNames.Names
case KafkaBrokersKey:
kafkaBrokers := &ab.KafkaBrokers{}
if err := proto.Unmarshal(configItem.Value, kafkaBrokers); err != nil {
if err := proto.Unmarshal(configValue.Value, kafkaBrokers); err != nil {
return fmt.Errorf("Unmarshaling error for KafkaBrokers: %s", err)
}
if len(kafkaBrokers.Brokers) == 0 {
Expand Down
Loading

0 comments on commit a052b61

Please sign in to comment.