From d3419e71f006513df279e9a870a55a3e03a2a5b8 Mon Sep 17 00:00:00 2001 From: Jason Yellick Date: Tue, 7 Feb 2017 16:02:53 -0500 Subject: [PATCH] [FAB-2104] Make channel shared config consistent https://jira.hyperledger.org/browse/FAB-2104 The orderer and application shared config have just been moved under common/configtx. The channel configuration should be normalized to this pattern and location as well. Change-Id: I3cda8b73a4c94ff528db6e5cdbc987b4c69f90f8 Signed-off-by: Jason Yellick --- common/configtx/api/api.go | 19 ++++++-- .../handlers/channel/sharedconfig.go} | 45 ++++++------------- .../handlers/channel/sharedconfig_test.go} | 17 +++---- .../handlers/channel/sharedconfig_util.go} | 2 +- common/configtx/resources.go | 16 +++---- common/mocks/configtx/configtx.go | 11 +++-- .../handlers/channel/sharedconfig.go} | 12 ++--- .../handlers/channel/sharedconfig_test.go} | 6 +-- .../bootstrap/provisional/provisional.go | 8 ++-- orderer/multichain/systemchain.go | 3 +- orderer/multichain/systemchain_test.go | 9 ++-- 11 files changed, 71 insertions(+), 77 deletions(-) rename common/{chainconfig/chainconfig.go => configtx/handlers/channel/sharedconfig.go} (73%) rename common/{chainconfig/chainconfig_test.go => configtx/handlers/channel/sharedconfig_test.go} (92%) rename common/{chainconfig/chainconfig_util.go => configtx/handlers/channel/sharedconfig_util.go} (99%) rename common/mocks/{chainconfig/chainconfig.go => configtx/handlers/channel/sharedconfig.go} (83%) rename common/mocks/{chainconfig/chainconfig_test.go => configtx/handlers/channel/sharedconfig_test.go} (83%) diff --git a/common/configtx/api/api.go b/common/configtx/api/api.go index 7708473c219..a8a75446964 100644 --- a/common/configtx/api/api.go +++ b/common/configtx/api/api.go @@ -19,7 +19,6 @@ package api import ( "time" - "github.com/hyperledger/fabric/common/chainconfig" "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/msp" cb "github.com/hyperledger/fabric/protos/common" @@ -27,6 +26,20 @@ import ( pb "github.com/hyperledger/fabric/protos/peer" ) +// ChannelConfig stores the common channel config +type ChannelConfig interface { + // HashingAlgorithm returns the default algorithm to be used when hashing + // such as computing block hashes, and CreationPolicy digests + HashingAlgorithm() func(input []byte) []byte + + // BlockDataHashingStructureWidth returns the width to use when constructing the + // Merkle tree to compute the BlockData hash + BlockDataHashingStructureWidth() uint32 + + // OrdererAddresses returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver + OrdererAddresses() []string +} + // ApplicationConfig stores the common shared application config type ApplicationConfig interface { // AnchorPeers returns the list of gossip anchor peers @@ -99,8 +112,8 @@ type Resources interface { // PolicyManager returns the policies.Manager for the chain PolicyManager() policies.Manager - // ChainConfig returns the chainconfig.Descriptor for the chain - ChainConfig() chainconfig.Descriptor + // ChannelConfig returns the ChannelConfig for the chain + ChannelConfig() ChannelConfig // MSPManager returns the msp.MSPManager for the chain MSPManager() msp.MSPManager diff --git a/common/chainconfig/chainconfig.go b/common/configtx/handlers/channel/sharedconfig.go similarity index 73% rename from common/chainconfig/chainconfig.go rename to common/configtx/handlers/channel/sharedconfig.go index f5f7c22573e..59c96072956 100644 --- a/common/chainconfig/chainconfig.go +++ b/common/configtx/handlers/channel/sharedconfig.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package chainconfig +package channel import ( "fmt" @@ -45,24 +45,7 @@ const ( SHA3Shake256 = "SHAKE256" ) -var logger = logging.MustGetLogger("common/chainconfig") - -// Descriptor stores the common chain config -// It is intended to be the primary accessor of DescriptorImpl -// It is intended to discourage use of the other exported DescriptorImpl methods -// which are used for updating the chain config by the configtx.Manager -type Descriptor interface { - // HashingAlgorithm returns the default algorithm to be used when hashing - // such as computing block hashes, and CreationPolicy digests - HashingAlgorithm() func(input []byte) []byte - - // BlockDataHashingStructureWidth returns the width to use when constructing the - // Merkle tree to compute the BlockData hash - BlockDataHashingStructureWidth() uint32 - - // OrdererAddresses returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver - OrdererAddresses() []string -} +var logger = logging.MustGetLogger("configtx/handlers/chainconfig") type chainConfig struct { hashingAlgorithm func(input []byte) []byte @@ -70,37 +53,37 @@ type chainConfig struct { ordererAddresses []string } -// DescriptorImpl is an implementation of Manager and configtx.ConfigHandler +// SharedConfigImpl is an implementation of Manager and configtx.ConfigHandler // In general, it should only be referenced as an Impl for the configtx.Manager -type DescriptorImpl struct { +type SharedConfigImpl struct { pendingConfig *chainConfig config *chainConfig } -// NewDescriptorImpl creates a new DescriptorImpl with the given CryptoHelper -func NewDescriptorImpl() *DescriptorImpl { - return &DescriptorImpl{ +// NewSharedConfigImpl creates a new SharedConfigImpl with the given CryptoHelper +func NewSharedConfigImpl() *SharedConfigImpl { + return &SharedConfigImpl{ config: &chainConfig{}, } } // HashingAlgorithm returns a function pointer to the chain hashing algorihtm -func (pm *DescriptorImpl) HashingAlgorithm() func(input []byte) []byte { +func (pm *SharedConfigImpl) HashingAlgorithm() func(input []byte) []byte { return pm.config.hashingAlgorithm } // BlockDataHashingStructure returns the width to use when forming the block data hashing structure -func (pm *DescriptorImpl) BlockDataHashingStructureWidth() uint32 { +func (pm *SharedConfigImpl) BlockDataHashingStructureWidth() uint32 { return pm.config.blockDataHashingStructureWidth } // OrdererAddresses returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver -func (pm *DescriptorImpl) OrdererAddresses() []string { +func (pm *SharedConfigImpl) OrdererAddresses() []string { return pm.config.ordererAddresses } // BeginConfig is used to start a new config proposal -func (pm *DescriptorImpl) BeginConfig() { +func (pm *SharedConfigImpl) BeginConfig() { if pm.pendingConfig != nil { logger.Panicf("Programming error, cannot call begin in the middle of a proposal") } @@ -108,12 +91,12 @@ func (pm *DescriptorImpl) BeginConfig() { } // RollbackConfig is used to abandon a new config proposal -func (pm *DescriptorImpl) RollbackConfig() { +func (pm *SharedConfigImpl) RollbackConfig() { pm.pendingConfig = nil } // CommitConfig is used to commit a new config proposal -func (pm *DescriptorImpl) CommitConfig() { +func (pm *SharedConfigImpl) CommitConfig() { if pm.pendingConfig == nil { logger.Panicf("Programming error, cannot call commit without an existing proposal") } @@ -122,7 +105,7 @@ func (pm *DescriptorImpl) CommitConfig() { } // ProposeConfig is used to add new config to the config proposal -func (pm *DescriptorImpl) ProposeConfig(configItem *cb.ConfigItem) error { +func (pm *SharedConfigImpl) ProposeConfig(configItem *cb.ConfigItem) error { if configItem.Type != cb.ConfigItem_CHAIN { return fmt.Errorf("Expected type of ConfigItem_Chain, got %v", configItem.Type) } diff --git a/common/chainconfig/chainconfig_test.go b/common/configtx/handlers/channel/sharedconfig_test.go similarity index 92% rename from common/chainconfig/chainconfig_test.go rename to common/configtx/handlers/channel/sharedconfig_test.go index 96dd0ae4444..1c145092343 100644 --- a/common/chainconfig/chainconfig_test.go +++ b/common/configtx/handlers/channel/sharedconfig_test.go @@ -14,12 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. */ -package chainconfig +package channel import ( "reflect" "testing" + configtxapi "github.com/hyperledger/fabric/common/configtx/api" cb "github.com/hyperledger/fabric/protos/common" logging "github.com/op/go-logging" @@ -38,7 +39,7 @@ func makeInvalidConfigItem(key string) *cb.ConfigItem { } func TestInterface(t *testing.T) { - _ = Descriptor(NewDescriptorImpl()) + _ = configtxapi.ChannelConfig(NewSharedConfigImpl()) } func TestDoubleBegin(t *testing.T) { @@ -48,7 +49,7 @@ func TestDoubleBegin(t *testing.T) { } }() - m := NewDescriptorImpl() + m := NewSharedConfigImpl() m.BeginConfig() m.BeginConfig() } @@ -60,12 +61,12 @@ func TestCommitWithoutBegin(t *testing.T) { } }() - m := NewDescriptorImpl() + m := NewSharedConfigImpl() m.CommitConfig() } func TestRollback(t *testing.T) { - m := NewDescriptorImpl() + m := NewSharedConfigImpl() m.pendingConfig = &chainConfig{} m.RollbackConfig() if m.pendingConfig != nil { @@ -78,7 +79,7 @@ func TestHashingAlgorithm(t *testing.T) { invalidAlgorithm := TemplateHashingAlgorithm("MD5") validAlgorithm := DefaultHashingAlgorithm() - m := NewDescriptorImpl() + m := NewSharedConfigImpl() m.BeginConfig() err := m.ProposeConfig(invalidMessage) @@ -108,7 +109,7 @@ func TestBlockDataHashingStructure(t *testing.T) { invalidWidth := TemplateBlockDataHashingStructure(0) validWidth := DefaultBlockDataHashingStructure() - m := NewDescriptorImpl() + m := NewSharedConfigImpl() m.BeginConfig() err := m.ProposeConfig(invalidMessage) @@ -136,7 +137,7 @@ func TestBlockDataHashingStructure(t *testing.T) { func TestOrdererAddresses(t *testing.T) { invalidMessage := makeInvalidConfigItem(OrdererAddressesKey) validMessage := DefaultOrdererAddresses() - m := NewDescriptorImpl() + m := NewSharedConfigImpl() m.BeginConfig() err := m.ProposeConfig(invalidMessage) diff --git a/common/chainconfig/chainconfig_util.go b/common/configtx/handlers/channel/sharedconfig_util.go similarity index 99% rename from common/chainconfig/chainconfig_util.go rename to common/configtx/handlers/channel/sharedconfig_util.go index c60c5162414..8496b32095c 100644 --- a/common/chainconfig/chainconfig_util.go +++ b/common/configtx/handlers/channel/sharedconfig_util.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package chainconfig +package channel import ( "math" diff --git a/common/configtx/resources.go b/common/configtx/resources.go index 0655a0cac1a..9279581b6f0 100644 --- a/common/configtx/resources.go +++ b/common/configtx/resources.go @@ -18,8 +18,8 @@ package configtx import ( "github.com/hyperledger/fabric/common/cauthdsl" - "github.com/hyperledger/fabric/common/chainconfig" "github.com/hyperledger/fabric/common/configtx/api" + "github.com/hyperledger/fabric/common/configtx/handlers/channel" "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/msp" mspmgmt "github.com/hyperledger/fabric/msp/mgmt" @@ -29,7 +29,7 @@ import ( type resources struct { handlers map[cb.ConfigItem_ConfigType]api.Handler policyManager policies.Manager - chainConfig chainconfig.Descriptor + channelConfig api.ChannelConfig mspConfigHandler *mspmgmt.MSPConfigHandler } @@ -38,9 +38,9 @@ func (r *resources) PolicyManager() policies.Manager { return r.policyManager } -// ChainConfig returns the chainconfig.Descriptor for the chain -func (r *resources) ChainConfig() chainconfig.Descriptor { - return r.chainConfig +// ChannelConfig returns the api.ChannelConfig for the chain +func (r *resources) ChannelConfig() api.ChannelConfig { + return r.channelConfig } // MSPManager returns the msp.MSPManager for the chain @@ -70,14 +70,14 @@ func NewInitializer() api.Initializer { } policyManager := policies.NewManagerImpl(policyProviderMap) - chainConfig := chainconfig.NewDescriptorImpl() + channelConfig := channel.NewSharedConfigImpl() handlers := make(map[cb.ConfigItem_ConfigType]api.Handler) for ctype := range cb.ConfigItem_ConfigType_name { rtype := cb.ConfigItem_ConfigType(ctype) switch rtype { case cb.ConfigItem_CHAIN: - handlers[rtype] = chainConfig + handlers[rtype] = channelConfig case cb.ConfigItem_POLICY: handlers[rtype] = policyManager case cb.ConfigItem_MSP: @@ -90,7 +90,7 @@ func NewInitializer() api.Initializer { return &resources{ handlers: handlers, policyManager: policyManager, - chainConfig: chainConfig, + channelConfig: channelConfig, mspConfigHandler: mspConfigHandler, } } diff --git a/common/mocks/configtx/configtx.go b/common/mocks/configtx/configtx.go index d302084a026..4300309328a 100644 --- a/common/mocks/configtx/configtx.go +++ b/common/mocks/configtx/configtx.go @@ -17,7 +17,6 @@ limitations under the License. package configtx import ( - "github.com/hyperledger/fabric/common/chainconfig" configtxapi "github.com/hyperledger/fabric/common/configtx/api" "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/msp" @@ -31,8 +30,8 @@ type Initializer struct { // PolicyManagerVal is returned as the result of PolicyManager() PolicyManagerVal policies.Manager - // ChainConfigVal is returned as the result of ChainConfig() - ChainConfigVal chainconfig.Descriptor + // ChannelConfigVal is returned as the result of ChannelConfig() + ChannelConfigVal configtxapi.ChannelConfig // MSPManagerVal is returned as the result of MSPManager() MSPManagerVal msp.MSPManager @@ -48,9 +47,9 @@ func (i *Initializer) PolicyManager() policies.Manager { return i.PolicyManagerVal } -// Returns the ChainConfigVal -func (i *Initializer) ChainConfig() chainconfig.Descriptor { - return i.ChainConfigVal +// Returns the ChannelConfigVal +func (i *Initializer) ChannelConfig() configtxapi.ChannelConfig { + return i.ChannelConfigVal } // Returns the MSPManagerVal diff --git a/common/mocks/chainconfig/chainconfig.go b/common/mocks/configtx/handlers/channel/sharedconfig.go similarity index 83% rename from common/mocks/chainconfig/chainconfig.go rename to common/mocks/configtx/handlers/channel/sharedconfig.go index 456112c21a3..6e4919a10f3 100644 --- a/common/mocks/chainconfig/chainconfig.go +++ b/common/mocks/configtx/handlers/channel/sharedconfig.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package chainconfig +package channel import "github.com/hyperledger/fabric/common/util" @@ -22,8 +22,8 @@ func nearIdentityHash(input []byte) []byte { return util.ConcatenateBytes([]byte("FakeHash("), input, []byte("")) } -// Descriptor is a mock implementation of sharedconfig.Descriptor -type Descriptor struct { +// SharedConfig is a mock implementation of sharedconfig.SharedConfig +type SharedConfig struct { // HashingAlgorithmVal is returned as the result of HashingAlgorithm() if set HashingAlgorithmVal func([]byte) []byte // BlockDataHashingStructureWidthVal is returned as the result of BlockDataHashingStructureWidth() @@ -33,7 +33,7 @@ type Descriptor struct { } // HashingAlgorithm returns the HashingAlgorithmVal if set, otherwise a fake simple hash function -func (scm *Descriptor) HashingAlgorithm() func([]byte) []byte { +func (scm *SharedConfig) HashingAlgorithm() func([]byte) []byte { if scm.HashingAlgorithmVal == nil { return nearIdentityHash } @@ -41,11 +41,11 @@ func (scm *Descriptor) HashingAlgorithm() func([]byte) []byte { } // BlockDataHashingStructureWidth returns the BlockDataHashingStructureWidthVal -func (scm *Descriptor) BlockDataHashingStructureWidth() uint32 { +func (scm *SharedConfig) BlockDataHashingStructureWidth() uint32 { return scm.BlockDataHashingStructureWidthVal } // OrdererAddresses returns the OrdererAddressesVal -func (scm *Descriptor) OrdererAddresses() []string { +func (scm *SharedConfig) OrdererAddresses() []string { return scm.OrdererAddressesVal } diff --git a/common/mocks/chainconfig/chainconfig_test.go b/common/mocks/configtx/handlers/channel/sharedconfig_test.go similarity index 83% rename from common/mocks/chainconfig/chainconfig_test.go rename to common/mocks/configtx/handlers/channel/sharedconfig_test.go index ba93bc97e1e..dd45651f0d8 100644 --- a/common/mocks/chainconfig/chainconfig_test.go +++ b/common/mocks/configtx/handlers/channel/sharedconfig_test.go @@ -14,14 +14,14 @@ See the License for the specific language governing permissions and limitations under the License. */ -package chainconfig +package channel import ( "testing" - "github.com/hyperledger/fabric/common/chainconfig" + configtxapi "github.com/hyperledger/fabric/common/configtx/api" ) func TestChainConfigInterface(t *testing.T) { - _ = chainconfig.Descriptor(&Descriptor{}) + _ = configtxapi.ChannelConfig(&SharedConfig{}) } diff --git a/orderer/common/bootstrap/provisional/provisional.go b/orderer/common/bootstrap/provisional/provisional.go index 5ab1213baee..83a6b58f034 100644 --- a/orderer/common/bootstrap/provisional/provisional.go +++ b/orderer/common/bootstrap/provisional/provisional.go @@ -20,8 +20,8 @@ import ( "fmt" "github.com/hyperledger/fabric/common/cauthdsl" - "github.com/hyperledger/fabric/common/chainconfig" "github.com/hyperledger/fabric/common/configtx" + configtxchannel "github.com/hyperledger/fabric/common/configtx/handlers/channel" configtxorderer "github.com/hyperledger/fabric/common/configtx/handlers/orderer" "github.com/hyperledger/fabric/common/genesis" "github.com/hyperledger/fabric/orderer/common/bootstrap" @@ -69,9 +69,9 @@ func New(conf *config.TopLevel) Generator { bs := &bootstrapper{ minimalItems: []*cb.ConfigItem{ // Chain Config Types - chainconfig.DefaultHashingAlgorithm(), - chainconfig.DefaultBlockDataHashingStructure(), - chainconfig.TemplateOrdererAddresses([]string{fmt.Sprintf("%s:%d", conf.General.ListenAddress, conf.General.ListenPort)}), + configtxchannel.DefaultHashingAlgorithm(), + configtxchannel.DefaultBlockDataHashingStructure(), + configtxchannel.TemplateOrdererAddresses([]string{fmt.Sprintf("%s:%d", conf.General.ListenAddress, conf.General.ListenPort)}), // Orderer Config Types configtxorderer.TemplateConsensusType(conf.Genesis.OrdererType), diff --git a/orderer/multichain/systemchain.go b/orderer/multichain/systemchain.go index 84eae7e636d..5e59cf63d31 100644 --- a/orderer/multichain/systemchain.go +++ b/orderer/multichain/systemchain.go @@ -17,7 +17,6 @@ limitations under the License. package multichain import ( - "github.com/hyperledger/fabric/common/chainconfig" "github.com/hyperledger/fabric/common/configtx" configtxapi "github.com/hyperledger/fabric/common/configtx/api" "github.com/hyperledger/fabric/common/policies" @@ -38,7 +37,7 @@ type limitedSupport interface { ChainID() string PolicyManager() policies.Manager SharedConfig() configtxapi.OrdererConfig - ChainConfig() chainconfig.Descriptor + ChannelConfig() configtxapi.ChannelConfig Enqueue(env *cb.Envelope) bool } diff --git a/orderer/multichain/systemchain_test.go b/orderer/multichain/systemchain_test.go index 48cee247695..7a97ef5bb2a 100644 --- a/orderer/multichain/systemchain_test.go +++ b/orderer/multichain/systemchain_test.go @@ -20,10 +20,9 @@ import ( "reflect" "testing" - "github.com/hyperledger/fabric/common/chainconfig" "github.com/hyperledger/fabric/common/configtx" configtxapi "github.com/hyperledger/fabric/common/configtx/api" - mockchainconfig "github.com/hyperledger/fabric/common/mocks/chainconfig" + mockconfigtxchannel "github.com/hyperledger/fabric/common/mocks/configtx/handlers/channel" mockconfigtxorderer "github.com/hyperledger/fabric/common/mocks/configtx/handlers/orderer" "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/orderer/common/bootstrap/provisional" @@ -53,7 +52,7 @@ type mockSupport struct { msc *mockconfigtxorderer.SharedConfig chainID string queue []*cb.Envelope - chainConfig *mockchainconfig.Descriptor + chainConfig *mockconfigtxchannel.SharedConfig } func newMockSupport(chainID string) *mockSupport { @@ -61,7 +60,7 @@ func newMockSupport(chainID string) *mockSupport { mpm: &mockPolicyManager{}, msc: &mockconfigtxorderer.SharedConfig{}, chainID: chainID, - chainConfig: &mockchainconfig.Descriptor{}, + chainConfig: &mockconfigtxchannel.SharedConfig{}, } } @@ -82,7 +81,7 @@ func (ms *mockSupport) SharedConfig() configtxapi.OrdererConfig { return ms.msc } -func (ms *mockSupport) ChainConfig() chainconfig.Descriptor { +func (ms *mockSupport) ChannelConfig() configtxapi.ChannelConfig { return ms.chainConfig }