Skip to content

Commit efa8237

Browse files
author
Jason Yellick
committed
[FAB-2399] ApplicationOrg config to common Proposer
https://jira.hyperledger.org/browse/FAB-2399 This CR moves the last standalone config component, the application org config over to the common Proposer framework. Change-Id: I1f6aa2426b2e41d89dc972963dfe6e5643e08a06 Signed-off-by: Jason Yellick <[email protected]>
1 parent 2541878 commit efa8237

File tree

4 files changed

+47
-125
lines changed

4 files changed

+47
-125
lines changed

common/configvalues/root/application.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ func NewApplicationGroup(mspConfig *msp.MSPConfigHandler) *ApplicationGroup {
5353
}
5454

5555
func (ag *ApplicationGroup) NewGroup(name string) (api.ValueProposer, error) {
56-
return NewApplicationOrgConfig(name, ag.mspConfig), nil
56+
return NewApplicationOrgGroup(name, ag.mspConfig), nil
5757
}
5858

59-
// Allocate returns the
59+
// Allocate returns a new instance of the ApplicationConfig
6060
func (ag *ApplicationGroup) Allocate() Values {
6161
return NewApplicationConfig(ag)
6262
}
@@ -79,7 +79,7 @@ func (ac *ApplicationConfig) Validate(groups map[string]api.ValueProposer) error
7979
ac.applicationOrgs = make(map[string]api.ApplicationOrg)
8080
var ok bool
8181
for key, value := range groups {
82-
ac.applicationOrgs[key], ok = value.(*ApplicationOrgConfig)
82+
ac.applicationOrgs[key], ok = value.(*ApplicationOrgGroup)
8383
if !ok {
8484
return fmt.Errorf("Application sub-group %s was not an ApplicationOrgGroup, actually %T", key, value)
8585
}

common/configvalues/root/applicationorg.go

+41-61
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@ limitations under the License.
1717
package config
1818

1919
import (
20-
"fmt"
21-
2220
api "github.com/hyperledger/fabric/common/configvalues"
2321
mspconfig "github.com/hyperledger/fabric/common/configvalues/msp"
24-
cb "github.com/hyperledger/fabric/protos/common"
2522
pb "github.com/hyperledger/fabric/protos/peer"
2623

27-
"github.com/golang/protobuf/proto"
2824
logging "github.com/op/go-logging"
2925
)
3026

@@ -34,82 +30,66 @@ const (
3430
AnchorPeersKey = "AnchorPeers"
3531
)
3632

37-
type applicationOrgConfig struct {
38-
anchorPeers []*pb.AnchorPeer
33+
type ApplicationOrgProtos struct {
34+
AnchorPeers *pb.AnchorPeers
3935
}
4036

41-
// SharedConfigImpl is an implementation of Manager and configtx.ConfigHandler
42-
// In general, it should only be referenced as an Impl for the configtx.Manager
4337
type ApplicationOrgConfig struct {
44-
*OrganizationGroup
45-
pendingConfig *applicationOrgConfig
46-
config *applicationOrgConfig
38+
*OrganizationConfig
39+
protos *ApplicationOrgProtos
40+
41+
applicationOrgGroup *ApplicationOrgGroup
42+
}
4743

48-
mspConfig *mspconfig.MSPConfigHandler
44+
// ApplicationOrgGroup defines the configuration for an application org
45+
type ApplicationOrgGroup struct {
46+
*Proposer
47+
*OrganizationGroup
48+
*ApplicationOrgConfig
4949
}
5050

51-
// NewSharedConfigImpl creates a new SharedConfigImpl with the given CryptoHelper
52-
func NewApplicationOrgConfig(id string, mspConfig *mspconfig.MSPConfigHandler) *ApplicationOrgConfig {
53-
return &ApplicationOrgConfig{
51+
// NewApplicationOrgGroup creates a new ApplicationOrgGroup
52+
func NewApplicationOrgGroup(id string, mspConfig *mspconfig.MSPConfigHandler) *ApplicationOrgGroup {
53+
aog := &ApplicationOrgGroup{
5454
OrganizationGroup: NewOrganizationGroup(id, mspConfig),
55-
config: &applicationOrgConfig{},
5655
}
56+
aog.Proposer = NewProposer(aog)
57+
return aog
5758
}
5859

5960
// AnchorPeers returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver
60-
func (oc *ApplicationOrgConfig) AnchorPeers() []*pb.AnchorPeer {
61-
return oc.config.anchorPeers
61+
func (aog *ApplicationOrgConfig) AnchorPeers() []*pb.AnchorPeer {
62+
return aog.protos.AnchorPeers.AnchorPeers
6263
}
6364

64-
// BeginValueProposals is used to start a new config proposal
65-
func (oc *ApplicationOrgConfig) BeginValueProposals(groups []string) ([]api.ValueProposer, error) {
66-
logger.Debugf("Beginning a possible new org config")
67-
if len(groups) != 0 {
68-
return nil, fmt.Errorf("ApplicationGroup does not support subgroups")
69-
}
70-
if oc.pendingConfig != nil {
71-
logger.Panicf("Programming error, cannot call begin in the middle of a proposal")
72-
}
73-
oc.pendingConfig = &applicationOrgConfig{}
74-
return oc.OrganizationGroup.BeginValueProposals(groups)
65+
func (aog *ApplicationOrgGroup) Allocate() Values {
66+
return NewApplicationOrgConfig(aog)
7567
}
7668

77-
// RollbackProposals is used to abandon a new config proposal
78-
func (oc *ApplicationOrgConfig) RollbackProposals() {
79-
logger.Debugf("Rolling back proposed org config")
80-
oc.pendingConfig = nil
81-
oc.OrganizationGroup.RollbackProposals()
69+
func (aoc *ApplicationOrgConfig) Commit() {
70+
aoc.applicationOrgGroup.ApplicationOrgConfig = aoc
71+
aoc.OrganizationConfig.Commit()
8272
}
8373

84-
// CommitProposals is used to commit a new config proposal
85-
func (oc *ApplicationOrgConfig) CommitProposals() {
86-
logger.Debugf("Committing new org config")
87-
if oc.pendingConfig == nil {
88-
logger.Panicf("Programming error, cannot call commit without an existing proposal")
89-
}
90-
oc.config = oc.pendingConfig
91-
oc.pendingConfig = nil
92-
oc.OrganizationGroup.CommitProposals()
93-
}
74+
func NewApplicationOrgConfig(aog *ApplicationOrgGroup) *ApplicationOrgConfig {
75+
aoc := &ApplicationOrgConfig{
76+
protos: &ApplicationOrgProtos{},
77+
OrganizationConfig: NewOrganizationConfig(aog.OrganizationGroup),
9478

95-
// ProposeValue is used to add new config to the config proposal
96-
func (oc *ApplicationOrgConfig) ProposeValue(key string, configValue *cb.ConfigValue) error {
97-
switch key {
98-
case AnchorPeersKey:
99-
anchorPeers := &pb.AnchorPeers{}
100-
if err := proto.Unmarshal(configValue.Value, anchorPeers); err != nil {
101-
return fmt.Errorf("Unmarshaling error for %s: %s", key, err)
102-
}
103-
if logger.IsEnabledFor(logging.DEBUG) {
104-
logger.Debugf("Setting %s to %v", key, anchorPeers.AnchorPeers)
105-
}
106-
oc.pendingConfig.anchorPeers = anchorPeers.AnchorPeers
107-
default:
108-
return oc.OrganizationGroup.ProposeValue(key, configValue)
79+
applicationOrgGroup: aog,
80+
}
81+
var err error
82+
aoc.standardValues, err = NewStandardValues(aoc.protos, aoc.OrganizationConfig.protos)
83+
if err != nil {
84+
logger.Panicf("Programming error: %s", err)
10985
}
11086

111-
return nil
87+
return aoc
11288
}
11389

114-
// PreCommit returns nil
115-
func (c *ApplicationOrgConfig) PreCommit() error { return nil }
90+
func (aoc *ApplicationOrgConfig) Validate(groups map[string]api.ValueProposer) error {
91+
if logger.IsEnabledFor(logging.DEBUG) {
92+
logger.Debugf("Anchor peers for org %s are %v", aoc.applicationOrgGroup.name, aoc.protos.AnchorPeers)
93+
}
94+
return aoc.OrganizationConfig.Validate(groups)
95+
}

common/configvalues/root/applicationorg_test.go

+1-60
Original file line numberDiff line numberDiff line change
@@ -20,67 +20,8 @@ import (
2020
"testing"
2121

2222
api "github.com/hyperledger/fabric/common/configvalues"
23-
cb "github.com/hyperledger/fabric/protos/common"
24-
pb "github.com/hyperledger/fabric/protos/peer"
25-
26-
logging "github.com/op/go-logging"
27-
"github.com/stretchr/testify/assert"
2823
)
2924

30-
func init() {
31-
logging.SetLevel(logging.DEBUG, "")
32-
}
33-
34-
func makeInvalidConfigValue() *cb.ConfigValue {
35-
return &cb.ConfigValue{
36-
Value: []byte("Garbage Data"),
37-
}
38-
}
39-
40-
func groupToKeyValue(configGroup *cb.ConfigGroup) (string, *cb.ConfigValue) {
41-
for _, group := range configGroup.Groups[ApplicationGroupKey].Groups {
42-
for key, value := range group.Values {
43-
return key, value
44-
}
45-
}
46-
panic("No value encoded")
47-
}
48-
4925
func TestApplicationOrgInterface(t *testing.T) {
50-
_ = api.ValueProposer(NewApplicationOrgConfig("id", nil))
51-
}
52-
53-
func TestApplicationOrgDoubleBegin(t *testing.T) {
54-
m := NewApplicationOrgConfig("id", nil)
55-
m.BeginValueProposals(nil)
56-
assert.Panics(t, func() { m.BeginValueProposals(nil) }, "Two begins back to back should have caused a panic")
57-
}
58-
59-
func TestApplicationOrgCommitWithoutBegin(t *testing.T) {
60-
m := NewApplicationOrgConfig("id", nil)
61-
assert.Panics(t, m.CommitProposals, "Committing without beginning should have caused a panic")
62-
}
63-
64-
func TestApplicationOrgRollback(t *testing.T) {
65-
m := NewApplicationOrgConfig("id", nil)
66-
m.pendingConfig = &applicationOrgConfig{}
67-
m.RollbackProposals()
68-
assert.Nil(t, m.pendingConfig, "Should have cleared pending config on rollback")
69-
}
70-
71-
func TestApplicationOrgAnchorPeers(t *testing.T) {
72-
endVal := []*pb.AnchorPeer{
73-
&pb.AnchorPeer{Host: "foo", Port: 234},
74-
&pb.AnchorPeer{Host: "bar", Port: 237},
75-
}
76-
invalidMessage := makeInvalidConfigValue()
77-
validMessage := TemplateAnchorPeers("id", endVal)
78-
m := NewApplicationOrgConfig("id", nil)
79-
m.BeginValueProposals(nil)
80-
81-
assert.Error(t, m.ProposeValue(AnchorPeersKey, invalidMessage), "Should have failed on invalid message")
82-
assert.NoError(t, m.ProposeValue(groupToKeyValue(validMessage)), "Should not have failed on invalid message")
83-
m.CommitProposals()
84-
85-
assert.Equal(t, m.AnchorPeers(), endVal, "Did not set updated anchor peers")
26+
_ = api.ValueProposer(NewApplicationOrgGroup("id", nil))
8627
}

common/configvalues/root/standardvalues.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func NewStandardValues(protosStructs ...interface{}) (*standardValues, error) {
3838
}
3939

4040
for _, protosStruct := range protosStructs {
41+
logger.Debugf("Initializing protos for %T\n", protosStruct)
4142
if err := sv.initializeProtosStruct(reflect.ValueOf(protosStruct)); err != nil {
4243
return nil, err
4344
}
@@ -63,7 +64,7 @@ func (sv *standardValues) initializeProtosStruct(objValue reflect.Value) error {
6364
numFields := objValue.Elem().NumField()
6465
for i := 0; i < numFields; i++ {
6566
structField := objType.Elem().Field(i)
66-
fmt.Printf("Processing field: %s\n", structField.Name)
67+
logger.Debugf("Processing field: %s\n", structField.Name)
6768
switch structField.Type.Kind() {
6869
case reflect.Ptr:
6970
fieldPtr := objValue.Elem().Field(i)

0 commit comments

Comments
 (0)