Skip to content

Commit

Permalink
[FAB-2126] Move MSP configtx Handler to handlers
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-2126

The other handlers have been moved from their component pieces to reside
within configtx/handlers in preparation of the impending configuration
overhaul, this does the same for the last remaining handler (MSP).

Change-Id: Idec422b2402fab6b5e036b1a5cdabfc2fc88ad72
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Feb 11, 2017
1 parent d3419e7 commit 4ae2508
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 82 deletions.
74 changes: 6 additions & 68 deletions msp/mgmt/config.go → common/configtx/handlers/msp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,79 +14,22 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package mgmt
package msp

import (
"fmt"
"os"
"path/filepath"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protos/common"
mspprotos "github.com/hyperledger/fabric/protos/msp"
)

func LoadLocalMsp(dir string) error {
conf, err := msp.GetLocalMspConfig(dir)
if err != nil {
return err
}

return GetLocalMSP().Setup(conf)
}

func getConfigPath(dir string) (string, error) {
// Try to read the dir
if _, err := os.Stat(dir); err != nil {
cfg := os.Getenv("PEER_CFG_PATH")
if cfg != "" {
dir = filepath.Join(cfg, dir)
} else {
dir = filepath.Join(os.Getenv("GOPATH"), "/src/github.com/hyperledger/fabric/msp/sampleconfig/")
}
if _, err := os.Stat(dir); err != nil {
return "", err
}
}
return dir, nil
}

// FIXME: this is required for now because we need a local MSP
// and also the MSP mgr for the test chain; as soon as the code
// to setup chains is ready, the chain should be setup using
// the method below and this method should disappear
func LoadFakeSetupWithLocalMspAndTestChainMsp(dir string) error {
var err error
if dir, err = getConfigPath(dir); err != nil {
return err
}
conf, err := msp.GetLocalMspConfig(dir)
if err != nil {
return err
}

err = GetLocalMSP().Setup(conf)
if err != nil {
return err
}

fakeConfig := []*mspprotos.MSPConfig{conf}

err = GetManagerForChain(util.GetTestChainID()).Setup(fakeConfig)
if err != nil {
return err
}

return nil
}

// MSPConfigHandler
type MSPConfigHandler struct {
config []*mspprotos.MSPConfig
proposedMgr msp.MSPManager
committedMgr msp.MSPManager
msp.MSPManager
config []*mspprotos.MSPConfig
proposedMgr msp.MSPManager
}

// BeginConfig called when a config proposal is begun
Expand All @@ -109,7 +52,7 @@ func (bh *MSPConfigHandler) CommitConfig() {
panic("Programming error, called CommitConfig with no proposal in process")
}

bh.committedMgr = bh.proposedMgr
bh.MSPManager = bh.proposedMgr
bh.config = nil
bh.proposedMgr = nil
}
Expand All @@ -131,12 +74,7 @@ func (bh *MSPConfigHandler) ProposeConfig(configItem *common.ConfigItem) error {
return bh.proposedMgr.Setup(bh.config)
}

// GetMSPManager returns the currently committed MSP manager
func (bh *MSPConfigHandler) GetMSPManager() msp.MSPManager {
return bh.committedMgr
}

// DesierializeIdentity allows *MSPConfigHandler to implement the msp.Common interface
func (bh *MSPConfigHandler) DeserializeIdentity(serializedIdentity []byte) (msp.Identity, error) {
return bh.committedMgr.DeserializeIdentity(serializedIdentity)
return bh.DeserializeIdentity(serializedIdentity)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package mgmt_test
package msp

import (
"testing"

"github.com/golang/protobuf/proto"
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
"github.com/hyperledger/fabric/msp"
. "github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/protos/common"
"github.com/stretchr/testify/assert"
)

func TestMSPConfigManager(t *testing.T) {
conf, err := msp.GetLocalMspConfig("../sampleconfig/")
conf, err := msp.GetLocalMspConfig("../../../../msp/sampleconfig/")
assert.NoError(t, err)

confBytes, err := proto.Marshal(conf)
Expand All @@ -39,17 +37,13 @@ func TestMSPConfigManager(t *testing.T) {
// test success:

// begin/propose/commit
var mspCH configtxapi.Handler
mspCH = &MSPConfigHandler{}
mspCH := &MSPConfigHandler{}
mspCH.BeginConfig()
err = mspCH.ProposeConfig(ci)
assert.NoError(t, err)
mspCH.CommitConfig()

// get the manager we just created
mgr := mspCH.(*MSPConfigHandler).GetMSPManager()

msps, err := mgr.GetMSPs()
msps, err := mspCH.GetMSPs()
assert.NoError(t, err)

if msps == nil || len(msps) == 0 {
Expand Down
8 changes: 4 additions & 4 deletions common/configtx/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ import (
"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/configtx/api"
"github.com/hyperledger/fabric/common/configtx/handlers/channel"
configtxmsp "github.com/hyperledger/fabric/common/configtx/handlers/msp"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/msp"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
cb "github.com/hyperledger/fabric/protos/common"
)

type resources struct {
handlers map[cb.ConfigItem_ConfigType]api.Handler
policyManager policies.Manager
channelConfig api.ChannelConfig
mspConfigHandler *mspmgmt.MSPConfigHandler
mspConfigHandler *configtxmsp.MSPConfigHandler
}

// PolicyManager returns the policies.Manager for the chain
Expand All @@ -45,7 +45,7 @@ func (r *resources) ChannelConfig() api.ChannelConfig {

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

// Handlers returns the handlers to be used when initializing the configtx.Manager
Expand All @@ -55,7 +55,7 @@ func (r *resources) Handlers() map[cb.ConfigItem_ConfigType]api.Handler {

// NewInitializer creates a chain initializer for the basic set of common chain resources
func NewInitializer() api.Initializer {
mspConfigHandler := &mspmgmt.MSPConfigHandler{}
mspConfigHandler := &configtxmsp.MSPConfigHandler{}
policyProviderMap := make(map[int32]policies.Provider)
for pType := range cb.Policy_PolicyType_name {
rtype := cb.Policy_PolicyType(pType)
Expand Down
59 changes: 59 additions & 0 deletions msp/mgmt/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,71 @@ limitations under the License.
package mgmt

import (
"os"
"path/filepath"
"sync"

"github.com/hyperledger/fabric/common/util"
mspprotos "github.com/hyperledger/fabric/protos/msp"

"github.com/hyperledger/fabric/msp"
"github.com/op/go-logging"
)

func LoadLocalMsp(dir string) error {
conf, err := msp.GetLocalMspConfig(dir)
if err != nil {
return err
}

return GetLocalMSP().Setup(conf)
}

func getConfigPath(dir string) (string, error) {
// Try to read the dir
if _, err := os.Stat(dir); err != nil {
cfg := os.Getenv("PEER_CFG_PATH")
if cfg != "" {
dir = filepath.Join(cfg, dir)
} else {
dir = filepath.Join(os.Getenv("GOPATH"), "/src/github.com/hyperledger/fabric/msp/sampleconfig/")
}
if _, err := os.Stat(dir); err != nil {
return "", err
}
}
return dir, nil
}

// FIXME: this is required for now because we need a local MSP
// and also the MSP mgr for the test chain; as soon as the code
// to setup chains is ready, the chain should be setup using
// the method below and this method should disappear
func LoadFakeSetupWithLocalMspAndTestChainMsp(dir string) error {
var err error
if dir, err = getConfigPath(dir); err != nil {
return err
}
conf, err := msp.GetLocalMspConfig(dir)
if err != nil {
return err
}

err = GetLocalMSP().Setup(conf)
if err != nil {
return err
}

fakeConfig := []*mspprotos.MSPConfig{conf}

err = GetManagerForChain(util.GetTestChainID()).Setup(fakeConfig)
if err != nil {
return err
}

return nil
}

// FIXME: AS SOON AS THE CHAIN MANAGEMENT CODE IS COMPLETE,
// THESE MAPS AND HELPSER FUNCTIONS SHOULD DISAPPEAR BECAUSE
// OWNERSHIP OF PER-CHAIN MSP MANAGERS WILL BE HANDLED BY IT;
Expand Down

0 comments on commit 4ae2508

Please sign in to comment.