Skip to content

Commit 39378d3

Browse files
author
Jason Yellick
committed
[FAB-2144] Move configtx.Manager to ConfigNext
https://jira.hyperledger.org/browse/FAB-2144 While integrating the ConfigNext proto, adapter code was written to convert it back to the original Config type. This conversion code needs to go away, and as a first step, the configtx.Manager needs to begin using the updated format. Change-Id: I4ea95795dcdf6f9395bec17f4b22e184e8c4780a Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent ed4f136 commit 39378d3

File tree

16 files changed

+417
-251
lines changed

16 files changed

+417
-251
lines changed

common/configtx/api/api.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,6 @@ type OrdererConfig interface {
7575

7676
// Handler provides a hook which allows other pieces of code to participate in config proposals
7777
type Handler interface {
78-
// BeginConfig called when a config proposal is begun
79-
BeginConfig()
80-
81-
// RollbackConfig called when a config proposal is abandoned
82-
RollbackConfig()
83-
84-
// CommitConfig called when a config proposal is committed
85-
CommitConfig()
86-
8778
// ProposeConfig called when config is added to a proposal
8879
ProposeConfig(configItem *cb.ConfigItem) error
8980
}
@@ -125,11 +116,28 @@ type Resources interface {
125116
MSPManager() msp.MSPManager
126117
}
127118

128-
// Initializer is a structure which is only useful before a configtx.Manager
129-
// has been instantiated for a chain, afterwards, it is of no utility, which
130-
// is why it embeds the Resources interface
119+
// SubInitializer is used downstream from initializer
120+
type SubInitializer interface {
121+
// BeginConfig called when a config proposal is begun
122+
BeginConfig()
123+
124+
// RollbackConfig called when a config proposal is abandoned
125+
RollbackConfig()
126+
127+
// CommitConfig called when a config proposal is committed
128+
CommitConfig()
129+
130+
// Handler returns the associated value handler for a given config path
131+
Handler(path []string) (Handler, error)
132+
}
133+
134+
// Initializer is used as indirection between Manager and Handler to allow
135+
// for single Handlers to handle multiple paths
131136
type Initializer interface {
137+
SubInitializer
138+
132139
Resources
133-
// Handlers returns the handlers to be used when initializing the configtx.Manager
134-
Handlers() map[cb.ConfigItem_ConfigType]Handler
140+
141+
// PolicyProposer as a Handler is a temporary hack
142+
PolicyProposer() Handler
135143
}

common/configtx/bytes_handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616

1717
package configtx
1818

19+
// XXX Remove this
20+
1921
import (
2022
cb "github.com/hyperledger/fabric/protos/common"
2123
)
@@ -57,7 +59,6 @@ func (bh *BytesHandler) CommitConfig() {
5759

5860
// ProposeConfig called when config is added to a proposal
5961
func (bh *BytesHandler) ProposeConfig(configItem *cb.ConfigItem) error {
60-
bh.proposed[configItem.Key] = configItem.Value
6162
return nil
6263
}
6364

common/configtx/compare.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type comparable struct {
2626
*cb.ConfigGroup
2727
*cb.ConfigValue
2828
*cb.ConfigPolicy
29+
path []string
2930
}
3031

3132
func (cg comparable) equals(other comparable) bool {
@@ -51,6 +52,34 @@ func (cg comparable) equals(other comparable) bool {
5152
return false
5253
}
5354

55+
func (cg comparable) version() uint64 {
56+
switch {
57+
case cg.ConfigGroup != nil:
58+
return cg.ConfigGroup.Version
59+
case cg.ConfigValue != nil:
60+
return cg.ConfigValue.Version
61+
case cg.ConfigPolicy != nil:
62+
return cg.ConfigPolicy.Version
63+
}
64+
65+
// Unreachable
66+
return 0
67+
}
68+
69+
func (cg comparable) modPolicy() string {
70+
switch {
71+
case cg.ConfigGroup != nil:
72+
return cg.ConfigGroup.ModPolicy
73+
case cg.ConfigValue != nil:
74+
return cg.ConfigValue.ModPolicy
75+
case cg.ConfigPolicy != nil:
76+
return cg.ConfigPolicy.ModPolicy
77+
}
78+
79+
// Unreachable
80+
return ""
81+
}
82+
5483
func equalConfigValues(lhs, rhs *cb.ConfigValue) bool {
5584
return lhs.Version == rhs.Version &&
5685
lhs.ModPolicy == rhs.ModPolicy &&

common/configtx/handlers/application/sharedconfig.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package application
1919
import (
2020
"fmt"
2121

22+
"github.com/hyperledger/fabric/common/configtx/api"
23+
"github.com/hyperledger/fabric/common/configtx/handlers/msp"
2224
cb "github.com/hyperledger/fabric/protos/common"
2325
pb "github.com/hyperledger/fabric/protos/peer"
2426

@@ -65,12 +67,15 @@ type sharedConfig struct {
6567
type SharedConfigImpl struct {
6668
pendingConfig *sharedConfig
6769
config *sharedConfig
70+
71+
mspConfig *msp.MSPConfigHandler
6872
}
6973

7074
// NewSharedConfigImpl creates a new SharedConfigImpl with the given CryptoHelper
71-
func NewSharedConfigImpl() *SharedConfigImpl {
75+
func NewSharedConfigImpl(mspConfig *msp.MSPConfigHandler) *SharedConfigImpl {
7276
return &SharedConfigImpl{
73-
config: &sharedConfig{},
77+
config: &sharedConfig{},
78+
mspConfig: mspConfig,
7479
}
7580
}
7681

@@ -106,10 +111,6 @@ func (di *SharedConfigImpl) CommitConfig() {
106111

107112
// ProposeConfig is used to add new config to the config proposal
108113
func (di *SharedConfigImpl) ProposeConfig(configItem *cb.ConfigItem) error {
109-
if configItem.Type != cb.ConfigItem_PEER {
110-
return fmt.Errorf("Expected type of ConfigItem_Peer, got %v", configItem.Type)
111-
}
112-
113114
switch configItem.Key {
114115
case AnchorPeersKey:
115116
anchorPeers := &pb.AnchorPeers{}
@@ -125,3 +126,16 @@ func (di *SharedConfigImpl) ProposeConfig(configItem *cb.ConfigItem) error {
125126
}
126127
return nil
127128
}
129+
130+
// Handler returns the associated api.Handler for the given path
131+
func (pm *SharedConfigImpl) Handler(path []string) (api.Handler, error) {
132+
if len(path) == 0 {
133+
return pm, nil
134+
}
135+
136+
if len(path) > 1 {
137+
return nil, fmt.Errorf("Application group allows only one further level of nesting")
138+
}
139+
140+
return pm.mspConfig.Handler(path[1:])
141+
}

common/configtx/handlers/application/sharedconfig_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func makeInvalidConfigItem(key string) *cb.ConfigItem {
4040
}
4141

4242
func TestInterface(t *testing.T) {
43-
_ = configtxapi.ApplicationConfig(NewSharedConfigImpl())
43+
_ = configtxapi.ApplicationConfig(NewSharedConfigImpl(nil))
4444
}
4545

4646
func TestDoubleBegin(t *testing.T) {
@@ -50,7 +50,7 @@ func TestDoubleBegin(t *testing.T) {
5050
}
5151
}()
5252

53-
m := NewSharedConfigImpl()
53+
m := NewSharedConfigImpl(nil)
5454
m.BeginConfig()
5555
m.BeginConfig()
5656
}
@@ -62,12 +62,12 @@ func TestCommitWithoutBegin(t *testing.T) {
6262
}
6363
}()
6464

65-
m := NewSharedConfigImpl()
65+
m := NewSharedConfigImpl(nil)
6666
m.CommitConfig()
6767
}
6868

6969
func TestRollback(t *testing.T) {
70-
m := NewSharedConfigImpl()
70+
m := NewSharedConfigImpl(nil)
7171
m.pendingConfig = &sharedConfig{}
7272
m.RollbackConfig()
7373
if m.pendingConfig != nil {
@@ -82,7 +82,7 @@ func TestAnchorPeers(t *testing.T) {
8282
}
8383
invalidMessage := makeInvalidConfigItem(AnchorPeersKey)
8484
validMessage := TemplateAnchorPeers(endVal)
85-
m := NewSharedConfigImpl()
85+
m := NewSharedConfigImpl(nil)
8686
m.BeginConfig()
8787

8888
err := m.ProposeConfig(invalidMessage)

common/configtx/handlers/channel/sharedconfig.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"math"
2222

23+
"github.com/hyperledger/fabric/common/configtx/api"
2324
"github.com/hyperledger/fabric/common/configtx/handlers/application"
2425
"github.com/hyperledger/fabric/common/configtx/handlers/orderer"
2526
"github.com/hyperledger/fabric/common/util"
@@ -83,12 +84,17 @@ type chainConfig struct {
8384
type SharedConfigImpl struct {
8485
pendingConfig *chainConfig
8586
config *chainConfig
87+
88+
ordererConfig *orderer.ManagerImpl
89+
applicationConfig *application.SharedConfigImpl
8690
}
8791

8892
// NewSharedConfigImpl creates a new SharedConfigImpl with the given CryptoHelper
89-
func NewSharedConfigImpl() *SharedConfigImpl {
93+
func NewSharedConfigImpl(ordererConfig *orderer.ManagerImpl, applicationConfig *application.SharedConfigImpl) *SharedConfigImpl {
9094
return &SharedConfigImpl{
91-
config: &chainConfig{},
95+
config: &chainConfig{},
96+
ordererConfig: ordererConfig,
97+
applicationConfig: applicationConfig,
9298
}
9399
}
94100

@@ -131,10 +137,6 @@ func (pm *SharedConfigImpl) CommitConfig() {
131137

132138
// ProposeConfig is used to add new config to the config proposal
133139
func (pm *SharedConfigImpl) ProposeConfig(configItem *cb.ConfigItem) error {
134-
if configItem.Type != cb.ConfigItem_CHAIN {
135-
return fmt.Errorf("Expected type of ConfigItem_Chain, got %v", configItem.Type)
136-
}
137-
138140
switch configItem.Key {
139141
case HashingAlgorithmKey:
140142
hashingAlgorithm := &cb.HashingAlgorithm{}
@@ -169,3 +171,25 @@ func (pm *SharedConfigImpl) ProposeConfig(configItem *cb.ConfigItem) error {
169171
}
170172
return nil
171173
}
174+
175+
// Handler returns the associated api.Handler for the given path
176+
func (pm *SharedConfigImpl) Handler(path []string) (api.Handler, error) {
177+
if len(path) == 0 {
178+
return pm, nil
179+
}
180+
181+
var initializer api.SubInitializer
182+
183+
switch path[0] {
184+
case ApplicationGroupKey:
185+
initializer = pm.applicationConfig
186+
case OrdererGroupKey:
187+
initializer = pm.ordererConfig
188+
default:
189+
return nil, fmt.Errorf("Disallowed channel group: %s", path[0])
190+
}
191+
192+
return initializer.Handler(path[1:])
193+
194+
return nil, fmt.Errorf("Unallowed group")
195+
}

common/configtx/handlers/channel/sharedconfig_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func makeInvalidConfigItem(key string) *cb.ConfigItem {
3939
}
4040

4141
func TestInterface(t *testing.T) {
42-
_ = configtxapi.ChannelConfig(NewSharedConfigImpl())
42+
_ = configtxapi.ChannelConfig(NewSharedConfigImpl(nil, nil))
4343
}
4444

4545
func TestDoubleBegin(t *testing.T) {
@@ -49,7 +49,7 @@ func TestDoubleBegin(t *testing.T) {
4949
}
5050
}()
5151

52-
m := NewSharedConfigImpl()
52+
m := NewSharedConfigImpl(nil, nil)
5353
m.BeginConfig()
5454
m.BeginConfig()
5555
}
@@ -61,12 +61,12 @@ func TestCommitWithoutBegin(t *testing.T) {
6161
}
6262
}()
6363

64-
m := NewSharedConfigImpl()
64+
m := NewSharedConfigImpl(nil, nil)
6565
m.CommitConfig()
6666
}
6767

6868
func TestRollback(t *testing.T) {
69-
m := NewSharedConfigImpl()
69+
m := NewSharedConfigImpl(nil, nil)
7070
m.pendingConfig = &chainConfig{}
7171
m.RollbackConfig()
7272
if m.pendingConfig != nil {
@@ -79,7 +79,7 @@ func TestHashingAlgorithm(t *testing.T) {
7979
invalidAlgorithm := TemplateHashingAlgorithm("MD5")
8080
validAlgorithm := DefaultHashingAlgorithm()
8181

82-
m := NewSharedConfigImpl()
82+
m := NewSharedConfigImpl(nil, nil)
8383
m.BeginConfig()
8484

8585
err := m.ProposeConfig(invalidMessage)
@@ -109,7 +109,7 @@ func TestBlockDataHashingStructure(t *testing.T) {
109109
invalidWidth := TemplateBlockDataHashingStructure(0)
110110
validWidth := DefaultBlockDataHashingStructure()
111111

112-
m := NewSharedConfigImpl()
112+
m := NewSharedConfigImpl(nil, nil)
113113
m.BeginConfig()
114114

115115
err := m.ProposeConfig(invalidMessage)
@@ -137,7 +137,7 @@ func TestBlockDataHashingStructure(t *testing.T) {
137137
func TestOrdererAddresses(t *testing.T) {
138138
invalidMessage := makeInvalidConfigItem(OrdererAddressesKey)
139139
validMessage := DefaultOrdererAddresses()
140-
m := NewSharedConfigImpl()
140+
m := NewSharedConfigImpl(nil, nil)
141141
m.BeginConfig()
142142

143143
err := m.ProposeConfig(invalidMessage)

common/configtx/handlers/msp/config.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ package msp
1818

1919
import (
2020
"fmt"
21+
"reflect"
2122

2223
"github.com/golang/protobuf/proto"
24+
"github.com/hyperledger/fabric/common/configtx/api"
2325
"github.com/hyperledger/fabric/msp"
2426
"github.com/hyperledger/fabric/protos/common"
2527
mspprotos "github.com/hyperledger/fabric/protos/msp"
@@ -65,6 +67,14 @@ func (bh *MSPConfigHandler) ProposeConfig(configItem *common.ConfigItem) error {
6567
return fmt.Errorf("Error unmarshalling msp config item, err %s", err)
6668
}
6769

70+
// TODO handle deduplication more gracefully
71+
for _, oMsp := range bh.config {
72+
if reflect.DeepEqual(oMsp, mspconfig) {
73+
// The MSP is already in the list
74+
return nil
75+
}
76+
}
77+
6878
bh.config = append(bh.config, []*mspprotos.MSPConfig{mspconfig}...)
6979
// the only way to make sure that I have a
7080
// workable config is to toss the proposed
@@ -73,3 +83,12 @@ func (bh *MSPConfigHandler) ProposeConfig(configItem *common.ConfigItem) error {
7383
bh.proposedMgr = msp.NewMSPManager()
7484
return bh.proposedMgr.Setup(bh.config)
7585
}
86+
87+
// Handler returns the associated api.Handler for the given path
88+
func (bh *MSPConfigHandler) Handler(path []string) (api.Handler, error) {
89+
if len(path) > 0 {
90+
return nil, fmt.Errorf("MSP handler does not support nested groups")
91+
}
92+
93+
return bh, nil
94+
}

0 commit comments

Comments
 (0)