Skip to content

Commit

Permalink
[FAB-5647] Extract common comp from config/channel
Browse files Browse the repository at this point in the history
In the last CR in these series, the entirety of the config structures
were moved to fabric/common/channel/config.  This includes code like the
Proposer and StandardValues which is applicable outside of the context
of the channel config.

This CR moves this subset back to the fabric/common/config directory.

Change-Id: I5ff5378a1362ee5da28deec2d46f5dd07ba889e0
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Aug 11, 2017
1 parent 8dc7883 commit 58ddd21
Show file tree
Hide file tree
Showing 23 changed files with 132 additions and 124 deletions.
23 changes: 23 additions & 0 deletions common/config/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package config

type ValueProposer interface {
// BeginValueProposals called when a config proposal is begun
BeginValueProposals(tx interface{}, groups []string) (ValueDeserializer, []ValueProposer, error)

// RollbackProposals called when a config proposal is abandoned
RollbackProposals(tx interface{})

// PreCommit is invoked before committing the config to catch
// any errors which cannot be caught on a per proposal basis
// TODO, rename other methods to remove Value/Proposal references
PreCommit(tx interface{}) error

// CommitProposals called when a config proposal is committed
CommitProposals(tx interface{})
}
16 changes: 0 additions & 16 deletions common/config/channel/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,3 @@ type Orderer interface {
// Organizations returns the organizations for the ordering service
Organizations() map[string]Org
}

type ValueProposer interface {
// BeginValueProposals called when a config proposal is begun
BeginValueProposals(tx interface{}, groups []string) (ValueDeserializer, []ValueProposer, error)

// RollbackProposals called when a config proposal is abandoned
RollbackProposals(tx interface{})

// PreCommit is invoked before committing the config to catch
// any errors which cannot be caught on a per proposal basis
// TODO, rename other methods to remove Value/Proposal references
PreCommit(tx interface{}) error

// CommitProposals called when a config proposal is committed
CommitProposals(tx interface{})
}
17 changes: 9 additions & 8 deletions common/config/channel/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package config
import (
"fmt"

"github.com/hyperledger/fabric/common/config"
"github.com/hyperledger/fabric/common/config/channel/msp"
)

Expand All @@ -29,13 +30,13 @@ const (

// ApplicationGroup represents the application config group
type ApplicationGroup struct {
*Proposer
*config.Proposer
*ApplicationConfig
mspConfig *msp.MSPConfigHandler
}

type ApplicationConfig struct {
*standardValues
*config.StandardValues

applicationGroup *ApplicationGroup
applicationOrgs map[string]ApplicationOrg
Expand All @@ -46,22 +47,22 @@ func NewApplicationGroup(mspConfig *msp.MSPConfigHandler) *ApplicationGroup {
ag := &ApplicationGroup{
mspConfig: mspConfig,
}
ag.Proposer = NewProposer(ag)
ag.Proposer = config.NewProposer(ag)

return ag
}

func (ag *ApplicationGroup) NewGroup(name string) (ValueProposer, error) {
func (ag *ApplicationGroup) NewGroup(name string) (config.ValueProposer, error) {
return NewApplicationOrgGroup(name, ag.mspConfig), nil
}

// Allocate returns a new instance of the ApplicationConfig
func (ag *ApplicationGroup) Allocate() Values {
func (ag *ApplicationGroup) Allocate() config.Values {
return NewApplicationConfig(ag)
}

func NewApplicationConfig(ag *ApplicationGroup) *ApplicationConfig {
sv, err := NewStandardValues(&(struct{}{}))
sv, err := config.NewStandardValues(&(struct{}{}))
if err != nil {
logger.Panicf("Programming error: %s", err)
}
Expand All @@ -70,11 +71,11 @@ func NewApplicationConfig(ag *ApplicationGroup) *ApplicationConfig {
applicationGroup: ag,

// Currently there are no config values
standardValues: sv,
StandardValues: sv,
}
}

func (ac *ApplicationConfig) Validate(tx interface{}, groups map[string]ValueProposer) error {
func (ac *ApplicationConfig) Validate(tx interface{}, groups map[string]config.ValueProposer) error {
ac.applicationOrgs = make(map[string]ApplicationOrg)
var ok bool
for key, value := range groups {
Expand Down
11 changes: 6 additions & 5 deletions common/config/channel/applicationorg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package config

import (
"github.com/hyperledger/fabric/common/config"
mspconfig "github.com/hyperledger/fabric/common/config/channel/msp"
pb "github.com/hyperledger/fabric/protos/peer"

Expand All @@ -42,7 +43,7 @@ type ApplicationOrgConfig struct {

// ApplicationOrgGroup defines the configuration for an application org
type ApplicationOrgGroup struct {
*Proposer
*config.Proposer
*OrganizationGroup
*ApplicationOrgConfig
}
Expand All @@ -52,7 +53,7 @@ func NewApplicationOrgGroup(id string, mspConfig *mspconfig.MSPConfigHandler) *A
aog := &ApplicationOrgGroup{
OrganizationGroup: NewOrganizationGroup(id, mspConfig),
}
aog.Proposer = NewProposer(aog)
aog.Proposer = config.NewProposer(aog)
return aog
}

Expand All @@ -61,7 +62,7 @@ func (aog *ApplicationOrgConfig) AnchorPeers() []*pb.AnchorPeer {
return aog.protos.AnchorPeers.AnchorPeers
}

func (aog *ApplicationOrgGroup) Allocate() Values {
func (aog *ApplicationOrgGroup) Allocate() config.Values {
return NewApplicationOrgConfig(aog)
}

Expand All @@ -78,15 +79,15 @@ func NewApplicationOrgConfig(aog *ApplicationOrgGroup) *ApplicationOrgConfig {
applicationOrgGroup: aog,
}
var err error
aoc.standardValues, err = NewStandardValues(aoc.protos, aoc.OrganizationConfig.protos)
aoc.StandardValues, err = config.NewStandardValues(aoc.protos, aoc.OrganizationConfig.protos)
if err != nil {
logger.Panicf("Programming error: %s", err)
}

return aoc
}

func (aoc *ApplicationOrgConfig) Validate(tx interface{}, groups map[string]ValueProposer) error {
func (aoc *ApplicationOrgConfig) Validate(tx interface{}, groups map[string]config.ValueProposer) error {
if logger.IsEnabledFor(logging.DEBUG) {
logger.Debugf("Anchor peers for org %s are %v", aoc.applicationOrgGroup.name, aoc.protos.AnchorPeers)
}
Expand Down
4 changes: 3 additions & 1 deletion common/config/channel/applicationorg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package config

import (
"testing"

"github.com/hyperledger/fabric/common/config"
)

func TestApplicationOrgInterface(t *testing.T) {
_ = ValueProposer(NewApplicationOrgGroup("id", nil))
_ = config.ValueProposer(NewApplicationOrgGroup("id", nil))
}
15 changes: 8 additions & 7 deletions common/config/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"math"

"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/common/config"
"github.com/hyperledger/fabric/common/config/channel/msp"
"github.com/hyperledger/fabric/common/util"
cb "github.com/hyperledger/fabric/protos/common"
Expand Down Expand Up @@ -78,7 +79,7 @@ func (ccs *channelConfigSetter) Commit() {
// ChannelGroup
type ChannelGroup struct {
*ChannelConfig
*Proposer
*config.Proposer
mspConfigHandler *msp.MSPConfigHandler
}

Expand All @@ -87,12 +88,12 @@ func NewChannelGroup(mspConfigHandler *msp.MSPConfigHandler) *ChannelGroup {
ChannelConfig: NewChannelConfig(),
mspConfigHandler: mspConfigHandler,
}
cg.Proposer = NewProposer(cg)
cg.Proposer = config.NewProposer(cg)
return cg
}

// Allocate creates new config resources for a pending config update
func (cg *ChannelGroup) Allocate() Values {
func (cg *ChannelGroup) Allocate() config.Values {
return &channelConfigSetter{
ChannelConfig: NewChannelConfig(),
target: &cg.ChannelConfig,
Expand All @@ -115,7 +116,7 @@ func (cg *ChannelGroup) ConsortiumsConfig() *ConsortiumsGroup {
}

// NewGroup instantiates either a new application or orderer config
func (cg *ChannelGroup) NewGroup(group string) (ValueProposer, error) {
func (cg *ChannelGroup) NewGroup(group string) (config.ValueProposer, error) {
switch group {
case ApplicationGroupKey:
return NewApplicationGroup(cg.mspConfigHandler), nil
Expand All @@ -130,7 +131,7 @@ func (cg *ChannelGroup) NewGroup(group string) (ValueProposer, error) {

// ChannelConfig stores the channel configuration
type ChannelConfig struct {
*standardValues
*config.StandardValues
protos *ChannelProtos

hashingAlgorithm func(input []byte) []byte
Expand All @@ -147,7 +148,7 @@ func NewChannelConfig() *ChannelConfig {
}

var err error
cc.standardValues, err = NewStandardValues(cc.protos)
cc.StandardValues, err = config.NewStandardValues(cc.protos)
if err != nil {
logger.Panicf("Programming error: %s", err)
}
Expand Down Expand Up @@ -176,7 +177,7 @@ func (cc *ChannelConfig) ConsortiumName() string {

// Validate inspects the generated configuration protos, ensures that the values are correct, and
// sets the ChannelConfig fields that may be referenced after Commit
func (cc *ChannelConfig) Validate(tx interface{}, groups map[string]ValueProposer) error {
func (cc *ChannelConfig) Validate(tx interface{}, groups map[string]config.ValueProposer) error {
for _, validator := range []func() error{
cc.validateHashingAlgorithm,
cc.validateBlockDataHashingStructure,
Expand Down
11 changes: 6 additions & 5 deletions common/config/channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/common/config"
"github.com/hyperledger/fabric/common/util"
cb "github.com/hyperledger/fabric/protos/common"

Expand Down Expand Up @@ -68,20 +69,20 @@ func TestChannelConfig(t *testing.T) {
ag := NewApplicationGroup(nil)
og := NewOrdererGroup(nil)
csg := NewConsortiumsGroup(nil)
good := make(map[string]ValueProposer)
good := make(map[string]config.ValueProposer)
good[ApplicationGroupKey] = ag
good[OrdererGroupKey] = og
good[ConsortiumsGroupKey] = csg

err := cc.Validate(nil, good)
assert.NoError(t, err, "Unexpected error validating good config groups")
err = cc.Validate(nil, map[string]ValueProposer{ApplicationGroupKey: NewConsortiumsGroup(nil)})
err = cc.Validate(nil, map[string]config.ValueProposer{ApplicationGroupKey: NewConsortiumsGroup(nil)})
assert.Error(t, err, "Expected error validating bad config group")
err = cc.Validate(nil, map[string]ValueProposer{OrdererGroupKey: NewConsortiumsGroup(nil)})
err = cc.Validate(nil, map[string]config.ValueProposer{OrdererGroupKey: NewConsortiumsGroup(nil)})
assert.Error(t, err, "Expected error validating bad config group")
err = cc.Validate(nil, map[string]ValueProposer{ConsortiumsGroupKey: NewOrdererGroup(nil)})
err = cc.Validate(nil, map[string]config.ValueProposer{ConsortiumsGroupKey: NewOrdererGroup(nil)})
assert.Error(t, err, "Expected error validating bad config group")
err = cc.Validate(nil, map[string]ValueProposer{ConsortiumKey: NewConsortiumGroup(nil)})
err = cc.Validate(nil, map[string]config.ValueProposer{ConsortiumKey: NewConsortiumGroup(nil)})
assert.Error(t, err, "Expected error validating bad config group")

}
Expand Down
35 changes: 8 additions & 27 deletions common/config/channel/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package config
import (
"fmt"

"github.com/hyperledger/fabric/common/config"
"github.com/hyperledger/fabric/common/config/channel/msp"
cb "github.com/hyperledger/fabric/protos/common"
)
Expand All @@ -30,7 +31,7 @@ type ConsortiumProtos struct {

// ConsortiumGroup stores the set of Consortium
type ConsortiumGroup struct {
*Proposer
*config.Proposer
*ConsortiumConfig

mspConfig *msp.MSPConfigHandler
Expand All @@ -41,43 +42,23 @@ func NewConsortiumGroup(mspConfig *msp.MSPConfigHandler) *ConsortiumGroup {
cg := &ConsortiumGroup{
mspConfig: mspConfig,
}
cg.Proposer = NewProposer(cg)
cg.Proposer = config.NewProposer(cg)
return cg
}

// NewGroup returns a Consortium instance
func (cg *ConsortiumGroup) NewGroup(name string) (ValueProposer, error) {
func (cg *ConsortiumGroup) NewGroup(name string) (config.ValueProposer, error) {
return NewOrganizationGroup(name, cg.mspConfig), nil
}

// Allocate returns the resources for a new config proposal
func (cg *ConsortiumGroup) Allocate() Values {
func (cg *ConsortiumGroup) Allocate() config.Values {
return NewConsortiumConfig(cg)
}

// BeginValueProposals calls through to Proposer after calling into the MSP config Handler
func (cg *ConsortiumGroup) BeginValueProposals(tx interface{}, groups []string) (ValueDeserializer, []ValueProposer, error) {
return cg.Proposer.BeginValueProposals(tx, groups)
}

// PreCommit intercepts the precommit request and commits the MSP config handler before calling the underlying proposer
func (cg *ConsortiumGroup) PreCommit(tx interface{}) error {
return cg.Proposer.PreCommit(tx)
}

// RollbackProposals intercepts the rollback request and commits the MSP config handler before calling the underlying proposer
func (cg *ConsortiumGroup) RollbackProposals(tx interface{}) {
cg.Proposer.RollbackProposals(tx)
}

// CommitProposals intercepts the commit request and commits the MSP config handler before calling the underlying proposer
func (cg *ConsortiumGroup) CommitProposals(tx interface{}) {
cg.Proposer.CommitProposals(tx)
}

// ConsortiumConfig holds the consoritums configuration information
type ConsortiumConfig struct {
*standardValues
*config.StandardValues
protos *ConsortiumProtos
orgs map[string]*OrganizationGroup

Expand All @@ -92,7 +73,7 @@ func NewConsortiumConfig(cg *ConsortiumGroup) *ConsortiumConfig {
consortiumGroup: cg,
}
var err error
cc.standardValues, err = NewStandardValues(cc.protos)
cc.StandardValues, err = config.NewStandardValues(cc.protos)
if err != nil {
logger.Panicf("Programming error: %s", err)
}
Expand All @@ -116,7 +97,7 @@ func (cc *ConsortiumConfig) Commit() {
}

// Validate builds the Consortium map
func (cc *ConsortiumConfig) Validate(tx interface{}, groups map[string]ValueProposer) error {
func (cc *ConsortiumConfig) Validate(tx interface{}, groups map[string]config.ValueProposer) error {
var ok bool
for key, group := range groups {
cc.orgs[key], ok = group.(*OrganizationGroup)
Expand Down
5 changes: 3 additions & 2 deletions common/config/channel/consortium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config
import (
"testing"

"github.com/hyperledger/fabric/common/config"
"github.com/hyperledger/fabric/common/config/channel/msp"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -58,12 +59,12 @@ func TestConsortiumConfig(t *testing.T) {
assert.Equal(t, cg.ConsortiumConfig, cc, "Error committing ConsortiumConfig")

og, _ := cg.NewGroup("testGroup")
err := cc.Validate(t, map[string]ValueProposer{
err := cc.Validate(t, map[string]config.ValueProposer{
"testGroup": og,
})
assert.NoError(t, err, "Validate returned unexpected error")
csg := NewConsortiumsGroup(nil)
err = cc.Validate(t, map[string]ValueProposer{
err = cc.Validate(t, map[string]config.ValueProposer{
"testGroup": csg,
})
assert.Error(t, err, "Validate should have failed")
Expand Down
Loading

0 comments on commit 58ddd21

Please sign in to comment.