Skip to content

Commit

Permalink
Merge "[FAB-2696] Default chain broken in peer"
Browse files Browse the repository at this point in the history
  • Loading branch information
binhn authored and Gerrit Code Review committed Mar 9, 2017
2 parents ba6be96 + a3e3940 commit ba1275c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
38 changes: 36 additions & 2 deletions common/configtx/tool/provisional/provisional.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package provisional

import (
"fmt"
"os"
"path/filepath"

"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/config"
Expand Down Expand Up @@ -68,6 +70,38 @@ const (
BlockValidationPolicyKey = "BlockValidation"
)

func resolveMSPDir(path string) string {
if path == "" || path[0] == os.PathSeparator {
return path
}

// Look for MSP dir first in current path, then in ORDERER_CFG_PATH, then PEER_CFG_PATH, and finally in GOPATH
searchPath := []string{
".",
os.Getenv("ORDERER_CFG_PATH"),
os.Getenv("PEER_CFG_PATH"),
}

for _, p := range filepath.SplitList(os.Getenv("GOPATH")) {
searchPath = append(searchPath, filepath.Join(p, "src/github.com/hyperledger/fabric/common/configtx/tool/"))
}

for _, baseDir := range searchPath {
logger.Infof("Checking for MSPDir at: %s", baseDir)
fqPath := filepath.Join(baseDir, path)
if _, err := os.Stat(fqPath); err != nil {
// The mspdir does not exist
continue
}
return fqPath
}

logger.Panicf("Unable to resolve a path for MSPDir: %s", path)

// Unreachable
return ""
}

// DefaultChainCreationPolicyNames is the default value of ChainCreatorsKey.
var DefaultChainCreationPolicyNames = []string{AcceptAllPolicyKey}

Expand Down Expand Up @@ -116,7 +150,7 @@ func New(conf *genesisconfig.Profile) Generator {
}

for _, org := range conf.Orderer.Organizations {
mspConfig, err := msp.GetVerifyingMspConfig(org.MSPDir, org.BCCSP, org.ID)
mspConfig, err := msp.GetVerifyingMspConfig(resolveMSPDir(org.MSPDir), org.BCCSP, org.ID)
if err != nil {
logger.Panicf("Error loading MSP configuration for org %s: %s", org.Name, err)
}
Expand Down Expand Up @@ -146,7 +180,7 @@ func New(conf *genesisconfig.Profile) Generator {
policies.TemplateImplicitMetaMajorityPolicy([]string{config.ApplicationGroupKey}, configvaluesmsp.AdminsPolicyKey),
}
for _, org := range conf.Application.Organizations {
mspConfig, err := msp.GetVerifyingMspConfig(org.MSPDir, org.BCCSP, org.ID)
mspConfig, err := msp.GetVerifyingMspConfig(resolveMSPDir(org.MSPDir), org.BCCSP, org.ID)
if err != nil {
logger.Panicf("Error loading MSP configuration for org %s: %s", org.Name, err)
}
Expand Down
48 changes: 24 additions & 24 deletions peer/node/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ import (
"syscall"
"time"

"github.com/hyperledger/fabric/common/config"
"github.com/hyperledger/fabric/common/config/msp"
"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/common/genesis"
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
"github.com/hyperledger/fabric/common/localmsp"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core"
"github.com/hyperledger/fabric/core/chaincode"
Expand All @@ -48,6 +44,7 @@ import (
"github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/peer/common"
"github.com/hyperledger/fabric/peer/gossip/mcs"
cb "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -59,6 +56,12 @@ var chaincodeDevMode bool
var peerDefaultChain bool
var orderingEndpoint string

// XXXDefaultChannelMSPID should not be defined in production code
// It should only be referenced in tests. However, it is necessary
// to support the 'default chain' setup so temporarilly adding until
// this concept can be removed to testing scenarios only
const XXXDefaultChannelMSPID = "DEFAULT"

func startCmd() *cobra.Command {
// Set the flags on the node start command.
flags := nodeStartCmd.Flags()
Expand Down Expand Up @@ -180,24 +183,21 @@ func serve(args []string) error {

chainID := util.GetTestChainID()

// add readers, writers and admin policies for the default chain
policyTemplate := configtx.NewSimpleTemplate(
policies.TemplateImplicitMetaAnyPolicy([]string{config.ApplicationGroupKey}, msp.ReadersPolicyKey),
policies.TemplateImplicitMetaAnyPolicy([]string{config.ApplicationGroupKey}, msp.WritersPolicyKey),
policies.TemplateImplicitMetaMajorityPolicy([]string{config.ApplicationGroupKey}, msp.AdminsPolicyKey),
)

// We create a genesis block for the test
// chain with its MSP so that we can transact
block, err := genesis.NewFactoryImpl(
configtx.NewCompositeTemplate(
test.ApplicationOrgTemplate(),
configtx.NewSimpleTemplate(config.TemplateOrdererAddresses([]string{orderingEndpoint})),
policyTemplate)).Block(chainID)

if nil != err {
logger.Panicf("Unable to create genesis block for [%s] due to [%s]", chainID, err)
}
var block *cb.Block

func() {
defer func() {
if err := recover(); err != nil {
logger.Fatalf("Peer configured to start with the default test chain, but supporting configuration files did not match. Please ensure that configtx.yaml contains the unmodified SampleSingleMSPSolo profile and that msp/sampleconfig is present.\n%s", err)
}
}()

genConf := genesisconfig.Load(genesisconfig.SampleSingleMSPSoloProfile)
genConf.Orderer.Addresses = []string{orderingEndpoint}
genConf.Application.Organizations[0].Name = XXXDefaultChannelMSPID
genConf.Application.Organizations[0].ID = XXXDefaultChannelMSPID
block = provisional.New(genConf).GenesisBlockForChannel(chainID)
}()

//this creates testchainid and sets up gossip
if err = peer.CreateChainFromBlock(block); err == nil {
Expand Down

0 comments on commit ba1275c

Please sign in to comment.