@@ -18,6 +18,7 @@ package chaincode
18
18
19
19
import (
20
20
"net"
21
+ "os"
21
22
"testing"
22
23
"time"
23
24
@@ -30,12 +31,46 @@ import (
30
31
"google.golang.org/grpc"
31
32
)
32
33
34
+ func deploySampleSysCC (t * testing.T , ctxt context.Context , chainID string ) error {
35
+ DeploySysCCs (chainID )
36
+
37
+ url := "github.com/hyperledger/fabric/core/system_chaincode/sample_syscc"
38
+
39
+ cdsforStop := & pb.ChaincodeDeploymentSpec {ExecEnv : 1 , ChaincodeSpec : & pb.ChaincodeSpec {Type : 1 , ChaincodeID : & pb.ChaincodeID {Name : "sample_syscc" , Path : url }, CtorMsg : & pb.ChaincodeInput {Args : [][]byte {[]byte ("" )}}}}
40
+
41
+ f := "putval"
42
+ args := util .ToChaincodeArgs (f , "greeting" , "hey there" )
43
+
44
+ spec := & pb.ChaincodeSpec {Type : 1 , ChaincodeID : & pb.ChaincodeID {Name : "sample_syscc" , Path : url }, CtorMsg : & pb.ChaincodeInput {Args : args }}
45
+ _ , _ , _ , err := invoke (ctxt , chainID , spec )
46
+ if err != nil {
47
+ theChaincodeSupport .Stop (ctxt , chainID , cdsforStop )
48
+ t .Logf ("Error invoking sample_syscc: %s" , err )
49
+ return err
50
+ }
51
+
52
+ f = "getval"
53
+ args = util .ToChaincodeArgs (f , "greeting" )
54
+ spec = & pb.ChaincodeSpec {Type : 1 , ChaincodeID : & pb.ChaincodeID {Name : "sample_syscc" , Path : url }, CtorMsg : & pb.ChaincodeInput {Args : args }}
55
+ _ , _ , _ , err = invoke (ctxt , chainID , spec )
56
+ if err != nil {
57
+ theChaincodeSupport .Stop (ctxt , chainID , cdsforStop )
58
+ t .Logf ("Error invoking sample_syscc: %s" , err )
59
+ return err
60
+ }
61
+
62
+ theChaincodeSupport .Stop (ctxt , chainID , cdsforStop )
63
+
64
+ return nil
65
+ }
66
+
33
67
// Test deploy of a transaction.
34
68
func TestExecuteDeploySysChaincode (t * testing.T ) {
35
69
var opts []grpc.ServerOption
36
70
grpcServer := grpc .NewServer (opts ... )
37
71
viper .Set ("peer.fileSystemPath" , "/var/hyperledger/test/tmpdb" )
38
72
kvledger .Initialize ("/var/hyperledger/test/tmpdb" )
73
+ defer os .RemoveAll ("/var/hyperledger/test/tmpdb" )
39
74
40
75
//use a different address than what we usually use for "peer"
41
76
//we override the peerAddress set in chaincode_support.go
@@ -75,35 +110,91 @@ func TestExecuteDeploySysChaincode(t *testing.T) {
75
110
76
111
chainID := util .GetTestChainID ()
77
112
78
- RegisterSysCCs (chainID )
113
+ RegisterSysCCs ()
79
114
80
- url := "github.com/hyperledger/fabric/core/system_chaincode/sample_syscc"
81
- f := "putval"
82
- args := util .ToChaincodeArgs (f , "greeting" , "hey there" )
115
+ /////^^^ system initialization completed ^^^
83
116
84
- spec := & pb.ChaincodeSpec {Type : 1 , ChaincodeID : & pb.ChaincodeID {Name : "sample_syscc" , Path : url }, CtorMsg : & pb.ChaincodeInput {Args : args }}
85
- _ , _ , _ , err = invoke (ctxt , chainID , spec )
117
+ kvledger .CreateLedger (chainID )
118
+
119
+ err = deploySampleSysCC (t , ctxt , chainID )
86
120
if err != nil {
87
121
closeListenerAndSleep (lis )
88
122
t .Fail ()
89
- t .Logf ("Error invoking sample_syscc: %s" , err )
90
123
return
91
124
}
92
125
93
- f = "getval"
94
- args = util .ToChaincodeArgs (f , "greeting" )
95
- spec = & pb.ChaincodeSpec {Type : 1 , ChaincodeID : & pb.ChaincodeID {Name : "sample_syscc" , Path : url }, CtorMsg : & pb.ChaincodeInput {Args : args }}
96
- _ , _ , _ , err = invoke (ctxt , chainID , spec )
126
+ closeListenerAndSleep (lis )
127
+ }
128
+
129
+ // Test multichains
130
+ func TestMultichains (t * testing.T ) {
131
+ var opts []grpc.ServerOption
132
+ grpcServer := grpc .NewServer (opts ... )
133
+ viper .Set ("peer.fileSystemPath" , "/var/hyperledger/test/tmpdb" )
134
+ kvledger .Initialize ("/var/hyperledger/test/tmpdb" )
135
+ defer os .RemoveAll ("/var/hyperledger/test/tmpdb" )
136
+
137
+ //use a different address than what we usually use for "peer"
138
+ //we override the peerAddress set in chaincode_support.go
139
+ // FIXME: Use peer.GetLocalAddress()
140
+ peerAddress := "0.0.0.0:21726"
141
+ lis , err := net .Listen ("tcp" , peerAddress )
142
+ if err != nil {
143
+ t .Fail ()
144
+ t .Logf ("Error starting peer listener %s" , err )
145
+ return
146
+ }
147
+
148
+ getPeerEndpoint := func () (* pb.PeerEndpoint , error ) {
149
+ return & pb.PeerEndpoint {ID : & pb.PeerID {Name : "testpeer" }, Address : peerAddress }, nil
150
+ }
151
+
152
+ ccStartupTimeout := time .Duration (5000 ) * time .Millisecond
153
+ pb .RegisterChaincodeSupportServer (grpcServer , NewChaincodeSupport (getPeerEndpoint , false , ccStartupTimeout ))
154
+
155
+ go grpcServer .Serve (lis )
156
+
157
+ var ctxt = context .Background ()
158
+
159
+ //set systemChaincodes to sample
160
+ systemChaincodes = []* SystemChaincode {
161
+ {
162
+ Enabled : true ,
163
+ Name : "sample_syscc" ,
164
+ Path : "github.com/hyperledger/fabric/core/system_chaincode/samplesyscc" ,
165
+ InitArgs : [][]byte {},
166
+ Chaincode : & samplesyscc.SampleSysCC {},
167
+ },
168
+ }
169
+
170
+ // System chaincode has to be enabled
171
+ viper .Set ("chaincode.system" , map [string ]string {"sample_syscc" : "true" })
172
+
173
+ RegisterSysCCs ()
174
+
175
+ /////^^^ system initialization completed ^^^
176
+
177
+ chainID := "chain1"
178
+
179
+ kvledger .CreateLedger (chainID )
180
+
181
+ err = deploySampleSysCC (t , ctxt , chainID )
97
182
if err != nil {
98
183
closeListenerAndSleep (lis )
99
184
t .Fail ()
100
- t .Logf ("Error invoking sample_syscc: %s" , err )
101
185
return
102
186
}
103
187
104
- cds := & pb.ChaincodeDeploymentSpec {ExecEnv : 1 , ChaincodeSpec : & pb.ChaincodeSpec {Type : 1 , ChaincodeID : & pb.ChaincodeID {Name : "sample_syscc" , Path : url }, CtorMsg : & pb.ChaincodeInput {Args : args }}}
188
+ chainID = "chain2"
189
+
190
+ kvledger .CreateLedger (chainID )
105
191
106
- theChaincodeSupport .Stop (ctxt , chainID , cds )
192
+ err = deploySampleSysCC (t , ctxt , chainID )
193
+ if err != nil {
194
+ closeListenerAndSleep (lis )
195
+ t .Fail ()
196
+ return
197
+ }
107
198
108
199
closeListenerAndSleep (lis )
109
200
}
0 commit comments