Invoking the chaincode

Now that we have finished setting up our channel and installing chaincode for trade, we need to implement functions to execute chaincode invocations. Our code for this lies in the invokeChaincode function in invoke-chaincode.js.

The procedure to invoke the chaincode is the same as we did for instantiation, and the code is similar as well. The caller must build a transaction proposal consisting of the name of the chaincode function to be invoked and the arguments to be passed to it. Just providing the chaincode ID (tradecc in our implementation) is sufficient to identify the chaincode process to guide the request to:

tx_id = client.newTransactionID();
var request = {
chaincodeId : Constants.CHAINCODE_ID,
fcn: funcName,
args: argList,
txId: tx_id,
};
channel.sendTransactionProposal(request);

One difference with the instantiation proposal is that this operation does not typically require an administrative user in the organization; any ordinary member may suffice. This proposal must be sent to enough endorsing peers to collect the right set of signatures to satisfy our endorsement policy. This is done by adding all four peers in our network to the channel object (which must be created and initialized in the same way as in the previous stages). Once the proposal responses have been collected and validated in the same way as the instantiation proposals were, a transaction request must be built and sent to the orderer:

var request = {
proposalResponses: proposalResponses,
proposal: proposal
};
channel.sendTransaction(request);

We call invokeChaincode from our test script in createTradeApp.js. The chaincode function we would like to execute is requestTrade, which chronologically is the first function that ought to be invoked by a user in an importer's role (recall that we built access control logic within our chaincode to ensure that only a member of the Importer's organization may submit a requestTrade):

var invokeCC = require('./invoke-chaincode.js'),
invokeCC.invokeChaincode(Constants.IMPORTER_ORG, Constants.CHAINCODE_VERSION, 'requestTrade', ['2ks89j9', '50000','Wood for Toys', 'Importer']);

The last parameter ('Importer') simply indicates the ID of the user in the importer’s organization who is to submit this transaction request. In the code, the credentials for this user are loaded if the user has already enrolled with the CA, otherwise a new user with that ID is registered using the clientUtils.getUserMember function.

As in the instantiation case, a successful channel.sendTransaction call simply indicates that the orderer accepted the transaction. Only subscribing to an event will tell us whether the transaction was successfully committed to the ledger.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.86.60