Skip to content

Commit

Permalink
[FAB-6369] support local idemix MSP for go CLI
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ale-linux committed Dec 6, 2017
1 parent 3689c5a commit 89d68d8
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 30 deletions.
14 changes: 14 additions & 0 deletions msp/configbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ func SetupBCCSPKeystoreConfig(bccspConfig *factory.FactoryOpts, keystoreDir stri
return bccspConfig
}

// GetLocalMspConfigWithType returns a local MSP
// configuration for the MSP in the specified
// directory, with the specified ID and type
func GetLocalMspConfigWithType(dir string, bccspConfig *factory.FactoryOpts, ID, mspType string) (*msp.MSPConfig, error) {
switch mspType {
case ProviderTypeToString(FABRIC):
return GetLocalMspConfig(dir, bccspConfig, ID)
case ProviderTypeToString(IDEMIX):
return GetIdemixMspConfig(dir, ID)
default:
return nil, errors.Errorf("unknown MSP type '%s'", mspType)
}
}

func GetLocalMspConfig(dir string, bccspConfig *factory.FactoryOpts, ID string) (*msp.MSPConfig, error) {
signcertDir := filepath.Join(dir, signcerts)
keystoreDir := filepath.Join(dir, keystore)
Expand Down
47 changes: 43 additions & 4 deletions msp/mgmt/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,23 @@ import (
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/msp/cache"
"github.com/pkg/errors"
"github.com/spf13/viper"
)

// LoadLocalMspWithType loads the local MSP with the specified type from the specified directory
func LoadLocalMspWithType(dir string, bccspConfig *factory.FactoryOpts, mspID, mspType string) error {
if mspID == "" {
return errors.New("the local MSP must have an ID")
}

conf, err := msp.GetLocalMspConfigWithType(dir, bccspConfig, mspID, mspType)
if err != nil {
return err
}

return GetLocalMSP().Setup(conf)
}

// LoadLocalMsp loads the local MSP from the specified directory
func LoadLocalMsp(dir string, bccspConfig *factory.FactoryOpts, mspID string) error {
if mspID == "" {
Expand Down Expand Up @@ -115,6 +130,23 @@ func GetLocalMSP() msp.MSP {
var lclMsp msp.MSP
var created bool = false
{
// determine the type of MSP (by default, we'll use bccspMSP)
mspType := viper.GetString("peer.localMspType")
if mspType == "" {
mspType = msp.ProviderTypeToString(msp.FABRIC)
}

// based on the MSP type, generate the new opts
var newOpts msp.NewOpts
switch mspType {
case msp.ProviderTypeToString(msp.FABRIC):
newOpts = &msp.BCCSPNewOpts{NewBaseOpts: msp.NewBaseOpts{Version: msp.MSPv1_0}}
case msp.ProviderTypeToString(msp.IDEMIX):
newOpts = &msp.IdemixNewOpts{msp.NewBaseOpts{Version: msp.MSPv1_1}}
default:
panic("msp type " + mspType + " unknown")
}

m.Lock()
defer m.Unlock()

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

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

lclMsp, err = cache.New(mspInst)
if err != nil {
mspLogger.Fatalf("Failed to initialize local MSP, received err %+v", err)
switch mspType {
case msp.ProviderTypeToString(msp.FABRIC):
lclMsp, err = cache.New(mspInst)
if err != nil {
mspLogger.Fatalf("Failed to initialize local MSP, received err %+v", err)
}
case msp.ProviderTypeToString(msp.IDEMIX):
lclMsp = mspInst
default:
panic("msp type " + mspType + " unknown")
}
localMsp = lclMsp
}
Expand Down
6 changes: 3 additions & 3 deletions peer/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func InitConfig(cmdRoot string) error {
}

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

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

return nil
Expand Down
10 changes: 5 additions & 5 deletions peer/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func TestInitConfig(t *testing.T) {
}
}

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

mspConfigPath, err := config.GetDevMspDir()
localMspId := "DEFAULT"
err = common.InitCrypto(mspConfigPath, localMspId)
err = common.InitCrypto(mspConfigPath, localMspId, msp.ProviderTypeToString(msp.FABRIC))
assert.NoError(t, err, "Unexpected error [%s] calling InitCrypto()", err)
err = common.InitCrypto("/etc/foobaz", localMspId)
err = common.InitCrypto("/etc/foobaz", localMspId, msp.ProviderTypeToString(msp.FABRIC))
assert.Error(t, err, fmt.Sprintf("Expected error [%s] calling InitCrypto()", err))
localMspId = ""
err = common.InitCrypto(mspConfigPath, localMspId)
err = common.InitCrypto(mspConfigPath, localMspId, msp.ProviderTypeToString(msp.FABRIC))
assert.Error(t, err, fmt.Sprintf("Expected error [%s] calling InitCrypto()", err))
}

Expand Down
29 changes: 11 additions & 18 deletions peer/main.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package main

import (
"fmt"
_ "net/http/pprof"
"os"
"runtime"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"

_ "net/http/pprof"

"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/peer/chaincode"
"github.com/hyperledger/fabric/peer/channel"
"github.com/hyperledger/fabric/peer/clilogging"
"github.com/hyperledger/fabric/peer/common"
"github.com/hyperledger/fabric/peer/node"
"github.com/hyperledger/fabric/peer/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var logger = flogging.MustGetLogger("main")
Expand Down Expand Up @@ -102,7 +91,11 @@ func main() {
// Init the MSP
var mspMgrConfigDir = config.GetPath("peer.mspConfigPath")
var mspID = viper.GetString("peer.localMspId")
err = common.InitCrypto(mspMgrConfigDir, mspID)
var mspType = viper.GetString("peer.localMspType")
if mspType == "" {
mspType = msp.ProviderTypeToString(msp.FABRIC)
}
err = common.InitCrypto(mspMgrConfigDir, mspID, mspType)
if err != nil { // Handle errors reading the config file
logger.Errorf("Cannot run peer because %s", err.Error())
os.Exit(1)
Expand Down
12 changes: 12 additions & 0 deletions peer/node/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/hyperledger/fabric/core/scc"
"github.com/hyperledger/fabric/events/producer"
"github.com/hyperledger/fabric/gossip/service"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/peer/common"
peergossip "github.com/hyperledger/fabric/peer/gossip"
Expand Down Expand Up @@ -93,6 +94,17 @@ func initSysCCs() {
}

func serve(args []string) error {
// currently the peer only works with the standard MSP
// because in certain scenarios the MSP has to make sure
// that from a single credential you only have a single 'identity'.
// Idemix does not support this *YET* but it can be easily
// fixed to support it. For now, we just make sure that
// the peer only comes up with the standard MSP
mspType := mgmt.GetLocalMSP().GetType()
if mspType != msp.FABRIC {
panic("Unsupported msp type " + msp.ProviderTypeToString(mspType))
}

logger.Infof("Starting %s", version.GetInfo())

//aclmgmt initializes a proxy Processor that will be redirected to RSCC provider
Expand Down
3 changes: 3 additions & 0 deletions sampleconfig/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ peer:
# attempts until its retry logic gives up and returns an error
reconnectTotalTimeThreshold: 3600s

# Type for the local MSP - by default it's of type bccsp
localMspType: bccsp

# Used with Go profiling tools only in none production environment. In
# production, it should be disabled (eg enabled: false)
profile:
Expand Down

0 comments on commit 89d68d8

Please sign in to comment.