Skip to content

Commit f30e1b6

Browse files
committed
Get ID for local MSP from core.yaml
This change-set removes the hardcoded ID of the local MSP and reads it from core.yaml instead. Additionally, the code that sets up the MSPs for the test chain has been cleaned up - the test setup is now only used in testing and the peer always sets up chain MSPs from the config block (whether it's the test chain or a chain it is asked to join). Change-Id: Ife55c9be831172e603b4495bdfa37982a571d3a9 Signed-off-by: Alessandro Sorniotti <[email protected]>
1 parent d736204 commit f30e1b6

32 files changed

+208
-114
lines changed

common/configtx/handlers/msp/config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
func TestMSPConfigManager(t *testing.T) {
29-
conf, err := msp.GetLocalMspConfig("../../../../msp/sampleconfig/")
29+
conf, err := msp.GetLocalMspConfig("../../../../msp/sampleconfig/", "DEFAULT")
3030
assert.NoError(t, err)
3131

3232
confBytes, err := proto.Marshal(conf)

common/localmsp/signer_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestMain(m *testing.M) {
3737
mspMgrConfigDir = os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/msp/sampleconfig/"
3838
}
3939

40-
if err := mspmgmt.LoadLocalMsp(mspMgrConfigDir); err != nil {
40+
if err := mspmgmt.LoadLocalMsp(mspMgrConfigDir, "DEFAULT"); err != nil {
4141
os.Exit(-1)
4242
}
4343

core/chaincode/exectransaction_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
4444
"github.com/hyperledger/fabric/msp"
4545
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
46+
"github.com/hyperledger/fabric/msp/mgmt/testtools"
4647
"github.com/hyperledger/fabric/protos/common"
4748
"github.com/spf13/viper"
4849
"golang.org/x/net/context"
@@ -1152,7 +1153,7 @@ func TestMain(m *testing.M) {
11521153

11531154
// setup the MSP manager so that we can sign/verify
11541155
mspMgrConfigDir := "../../msp/sampleconfig/"
1155-
mspmgmt.LoadFakeSetupWithLocalMspAndTestChainMsp(mspMgrConfigDir)
1156+
msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir)
11561157
signer, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
11571158
if err != nil {
11581159
os.Exit(-1)

core/common/validation/fullflow_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/hyperledger/fabric/common/util"
2828
"github.com/hyperledger/fabric/msp"
2929
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
30+
"github.com/hyperledger/fabric/msp/mgmt/testtools"
3031
"github.com/hyperledger/fabric/protos/common"
3132
"github.com/hyperledger/fabric/protos/peer"
3233
"github.com/hyperledger/fabric/protos/utils"
@@ -330,7 +331,7 @@ func TestMain(m *testing.M) {
330331
// setup crypto algorithms
331332
// setup the MSP manager so that we can sign/verify
332333
mspMgrConfigDir := "../../../msp/sampleconfig/"
333-
err := mspmgmt.LoadFakeSetupWithLocalMspAndTestChainMsp(mspMgrConfigDir)
334+
err := msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir)
334335
if err != nil {
335336
fmt.Printf("Could not initialize msp, err %s", err)
336337
os.Exit(-1)

core/endorser/endorser_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
syscc "github.com/hyperledger/fabric/core/scc"
3535
"github.com/hyperledger/fabric/msp"
3636
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
37+
"github.com/hyperledger/fabric/msp/mgmt/testtools"
3738
"github.com/hyperledger/fabric/protos/common"
3839
pb "github.com/hyperledger/fabric/protos/peer"
3940
pbutils "github.com/hyperledger/fabric/protos/utils"
@@ -426,7 +427,7 @@ func TestMain(m *testing.M) {
426427

427428
// setup the MSP manager so that we can sign/verify
428429
mspMgrConfigDir := "../../msp/sampleconfig/"
429-
err = mspmgmt.LoadFakeSetupWithLocalMspAndTestChainMsp(mspMgrConfigDir)
430+
err = msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir)
430431
if err != nil {
431432
fmt.Printf("Could not initialize msp/signer, err %s", err)
432433
os.Exit(-1)

core/peer/peer_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/hyperledger/fabric/core/mocks/ccprovider"
3030
"github.com/hyperledger/fabric/gossip/service"
3131
"github.com/hyperledger/fabric/msp/mgmt"
32+
"github.com/hyperledger/fabric/msp/mgmt/testtools"
3233
"github.com/spf13/viper"
3334
"github.com/stretchr/testify/assert"
3435
"google.golang.org/grpc"
@@ -82,7 +83,7 @@ func TestCreateChainFromBlock(t *testing.T) {
8283
go grpcServer.Serve(socket)
8384
defer grpcServer.Stop()
8485

85-
mgmt.LoadFakeSetupWithLocalMspAndTestChainMsp("../../msp/sampleconfig")
86+
msptesttools.LoadMSPSetupForTesting("../../msp/sampleconfig")
8687

8788
identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
8889
service.InitGossipServiceCustomDeliveryFactory(identity, "localhost:13611", grpcServer, &mockDeliveryClientFactory{})

core/scc/cscc/configer_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/hyperledger/fabric/core/peer"
3333
"github.com/hyperledger/fabric/gossip/service"
3434
"github.com/hyperledger/fabric/msp/mgmt"
35+
"github.com/hyperledger/fabric/msp/mgmt/testtools"
3536
"github.com/hyperledger/fabric/protos/common"
3637
pb "github.com/hyperledger/fabric/protos/peer"
3738
"github.com/hyperledger/fabric/protos/utils"
@@ -142,7 +143,7 @@ func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
142143
go grpcServer.Serve(socket)
143144
defer grpcServer.Stop()
144145

145-
mgmt.LoadFakeSetupWithLocalMspAndTestChainMsp("../../../msp/sampleconfig")
146+
msptesttools.LoadMSPSetupForTesting("../../../msp/sampleconfig")
146147
identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
147148

148149
service.InitGossipServiceCustomDeliveryFactory(identity, "localhost:13611", grpcServer, &mockDeliveryClientFactory{})

core/scc/escc/endorser_onevalidsignature_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/hyperledger/fabric/core/chaincode/shim"
2828
"github.com/hyperledger/fabric/core/common/validation"
2929
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
30+
"github.com/hyperledger/fabric/msp/mgmt/testtools"
3031
"github.com/hyperledger/fabric/protos/common"
3132
pb "github.com/hyperledger/fabric/protos/peer"
3233
putils "github.com/hyperledger/fabric/protos/utils"
@@ -311,7 +312,7 @@ func validateProposalResponse(prBytes []byte, proposal *pb.Proposal, visibility
311312

312313
func TestMain(m *testing.M) {
313314
mspMgrConfigDir := "../../../msp/sampleconfig/"
314-
mspmgmt.LoadFakeSetupWithLocalMspAndTestChainMsp(mspMgrConfigDir)
315+
msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir)
315316

316317
os.Exit(m.Run())
317318
}

core/scc/vscc/validator_onevalidsignature_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/hyperledger/fabric/core/chaincode/shim"
2727
"github.com/hyperledger/fabric/msp"
2828
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
29+
"github.com/hyperledger/fabric/msp/mgmt/testtools"
2930
"github.com/hyperledger/fabric/protos/common"
3031
"github.com/hyperledger/fabric/protos/peer"
3132
"github.com/hyperledger/fabric/protos/utils"
@@ -136,7 +137,7 @@ func TestMain(m *testing.M) {
136137

137138
// setup the MSP manager so that we can sign/verify
138139
mspMgrConfigDir := "../../../msp/sampleconfig/"
139-
mspmgmt.LoadFakeSetupWithLocalMspAndTestChainMsp(mspMgrConfigDir)
140+
msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir)
140141

141142
id, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
142143
if err != nil {

examples/ccchecker/init.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ func initYaml(mainFlags *pflag.FlagSet) {
6363
//initialize MSP from -m <mspconfigdir>. Defaults to ../../msp/sampleconfig
6464
func initMSP(mainFlags *pflag.FlagSet) {
6565
mspMgrConfigDir := ""
66+
mspID := ""
6667
mainFlags.StringVarP(&mspMgrConfigDir, "mspcfgdir", "m", "../../msp/sampleconfig/", "Path to MSP dir")
68+
mainFlags.StringVarP(&mspID, "mspid", "i", "DEFAULT", "MSP ID")
6769

68-
err := common.InitCrypto(mspMgrConfigDir)
70+
err := common.InitCrypto(mspMgrConfigDir, mspID)
6971
if err != nil {
7072
panic(err.Error())
7173
}

gossip/integration/integration_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/hyperledger/fabric/gossip/common"
2828
"github.com/hyperledger/fabric/gossip/identity"
2929
"github.com/hyperledger/fabric/msp/mgmt"
30+
"github.com/hyperledger/fabric/msp/mgmt/testtools"
3031
"github.com/spf13/viper"
3132
"google.golang.org/grpc"
3233
)
@@ -46,7 +47,7 @@ func TestNewGossipCryptoService(t *testing.T) {
4647
endpoint2 := "localhost:5612"
4748
endpoint3 := "localhost:5613"
4849

49-
mgmt.LoadFakeSetupWithLocalMspAndTestChainMsp("../../msp/sampleconfig")
50+
msptesttools.LoadMSPSetupForTesting("../../msp/sampleconfig")
5051
peerIdentity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
5152

5253
cryptSvc := &cryptoService{}

gossip/service/gossip_service_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/hyperledger/fabric/gossip/api"
2626
"github.com/hyperledger/fabric/msp/mgmt"
27+
"github.com/hyperledger/fabric/msp/mgmt/testtools"
2728
"github.com/stretchr/testify/assert"
2829
"google.golang.org/grpc"
2930
)
@@ -37,7 +38,7 @@ func TestInitGossipService(t *testing.T) {
3738
go grpcServer.Serve(socket)
3839
defer grpcServer.Stop()
3940

40-
mgmt.LoadFakeSetupWithLocalMspAndTestChainMsp("../../msp/sampleconfig")
41+
msptesttools.LoadMSPSetupForTesting("../../msp/sampleconfig")
4142
identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
4243

4344
wg := sync.WaitGroup{}

msp/configbuilder.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const (
8787
intermediatecerts = "intermediatecerts"
8888
)
8989

90-
func GetLocalMspConfig(dir string) (*msp.MSPConfig, error) {
90+
func GetLocalMspConfig(dir string, ID string) (*msp.MSPConfig, error) {
9191
cacertDir := filepath.Join(dir, cacerts)
9292
signcertDir := filepath.Join(dir, signcerts)
9393
admincertDir := filepath.Join(dir, admincerts)
@@ -131,7 +131,7 @@ func GetLocalMspConfig(dir string) (*msp.MSPConfig, error) {
131131
RootCerts: cacerts,
132132
IntermediateCerts: intermediatecert,
133133
SigningIdentity: sigid,
134-
Name: "DEFAULT"}
134+
Name: ID}
135135

136136
fmpsjs, _ := proto.Marshal(fmspconf)
137137

msp/mgmt/mgmt.go

+7-50
Original file line numberDiff line numberDiff line change
@@ -17,69 +17,26 @@ limitations under the License.
1717
package mgmt
1818

1919
import (
20-
"os"
21-
"path/filepath"
2220
"sync"
2321

24-
"github.com/hyperledger/fabric/common/util"
25-
mspprotos "github.com/hyperledger/fabric/protos/msp"
22+
"errors"
2623

2724
"github.com/hyperledger/fabric/msp"
2825
"github.com/op/go-logging"
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
28+
// LoadLocalMsp loads the local MSP from the specified directory
29+
func LoadLocalMsp(dir string, mspID string) error {
30+
if mspID == "" {
31+
return errors.New("The local MSP must have an ID")
6832
}
6933

70-
err = GetLocalMSP().Setup(conf)
34+
conf, err := msp.GetLocalMspConfig(dir, mspID)
7135
if err != nil {
7236
return err
7337
}
7438

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
39+
return GetLocalMSP().Setup(conf)
8340
}
8441

8542
// FIXME: AS SOON AS THE CHAIN MANAGEMENT CODE IS COMPLETE,

msp/mgmt/peermsp_test.go

+1-25
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"io/ioutil"
2121
"os"
2222
"testing"
23-
24-
"github.com/hyperledger/fabric/common/util"
2523
)
2624

2725
// getTestMSPConfigPath returns the path to sampleconfig for unit tests
@@ -35,21 +33,8 @@ func getTestMSPConfigPath() string {
3533

3634
func TestLocalMSP(t *testing.T) {
3735
testMSPConfigPath := getTestMSPConfigPath()
38-
err := LoadLocalMsp(testMSPConfigPath)
39-
if err != nil {
40-
t.Fatalf("LoadLocalMsp failed, err %s", err)
41-
}
42-
43-
_, err = GetLocalMSP().GetDefaultSigningIdentity()
44-
if err != nil {
45-
t.Fatalf("GetDefaultSigningIdentity failed, err %s", err)
46-
}
47-
}
36+
err := LoadLocalMsp(testMSPConfigPath, "DEFAULT")
4837

49-
// TODO: as soon as proper per-chain MSP support is developed, this test will no longer be required
50-
func TestFakeSetup(t *testing.T) {
51-
testMSPConfigPath := getTestMSPConfigPath()
52-
err := LoadFakeSetupWithLocalMspAndTestChainMsp(testMSPConfigPath)
5338
if err != nil {
5439
t.Fatalf("LoadLocalMsp failed, err %s", err)
5540
}
@@ -58,13 +43,4 @@ func TestFakeSetup(t *testing.T) {
5843
if err != nil {
5944
t.Fatalf("GetDefaultSigningIdentity failed, err %s", err)
6045
}
61-
62-
msps, err := GetManagerForChain(util.GetTestChainID()).GetMSPs()
63-
if err != nil {
64-
t.Fatalf("EnlistedMSPs failed, err %s", err)
65-
}
66-
67-
if msps == nil || len(msps) == 0 {
68-
t.Fatalf("There are no MSPS in the manager for chain %s", util.GetTestChainID())
69-
}
7046
}

msp/mgmt/testtools/config.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package msptesttools
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
23+
"github.com/hyperledger/fabric/common/util"
24+
"github.com/hyperledger/fabric/msp"
25+
"github.com/hyperledger/fabric/msp/mgmt"
26+
mspprotos "github.com/hyperledger/fabric/protos/msp"
27+
)
28+
29+
func getConfigPath(dir string) (string, error) {
30+
// Try to read the dir
31+
if _, err := os.Stat(dir); err != nil {
32+
cfg := os.Getenv("PEER_CFG_PATH")
33+
if cfg != "" {
34+
dir = filepath.Join(cfg, dir)
35+
} else {
36+
dir = filepath.Join(os.Getenv("GOPATH"), "/src/github.com/hyperledger/fabric/msp/sampleconfig/")
37+
}
38+
if _, err := os.Stat(dir); err != nil {
39+
return "", err
40+
}
41+
}
42+
return dir, nil
43+
}
44+
45+
// LoadTestMSPSetup sets up the local MSP
46+
// and a chain MSP for the default chain
47+
func LoadMSPSetupForTesting(dir string) error {
48+
var err error
49+
if dir, err = getConfigPath(dir); err != nil {
50+
return err
51+
}
52+
conf, err := msp.GetLocalMspConfig(dir, "DEFAULT")
53+
if err != nil {
54+
return err
55+
}
56+
57+
err = mgmt.GetLocalMSP().Setup(conf)
58+
if err != nil {
59+
return err
60+
}
61+
62+
fakeConfig := []*mspprotos.MSPConfig{conf}
63+
64+
err = mgmt.GetManagerForChain(util.GetTestChainID()).Setup(fakeConfig)
65+
if err != nil {
66+
return err
67+
}
68+
69+
return nil
70+
}

0 commit comments

Comments
 (0)