Skip to content

Commit 0797a52

Browse files
author
Jason Yellick
committed
[FAB-2321] Cleanup channel config
https://jira.hyperledger.org/browse/FAB-2321 The channel config values handling has been moved around a lot, this is the first in a series of CRs designed to cleanup the style of the config packages. Change-Id: I6456516fe182e1e37bcd9585ebff64f314941a63 Signed-off-by: Jason Yellick <[email protected]>
1 parent a971b0f commit 0797a52

27 files changed

+92
-90
lines changed

common/configtx/api/api.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ limitations under the License.
1717
package api
1818

1919
import (
20-
configvalues "github.com/hyperledger/fabric/common/configvalues/api"
20+
configvalues "github.com/hyperledger/fabric/common/configvalues"
21+
configvalueschannel "github.com/hyperledger/fabric/common/configvalues/channel"
2122
"github.com/hyperledger/fabric/common/policies"
2223
"github.com/hyperledger/fabric/msp"
2324
cb "github.com/hyperledger/fabric/protos/common"
@@ -51,7 +52,7 @@ type Resources interface {
5152
PolicyManager() policies.Manager
5253

5354
// ChannelConfig returns the ChannelConfig for the chain
54-
ChannelConfig() configvalues.Channel
55+
ChannelConfig() configvalueschannel.ConfigReader
5556

5657
// OrdererConfig returns the configtxorderer.SharedConfig for the channel
5758
OrdererConfig() configvalues.Orderer

common/configtx/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"fmt"
2121

2222
"github.com/hyperledger/fabric/common/configtx/api"
23-
configvaluesapi "github.com/hyperledger/fabric/common/configvalues/api"
23+
configvaluesapi "github.com/hyperledger/fabric/common/configvalues"
2424
"github.com/hyperledger/fabric/common/policies"
2525
cb "github.com/hyperledger/fabric/protos/common"
2626
)

common/configtx/initializer.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121

2222
"github.com/hyperledger/fabric/common/cauthdsl"
2323
"github.com/hyperledger/fabric/common/configtx/api"
24-
configvaluesapi "github.com/hyperledger/fabric/common/configvalues/api"
25-
configtxchannel "github.com/hyperledger/fabric/common/configvalues/channel"
24+
configvaluesapi "github.com/hyperledger/fabric/common/configvalues"
25+
configvalueschannel "github.com/hyperledger/fabric/common/configvalues/channel"
2626
configtxapplication "github.com/hyperledger/fabric/common/configvalues/channel/application"
2727
configtxorderer "github.com/hyperledger/fabric/common/configvalues/channel/orderer"
2828
configtxmsp "github.com/hyperledger/fabric/common/configvalues/msp"
@@ -33,7 +33,7 @@ import (
3333

3434
type resources struct {
3535
policyManager *policies.ManagerImpl
36-
channelConfig *configtxchannel.SharedConfigImpl
36+
channelConfig *configvalueschannel.Config
3737
ordererConfig *configtxorderer.ManagerImpl
3838
applicationConfig *configtxapplication.SharedConfigImpl
3939
mspConfigHandler *configtxmsp.MSPConfigHandler
@@ -45,7 +45,7 @@ func (r *resources) PolicyManager() policies.Manager {
4545
}
4646

4747
// ChannelConfig returns the api.ChannelConfig for the chain
48-
func (r *resources) ChannelConfig() configvaluesapi.Channel {
48+
func (r *resources) ChannelConfig() configvalueschannel.ConfigReader {
4949
return r.channelConfig
5050
}
5151

@@ -85,15 +85,15 @@ func newResources() *resources {
8585

8686
return &resources{
8787
policyManager: policies.NewManagerImpl(RootGroupKey, policyProviderMap),
88-
channelConfig: configtxchannel.NewSharedConfigImpl(ordererConfig, applicationConfig),
88+
channelConfig: configvalueschannel.NewConfig(ordererConfig, applicationConfig),
8989
ordererConfig: ordererConfig,
9090
applicationConfig: applicationConfig,
9191
mspConfigHandler: mspConfigHandler,
9292
}
9393
}
9494

9595
type valueProposerRoot struct {
96-
channelConfig *configtxchannel.SharedConfigImpl
96+
channelConfig *configvalueschannel.Config
9797
mspConfigHandler *configtxmsp.MSPConfigHandler
9898
}
9999

common/configvalues/api/api.go common/configvalues/api.go

-14
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@ import (
2424
pb "github.com/hyperledger/fabric/protos/peer"
2525
)
2626

27-
// Channel stores the common channel config
28-
type Channel interface {
29-
// HashingAlgorithm returns the default algorithm to be used when hashing
30-
// such as computing block hashes, and CreationPolicy digests
31-
HashingAlgorithm() func(input []byte) []byte
32-
33-
// BlockDataHashingStructureWidth returns the width to use when constructing the
34-
// Merkle tree to compute the BlockData hash
35-
BlockDataHashingStructureWidth() uint32
36-
37-
// OrdererAddresses returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver
38-
OrdererAddresses() []string
39-
}
40-
4127
// Org stores the common organizational config
4228
type Org interface {
4329
// Name returns the name this org is referred to in config

common/configvalues/channel/application/organization.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package application
1919
import (
2020
"fmt"
2121

22-
"github.com/hyperledger/fabric/common/configvalues/api"
22+
"github.com/hyperledger/fabric/common/configvalues"
2323
"github.com/hyperledger/fabric/common/configvalues/channel/common/organization"
2424
mspconfig "github.com/hyperledger/fabric/common/configvalues/msp"
2525
cb "github.com/hyperledger/fabric/protos/common"

common/configvalues/channel/application/organization_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package application
1919
import (
2020
"testing"
2121

22-
"github.com/hyperledger/fabric/common/configvalues/api"
22+
api "github.com/hyperledger/fabric/common/configvalues"
2323
cb "github.com/hyperledger/fabric/protos/common"
2424
pb "github.com/hyperledger/fabric/protos/peer"
2525

common/configvalues/channel/application/sharedconfig.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package application
1818

1919
import (
20-
"github.com/hyperledger/fabric/common/configvalues/api"
20+
api "github.com/hyperledger/fabric/common/configvalues"
2121
"github.com/hyperledger/fabric/common/configvalues/channel/common/organization"
2222
"github.com/hyperledger/fabric/common/configvalues/msp"
2323
cb "github.com/hyperledger/fabric/protos/common"

common/configvalues/channel/application/sharedconfig_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package application
1919
import (
2020
"testing"
2121

22-
"github.com/hyperledger/fabric/common/configvalues/api"
22+
api "github.com/hyperledger/fabric/common/configvalues"
2323

2424
logging "github.com/op/go-logging"
2525
)

common/configvalues/channel/common/organization/organization.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package organization
1919
import (
2020
"fmt"
2121

22-
"github.com/hyperledger/fabric/common/configvalues/api"
22+
"github.com/hyperledger/fabric/common/configvalues"
2323
mspconfig "github.com/hyperledger/fabric/common/configvalues/msp"
2424
"github.com/hyperledger/fabric/msp"
2525
cb "github.com/hyperledger/fabric/protos/common"

common/configvalues/channel/common/organization/organization_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package organization
1919
import (
2020
"testing"
2121

22-
"github.com/hyperledger/fabric/common/configvalues/api"
22+
api "github.com/hyperledger/fabric/common/configvalues"
2323

2424
logging "github.com/op/go-logging"
2525
)

common/configvalues/channel/config.go

+44-31
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"fmt"
2121
"math"
2222

23-
"github.com/hyperledger/fabric/common/configvalues/api"
23+
api "github.com/hyperledger/fabric/common/configvalues"
2424
"github.com/hyperledger/fabric/common/configvalues/channel/application"
2525
"github.com/hyperledger/fabric/common/configvalues/channel/orderer"
2626
"github.com/hyperledger/fabric/common/util"
@@ -45,7 +45,7 @@ var Schema = &cb.ConfigGroupSchema{
4545
},
4646
}
4747

48-
// Chain config keys
48+
// Channel config keys
4949
const (
5050
// HashingAlgorithmKey is the cb.ConfigItem type key name for the HashingAlgorithm message
5151
HashingAlgorithmKey = "HashingAlgorithm"
@@ -63,87 +63,100 @@ const (
6363
SHA3Shake256 = "SHAKE256"
6464
)
6565

66-
var logger = logging.MustGetLogger("configtx/handlers/chainconfig")
66+
var logger = logging.MustGetLogger("configvalues/channel")
6767

68-
type chainConfig struct {
68+
type ConfigReader interface {
69+
// HashingAlgorithm returns the default algorithm to be used when hashing
70+
// such as computing block hashes, and CreationPolicy digests
71+
HashingAlgorithm() func(input []byte) []byte
72+
73+
// BlockDataHashingStructureWidth returns the width to use when constructing the
74+
// Merkle tree to compute the BlockData hash
75+
BlockDataHashingStructureWidth() uint32
76+
77+
// OrdererAddresses returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver
78+
OrdererAddresses() []string
79+
}
80+
81+
type values struct {
6982
hashingAlgorithm func(input []byte) []byte
7083
blockDataHashingStructureWidth uint32
7184
ordererAddresses []string
7285
}
7386

7487
// SharedConfigImpl is an implementation of Manager and configtx.ConfigHandler
7588
// In general, it should only be referenced as an Impl for the configtx.Manager
76-
type SharedConfigImpl struct {
77-
pendingConfig *chainConfig
78-
config *chainConfig
89+
type Config struct {
90+
pending *values
91+
current *values
7992

8093
ordererConfig *orderer.ManagerImpl
8194
applicationConfig *application.SharedConfigImpl
8295
}
8396

8497
// NewSharedConfigImpl creates a new SharedConfigImpl with the given CryptoHelper
85-
func NewSharedConfigImpl(ordererConfig *orderer.ManagerImpl, applicationConfig *application.SharedConfigImpl) *SharedConfigImpl {
86-
return &SharedConfigImpl{
87-
config: &chainConfig{},
98+
func NewConfig(ordererConfig *orderer.ManagerImpl, applicationConfig *application.SharedConfigImpl) *Config {
99+
return &Config{
100+
current: &values{},
88101
ordererConfig: ordererConfig,
89102
applicationConfig: applicationConfig,
90103
}
91104
}
92105

93106
// HashingAlgorithm returns a function pointer to the chain hashing algorihtm
94-
func (pm *SharedConfigImpl) HashingAlgorithm() func(input []byte) []byte {
95-
return pm.config.hashingAlgorithm
107+
func (c *Config) HashingAlgorithm() func(input []byte) []byte {
108+
return c.current.hashingAlgorithm
96109
}
97110

98111
// BlockDataHashingStructure returns the width to use when forming the block data hashing structure
99-
func (pm *SharedConfigImpl) BlockDataHashingStructureWidth() uint32 {
100-
return pm.config.blockDataHashingStructureWidth
112+
func (c *Config) BlockDataHashingStructureWidth() uint32 {
113+
return c.current.blockDataHashingStructureWidth
101114
}
102115

103116
// OrdererAddresses returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver
104-
func (pm *SharedConfigImpl) OrdererAddresses() []string {
105-
return pm.config.ordererAddresses
117+
func (c *Config) OrdererAddresses() []string {
118+
return c.current.ordererAddresses
106119
}
107120

108121
// BeginValueProposals is used to start a new config proposal
109-
func (pm *SharedConfigImpl) BeginValueProposals(groups []string) ([]api.ValueProposer, error) {
122+
func (c *Config) BeginValueProposals(groups []string) ([]api.ValueProposer, error) {
110123
handlers := make([]api.ValueProposer, len(groups))
111124

112125
for i, group := range groups {
113126
switch group {
114127
case application.GroupKey:
115-
handlers[i] = pm.applicationConfig
128+
handlers[i] = c.applicationConfig
116129
case orderer.GroupKey:
117-
handlers[i] = pm.ordererConfig
130+
handlers[i] = c.ordererConfig
118131
default:
119132
return nil, fmt.Errorf("Disallowed channel group: %s", group)
120133
}
121134
}
122135

123-
if pm.pendingConfig != nil {
136+
if c.pending != nil {
124137
logger.Panicf("Programming error, cannot call begin in the middle of a proposal")
125138
}
126139

127-
pm.pendingConfig = &chainConfig{}
140+
c.pending = &values{}
128141
return handlers, nil
129142
}
130143

131144
// RollbackProposals is used to abandon a new config proposal
132-
func (pm *SharedConfigImpl) RollbackProposals() {
133-
pm.pendingConfig = nil
145+
func (c *Config) RollbackProposals() {
146+
c.pending = nil
134147
}
135148

136149
// CommitProposals is used to commit a new config proposal
137-
func (pm *SharedConfigImpl) CommitProposals() {
138-
if pm.pendingConfig == nil {
150+
func (c *Config) CommitProposals() {
151+
if c.pending == nil {
139152
logger.Panicf("Programming error, cannot call commit without an existing proposal")
140153
}
141-
pm.config = pm.pendingConfig
142-
pm.pendingConfig = nil
154+
c.current = c.pending
155+
c.pending = nil
143156
}
144157

145158
// ProposeValue is used to add new config to the config proposal
146-
func (pm *SharedConfigImpl) ProposeValue(key string, configValue *cb.ConfigValue) error {
159+
func (c *Config) ProposeValue(key string, configValue *cb.ConfigValue) error {
147160
switch key {
148161
case HashingAlgorithmKey:
149162
hashingAlgorithm := &cb.HashingAlgorithm{}
@@ -152,7 +165,7 @@ func (pm *SharedConfigImpl) ProposeValue(key string, configValue *cb.ConfigValue
152165
}
153166
switch hashingAlgorithm.Name {
154167
case SHA3Shake256:
155-
pm.pendingConfig.hashingAlgorithm = util.ComputeCryptoHash
168+
c.pending.hashingAlgorithm = util.ComputeCryptoHash
156169
default:
157170
return fmt.Errorf("Unknown hashing algorithm type: %s", hashingAlgorithm.Name)
158171
}
@@ -166,13 +179,13 @@ func (pm *SharedConfigImpl) ProposeValue(key string, configValue *cb.ConfigValue
166179
return fmt.Errorf("BlockDataHashStructure width only supported at MaxUint32 in this version")
167180
}
168181

169-
pm.pendingConfig.blockDataHashingStructureWidth = blockDataHashingStructure.Width
182+
c.pending.blockDataHashingStructureWidth = blockDataHashingStructure.Width
170183
case OrdererAddressesKey:
171184
ordererAddresses := &cb.OrdererAddresses{}
172185
if err := proto.Unmarshal(configValue.Value, ordererAddresses); err != nil {
173186
return fmt.Errorf("Unmarshaling error for HashingAlgorithm: %s", err)
174187
}
175-
pm.pendingConfig.ordererAddresses = ordererAddresses.Addresses
188+
c.pending.ordererAddresses = ordererAddresses.Addresses
176189
default:
177190
logger.Warningf("Uknown Chain config item with key %s", key)
178191
}

common/configvalues/channel/config_test.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"reflect"
2121
"testing"
2222

23-
"github.com/hyperledger/fabric/common/configvalues/api"
2423
cb "github.com/hyperledger/fabric/protos/common"
2524

2625
logging "github.com/op/go-logging"
@@ -44,7 +43,7 @@ func makeInvalidConfigValue() *cb.ConfigValue {
4443
}
4544

4645
func TestInterface(t *testing.T) {
47-
_ = api.Channel(NewSharedConfigImpl(nil, nil))
46+
_ = ConfigReader(NewConfig(nil, nil))
4847
}
4948

5049
func TestDoubleBegin(t *testing.T) {
@@ -54,7 +53,7 @@ func TestDoubleBegin(t *testing.T) {
5453
}
5554
}()
5655

57-
m := NewSharedConfigImpl(nil, nil)
56+
m := NewConfig(nil, nil)
5857
m.BeginValueProposals(nil)
5958
m.BeginValueProposals(nil)
6059
}
@@ -66,15 +65,15 @@ func TestCommitWithoutBegin(t *testing.T) {
6665
}
6766
}()
6867

69-
m := NewSharedConfigImpl(nil, nil)
68+
m := NewConfig(nil, nil)
7069
m.CommitProposals()
7170
}
7271

7372
func TestRollback(t *testing.T) {
74-
m := NewSharedConfigImpl(nil, nil)
75-
m.pendingConfig = &chainConfig{}
73+
m := NewConfig(nil, nil)
74+
m.pending = &values{}
7675
m.RollbackProposals()
77-
if m.pendingConfig != nil {
76+
if m.pending != nil {
7877
t.Fatalf("Should have cleared pending config on rollback")
7978
}
8079
}
@@ -84,7 +83,7 @@ func TestHashingAlgorithm(t *testing.T) {
8483
invalidAlgorithm := TemplateHashingAlgorithm("MD5")
8584
validAlgorithm := DefaultHashingAlgorithm()
8685

87-
m := NewSharedConfigImpl(nil, nil)
86+
m := NewConfig(nil, nil)
8887
m.BeginValueProposals(nil)
8988

9089
err := m.ProposeValue(HashingAlgorithmKey, invalidMessage)
@@ -114,7 +113,7 @@ func TestBlockDataHashingStructure(t *testing.T) {
114113
invalidWidth := TemplateBlockDataHashingStructure(0)
115114
validWidth := DefaultBlockDataHashingStructure()
116115

117-
m := NewSharedConfigImpl(nil, nil)
116+
m := NewConfig(nil, nil)
118117
m.BeginValueProposals(nil)
119118

120119
err := m.ProposeValue(BlockDataHashingStructureKey, invalidMessage)
@@ -142,7 +141,7 @@ func TestBlockDataHashingStructure(t *testing.T) {
142141
func TestOrdererAddresses(t *testing.T) {
143142
invalidMessage := makeInvalidConfigValue()
144143
validMessage := DefaultOrdererAddresses()
145-
m := NewSharedConfigImpl(nil, nil)
144+
m := NewConfig(nil, nil)
146145
m.BeginValueProposals(nil)
147146

148147
err := m.ProposeValue(OrdererAddressesKey, invalidMessage)

0 commit comments

Comments
 (0)