Skip to content

Commit

Permalink
[FAB-2104] Make channel shared config consistent
Browse files Browse the repository at this point in the history
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 <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Feb 11, 2017
1 parent cc11fcb commit d3419e7
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 77 deletions.
19 changes: 16 additions & 3 deletions common/configtx/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@ 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"
ab "github.com/hyperledger/fabric/protos/orderer"
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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package chainconfig
package channel

import (
"fmt"
Expand Down Expand Up @@ -45,75 +45,58 @@ 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
blockDataHashingStructureWidth uint32
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")
}
pm.pendingConfig = &chainConfig{}
}

// 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")
}
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -48,7 +49,7 @@ func TestDoubleBegin(t *testing.T) {
}
}()

m := NewDescriptorImpl()
m := NewSharedConfigImpl()
m.BeginConfig()
m.BeginConfig()
}
Expand All @@ -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 {
Expand All @@ -78,7 +79,7 @@ func TestHashingAlgorithm(t *testing.T) {
invalidAlgorithm := TemplateHashingAlgorithm("MD5")
validAlgorithm := DefaultHashingAlgorithm()

m := NewDescriptorImpl()
m := NewSharedConfigImpl()
m.BeginConfig()

err := m.ProposeConfig(invalidMessage)
Expand Down Expand Up @@ -108,7 +109,7 @@ func TestBlockDataHashingStructure(t *testing.T) {
invalidWidth := TemplateBlockDataHashingStructure(0)
validWidth := DefaultBlockDataHashingStructure()

m := NewDescriptorImpl()
m := NewSharedConfigImpl()
m.BeginConfig()

err := m.ProposeConfig(invalidMessage)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package chainconfig
package channel

import (
"math"
Expand Down
16 changes: 8 additions & 8 deletions common/configtx/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -90,7 +90,7 @@ func NewInitializer() api.Initializer {
return &resources{
handlers: handlers,
policyManager: policyManager,
chainConfig: chainConfig,
channelConfig: channelConfig,
mspConfigHandler: mspConfigHandler,
}
}
11 changes: 5 additions & 6 deletions common/mocks/configtx/configtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ 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"

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()
Expand All @@ -33,19 +33,19 @@ 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
}
return scm.HashingAlgorithmVal
}

// 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
Loading

0 comments on commit d3419e7

Please sign in to comment.