Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Commit 708af63

Browse files
committed
Add basic support for eventhub
Change-Id: I2cd64df933130c000bb89c8ee6a31943cc9b5209 Signed-off-by: Greg Haskins <gregory.haskins@gmail.com>
1 parent 62f24c2 commit 708af63

File tree

2 files changed

+114
-79
lines changed

2 files changed

+114
-79
lines changed

examples/example02/client/nodejs/client.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ var hfcutils = require('fabric-client/lib/utils.js');
1414
var utils = require('./lib/util.js');
1515
var Peer = require('fabric-client/lib/Peer.js');
1616
var Orderer = require('fabric-client/lib/Orderer.js');
17-
var CA = require('fabric-ca-client/lib/FabricCAClientImpl.js');
17+
var EventHub = require('fabric-client/lib/EventHub.js');
18+
var CA = require('fabric-ca-client');
1819
var User = require('fabric-client/lib/User.js');
1920

2021
var chain;
2122
var peer;
23+
var eventhub;
2224

2325
function createRequest(fcn, args) {
2426
var tx_id = hfcutils.buildTransactionID({length:12});
@@ -43,20 +45,31 @@ function connect() {
4345
var client = new hfc();
4446
chain = client.newChain('chaintool-demo');
4547

48+
eventhub = new EventHub();
49+
eventhub.setPeerAddr('grpc://localhost:7053');
50+
eventhub.connect();
51+
4652
peer = new Peer('grpc://localhost:7051');
4753
var orderer = new Orderer('grpc://localhost:7050');
4854

4955
chain.addOrderer(orderer);
5056
chain.addPeer(peer);
5157

5258
return utils.setStateStore(client, ".hfc-kvstore")
53-
.then(function() {
59+
.then(() => {
5460
var ca = new CA('http://localhost:7054');
5561

5662
return utils.getUser(client, ca, 'admin', 'adminpw');
5763
});
5864
}
5965

66+
function disconnect() {
67+
return new Promise((resolve, reject) => {
68+
eventhub.disconnect();
69+
resolve();
70+
});
71+
}
72+
6073
function deploy(args, path) {
6174

6275
var request = createRequest('init', new init.Init(args));
@@ -68,15 +81,19 @@ function deploy(args, path) {
6881

6982
// send proposal to endorser
7083
return chain.sendDeploymentProposal(request)
71-
.then(function(response) { utils.processProposalResponse(chain, response); })
72-
.then(utils.intradelay);
84+
.then((response) => {
85+
return utils.processResponse(chain, eventhub, request, response, 60000);
86+
});
7387
}
7488

7589
function sendTransaction(fcn, args) {
90+
7691
var request = createRequest(fcn, args);
92+
7793
return chain.sendTransactionProposal(request)
78-
.then(function(response) { utils.processProposalResponse(chain, response); })
79-
.then(utils.intradelay);
94+
.then((response) => {
95+
return utils.processResponse(chain, eventhub, request, response, 20000);
96+
});
8097
}
8198

8299
function sendQuery(fcn, args) {
@@ -92,7 +109,7 @@ function makePayment(args) {
92109
function checkBalance(args) {
93110
return sendQuery('org.hyperledger.chaincode.example02/fcn/3',
94111
new app.Entity(args))
95-
.then(function(results) {
112+
.then((results) => {
96113
return app.BalanceResult.decode(results[0]);
97114
});
98115
}
@@ -104,47 +121,54 @@ program
104121
.command('deploy')
105122
.description('deploy description')
106123
.option("-p, --path <path>", "Path to chaincode.car")
107-
.action(function(options){
124+
.action((options) => {
108125
return connect()
109-
.then(function() {
126+
.then(() => {
110127
return deploy({
111128
'partyA': {'entity':'A', 'value':100},
112129
'partyB': {'entity':'B', 'value':200}},
113130
options.path);
114131
})
115-
.catch(function(err) {
132+
.then(() => {
133+
return disconnect();
134+
})
135+
.catch((err) => {
116136
console.log("error:" + err);
117137
});
118138
});
119139

120140
program
121141
.command('makepayment <partySrc> <partyDst> <amount>')
122142
.description('makepayment description')
123-
.action(function(partySrc, partyDst, amount){
143+
.action((partySrc, partyDst, amount) => {
124144
return connect()
125-
.then(function() {
145+
.then(() => {
126146
return makePayment({
127147
'partySrc': partySrc,
128148
'partyDst': partyDst,
129149
'amount': parseInt(amount)});
130150
})
131-
.catch(function(err) {
151+
.then(() => {
152+
return disconnect();
153+
})
154+
.catch((err) => {
132155
console.log("error:" + err);
133156
});
134157
});
135158

136159
program
137160
.command('checkbalance <id>')
138161
.description('checkbalance description')
139-
.action(function(id){
162+
.action((id) => {
140163
return connect()
141-
.then(function() {
164+
.then(() => {
142165
return checkBalance({'id':id});
143166
})
144-
.then(function(result) {
167+
.then((result) => {
145168
console.log("balance:" + result.balance);
169+
return disconnect();
146170
})
147-
.catch(function(err) {
171+
.catch((err) => {
148172
console.log("error:" + err);
149173
});
150174
});

examples/example02/client/nodejs/lib/util.js

Lines changed: 73 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,90 @@ var hfc = require('fabric-client');
22
var utils = require('fabric-client/lib/utils.js');
33
var User = require('fabric-client/lib/User.js');
44

5+
function registerTxEvent(eventhub, txid, tmo) {
6+
return new Promise((resolve, reject) => {
7+
8+
var handle = setTimeout(() => {
9+
eventhub.unregisterTxEvent(txid);
10+
reject("Timeout!");
11+
}, tmo);
12+
13+
eventhub.registerTxEvent(txid, (tx) => {
14+
resolve();
15+
eventhub.unregisterTxEvent(txid);
16+
clearTimeout(handle);
17+
});
18+
});
19+
}
20+
21+
function sendTransaction(chain, results) {
22+
var proposalResponses = results[0];
23+
var proposal = results[1];
24+
var header = results[2];
25+
var all_good = true;
26+
27+
for(var i in proposalResponses) {
28+
let one_good = false;
29+
30+
if (proposalResponses &&
31+
proposalResponses[0].response &&
32+
proposalResponses[0].response.status === 200) {
33+
34+
one_good = true;
35+
}
36+
all_good = all_good & one_good;
37+
}
38+
39+
if (all_good) {
40+
var request = {
41+
proposalResponses: proposalResponses,
42+
proposal: proposal,
43+
header: header
44+
};
45+
return chain.sendTransaction(request);
46+
} else {
47+
return Promise.reject("bad result:" + results);
48+
}
49+
}
50+
551
module.exports = {
6-
setStateStore: function(client, path) {
7-
return new Promise(function(resolve, reject) {
52+
setStateStore: (client, path) => {
53+
return new Promise((resolve, reject) => {
854
return hfc.newDefaultKeyValueStore({path: path})
9-
.then(function(store) {
55+
.then((store) => {
1056
client.setStateStore(store);
1157
resolve(true);
1258
});
1359
});
1460
},
1561

16-
getUser: function(client, cop, username, password) {
62+
getUser: (client, cop, username, password) => {
1763
return client.getUserContext(username)
18-
.then(
19-
function(user) {
20-
if (user && user.isEnrolled()) {
21-
return Promise.resolve(user);
22-
} else {
23-
// need to enroll it with COP server
24-
console.log("enrolling");
25-
return cop.enroll({
26-
enrollmentID: username,
27-
enrollmentSecret: password
28-
}).then(
29-
function(enrollment) {
30-
console.log("enrollment");
31-
var member = new User(username, client);
32-
return member.setEnrollment(enrollment.key, enrollment.certificate)
33-
.then(function() {
34-
return client.setUserContext(member);
35-
});
36-
}
37-
);
38-
}
64+
.then((user) => {
65+
if (user && user.isEnrolled()) {
66+
return Promise.resolve(user);
67+
} else {
68+
// need to enroll it with COP server
69+
console.log("enrolling");
70+
return cop.enroll({
71+
enrollmentID: username,
72+
enrollmentSecret: password
73+
}).then((enrollment) => {
74+
console.log("enrollment");
75+
var member = new User(username, client);
76+
return member.setEnrollment(enrollment.key,
77+
enrollment.certificate)
78+
.then(() => {
79+
return client.setUserContext(member);
80+
});
81+
});
3982
}
40-
);
41-
},
42-
43-
intradelay: function() {
44-
return new Promise(function(resolve, reject) {
45-
setTimeout(resolve, 20000);
46-
});
83+
});
4784
},
4885

49-
processProposalResponse: function(chain, results) {
50-
var proposalResponses = results[0];
51-
//logger.debug('deploy proposalResponses:'+JSON.stringify(proposalResponses));
52-
var proposal = results[1];
53-
var header = results[2];
54-
var all_good = true;
55-
56-
for(var i in proposalResponses) {
57-
let one_good = false;
58-
59-
if (proposalResponses &&
60-
proposalResponses[0].response &&
61-
proposalResponses[0].response.status === 200) {
62-
63-
one_good = true;
64-
}
65-
all_good = all_good & one_good;
66-
}
67-
68-
if (all_good) {
69-
var request = {
70-
proposalResponses: proposalResponses,
71-
proposal: proposal,
72-
header: header
73-
};
74-
return chain.sendTransaction(request);
75-
} else {
76-
throw "bad result:" + results;
77-
}
78-
86+
processResponse: (chain, eventhub, request, response, tmo) => {
87+
return Promise.all(
88+
[registerTxEvent(eventhub, request.txId.toString(), tmo),
89+
sendTransaction(chain, response)]);
7990
}
8091
};

0 commit comments

Comments
 (0)