Skip to content

Commit 89d68d8

Browse files
committed
[FAB-6369] support local idemix MSP for go CLI
This change set enables local MSP support for the idemix MSP type. A new core.yaml option (localMspType) is used to determine the MSP type. The default is 'bccsp', but it can be set to 'idemix' to support idemix. Change-Id: Ia48905b53e2411e8f0152f9f6b8c3e16808bfee0 Signed-off-by: Alessandro Sorniotti <ale.linux@sopit.net>
1 parent 3689c5a commit 89d68d8

File tree

7 files changed

+91
-30
lines changed

7 files changed

+91
-30
lines changed

msp/configbuilder.go

+14
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ func SetupBCCSPKeystoreConfig(bccspConfig *factory.FactoryOpts, keystoreDir stri
146146
return bccspConfig
147147
}
148148

149+
// GetLocalMspConfigWithType returns a local MSP
150+
// configuration for the MSP in the specified
151+
// directory, with the specified ID and type
152+
func GetLocalMspConfigWithType(dir string, bccspConfig *factory.FactoryOpts, ID, mspType string) (*msp.MSPConfig, error) {
153+
switch mspType {
154+
case ProviderTypeToString(FABRIC):
155+
return GetLocalMspConfig(dir, bccspConfig, ID)
156+
case ProviderTypeToString(IDEMIX):
157+
return GetIdemixMspConfig(dir, ID)
158+
default:
159+
return nil, errors.Errorf("unknown MSP type '%s'", mspType)
160+
}
161+
}
162+
149163
func GetLocalMspConfig(dir string, bccspConfig *factory.FactoryOpts, ID string) (*msp.MSPConfig, error) {
150164
signcertDir := filepath.Join(dir, signcerts)
151165
keystoreDir := filepath.Join(dir, keystore)

msp/mgmt/mgmt.go

+43-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,23 @@ import (
2626
"github.com/hyperledger/fabric/msp"
2727
"github.com/hyperledger/fabric/msp/cache"
2828
"github.com/pkg/errors"
29+
"github.com/spf13/viper"
2930
)
3031

32+
// LoadLocalMspWithType loads the local MSP with the specified type from the specified directory
33+
func LoadLocalMspWithType(dir string, bccspConfig *factory.FactoryOpts, mspID, mspType string) error {
34+
if mspID == "" {
35+
return errors.New("the local MSP must have an ID")
36+
}
37+
38+
conf, err := msp.GetLocalMspConfigWithType(dir, bccspConfig, mspID, mspType)
39+
if err != nil {
40+
return err
41+
}
42+
43+
return GetLocalMSP().Setup(conf)
44+
}
45+
3146
// LoadLocalMsp loads the local MSP from the specified directory
3247
func LoadLocalMsp(dir string, bccspConfig *factory.FactoryOpts, mspID string) error {
3348
if mspID == "" {
@@ -115,6 +130,23 @@ func GetLocalMSP() msp.MSP {
115130
var lclMsp msp.MSP
116131
var created bool = false
117132
{
133+
// determine the type of MSP (by default, we'll use bccspMSP)
134+
mspType := viper.GetString("peer.localMspType")
135+
if mspType == "" {
136+
mspType = msp.ProviderTypeToString(msp.FABRIC)
137+
}
138+
139+
// based on the MSP type, generate the new opts
140+
var newOpts msp.NewOpts
141+
switch mspType {
142+
case msp.ProviderTypeToString(msp.FABRIC):
143+
newOpts = &msp.BCCSPNewOpts{NewBaseOpts: msp.NewBaseOpts{Version: msp.MSPv1_0}}
144+
case msp.ProviderTypeToString(msp.IDEMIX):
145+
newOpts = &msp.IdemixNewOpts{msp.NewBaseOpts{Version: msp.MSPv1_1}}
146+
default:
147+
panic("msp type " + mspType + " unknown")
148+
}
149+
118150
m.Lock()
119151
defer m.Unlock()
120152

@@ -123,14 +155,21 @@ func GetLocalMSP() msp.MSP {
123155
var err error
124156
created = true
125157

126-
mspInst, err := msp.New(&msp.BCCSPNewOpts{NewBaseOpts: msp.NewBaseOpts{Version: msp.MSPv1_0}})
158+
mspInst, err := msp.New(newOpts)
127159
if err != nil {
128160
mspLogger.Fatalf("Failed to initialize local MSP, received err %+v", err)
129161
}
130162

131-
lclMsp, err = cache.New(mspInst)
132-
if err != nil {
133-
mspLogger.Fatalf("Failed to initialize local MSP, received err %+v", err)
163+
switch mspType {
164+
case msp.ProviderTypeToString(msp.FABRIC):
165+
lclMsp, err = cache.New(mspInst)
166+
if err != nil {
167+
mspLogger.Fatalf("Failed to initialize local MSP, received err %+v", err)
168+
}
169+
case msp.ProviderTypeToString(msp.IDEMIX):
170+
lclMsp = mspInst
171+
default:
172+
panic("msp type " + mspType + " unknown")
134173
}
135174
localMsp = lclMsp
136175
}

peer/common/common.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func InitConfig(cmdRoot string) error {
8585
}
8686

8787
//InitCrypto initializes crypto for this peer
88-
func InitCrypto(mspMgrConfigDir string, localMSPID string) error {
88+
func InitCrypto(mspMgrConfigDir, localMSPID, localMSPType string) error {
8989
var err error
9090
// Check whenever msp folder exists
9191
_, err = os.Stat(mspMgrConfigDir)
@@ -101,9 +101,9 @@ func InitCrypto(mspMgrConfigDir string, localMSPID string) error {
101101
return errors.WithMessage(err, "could not parse YAML config")
102102
}
103103

104-
err = mspmgmt.LoadLocalMsp(mspMgrConfigDir, bccspConfig, localMSPID)
104+
err = mspmgmt.LoadLocalMspWithType(mspMgrConfigDir, bccspConfig, localMSPID, localMSPType)
105105
if err != nil {
106-
return errors.WithMessage(err, fmt.Sprintf("error when setting up MSP from directory %s", mspMgrConfigDir))
106+
return errors.WithMessage(err, fmt.Sprintf("error when setting up MSP of type %s from directory %s", localMSPType, mspMgrConfigDir))
107107
}
108108

109109
return nil

peer/common/common_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ func TestInitConfig(t *testing.T) {
5353
}
5454
}
5555

56-
func TestINitCryptoMissingDir(t *testing.T) {
56+
func TestInitCryptoMissingDir(t *testing.T) {
5757
dir := os.TempDir() + "/" + util.GenerateUUID()
58-
err := common.InitCrypto(dir, "DEFAULT")
58+
err := common.InitCrypto(dir, "DEFAULT", msp.ProviderTypeToString(msp.FABRIC))
5959
assert.Error(t, err, "Should be able to initialize crypto with non-existing directory")
6060
assert.Contains(t, err.Error(), fmt.Sprintf("missing %s folder", dir))
6161
}
@@ -64,12 +64,12 @@ func TestInitCrypto(t *testing.T) {
6464

6565
mspConfigPath, err := config.GetDevMspDir()
6666
localMspId := "DEFAULT"
67-
err = common.InitCrypto(mspConfigPath, localMspId)
67+
err = common.InitCrypto(mspConfigPath, localMspId, msp.ProviderTypeToString(msp.FABRIC))
6868
assert.NoError(t, err, "Unexpected error [%s] calling InitCrypto()", err)
69-
err = common.InitCrypto("/etc/foobaz", localMspId)
69+
err = common.InitCrypto("/etc/foobaz", localMspId, msp.ProviderTypeToString(msp.FABRIC))
7070
assert.Error(t, err, fmt.Sprintf("Expected error [%s] calling InitCrypto()", err))
7171
localMspId = ""
72-
err = common.InitCrypto(mspConfigPath, localMspId)
72+
err = common.InitCrypto(mspConfigPath, localMspId, msp.ProviderTypeToString(msp.FABRIC))
7373
assert.Error(t, err, fmt.Sprintf("Expected error [%s] calling InitCrypto()", err))
7474
}
7575

peer/main.go

+11-18
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,29 @@
11
/*
2-
Copyright IBM Corp. 2016 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
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.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package main
188

199
import (
2010
"fmt"
11+
_ "net/http/pprof"
2112
"os"
2213
"runtime"
2314
"strings"
2415

25-
"github.com/spf13/cobra"
26-
"github.com/spf13/viper"
27-
28-
_ "net/http/pprof"
29-
3016
"github.com/hyperledger/fabric/common/flogging"
3117
"github.com/hyperledger/fabric/core/config"
18+
"github.com/hyperledger/fabric/msp"
3219
"github.com/hyperledger/fabric/peer/chaincode"
3320
"github.com/hyperledger/fabric/peer/channel"
3421
"github.com/hyperledger/fabric/peer/clilogging"
3522
"github.com/hyperledger/fabric/peer/common"
3623
"github.com/hyperledger/fabric/peer/node"
3724
"github.com/hyperledger/fabric/peer/version"
25+
"github.com/spf13/cobra"
26+
"github.com/spf13/viper"
3827
)
3928

4029
var logger = flogging.MustGetLogger("main")
@@ -102,7 +91,11 @@ func main() {
10291
// Init the MSP
10392
var mspMgrConfigDir = config.GetPath("peer.mspConfigPath")
10493
var mspID = viper.GetString("peer.localMspId")
105-
err = common.InitCrypto(mspMgrConfigDir, mspID)
94+
var mspType = viper.GetString("peer.localMspType")
95+
if mspType == "" {
96+
mspType = msp.ProviderTypeToString(msp.FABRIC)
97+
}
98+
err = common.InitCrypto(mspMgrConfigDir, mspID, mspType)
10699
if err != nil { // Handle errors reading the config file
107100
logger.Errorf("Cannot run peer because %s", err.Error())
108101
os.Exit(1)

peer/node/start.go

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/hyperledger/fabric/core/scc"
3838
"github.com/hyperledger/fabric/events/producer"
3939
"github.com/hyperledger/fabric/gossip/service"
40+
"github.com/hyperledger/fabric/msp"
4041
"github.com/hyperledger/fabric/msp/mgmt"
4142
"github.com/hyperledger/fabric/peer/common"
4243
peergossip "github.com/hyperledger/fabric/peer/gossip"
@@ -93,6 +94,17 @@ func initSysCCs() {
9394
}
9495

9596
func serve(args []string) error {
97+
// currently the peer only works with the standard MSP
98+
// because in certain scenarios the MSP has to make sure
99+
// that from a single credential you only have a single 'identity'.
100+
// Idemix does not support this *YET* but it can be easily
101+
// fixed to support it. For now, we just make sure that
102+
// the peer only comes up with the standard MSP
103+
mspType := mgmt.GetLocalMSP().GetType()
104+
if mspType != msp.FABRIC {
105+
panic("Unsupported msp type " + msp.ProviderTypeToString(mspType))
106+
}
107+
96108
logger.Infof("Starting %s", version.GetInfo())
97109

98110
//aclmgmt initializes a proxy Processor that will be redirected to RSCC provider

sampleconfig/core.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ peer:
331331
# attempts until its retry logic gives up and returns an error
332332
reconnectTotalTimeThreshold: 3600s
333333

334+
# Type for the local MSP - by default it's of type bccsp
335+
localMspType: bccsp
336+
334337
# Used with Go profiling tools only in none production environment. In
335338
# production, it should be disabled (eg enabled: false)
336339
profile:

0 commit comments

Comments
 (0)