@@ -17,15 +17,18 @@ limitations under the License.
17
17
package test
18
18
19
19
import (
20
- "io/ioutil"
21
20
"os"
21
+ "path/filepath"
22
22
23
23
"github.com/hyperledger/fabric/common/configtx"
24
+ configtxapplication "github.com/hyperledger/fabric/common/configtx/handlers/application"
25
+ configtxmsp "github.com/hyperledger/fabric/common/configtx/handlers/msp"
26
+ genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
27
+ "github.com/hyperledger/fabric/common/configtx/tool/provisional"
24
28
"github.com/hyperledger/fabric/common/genesis"
29
+ "github.com/hyperledger/fabric/msp"
25
30
cb "github.com/hyperledger/fabric/protos/common"
26
- "github.com/hyperledger/fabric/protos/utils"
27
31
28
- "github.com/golang/protobuf/proto"
29
32
logging "github.com/op/go-logging"
30
33
)
31
34
@@ -36,91 +39,66 @@ const (
36
39
AcceptAllPolicyKey = "AcceptAllPolicy"
37
40
)
38
41
39
- const (
40
- OrdererTemplateName = "orderer.template"
41
- MSPTemplateName = "msp.template"
42
- PeerTemplateName = "peer.template"
43
- )
44
-
45
- var ordererTemplate configtx.Template
46
- var mspTemplate configtx.Template
47
- var peerTemplate configtx.Template
48
-
49
- var compositeTemplate configtx.Template
50
-
51
- var genesisFactory genesis.Factory
42
+ var sampleMSPPath string
52
43
53
- func init () {
54
- ordererTemplate = readTemplate (OrdererTemplateName )
55
- mspTemplate = readTemplate (MSPTemplateName )
56
- peerTemplate = readTemplate (PeerTemplateName )
57
-
58
- compositeTemplate = configtx .NewCompositeTemplate (mspTemplate , ordererTemplate , peerTemplate )
59
- genesisFactory = genesis .NewFactoryImpl (compositeTemplate )
44
+ func dirExists (path string ) bool {
45
+ _ , err := os .Stat (path )
46
+ return err == nil
60
47
}
61
48
62
- func resolveName (name string ) (string , []byte ) {
63
- path := os .Getenv ("GOPATH" ) + "/src/github.com/hyperledger/fabric/common/configtx/test/" + name
64
- data , err := ioutil .ReadFile (path )
65
- if err == nil {
66
- return path , data
49
+ func init () {
50
+ mspSampleConfig := "/msp/sampleconfig"
51
+ peerPath := filepath .Join (os .Getenv ("PEER_CFG_PATH" ), mspSampleConfig )
52
+ ordererPath := filepath .Join (os .Getenv ("ORDERER_CFG_PATH" ), mspSampleConfig )
53
+ switch {
54
+ case dirExists (peerPath ):
55
+ sampleMSPPath = peerPath
56
+ return
57
+ case dirExists (ordererPath ):
58
+ sampleMSPPath = ordererPath
59
+ return
67
60
}
68
61
69
- path = os .Getenv ("PEER_CFG_PATH" ) + "/common/configtx/test/" + name
70
- data , err = ioutil .ReadFile (path )
71
- if err != nil {
72
- panic (err )
62
+ gopath := os .Getenv ("GOPATH" )
63
+ for _ , p := range filepath .SplitList (gopath ) {
64
+ samplePath := filepath .Join (p , "src/github.com/hyperledger/fabric" , mspSampleConfig )
65
+ if ! dirExists (samplePath ) {
66
+ continue
67
+ }
68
+ sampleMSPPath = samplePath
73
69
}
74
70
75
- return path , data
76
- }
77
-
78
- func readTemplate (name string ) configtx.Template {
79
- _ , data := resolveName (name )
80
-
81
- templateProto := & cb.ConfigTemplate {}
82
- err := proto .Unmarshal (data , templateProto )
83
- if err != nil {
84
- panic (err )
71
+ if sampleMSPPath == "" {
72
+ logger .Panicf ("Could not find genesis.yaml, try setting PEER_CFG_PATH, ORDERER_CFG_PATH, or GOPATH correctly" )
85
73
}
86
-
87
- return configtx .NewSimpleTemplate (templateProto .Items ... )
88
- }
89
-
90
- // WriteTemplate takes an output file and set of config items and writes them to that file as a marshaled ConfigTemplate
91
- func WriteTemplate (name string , items ... * cb.ConfigItem ) {
92
- path , _ := resolveName (name )
93
-
94
- logger .Debugf ("Encoding config template" )
95
- outputData := utils .MarshalOrPanic (& cb.ConfigTemplate {
96
- Items : items ,
97
- })
98
-
99
- logger .Debugf ("Writing config to %s" , path )
100
- ioutil .WriteFile (path , outputData , 0644 )
101
74
}
102
75
103
76
// MakeGenesisBlock creates a genesis block using the test templates for the given chainID
104
77
func MakeGenesisBlock (chainID string ) (* cb.Block , error ) {
105
- return genesisFactory .Block (chainID )
78
+ return genesis . NewFactoryImpl ( CompositeTemplate ()) .Block (chainID )
106
79
}
107
80
108
81
// OrderererTemplate returns the test orderer template
109
82
func OrdererTemplate () configtx.Template {
110
- return ordererTemplate
83
+ genConf := genesisconfig .Load ()
84
+ return provisional .New (genConf ).ChannelTemplate ()
111
85
}
112
86
113
- // MSPerTemplate returns the test MSP template
87
+ // MSPTemplate returns the test MSP template
114
88
func MSPTemplate () configtx.Template {
115
- return mspTemplate
89
+ mspConf , err := msp .GetLocalMspConfig (sampleMSPPath , "SAMPLE" )
90
+ if err != nil {
91
+ logger .Panicf ("Could not load sample MSP config: %s" , err )
92
+ }
93
+ return configtx .NewSimpleTemplateNext (configtxmsp .TemplateGroupMSP ([]string {configtxapplication .GroupKey }, mspConf ))
116
94
}
117
95
118
- // MSPerTemplate returns the test peer template
119
- func PeerTemplate () configtx.Template {
120
- return peerTemplate
96
+ // ApplicationTemplate returns the test application template
97
+ func ApplicationTemplate () configtx.Template {
98
+ return configtx . NewSimpleTemplateNext ( configtxapplication . DefaultAnchorPeers ())
121
99
}
122
100
123
101
// CompositeTemplate returns the composite template of peer, orderer, and MSP
124
102
func CompositeTemplate () configtx.Template {
125
- return compositeTemplate
103
+ return configtx . NewCompositeTemplate ( MSPTemplate (), OrdererTemplate (), ApplicationTemplate ())
126
104
}
0 commit comments