Skip to content

Commit 4ae2508

Browse files
author
Jason Yellick
committed
[FAB-2126] Move MSP configtx Handler to handlers
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 <[email protected]>
1 parent d3419e7 commit 4ae2508

File tree

4 files changed

+73
-82
lines changed

4 files changed

+73
-82
lines changed

msp/mgmt/config.go common/configtx/handlers/msp/config.go

+6-68
Original file line numberDiff line numberDiff line change
@@ -14,79 +14,22 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package mgmt
17+
package msp
1818

1919
import (
2020
"fmt"
21-
"os"
22-
"path/filepath"
2321

2422
"github.com/golang/protobuf/proto"
25-
"github.com/hyperledger/fabric/common/util"
2623
"github.com/hyperledger/fabric/msp"
2724
"github.com/hyperledger/fabric/protos/common"
2825
mspprotos "github.com/hyperledger/fabric/protos/msp"
2926
)
3027

31-
func LoadLocalMsp(dir string) error {
32-
conf, err := msp.GetLocalMspConfig(dir)
33-
if err != nil {
34-
return err
35-
}
36-
37-
return GetLocalMSP().Setup(conf)
38-
}
39-
40-
func getConfigPath(dir string) (string, error) {
41-
// Try to read the dir
42-
if _, err := os.Stat(dir); err != nil {
43-
cfg := os.Getenv("PEER_CFG_PATH")
44-
if cfg != "" {
45-
dir = filepath.Join(cfg, dir)
46-
} else {
47-
dir = filepath.Join(os.Getenv("GOPATH"), "/src/github.com/hyperledger/fabric/msp/sampleconfig/")
48-
}
49-
if _, err := os.Stat(dir); err != nil {
50-
return "", err
51-
}
52-
}
53-
return dir, nil
54-
}
55-
56-
// FIXME: this is required for now because we need a local MSP
57-
// and also the MSP mgr for the test chain; as soon as the code
58-
// to setup chains is ready, the chain should be setup using
59-
// the method below and this method should disappear
60-
func LoadFakeSetupWithLocalMspAndTestChainMsp(dir string) error {
61-
var err error
62-
if dir, err = getConfigPath(dir); err != nil {
63-
return err
64-
}
65-
conf, err := msp.GetLocalMspConfig(dir)
66-
if err != nil {
67-
return err
68-
}
69-
70-
err = GetLocalMSP().Setup(conf)
71-
if err != nil {
72-
return err
73-
}
74-
75-
fakeConfig := []*mspprotos.MSPConfig{conf}
76-
77-
err = GetManagerForChain(util.GetTestChainID()).Setup(fakeConfig)
78-
if err != nil {
79-
return err
80-
}
81-
82-
return nil
83-
}
84-
8528
// MSPConfigHandler
8629
type MSPConfigHandler struct {
87-
config []*mspprotos.MSPConfig
88-
proposedMgr msp.MSPManager
89-
committedMgr msp.MSPManager
30+
msp.MSPManager
31+
config []*mspprotos.MSPConfig
32+
proposedMgr msp.MSPManager
9033
}
9134

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

112-
bh.committedMgr = bh.proposedMgr
55+
bh.MSPManager = bh.proposedMgr
11356
bh.config = nil
11457
bh.proposedMgr = nil
11558
}
@@ -131,12 +74,7 @@ func (bh *MSPConfigHandler) ProposeConfig(configItem *common.ConfigItem) error {
13174
return bh.proposedMgr.Setup(bh.config)
13275
}
13376

134-
// GetMSPManager returns the currently committed MSP manager
135-
func (bh *MSPConfigHandler) GetMSPManager() msp.MSPManager {
136-
return bh.committedMgr
137-
}
138-
13977
// DesierializeIdentity allows *MSPConfigHandler to implement the msp.Common interface
14078
func (bh *MSPConfigHandler) DeserializeIdentity(serializedIdentity []byte) (msp.Identity, error) {
141-
return bh.committedMgr.DeserializeIdentity(serializedIdentity)
79+
return bh.DeserializeIdentity(serializedIdentity)
14280
}

msp/mgmt/mspconfigmgr_test.go common/configtx/handlers/msp/config_test.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package mgmt_test
17+
package msp
1818

1919
import (
2020
"testing"
2121

2222
"github.com/golang/protobuf/proto"
23-
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
2423
"github.com/hyperledger/fabric/msp"
25-
. "github.com/hyperledger/fabric/msp/mgmt"
2624
"github.com/hyperledger/fabric/protos/common"
2725
"github.com/stretchr/testify/assert"
2826
)
2927

3028
func TestMSPConfigManager(t *testing.T) {
31-
conf, err := msp.GetLocalMspConfig("../sampleconfig/")
29+
conf, err := msp.GetLocalMspConfig("../../../../msp/sampleconfig/")
3230
assert.NoError(t, err)
3331

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

4139
// begin/propose/commit
42-
var mspCH configtxapi.Handler
43-
mspCH = &MSPConfigHandler{}
40+
mspCH := &MSPConfigHandler{}
4441
mspCH.BeginConfig()
4542
err = mspCH.ProposeConfig(ci)
4643
assert.NoError(t, err)
4744
mspCH.CommitConfig()
4845

49-
// get the manager we just created
50-
mgr := mspCH.(*MSPConfigHandler).GetMSPManager()
51-
52-
msps, err := mgr.GetMSPs()
46+
msps, err := mspCH.GetMSPs()
5347
assert.NoError(t, err)
5448

5549
if msps == nil || len(msps) == 0 {

common/configtx/resources.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ import (
2020
"github.com/hyperledger/fabric/common/cauthdsl"
2121
"github.com/hyperledger/fabric/common/configtx/api"
2222
"github.com/hyperledger/fabric/common/configtx/handlers/channel"
23+
configtxmsp "github.com/hyperledger/fabric/common/configtx/handlers/msp"
2324
"github.com/hyperledger/fabric/common/policies"
2425
"github.com/hyperledger/fabric/msp"
25-
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
2626
cb "github.com/hyperledger/fabric/protos/common"
2727
)
2828

2929
type resources struct {
3030
handlers map[cb.ConfigItem_ConfigType]api.Handler
3131
policyManager policies.Manager
3232
channelConfig api.ChannelConfig
33-
mspConfigHandler *mspmgmt.MSPConfigHandler
33+
mspConfigHandler *configtxmsp.MSPConfigHandler
3434
}
3535

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

4646
// MSPManager returns the msp.MSPManager for the chain
4747
func (r *resources) MSPManager() msp.MSPManager {
48-
return r.mspConfigHandler.GetMSPManager()
48+
return r.mspConfigHandler
4949
}
5050

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

5656
// NewInitializer creates a chain initializer for the basic set of common chain resources
5757
func NewInitializer() api.Initializer {
58-
mspConfigHandler := &mspmgmt.MSPConfigHandler{}
58+
mspConfigHandler := &configtxmsp.MSPConfigHandler{}
5959
policyProviderMap := make(map[int32]policies.Provider)
6060
for pType := range cb.Policy_PolicyType_name {
6161
rtype := cb.Policy_PolicyType(pType)

msp/mgmt/mgmt.go

+59
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,71 @@ limitations under the License.
1717
package mgmt
1818

1919
import (
20+
"os"
21+
"path/filepath"
2022
"sync"
2123

24+
"github.com/hyperledger/fabric/common/util"
25+
mspprotos "github.com/hyperledger/fabric/protos/msp"
26+
2227
"github.com/hyperledger/fabric/msp"
2328
"github.com/op/go-logging"
2429
)
2530

31+
func LoadLocalMsp(dir string) error {
32+
conf, err := msp.GetLocalMspConfig(dir)
33+
if err != nil {
34+
return err
35+
}
36+
37+
return GetLocalMSP().Setup(conf)
38+
}
39+
40+
func getConfigPath(dir string) (string, error) {
41+
// Try to read the dir
42+
if _, err := os.Stat(dir); err != nil {
43+
cfg := os.Getenv("PEER_CFG_PATH")
44+
if cfg != "" {
45+
dir = filepath.Join(cfg, dir)
46+
} else {
47+
dir = filepath.Join(os.Getenv("GOPATH"), "/src/github.com/hyperledger/fabric/msp/sampleconfig/")
48+
}
49+
if _, err := os.Stat(dir); err != nil {
50+
return "", err
51+
}
52+
}
53+
return dir, nil
54+
}
55+
56+
// FIXME: this is required for now because we need a local MSP
57+
// and also the MSP mgr for the test chain; as soon as the code
58+
// to setup chains is ready, the chain should be setup using
59+
// the method below and this method should disappear
60+
func LoadFakeSetupWithLocalMspAndTestChainMsp(dir string) error {
61+
var err error
62+
if dir, err = getConfigPath(dir); err != nil {
63+
return err
64+
}
65+
conf, err := msp.GetLocalMspConfig(dir)
66+
if err != nil {
67+
return err
68+
}
69+
70+
err = GetLocalMSP().Setup(conf)
71+
if err != nil {
72+
return err
73+
}
74+
75+
fakeConfig := []*mspprotos.MSPConfig{conf}
76+
77+
err = GetManagerForChain(util.GetTestChainID()).Setup(fakeConfig)
78+
if err != nil {
79+
return err
80+
}
81+
82+
return nil
83+
}
84+
2685
// FIXME: AS SOON AS THE CHAIN MANAGEMENT CODE IS COMPLETE,
2786
// THESE MAPS AND HELPSER FUNCTIONS SHOULD DISAPPEAR BECAUSE
2887
// OWNERSHIP OF PER-CHAIN MSP MANAGERS WILL BE HANDLED BY IT;

0 commit comments

Comments
 (0)