Skip to content

Commit e3ce4f1

Browse files
author
Jason Yellick
committed
[FAB-1962] Utilize templates in peer test
https://jira.hyperledger.org/browse/FAB-1962 The peer already depends on the orderer template, but is not currently using the peer or MSP templates. Where appropriate, especially the MSP template should be utilized. This CR removes the last vestiges of the old MSP manual encoding which the peer depended upon. Change-Id: I12ccbf28fd10b6e8ee30bf3d54ccc3ad17d3becf Signed-off-by: Jason Yellick <[email protected]>
1 parent 6500a2f commit e3ce4f1

File tree

5 files changed

+28
-54
lines changed

5 files changed

+28
-54
lines changed

common/configtx/test/helper.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ var ordererTemplate configtx.Template
4646
var mspTemplate configtx.Template
4747
var peerTemplate configtx.Template
4848

49+
var compositeTemplate configtx.Template
50+
4951
var genesisFactory genesis.Factory
5052

5153
func init() {
5254
ordererTemplate = readTemplate(OrdererTemplateName)
5355
mspTemplate = readTemplate(MSPTemplateName)
5456
peerTemplate = readTemplate(PeerTemplateName)
55-
genesisFactory = genesis.NewFactoryImpl(configtx.NewCompositeTemplate(mspTemplate, ordererTemplate, peerTemplate))
57+
58+
compositeTemplate = configtx.NewCompositeTemplate(mspTemplate, ordererTemplate, peerTemplate)
59+
genesisFactory = genesis.NewFactoryImpl(compositeTemplate)
5660
}
5761

5862
func resolveName(name string) (string, []byte) {
@@ -115,3 +119,8 @@ func MSPTemplate() configtx.Template {
115119
func PeerTemplate() configtx.Template {
116120
return peerTemplate
117121
}
122+
123+
// CompositeTemplate returns the composite template of peer, orderer, and MSP
124+
func CompositeTemplate() configtx.Template {
125+
return compositeTemplate
126+
}

core/common/validation/config_test.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,14 @@ package validation
1919
import (
2020
"testing"
2121

22-
"github.com/hyperledger/fabric/common/chainconfig"
2322
"github.com/hyperledger/fabric/common/configtx"
24-
"github.com/hyperledger/fabric/common/configtx/test"
23+
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
2524
"github.com/hyperledger/fabric/common/util"
26-
"github.com/hyperledger/fabric/protos/utils"
2725
)
2826

2927
func TestValidateConfigTx(t *testing.T) {
3028
chainID := util.GetTestChainID()
31-
oTemplate := test.OrdererTemplate()
32-
mspcfg := configtx.NewSimpleTemplate(utils.EncodeMSPUnsigned(chainID))
33-
chainCfg := configtx.NewSimpleTemplate(chainconfig.DefaultHashingAlgorithm())
34-
chCrtTemp := configtx.NewCompositeTemplate(oTemplate, mspcfg, chainCfg)
35-
chCrtEnv, err := configtx.MakeChainCreationTransaction(test.AcceptAllPolicyKey, chainID, signer, chCrtTemp)
29+
chCrtEnv, err := configtx.MakeChainCreationTransaction(configtxtest.AcceptAllPolicyKey, chainID, signer, configtxtest.CompositeTemplate())
3630
if err != nil {
3731
t.Fatalf("MakeChainCreationTransaction failed, err %s", err)
3832
return

msp/mgmt/peermsp_test.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,26 @@ limitations under the License.
1717
package mgmt
1818

1919
import (
20+
"io/ioutil"
21+
"os"
2022
"testing"
2123

2224
"github.com/hyperledger/fabric/common/util"
2325
"github.com/hyperledger/fabric/msp"
2426
"github.com/hyperledger/fabric/protos/msp/testutils"
25-
"github.com/hyperledger/fabric/protos/utils"
2627
)
2728

29+
// getTestMSPConfigPath returns the path to sampleconfig for unit tests
30+
func getTestMSPConfigPath() string {
31+
cfgPath := os.Getenv("PEER_CFG_PATH") + "/msp/sampleconfig/"
32+
if _, err := ioutil.ReadDir(cfgPath); err != nil {
33+
cfgPath = os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/msp/sampleconfig/"
34+
}
35+
return cfgPath
36+
}
37+
2838
func TestLocalMSP(t *testing.T) {
29-
testMSPConfigPath := utils.GetTESTMSPConfigPath()
39+
testMSPConfigPath := getTestMSPConfigPath()
3040
err := LoadLocalMsp(testMSPConfigPath)
3141
if err != nil {
3242
t.Fatalf("LoadLocalMsp failed, err %s", err)
@@ -40,7 +50,7 @@ func TestLocalMSP(t *testing.T) {
4050

4151
// TODO: as soon as proper per-chain MSP support is developed, this test will no longer be required
4252
func TestFakeSetup(t *testing.T) {
43-
testMSPConfigPath := utils.GetTESTMSPConfigPath()
53+
testMSPConfigPath := getTestMSPConfigPath()
4454
err := LoadFakeSetupWithLocalMspAndTestChainMsp(testMSPConfigPath)
4555
if err != nil {
4656
t.Fatalf("LoadLocalMsp failed, err %s", err)
@@ -62,7 +72,7 @@ func TestFakeSetup(t *testing.T) {
6272
}
6373

6474
func TestGetMSPManagerFromBlock(t *testing.T) {
65-
testMSPConfigPath := utils.GetTESTMSPConfigPath()
75+
testMSPConfigPath := getTestMSPConfigPath()
6676
conf, err := msp.GetLocalMspConfig(testMSPConfigPath)
6777
if err != nil {
6878
t.Fatalf("GetLocalMspConfig failed, err %s", err)

peer/channel/create.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/hyperledger/fabric/peer/common"
2929
"github.com/hyperledger/fabric/peer/sharedconfig"
3030
cb "github.com/hyperledger/fabric/protos/common"
31-
"github.com/hyperledger/fabric/protos/utils"
3231
"github.com/spf13/cobra"
3332
)
3433

@@ -53,9 +52,9 @@ func sendCreateChainTransaction(cf *ChannelCmdFactory) error {
5352
if err != nil {
5453
return err
5554
}
56-
//TODO this is a temporary hack until `orderer.template` is supplied from the CLI
55+
//TODO this is a temporary hack until `orderer.template` and 'msp.template' is supplied from the CLI
5756
oTemplate := configtxtest.OrdererTemplate()
58-
mspTemplate := configtx.NewSimpleTemplate(utils.EncodeMSPUnsigned(chainID))
57+
mspTemplate := configtxtest.MSPTemplate()
5958
gossTemplate := configtx.NewSimpleTemplate(sharedconfig.TemplateAnchorPeers(anchorPeers))
6059
chCrtTemp := configtx.NewCompositeTemplate(oTemplate, mspTemplate, gossTemplate)
6160

protos/utils/blockutils.go

-38
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ package utils
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
22-
"os"
2321

2422
"github.com/golang/protobuf/proto"
25-
26-
"github.com/hyperledger/fabric/msp"
2723
cb "github.com/hyperledger/fabric/protos/common"
2824
)
2925

@@ -112,37 +108,3 @@ func InitBlockMetadata(block *cb.Block) {
112108
}
113109
}
114110
}
115-
116-
const xxxDefaultModificationPolicyID = "DefaultModificationPolicy" // Break an import cycle during work to remove the below configtx construction methods
117-
118-
// GetTESTMSPConfigPath This function is needed to locate the MSP test configuration when running
119-
// in CI build env or local with "make unit-test". A better way to manage this
120-
// is to define a config path in yaml that may point to test or production
121-
// location of the config
122-
func GetTESTMSPConfigPath() string {
123-
cfgPath := os.Getenv("PEER_CFG_PATH") + "/msp/sampleconfig/"
124-
if _, err := ioutil.ReadDir(cfgPath); err != nil {
125-
cfgPath = os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/msp/sampleconfig/"
126-
}
127-
return cfgPath
128-
}
129-
130-
// EncodeMSPUnsigned gets the unsigned configuration item with the default MSP
131-
func EncodeMSPUnsigned(chainID string) *cb.ConfigurationItem {
132-
cfgPath := GetTESTMSPConfigPath()
133-
conf, err := msp.GetLocalMspConfig(cfgPath)
134-
if err != nil {
135-
panic(fmt.Sprintf("GetLocalMspConfig failed, err %s", err))
136-
}
137-
return &cb.ConfigurationItem{
138-
Type: cb.ConfigurationItem_MSP,
139-
Key: "DEFAULT", // XXX this should really be computed dynamically, but it's better than the old wrong "MSP"
140-
Value: MarshalOrPanic(conf),
141-
ModificationPolicy: xxxDefaultModificationPolicyID,
142-
}
143-
}
144-
145-
// EncodeMSP gets the signed configuration item with the default MSP
146-
func EncodeMSP(chainID string) *cb.SignedConfigurationItem {
147-
return &cb.SignedConfigurationItem{ConfigurationItem: MarshalOrPanic(EncodeMSPUnsigned(chainID))}
148-
}

0 commit comments

Comments
 (0)