Skip to content

Commit

Permalink
[FAB-2226] Move anchor peers to app org level
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-2226

The anchor peers are per application organization, so should be stored
at the app org level.

Change-Id: I930244c44c759d628531af2f71da3ced43b85631
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Feb 15, 2017
1 parent e99311d commit 7e0b4bf
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 170 deletions.
10 changes: 8 additions & 2 deletions common/configtx/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ type ChannelConfig interface {
OrdererAddresses() []string
}

// ApplicationConfig stores the common shared application config
type ApplicationConfig interface {
// ApplicationOrgConfig stores the per org application config
type ApplicationOrgConfig interface {
// AnchorPeers returns the list of gossip anchor peers
AnchorPeers() []*pb.AnchorPeer
}

// ApplicationConfig stores the common shared application config
type ApplicationConfig interface {
// Organizations returns a map of org ID to ApplicationOrgConfig
Organizations() map[string]ApplicationOrgConfig
}

// OrdererConfig stores the common shared orderer config
type OrdererConfig interface {
// ConsensusType returns the configured consensus type
Expand Down
106 changes: 106 additions & 0 deletions common/configtx/handlers/application/organization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
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 application

import (
"fmt"

"github.com/hyperledger/fabric/common/configtx/handlers"
mspconfig "github.com/hyperledger/fabric/common/configtx/handlers/msp"
cb "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"

"github.com/golang/protobuf/proto"
logging "github.com/op/go-logging"
)

// Application org config keys
const (
// AnchorPeersKey is the key name for the AnchorPeers ConfigValue
AnchorPeersKey = "AnchorPeers"
)

type applicationOrgConfig struct {
anchorPeers []*pb.AnchorPeer
}

// SharedConfigImpl is an implementation of Manager and configtx.ConfigHandler
// In general, it should only be referenced as an Impl for the configtx.Manager
type ApplicationOrgConfig struct {
*handlers.OrgConfig
pendingConfig *applicationOrgConfig
config *applicationOrgConfig

mspConfig *mspconfig.MSPConfigHandler
}

// NewSharedConfigImpl creates a new SharedConfigImpl with the given CryptoHelper
func NewApplicationOrgConfig(id string, mspConfig *mspconfig.MSPConfigHandler) *ApplicationOrgConfig {
return &ApplicationOrgConfig{
OrgConfig: handlers.NewOrgConfig(id, mspConfig),
config: &applicationOrgConfig{},
}
}

// AnchorPeers returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver
func (oc *ApplicationOrgConfig) AnchorPeers() []*pb.AnchorPeer {
return oc.config.anchorPeers
}

// BeginConfig is used to start a new config proposal
func (oc *ApplicationOrgConfig) BeginConfig() {
logger.Debugf("Beginning a possible new org config")
if oc.pendingConfig != nil {
logger.Panicf("Programming error, cannot call begin in the middle of a proposal")
}
oc.pendingConfig = &applicationOrgConfig{}
}

// RollbackConfig is used to abandon a new config proposal
func (oc *ApplicationOrgConfig) RollbackConfig() {
logger.Debugf("Rolling back proposed org config")
oc.pendingConfig = nil
}

// CommitConfig is used to commit a new config proposal
func (oc *ApplicationOrgConfig) CommitConfig() {
logger.Debugf("Committing new org config")
if oc.pendingConfig == nil {
logger.Panicf("Programming error, cannot call commit without an existing proposal")
}
oc.config = oc.pendingConfig
oc.pendingConfig = nil
}

// ProposeConfig is used to add new config to the config proposal
func (oc *ApplicationOrgConfig) ProposeConfig(key string, configValue *cb.ConfigValue) error {
switch key {
case AnchorPeersKey:
anchorPeers := &pb.AnchorPeers{}
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", key, anchorPeers.AnchorPeers)
}
oc.pendingConfig.anchorPeers = anchorPeers.AnchorPeers
default:
return oc.OrgConfig.ProposeConfig(key, configValue)
}

return nil
}
86 changes: 86 additions & 0 deletions common/configtx/handlers/application/organization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
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 application

import (
"testing"

configtxapi "github.com/hyperledger/fabric/common/configtx/api"
cb "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"

logging "github.com/op/go-logging"
"github.com/stretchr/testify/assert"
)

func init() {
logging.SetLevel(logging.DEBUG, "")
}

func makeInvalidConfigValue() *cb.ConfigValue {
return &cb.ConfigValue{
Value: []byte("Garbage Data"),
}
}

func groupToKeyValue(configGroup *cb.ConfigGroup) (string, *cb.ConfigValue) {
for _, group := range configGroup.Groups[GroupKey].Groups {
for key, value := range group.Values {
return key, value
}
}
panic("No value encoded")
}

func TestApplicationOrgInterface(t *testing.T) {
_ = configtxapi.SubInitializer(NewApplicationOrgConfig("id", nil))
}

func TestApplicationOrgDoubleBegin(t *testing.T) {
m := NewApplicationOrgConfig("id", nil)
m.BeginConfig()
assert.Panics(t, m.BeginConfig, "Two begins back to back should have caused a panic")
}

func TestApplicationOrgCommitWithoutBegin(t *testing.T) {
m := NewApplicationOrgConfig("id", nil)
assert.Panics(t, m.CommitConfig, "Committing without beginning should have caused a panic")
}

func TestApplicationOrgRollback(t *testing.T) {
m := NewApplicationOrgConfig("id", nil)
m.pendingConfig = &applicationOrgConfig{}
m.RollbackConfig()
assert.Nil(t, m.pendingConfig, "Should have cleared pending config on rollback")
}

func TestApplicationOrgAnchorPeers(t *testing.T) {
endVal := []*pb.AnchorPeer{
&pb.AnchorPeer{Host: "foo", Port: 234, Cert: []byte("foocert")},
&pb.AnchorPeer{Host: "bar", Port: 237, Cert: []byte("barcert")},
}
invalidMessage := makeInvalidConfigValue()
validMessage := TemplateAnchorPeers("id", endVal)
m := NewApplicationOrgConfig("id", nil)
m.BeginConfig()

assert.Error(t, m.ProposeConfig(AnchorPeersKey, invalidMessage), "Should have failed on invalid message")
assert.NoError(t, m.ProposeConfig(groupToKeyValue(validMessage)), "Should not have failed on invalid message")
m.CommitConfig()

assert.Equal(t, m.AnchorPeers(), endVal, "Did not set updated anchor peers")
}
50 changes: 14 additions & 36 deletions common/configtx/handlers/application/sharedconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import (
"github.com/hyperledger/fabric/common/configtx/handlers"
"github.com/hyperledger/fabric/common/configtx/handlers/msp"
cb "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"

"github.com/golang/protobuf/proto"
"github.com/op/go-logging"
)

Expand All @@ -37,7 +35,8 @@ const (
var orgSchema = &cb.ConfigGroupSchema{
Groups: map[string]*cb.ConfigGroupSchema{},
Values: map[string]*cb.ConfigValueSchema{
"MSP": nil, // TODO, consolidate into a constant once common org code exists
AnchorPeersKey: nil,
handlers.MSPKey: nil, // TODO, consolidate into a constant once common org code exists
},
Policies: map[string]*cb.ConfigPolicySchema{
// TODO, set appropriately once hierarchical policies are implemented
Expand All @@ -48,25 +47,16 @@ var Schema = &cb.ConfigGroupSchema{
Groups: map[string]*cb.ConfigGroupSchema{
"": orgSchema,
},
Values: map[string]*cb.ConfigValueSchema{
AnchorPeersKey: nil,
},
Values: map[string]*cb.ConfigValueSchema{},
Policies: map[string]*cb.ConfigPolicySchema{
// TODO, set appropriately once hierarchical policies are implemented
},
}

// Peer config keys
const (
// AnchorPeersKey is the key name for the AnchorPeers ConfigValue
AnchorPeersKey = "AnchorPeers"
)

var logger = logging.MustGetLogger("peer/sharedconfig")
var logger = logging.MustGetLogger("common/configtx/handlers/application")

type sharedConfig struct {
anchorPeers []*pb.AnchorPeer
orgs map[string]*handlers.OrgConfig
orgs map[string]api.ApplicationOrgConfig
}

// SharedConfigImpl is an implementation of Manager and configtx.ConfigHandler
Expand All @@ -86,19 +76,14 @@ func NewSharedConfigImpl(mspConfig *msp.MSPConfigHandler) *SharedConfigImpl {
}
}

// AnchorPeers returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver
func (di *SharedConfigImpl) AnchorPeers() []*pb.AnchorPeer {
return di.config.anchorPeers
}

// BeginConfig is used to start a new config proposal
func (di *SharedConfigImpl) BeginConfig() {
logger.Debugf("Beginning a possible new peer shared config")
if di.pendingConfig != nil {
logger.Panicf("Programming error, cannot call begin in the middle of a proposal")
}
di.pendingConfig = &sharedConfig{
orgs: make(map[string]*handlers.OrgConfig),
orgs: make(map[string]api.ApplicationOrgConfig),
}
}

Expand All @@ -120,22 +105,15 @@ func (di *SharedConfigImpl) CommitConfig() {

// ProposeConfig is used to add new config to the config proposal
func (di *SharedConfigImpl) ProposeConfig(key string, configValue *cb.ConfigValue) error {
switch key {
case AnchorPeersKey:
anchorPeers := &pb.AnchorPeers{}
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", key, anchorPeers.AnchorPeers)
}
di.pendingConfig.anchorPeers = anchorPeers.AnchorPeers
default:
logger.Warningf("Uknown Peer config item with key %s", key)
}
logger.Warningf("Uknown Peer config item with key %s", key)
return nil
}

// Organizations returns a map of org ID to ApplicationOrgConfig
func (di *SharedConfigImpl) Organizations() map[string]api.ApplicationOrgConfig {
return di.config.orgs
}

// Handler returns the associated api.Handler for the given path
func (pm *SharedConfigImpl) Handler(path []string) (api.Handler, error) {
if len(path) == 0 {
Expand All @@ -148,8 +126,8 @@ func (pm *SharedConfigImpl) Handler(path []string) (api.Handler, error) {

org, ok := pm.pendingConfig.orgs[path[0]]
if !ok {
org = handlers.NewOrgConfig(path[0], pm.mspConfig)
org = NewApplicationOrgConfig(path[0], pm.mspConfig)
pm.pendingConfig.orgs[path[0]] = org
}
return org, nil
return org.(*ApplicationOrgConfig), nil
}
Loading

0 comments on commit 7e0b4bf

Please sign in to comment.