Skip to content

Commit

Permalink
Get ID for local MSP from core.yaml
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ale-linux committed Feb 13, 2017
1 parent d736204 commit f30e1b6
Show file tree
Hide file tree
Showing 32 changed files with 208 additions and 114 deletions.
2 changes: 1 addition & 1 deletion common/configtx/handlers/msp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

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

confBytes, err := proto.Marshal(conf)
Expand Down
2 changes: 1 addition & 1 deletion common/localmsp/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestMain(m *testing.M) {
mspMgrConfigDir = os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/msp/sampleconfig/"
}

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

Expand Down
3 changes: 2 additions & 1 deletion core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
"github.com/hyperledger/fabric/msp"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/hyperledger/fabric/protos/common"
"github.com/spf13/viper"
"golang.org/x/net/context"
Expand Down Expand Up @@ -1152,7 +1153,7 @@ func TestMain(m *testing.M) {

// setup the MSP manager so that we can sign/verify
mspMgrConfigDir := "../../msp/sampleconfig/"
mspmgmt.LoadFakeSetupWithLocalMspAndTestChainMsp(mspMgrConfigDir)
msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir)
signer, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
if err != nil {
os.Exit(-1)
Expand Down
3 changes: 2 additions & 1 deletion core/common/validation/fullflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/msp"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
Expand Down Expand Up @@ -330,7 +331,7 @@ func TestMain(m *testing.M) {
// setup crypto algorithms
// setup the MSP manager so that we can sign/verify
mspMgrConfigDir := "../../../msp/sampleconfig/"
err := mspmgmt.LoadFakeSetupWithLocalMspAndTestChainMsp(mspMgrConfigDir)
err := msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir)
if err != nil {
fmt.Printf("Could not initialize msp, err %s", err)
os.Exit(-1)
Expand Down
3 changes: 2 additions & 1 deletion core/endorser/endorser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
syscc "github.com/hyperledger/fabric/core/scc"
"github.com/hyperledger/fabric/msp"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
pbutils "github.com/hyperledger/fabric/protos/utils"
Expand Down Expand Up @@ -426,7 +427,7 @@ func TestMain(m *testing.M) {

// setup the MSP manager so that we can sign/verify
mspMgrConfigDir := "../../msp/sampleconfig/"
err = mspmgmt.LoadFakeSetupWithLocalMspAndTestChainMsp(mspMgrConfigDir)
err = msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir)
if err != nil {
fmt.Printf("Could not initialize msp/signer, err %s", err)
os.Exit(-1)
Expand Down
3 changes: 2 additions & 1 deletion core/peer/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/hyperledger/fabric/core/mocks/ccprovider"
"github.com/hyperledger/fabric/gossip/service"
"github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestCreateChainFromBlock(t *testing.T) {
go grpcServer.Serve(socket)
defer grpcServer.Stop()

mgmt.LoadFakeSetupWithLocalMspAndTestChainMsp("../../msp/sampleconfig")
msptesttools.LoadMSPSetupForTesting("../../msp/sampleconfig")

identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
service.InitGossipServiceCustomDeliveryFactory(identity, "localhost:13611", grpcServer, &mockDeliveryClientFactory{})
Expand Down
3 changes: 2 additions & 1 deletion core/scc/cscc/configer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/gossip/service"
"github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
Expand Down Expand Up @@ -142,7 +143,7 @@ func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
go grpcServer.Serve(socket)
defer grpcServer.Stop()

mgmt.LoadFakeSetupWithLocalMspAndTestChainMsp("../../../msp/sampleconfig")
msptesttools.LoadMSPSetupForTesting("../../../msp/sampleconfig")
identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()

service.InitGossipServiceCustomDeliveryFactory(identity, "localhost:13611", grpcServer, &mockDeliveryClientFactory{})
Expand Down
3 changes: 2 additions & 1 deletion core/scc/escc/endorser_onevalidsignature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/common/validation"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
putils "github.com/hyperledger/fabric/protos/utils"
Expand Down Expand Up @@ -311,7 +312,7 @@ func validateProposalResponse(prBytes []byte, proposal *pb.Proposal, visibility

func TestMain(m *testing.M) {
mspMgrConfigDir := "../../../msp/sampleconfig/"
mspmgmt.LoadFakeSetupWithLocalMspAndTestChainMsp(mspMgrConfigDir)
msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir)

os.Exit(m.Run())
}
3 changes: 2 additions & 1 deletion core/scc/vscc/validator_onevalidsignature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/msp"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
Expand Down Expand Up @@ -136,7 +137,7 @@ func TestMain(m *testing.M) {

// setup the MSP manager so that we can sign/verify
mspMgrConfigDir := "../../../msp/sampleconfig/"
mspmgmt.LoadFakeSetupWithLocalMspAndTestChainMsp(mspMgrConfigDir)
msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir)

id, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion examples/ccchecker/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ func initYaml(mainFlags *pflag.FlagSet) {
//initialize MSP from -m <mspconfigdir>. Defaults to ../../msp/sampleconfig
func initMSP(mainFlags *pflag.FlagSet) {
mspMgrConfigDir := ""
mspID := ""
mainFlags.StringVarP(&mspMgrConfigDir, "mspcfgdir", "m", "../../msp/sampleconfig/", "Path to MSP dir")
mainFlags.StringVarP(&mspID, "mspid", "i", "DEFAULT", "MSP ID")

err := common.InitCrypto(mspMgrConfigDir)
err := common.InitCrypto(mspMgrConfigDir, mspID)
if err != nil {
panic(err.Error())
}
Expand Down
3 changes: 2 additions & 1 deletion gossip/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/hyperledger/fabric/gossip/common"
"github.com/hyperledger/fabric/gossip/identity"
"github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/spf13/viper"
"google.golang.org/grpc"
)
Expand All @@ -46,7 +47,7 @@ func TestNewGossipCryptoService(t *testing.T) {
endpoint2 := "localhost:5612"
endpoint3 := "localhost:5613"

mgmt.LoadFakeSetupWithLocalMspAndTestChainMsp("../../msp/sampleconfig")
msptesttools.LoadMSPSetupForTesting("../../msp/sampleconfig")
peerIdentity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()

cryptSvc := &cryptoService{}
Expand Down
3 changes: 2 additions & 1 deletion gossip/service/gossip_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/hyperledger/fabric/gossip/api"
"github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
)
Expand All @@ -37,7 +38,7 @@ func TestInitGossipService(t *testing.T) {
go grpcServer.Serve(socket)
defer grpcServer.Stop()

mgmt.LoadFakeSetupWithLocalMspAndTestChainMsp("../../msp/sampleconfig")
msptesttools.LoadMSPSetupForTesting("../../msp/sampleconfig")
identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()

wg := sync.WaitGroup{}
Expand Down
4 changes: 2 additions & 2 deletions msp/configbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const (
intermediatecerts = "intermediatecerts"
)

func GetLocalMspConfig(dir string) (*msp.MSPConfig, error) {
func GetLocalMspConfig(dir string, ID string) (*msp.MSPConfig, error) {
cacertDir := filepath.Join(dir, cacerts)
signcertDir := filepath.Join(dir, signcerts)
admincertDir := filepath.Join(dir, admincerts)
Expand Down Expand Up @@ -131,7 +131,7 @@ func GetLocalMspConfig(dir string) (*msp.MSPConfig, error) {
RootCerts: cacerts,
IntermediateCerts: intermediatecert,
SigningIdentity: sigid,
Name: "DEFAULT"}
Name: ID}

fmpsjs, _ := proto.Marshal(fmspconf)

Expand Down
57 changes: 7 additions & 50 deletions msp/mgmt/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,26 @@ limitations under the License.
package mgmt

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

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

"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
// LoadLocalMsp loads the local MSP from the specified directory
func LoadLocalMsp(dir string, mspID string) error {
if mspID == "" {
return errors.New("The local MSP must have an ID")
}

err = GetLocalMSP().Setup(conf)
conf, err := msp.GetLocalMspConfig(dir, mspID)
if err != nil {
return err
}

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

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

return nil
return GetLocalMSP().Setup(conf)
}

// FIXME: AS SOON AS THE CHAIN MANAGEMENT CODE IS COMPLETE,
Expand Down
26 changes: 1 addition & 25 deletions msp/mgmt/peermsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"io/ioutil"
"os"
"testing"

"github.com/hyperledger/fabric/common/util"
)

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

func TestLocalMSP(t *testing.T) {
testMSPConfigPath := getTestMSPConfigPath()
err := LoadLocalMsp(testMSPConfigPath)
if err != nil {
t.Fatalf("LoadLocalMsp failed, err %s", err)
}

_, err = GetLocalMSP().GetDefaultSigningIdentity()
if err != nil {
t.Fatalf("GetDefaultSigningIdentity failed, err %s", err)
}
}
err := LoadLocalMsp(testMSPConfigPath, "DEFAULT")

// TODO: as soon as proper per-chain MSP support is developed, this test will no longer be required
func TestFakeSetup(t *testing.T) {
testMSPConfigPath := getTestMSPConfigPath()
err := LoadFakeSetupWithLocalMspAndTestChainMsp(testMSPConfigPath)
if err != nil {
t.Fatalf("LoadLocalMsp failed, err %s", err)
}
Expand All @@ -58,13 +43,4 @@ func TestFakeSetup(t *testing.T) {
if err != nil {
t.Fatalf("GetDefaultSigningIdentity failed, err %s", err)
}

msps, err := GetManagerForChain(util.GetTestChainID()).GetMSPs()
if err != nil {
t.Fatalf("EnlistedMSPs failed, err %s", err)
}

if msps == nil || len(msps) == 0 {
t.Fatalf("There are no MSPS in the manager for chain %s", util.GetTestChainID())
}
}
70 changes: 70 additions & 0 deletions msp/mgmt/testtools/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright IBM Corp. 2017 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.
*/

package msptesttools

import (
"os"
"path/filepath"

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

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
}

// LoadTestMSPSetup sets up the local MSP
// and a chain MSP for the default chain
func LoadMSPSetupForTesting(dir string) error {
var err error
if dir, err = getConfigPath(dir); err != nil {
return err
}
conf, err := msp.GetLocalMspConfig(dir, "DEFAULT")
if err != nil {
return err
}

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

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

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

return nil
}
Loading

0 comments on commit f30e1b6

Please sign in to comment.