Skip to content

Commit

Permalink
[FAB-5813] Remove unused configtx.Manager function
Browse files Browse the repository at this point in the history
This is a step in the series of moving the channel config from a mutable
structure, to an immutable one.  Thanks to the removal of concerns for
the configtx.Manager which need now only validate, not apply config,
there is a nice chunk of complicated dead code which can be removed.

Change-Id: Ic0f6fef509afcd7fe1fa5d2e484dbbdd2dcc9e15
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Aug 30, 2017
1 parent 6327344 commit 116e3f0
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 698 deletions.
63 changes: 6 additions & 57 deletions common/channelconfig/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"

"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/config"
oldchannelconfig "github.com/hyperledger/fabric/common/config/channel"
oldmspconfig "github.com/hyperledger/fabric/common/config/channel/msp"
"github.com/hyperledger/fabric/common/configtx"
Expand All @@ -21,7 +20,6 @@ import (
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"

"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -84,67 +82,18 @@ func (b *Bundle) ConfigtxManager() configtxapi.Manager {
return b.configtxManager
}

type noopDeserializer struct{}

// Deserializer returns nil, nil
func (nd noopDeserializer) Deserialize(key string, value []byte) (proto.Message, error) {
return nil, nil
}

type noopProposer struct {
type simpleProposer struct {
policyManager policies.Manager
}

// BeginValueProposals returns a noop deserializer, and noop proposers for subgroups
func (np noopProposer) BeginValueProposals(tx interface{}, groups []string) (config.ValueDeserializer, []config.ValueProposer, error) {
proposers := make([]config.ValueProposer, len(groups))
for i := range proposers {
proposers[i] = noopProposer{}
}
return noopDeserializer{}, proposers, nil
}

// BeginValueProposals returns a noop deserializer, and noop proposers for subgroups
func (np noopProposer) BeginPolicyProposals(tx interface{}, groups []string) ([]policies.Proposer, error) {
proposers := make([]policies.Proposer, len(groups))
for i := range proposers {
proposers[i] = noopProposer{}
}
return proposers, nil
}

// ProposePolicy returns nil, nil
func (np noopProposer) ProposePolicy(tx interface{}, name string, policy *cb.ConfigPolicy) (proto.Message, error) {
return nil, nil
}

// RollbackProposals is a no-op
func (np noopProposer) RollbackProposals(tx interface{}) {}

// PreCommit returns nil
func (np noopProposer) PreCommit(tx interface{}) error { return nil }

// CommitProposals is a no-op
func (np noopProposer) CommitProposals(tx interface{}) {}

// ValueProposer returns noopProposer
func (np noopProposer) ValueProposer() config.ValueProposer {
return np
}

// PolicyProposer returns noopProposer
func (np noopProposer) PolicyProposer() policies.Proposer {
return np
}

// RootGroupKey returns RootGroupKey constant
func (np noopProposer) RootGroupKey() string {
func (sp simpleProposer) RootGroupKey() string {
return RootGroupKey
}

// PolicyManager() returns the policy manager for considering config changes
func (np noopProposer) PolicyManager() policies.Manager {
return np.policyManager
func (sp simpleProposer) PolicyManager() policies.Manager {
return sp.policyManager
}

// NewBundleFromEnvelope wraps the NewBundle function, extracting the needed
Expand Down Expand Up @@ -216,9 +165,9 @@ func NewBundle(channelID string, config *cb.Config) (*Bundle, error) {
return nil, errors.Wrap(err, "creating envelope for configtx manager failed")
}

configtxManager, err := configtx.NewManagerImpl(env, noopProposer{
configtxManager, err := configtx.NewManagerImpl(env, simpleProposer{
policyManager: policyManager,
}, nil)
})
if err != nil {
return nil, errors.Wrap(err, "initializing configtx manager failed")
}
Expand Down
186 changes: 3 additions & 183 deletions common/config/channel/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,129 +16,10 @@ limitations under the License.

package config

import (
"fmt"

"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/config"
configtxmsp "github.com/hyperledger/fabric/common/config/channel/msp"
"github.com/hyperledger/fabric/common/configtx"
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"

"github.com/golang/protobuf/proto"
)

const RootGroupKey = "Channel"

type resources struct {
policyManager *policies.ManagerImpl
configRoot *Root
mspConfigHandler *configtxmsp.MSPConfigHandler
configtxManager configtxapi.Manager
}

// PolicyManager returns the policies.Manager for the chain
func (r *resources) PolicyManager() policies.Manager {
return r.policyManager
}

// ConfigtxManager returns the configtxapi.Manager for the chain
func (r *resources) ConfigtxManager() configtxapi.Manager {
return r.configtxManager
}

// ChannelConfig returns the api.ChannelConfig for the chain
func (r *resources) ChannelConfig() Channel {
return r.configRoot.Channel()
}

// OrdererConfig returns the api.OrdererConfig for the chain
func (r *resources) OrdererConfig() (Orderer, bool) {
result := r.configRoot.Orderer()
if result == nil {
return nil, false
}
return result, true
}

// ApplicationConfig returns the api.ApplicationConfig for the chain
func (r *resources) ApplicationConfig() (Application, bool) {
result := r.configRoot.Application()
if result == nil {
return nil, false
}
return result, true
}

// ConsortiumsConfig returns the api.ConsortiumsConfig for the chain and whether or not
// this channel contains consortiums config
func (r *resources) ConsortiumsConfig() (Consortiums, bool) {
result := r.configRoot.Consortiums()
if result == nil {
return nil, false
}

return result, true
}

// MSPManager returns the msp.MSPManager for the chain
func (r *resources) MSPManager() msp.MSPManager {
return r.mspConfigHandler
}

func newResources() *resources {
mspConfigHandler := configtxmsp.NewMSPConfigHandler()

policyProviderMap := make(map[int32]policies.Provider)
for pType := range cb.Policy_PolicyType_name {
rtype := cb.Policy_PolicyType(pType)
switch rtype {
case cb.Policy_UNKNOWN:
// Do not register a handler
case cb.Policy_SIGNATURE:
policyProviderMap[pType] = cauthdsl.NewPolicyProvider(mspConfigHandler)
case cb.Policy_MSP:
// Add hook for MSP Handler here
}
}

return &resources{
policyManager: policies.NewManagerImpl(RootGroupKey, policyProviderMap),
configRoot: NewRoot(mspConfigHandler),
mspConfigHandler: mspConfigHandler,
}
}

type policyProposerRoot struct {
policyManager *policies.ManagerImpl
initializationLogSuppression bool
}

// BeginPolicyProposals is used to start a new config proposal
func (ppr *policyProposerRoot) BeginPolicyProposals(tx interface{}, groups []string) ([]policies.Proposer, error) {
if len(groups) != 1 {
logger.Panicf("policyProposerRoot only supports having one root group")
}
return []policies.Proposer{ppr.policyManager}, nil
}

func (ppr *policyProposerRoot) ProposePolicy(tx interface{}, key string, policy *cb.ConfigPolicy) (proto.Message, error) {
return nil, fmt.Errorf("Programming error, this should never be invoked")
}

// PreCommit is a no-op and returns nil
func (ppr *policyProposerRoot) PreCommit(tx interface{}) error {
return nil
}

// RollbackConfig is used to abandon a new config proposal
func (ppr *policyProposerRoot) RollbackProposals(tx interface{}) {}

// CommitConfig is used to commit a new config proposal
func (ppr *policyProposerRoot) CommitProposals(tx interface{}) {
// TODO, move this code elsewhere
/*
if ppr.initializationLogSuppression {
ppr.initializationLogSuppression = false
}
Expand Down Expand Up @@ -176,65 +57,4 @@ func (ppr *policyProposerRoot) CommitProposals(tx interface{}) {
}
}
}
}

type Initializer struct {
*resources
configtxapi.Manager
ppr *policyProposerRoot
}

func NewWithOneTimeSuppressedPolicyWarnings(envConfig *cb.Envelope, callOnUpdate []func(*Initializer)) (*Initializer, error) {
return commonNew(envConfig, callOnUpdate, true)
}

// New creates a new channel config, complete with backing configtx manager
func New(envConfig *cb.Envelope, callOnUpdate []func(*Initializer)) (*Initializer, error) {
return commonNew(envConfig, callOnUpdate, false)
}

func commonNew(envConfig *cb.Envelope, callOnUpdate []func(*Initializer), suppressFirstPolicyWarnings bool) (*Initializer, error) {
resources := newResources()
i := &Initializer{
resources: resources,
ppr: &policyProposerRoot{
policyManager: resources.policyManager,
initializationLogSuppression: suppressFirstPolicyWarnings,
},
}
var err error
initialized := false
i.Manager, err = configtx.NewManagerImpl(envConfig, i, []func(cm configtxapi.Manager){
func(cm configtxapi.Manager) {
// This gets invoked once at instantiation before we are ready for it
// we manually do this right after
if !initialized {
return
}
for _, callback := range callOnUpdate {
callback(i)
}
},
})
if err != nil {
return nil, err
}
initialized = true
i.resources.configtxManager = i.Manager
for _, callback := range callOnUpdate {
callback(i)
}
return i, err
}

func (i *Initializer) RootGroupKey() string {
return RootGroupKey
}

func (i *Initializer) PolicyProposer() policies.Proposer {
return i.ppr
}

func (i *Initializer) ValueProposer() config.ValueProposer {
return i.resources.configRoot
}
*/
3 changes: 2 additions & 1 deletion common/config/channel/realconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"testing"

newchannelconfig "github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/config"
. "github.com/hyperledger/fabric/common/config/channel"
"github.com/hyperledger/fabric/common/config/channel/msp"
Expand Down Expand Up @@ -90,6 +91,6 @@ func TestWithRealConfigtx(t *testing.T) {
conf := localconfig.Load(localconfig.SampleSingleMSPSoloProfile)
gb := provisional.New(conf).GenesisBlockForChannel("foo")
env := utils.ExtractEnvelopeOrPanic(gb, 0)
_, err := New(env, nil)
_, err := newchannelconfig.NewBundleFromEnvelope(env)
assert.NoError(t, err)
}
29 changes: 25 additions & 4 deletions common/config/resources/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"

"github.com/hyperledger/fabric/common/cauthdsl"
newchannelconfig "github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/config"
"github.com/hyperledger/fabric/common/configtx"
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
Expand Down Expand Up @@ -83,20 +84,40 @@ func New(envConfig *cb.Envelope, mspManager msp.MSPManager, channelPolicyManager
}

var err error
b.cm, err = configtx.NewManagerImpl(envConfig, b, nil)
b.cm, err = configtx.NewManagerImpl(envConfig, b)
if err != nil {
return nil, err
}
return b, err
configEnvelope := b.cm.ConfigEnvelope()
if configEnvelope.Config == nil || configEnvelope.Config.ChannelGroup == nil {
return nil, fmt.Errorf("config is nil")
}
err = newchannelconfig.InitializePolicyManager(b.ppr, &cb.ConfigGroup{
Groups: map[string]*cb.ConfigGroup{
RootGroupKey: configEnvelope.Config.ChannelGroup,
},
})
if err != nil {
return nil, err
}

err = newchannelconfig.InitializeConfigValues(b.vpr, &cb.ConfigGroup{
Groups: map[string]*cb.ConfigGroup{
RootGroupKey: configEnvelope.Config.ChannelGroup,
},
})
if err != nil {
return nil, err
}

return b, nil
}

func (b *Bundle) RootGroupKey() string {
return RootGroupKey
}

func (b *Bundle) PolicyProposer() policies.Proposer {
logger.Debug("Boo", b)
logger.Debug("boo", b.ppr)
return b.ppr
}

Expand Down
2 changes: 1 addition & 1 deletion common/config/resources/initializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func TestBundleGreenPath(t *testing.T) {
assert.NoError(t, err)

b, err := New(env, nil, nil)
assert.NotNil(t, b)
assert.NoError(t, err)
assert.NotNil(t, b)
assert.Equal(t, "/Resources/foo", b.ResourcePolicyMapper().PolicyRefForResource("Foo"))
assert.Equal(t, "/Channel/foo", b.ResourcePolicyMapper().PolicyRefForResource("Bar"))

Expand Down
2 changes: 1 addition & 1 deletion common/config/resources/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func newValueProposerRoot() *valueProposerRoot {
type failDeserializer string

func (fd failDeserializer) Deserialize(key string, value []byte) (proto.Message, error) {
return nil, fmt.Errorf(string(fd))
return nil, fmt.Errorf("%s: for key %s", string(fd), key)
}

// BeginValueProposals is used to start a new config proposal
Expand Down
Loading

0 comments on commit 116e3f0

Please sign in to comment.