14
14
* limitations under the License.
15
15
*/
16
16
'use strict' ;
17
- var util = require ( 'util' ) ;
18
- var helper = require ( './helper.js' ) ;
19
- var logger = helper . getLogger ( 'instantiate-chaincode' ) ;
17
+ const util = require ( 'util' ) ;
18
+ const helper = require ( './helper.js' ) ;
19
+ const logger = helper . getLogger ( 'instantiate-chaincode' ) ;
20
20
21
- var instantiateChaincode = async function ( peers , channelName , chaincodeName , chaincodeVersion , functionName , chaincodeType , args , username , org_name ) {
21
+ const instantiateChaincode = async function ( peers , channelName , chaincodeName , chaincodeVersion , functionName , chaincodeType , args , username , org_name ) {
22
22
logger . debug ( '\n\n============ Instantiate chaincode on channel ' + channelName +
23
23
' ============\n' ) ;
24
- var error_message = null ;
25
-
24
+ let error_message = null ;
25
+ let client = null ;
26
+ let channel = null ;
26
27
try {
27
28
// first setup the client for this org
28
- var client = await helper . getClientForOrg ( org_name , username ) ;
29
+ client = await helper . getClientForOrg ( org_name , username ) ;
29
30
logger . debug ( 'Successfully got the fabric client for the organization "%s"' , org_name ) ;
30
- var channel = client . getChannel ( channelName ) ;
31
+ channel = client . getChannel ( channelName ) ;
31
32
if ( ! channel ) {
32
33
let message = util . format ( 'Channel %s was not defined in the connection profile' , channelName ) ;
33
34
logger . error ( message ) ;
34
35
throw new Error ( message ) ;
35
36
}
36
- var tx_id = client . newTransactionID ( true ) ; // Get an admin based transactionID
37
+ const tx_id = client . newTransactionID ( true ) ; // Get an admin based transactionID
37
38
// An admin based transactionID will
38
39
// indicate that admin identity should
39
40
// be used to sign the proposal request.
40
41
// will need the transaction ID string for the event registration later
41
- var deployId = tx_id . getTransactionID ( ) ;
42
+ const deployId = tx_id . getTransactionID ( ) ;
42
43
43
44
// send proposal to endorser
44
- var request = {
45
+ const request = {
45
46
targets : peers ,
46
47
chaincodeId : chaincodeName ,
47
48
chaincodeType : chaincodeType ,
@@ -70,23 +71,24 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
70
71
// the returned object has both the endorsement results
71
72
// and the actual proposal, the proposal will be needed
72
73
// later when we send a transaction to the orderer
73
- var proposalResponses = results [ 0 ] ;
74
- var proposal = results [ 1 ] ;
74
+ const proposalResponses = results [ 0 ] ;
75
+ const proposal = results [ 1 ] ;
75
76
76
- // lets have a look at the responses to see if they are
77
- // all good, if good they will also include signatures
78
- // required to be committed
79
- var all_good = true ;
80
- for ( var i in proposalResponses ) {
81
- let one_good = false ;
82
- if ( proposalResponses && proposalResponses [ i ] . response &&
83
- proposalResponses [ i ] . response . status === 200 ) {
84
- one_good = true ;
77
+ // look at the responses to see if they are all are good
78
+ // response will also include signatures required to be committed
79
+ let all_good = true ;
80
+ for ( const i in proposalResponses ) {
81
+ if ( proposalResponses [ i ] instanceof Error ) {
82
+ all_good = false ;
83
+ error_message = util . format ( 'instantiate proposal resulted in an error :: %s' , proposalResponses [ i ] . toString ( ) ) ;
84
+ logger . error ( error_message ) ;
85
+ } else if ( proposalResponses [ i ] . response && proposalResponses [ i ] . response . status === 200 ) {
85
86
logger . info ( 'instantiate proposal was good' ) ;
86
87
} else {
87
- logger . error ( 'instantiate proposal was bad' ) ;
88
+ all_good = false ;
89
+ error_message = util . format ( 'instantiate proposal was bad for an unknown reason %j' , proposalResponses [ i ] ) ;
90
+ logger . error ( error_message ) ;
88
91
}
89
- all_good = all_good & one_good ;
90
92
}
91
93
92
94
if ( all_good ) {
@@ -97,8 +99,8 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
97
99
98
100
// wait for the channel-based event hub to tell us that the
99
101
// instantiate transaction was committed on the peer
100
- var promises = [ ] ;
101
- let event_hubs = channel . getChannelEventHubsForOrg ( ) ;
102
+ const promises = [ ] ;
103
+ const event_hubs = channel . getChannelEventHubsForOrg ( ) ;
102
104
logger . debug ( 'found %s eventhubs for this organization %s' , event_hubs . length , org_name ) ;
103
105
event_hubs . forEach ( ( eh ) => {
104
106
let instantiateEventPromise = new Promise ( ( resolve , reject ) => {
@@ -138,22 +140,22 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
138
140
promises . push ( instantiateEventPromise ) ;
139
141
} ) ;
140
142
141
- var orderer_request = {
143
+ const orderer_request = {
142
144
txId : tx_id , // must include the transaction id so that the outbound
143
- // transaction to the orderer will be signed by the admin
144
- // id as was the proposal above, notice that transactionID
145
- // generated above was based on the admin id not the current
146
- // user assigned to the 'client' instance.
145
+ // transaction to the orderer will be signed by the admin id
146
+ // the same as the proposal above, notice that transactionID
147
+ // generated above was based on the admin id not the current
148
+ // user assigned to the 'client' instance.
147
149
proposalResponses : proposalResponses ,
148
150
proposal : proposal
149
151
} ;
150
- var sendPromise = channel . sendTransaction ( orderer_request ) ;
152
+ const sendPromise = channel . sendTransaction ( orderer_request ) ;
151
153
// put the send to the orderer last so that the events get registered and
152
154
// are ready for the orderering and committing
153
155
promises . push ( sendPromise ) ;
154
- let results = await Promise . all ( promises ) ;
156
+ const results = await Promise . all ( promises ) ;
155
157
logger . debug ( util . format ( '------->>> R E S P O N S E : %j' , results ) ) ;
156
- let response = results . pop ( ) ; // orderer results are last in the results
158
+ const response = results . pop ( ) ; // orderer results are last in the results
157
159
if ( response . status === 'SUCCESS' ) {
158
160
logger . info ( 'Successfully sent transaction to the orderer.' ) ;
159
161
} else {
@@ -162,9 +164,9 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
162
164
}
163
165
164
166
// now see what each of the event hubs reported
165
- for ( let i in results ) {
166
- let event_hub_result = results [ i ] ;
167
- let event_hub = event_hubs [ i ] ;
167
+ for ( const i in results ) {
168
+ const event_hub_result = results [ i ] ;
169
+ const event_hub = event_hubs [ i ] ;
168
170
logger . debug ( 'Event results for event hub :%s' , event_hub . getPeerAddr ( ) ) ;
169
171
if ( typeof event_hub_result === 'string' ) {
170
172
logger . debug ( event_hub_result ) ;
@@ -173,30 +175,31 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
173
175
logger . debug ( event_hub_result . toString ( ) ) ;
174
176
}
175
177
}
176
- } else {
177
- error_message = util . format ( 'Failed to send Proposal and receive all good ProposalResponse' ) ;
178
- logger . debug ( error_message ) ;
179
178
}
180
179
} catch ( error ) {
181
180
logger . error ( 'Failed to send instantiate due to error: ' + error . stack ? error . stack : error ) ;
182
181
error_message = error . toString ( ) ;
182
+ } finally {
183
+ if ( channel ) {
184
+ channel . close ( ) ;
185
+ }
183
186
}
184
187
185
- if ( ! error_message ) {
186
- let message = util . format (
187
- 'Successfully instantiate chaincode in organization %s to the channel \'%s\'' ,
188
- org_name , channelName ) ;
189
- logger . info ( message ) ;
190
- // build a response to send back to the REST caller
191
- let response = {
192
- success : true ,
193
- message : message
194
- } ;
195
- return response ;
196
- } else {
197
- let message = util . format ( 'Failed to instantiate. cause:%s' , error_message ) ;
188
+ let success = true ;
189
+ let message = util . format ( 'Successfully instantiate chaincode in organization %s to the channel \'%s\'' , org_name , channelName ) ;
190
+ if ( error_message ) {
191
+ message = util . format ( 'Failed to instantiate the chaincode. cause:%s' , error_message ) ;
192
+ success = false ;
198
193
logger . error ( message ) ;
199
- throw new Error ( message ) ;
194
+ } else {
195
+ logger . info ( message ) ;
200
196
}
197
+
198
+ // build a response to send back to the REST caller
199
+ const response = {
200
+ success : success ,
201
+ message : message
202
+ } ;
203
+ return response ;
201
204
} ;
202
205
exports . instantiateChaincode = instantiateChaincode ;
0 commit comments