-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSendTx.java
More file actions
289 lines (241 loc) · 13.2 KB
/
SendTx.java
File metadata and controls
289 lines (241 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Copyright 2016, 2017 DTCC, Fujitsu Australia Software Technology, IBM - 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 org.hyperledger.fabric.sdkintegration;
import java.io.File;
import java.net.MalformedURLException;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;
import org.hyperledger.fabric.sdk.ChaincodeID;
import org.hyperledger.fabric.sdk.Channel;
import org.hyperledger.fabric.sdk.EventHub;
import org.hyperledger.fabric.sdk.HFClient;
import org.hyperledger.fabric.sdk.Peer;
import org.hyperledger.fabric.sdk.ProposalResponse;
import org.hyperledger.fabric.sdk.QueryByChaincodeRequest;
import org.hyperledger.fabric.sdk.SDKUtils;
import org.hyperledger.fabric.sdk.TestConfigHelper;
import org.hyperledger.fabric.sdk.TransactionProposalRequest;
import org.hyperledger.fabric.sdk.BlockEvent.TransactionEvent;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.hyperledger.fabric.sdk.testutils.TestConfig;
import org.hyperledger.fabric_ca.sdk.HFCAClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Test end to end scenario
*/
public class SendTx {
private static final TestConfig testConfig = TestConfig.getConfig();
private static final String TEST_ADMIN_NAME = "admin";
private static final String TESTUSER_1_NAME = "user1";
private static final String CHAIN_CODE_NAME = "autotestcc";
private static final String CHAIN_CODE_VERSION = "1.0";
private static final String FOO_CHANNEL_NAME = "mychannel";
private final TestConfigHelper configHelper = new TestConfigHelper();
private Collection<SampleOrg> testSampleOrgs;
@Before
public void checkConfig() throws NoSuchFieldException, SecurityException, IllegalArgumentException,
IllegalAccessException, MalformedURLException {
out("\n\n\nRUNNING: End2endIT.\n");
configHelper.clearConfig();
configHelper.customizeConfig();
testSampleOrgs = testConfig.getIntegrationTestsSampleOrgs();
//Set up hfca for each sample org
for (SampleOrg sampleOrg : testSampleOrgs) {
sampleOrg.setCAClient(HFCAClient.createNewInstance(sampleOrg.getCALocation(), sampleOrg.getCAProperties()));
}
}
@After
public void clearConfig() {
try {
configHelper.clearConfig();
} catch (Exception e) {
}
}
@Test
public void setup() {
try {
// Setup client
HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
//Set up USERS
File sampleStoreFile = new File(System.getProperty("java.io.tmpdir") + "/HFCSampletest.properties");
if (sampleStoreFile.exists()) { //For testing start fresh
sampleStoreFile.delete();
}
final SampleStore sampleStore = new SampleStore(sampleStoreFile);
// get users for all orgs
for (SampleOrg sampleOrg : testSampleOrgs) {
final String orgName = sampleOrg.getName();
SampleUser admin = sampleStore.getMember(TEST_ADMIN_NAME, orgName);
sampleOrg.setAdmin(admin); // The admin of this org.
// No need to enroll or register all done in End2endIt !
SampleUser user = sampleStore.getMember(TESTUSER_1_NAME, orgName);
sampleOrg.addUser(user); //Remember user belongs to this Org
final String sampleOrgName = sampleOrg.getName();
final String sampleOrgDomainName = sampleOrg.getDomainName();
SampleUser peerOrgAdmin = sampleStore.getMember(sampleOrgName + "Admin", sampleOrgName, sampleOrg
.getMSPID(),
Util.findFileSk(Paths.get(testConfig.getTestChannelPath(), "crypto-config/peerOrganizations/",
sampleOrgDomainName, format("/users/Admin@%s/msp/keystore", sampleOrgDomainName))
.toFile()),
Paths.get(testConfig.getTestChannelPath(), "crypto-config/peerOrganizations/",
sampleOrgDomainName,
format("/users/Admin@%s/msp/signcerts/Admin@%s-cert.pem", sampleOrgDomainName,
sampleOrgDomainName)).toFile());
sampleOrg.setPeerAdmin(peerOrgAdmin); //A special user that can create channels, join peers and
// install chaincode
}
SampleOrg sampleOrg = testConfig.getIntegrationTestsSampleOrg("peerOrg1");
Channel fooChannel = reconstructChannel(FOO_CHANNEL_NAME, client, sampleOrg);
ChaincodeID chaincodeID = ChaincodeID.newBuilder().setName(CHAIN_CODE_NAME).setVersion
(CHAIN_CODE_VERSION).build();
invoke(client, fooChannel, sampleOrg, chaincodeID);
query(client, fooChannel, chaincodeID);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
void invoke(HFClient client, Channel channel, SampleOrg sampleOrg, ChaincodeID chaincodeID) {
Collection<ProposalResponse> successful = new LinkedList<>();
Collection<ProposalResponse> failed = new LinkedList<>();
try {
client.setUserContext(sampleOrg.getPeerAdmin());
/// Send transaction proposal to all peers
TransactionProposalRequest transactionProposalRequest = client.newTransactionProposalRequest();
transactionProposalRequest.setChaincodeID(chaincodeID);
transactionProposalRequest.setFcn("invoke");
transactionProposalRequest.setProposalWaitTime(testConfig.getProposalWaitTime());
transactionProposalRequest.setArgs(new String[]{"a", "b", "10"});
Map<String, byte[]> tm2 = new HashMap<>();
tm2.put("HyperLedgerFabric", "TransactionProposalRequest:JavaSDK".getBytes(UTF_8));
tm2.put("method", "TransactionProposalRequest".getBytes(UTF_8));
tm2.put("result", ":)".getBytes(UTF_8)); /// This should be returned see chaincode.
transactionProposalRequest.setTransientMap(tm2);
out("sending transactionProposal to all peers with arguments: move(a,b,100)");
Collection<ProposalResponse> transactionPropResp = channel.sendTransactionProposal
(transactionProposalRequest, channel.getPeers());
for (ProposalResponse response : transactionPropResp) {
if (response.getStatus() == ProposalResponse.Status.SUCCESS) {
out("Successful transaction proposal response Txid: %s from peer %s", response.getTransactionID()
, response.getPeer().getName());
successful.add(response);
} else {
failed.add(response);
}
}
// Check that all the proposals are consistent with each other. We should have only one set
// where all the proposals above are consistent.
Collection<Set<ProposalResponse>> proposalConsistencySets = SDKUtils.getProposalConsistencySets
(transactionPropResp);
if (proposalConsistencySets.size() != 1) {
fail(format("Expected only one set of consistent proposal responses but got %d",
proposalConsistencySets.size()));
}
out("Received %d transaction proposal responses. Successful+verified: %d . Failed: %d",
transactionPropResp.size(), successful.size(), failed.size());
if (failed.size() > 0) {
ProposalResponse firstTransactionProposalResponse = failed.iterator().next();
fail("Not enough endorsers for invoke(move a,b,100):" + failed.size() + " endorser error: " +
firstTransactionProposalResponse.getMessage() +
". Was verified: " + firstTransactionProposalResponse.isVerified());
}
out("Successfully received transaction proposal responses.");
////////////////////////////
// Send Transaction Transaction to orderer
out("Sending chaincode transaction(move a,b,100) to orderer.");
TransactionEvent transactionEvent = channel.sendTransaction(successful).get(testConfig
.getTransactionWaitTime(), TimeUnit.SECONDS);
assertTrue(transactionEvent.isValid()); // must be valid to be here.
out("Finished transaction with transaction id %s", transactionEvent.getTransactionID());
} catch (Exception e) {
out("Caught an exception while invoking chaincode");
e.printStackTrace();
fail("Failed invoking chaincode with error : " + e.getMessage());
}
}
void query(HFClient client, Channel channel, ChaincodeID chaincodeID) {
try {
out("Now query chaincode for the value of b.");
QueryByChaincodeRequest queryByChaincodeRequest = client.newQueryProposalRequest();
queryByChaincodeRequest.setArgs(new String[]{"b"});
queryByChaincodeRequest.setFcn("query");
queryByChaincodeRequest.setChaincodeID(chaincodeID);
Map<String, byte[]> tm2 = new HashMap<>();
tm2.put("HyperLedgerFabric", "QueryByChaincodeRequest:JavaSDK".getBytes(UTF_8));
tm2.put("method", "QueryByChaincodeRequest".getBytes(UTF_8));
queryByChaincodeRequest.setTransientMap(tm2);
Collection<ProposalResponse> queryProposals = channel.queryByChaincode(queryByChaincodeRequest, channel
.getPeers());
for (ProposalResponse proposalResponse : queryProposals) {
if (!proposalResponse.isVerified() || proposalResponse.getStatus() != ProposalResponse.Status.SUCCESS) {
fail("Failed query proposal from peer " + proposalResponse.getPeer().getName() + " status: " +
proposalResponse.getStatus() +
". Messages: " + proposalResponse.getMessage()
+ ". Was verified : " + proposalResponse.isVerified());
} else {
String payload = proposalResponse.getProposalResponse().getResponse().getPayload().toStringUtf8();
out("Query payload of b from peer %s, Query Result: %s", proposalResponse.getPeer().getName(),
payload);
}
}
} catch (Exception e) {
out("Caught exception while running query");
e.printStackTrace();
fail("Failed during chaincode query with error : " + e.getMessage());
}
}
private Channel reconstructChannel(String name, HFClient client, SampleOrg sampleOrg) throws Exception {
client.setUserContext(sampleOrg.getPeerAdmin());
Channel newChannel = client.newChannel(name);
for (String orderName : sampleOrg.getOrdererNames()) {
newChannel.addOrderer(client.newOrderer(orderName, sampleOrg.getOrdererLocation(orderName),
testConfig.getOrdererProperties(orderName)));
}
for (String peerName : sampleOrg.getPeerNames()) {
String peerLocation = sampleOrg.getPeerLocation(peerName);
Peer peer = client.newPeer(peerName, peerLocation, testConfig.getPeerProperties(peerName));
//Query the actual peer for which channels it belongs to and check it belongs to this channel
Set<String> channels = client.queryChannels(peer);
if (!channels.contains(name)) {
throw new AssertionError(format("Peer %s does not appear to belong to channel %s", peerName, name));
}
newChannel.addPeer(peer);
// sampleOrg.addPeer(peer);
}
for (String eventHubName : sampleOrg.getEventHubNames()) {
EventHub eventHub = client.newEventHub(eventHubName, sampleOrg.getEventHubLocation(eventHubName),
testConfig.getEventHubProperties(eventHubName));
newChannel.addEventHub(eventHub);
}
newChannel.initialize();
// newChannel.setTransactionWaitTime(testConfig.getTransactionWaitTime());
// newChannel.setDeployWaitTime(testConfig.getDeployWaitTime());
return newChannel;
}
static void out(String format, Object... args) {
System.err.flush();
System.out.flush();
System.out.println(format(format, args));
System.err.flush();
System.out.flush();
}
}