Instantiation of chaincode

Now that the endorsing peers in the network have the chaincode, we must instantiate that chaincode across our channel to ensure that all copies of the ledger are initialized with the right dataset (or key-value pairs). This is the final step in the setup of our smart contract before we can open it up for regular operation. Instantiation is a transaction that invokes the LSCC to initialize a chaincode on a channel, thereby binding the two and isolating the former’s state to the latter.

This operation should be triggered centrally by any of the organizations authorized to initialize the chaincode (in our sample code, we use the administrator of the Importer's organization). Again, this follows the simple scenario (described in the installation section earlier) where the chaincode package is signed by a single organization administrator.

The default channel instantiation policy requires any channel MSP administrator to trigger the operation, but a different policy can be set in the Signed CDS structure if required.) In addition, the entity that triggers the instantiation operation must also be configured as a writer on the channel. Our procedure to create a channel configuration using configtxgen implicitly gave write permissions to administrators of the 4 organizations. (A detailed discussion of channel configuration policy is beyond the scope of this book.)

The main function to implement chaincode instantiation is implemented in instantiate-chaincode.js as instantiateOrUpgradeChaincode. This function can be used both to instantiate a newly deployed chaincode or update one that has already been running on the channel (see Chapter 9Life in a Blockchain Network) As in the previous stages, we must create client and channel handles, and associate the channel handle with the client. In addition, all the endorsing peers in the network must be added to the channel, and then the channel object must be initialized with the MSPs associated with the channel (from each of the four organizations):

channel.initialize();

This sets up the channel to verify certificates and signatures, for example, from endorsements received from the peers. Next, we build a proposal for instantiation and submit it to all of the endorsing peers on the channel (snippet from the buildChaincodeProposal function):

var tx_id = client.newTransactionID();
var request = {
chaincodePath: chaincode_path,
chaincodeId: Constants.CHAINCODE_ID,
chaincodeVersion: version,
fcn: funcName,
args: argList,
txId: tx_id,
'endorsement-policy': Constants.TRANSACTION_ENDORSEMENT_POLICY
};
channel.sendInstantiateProposal(request, 300000);

The path to the chaincode, and the ID and version, must match what was supplied in the installation proposal. In addition, we must supply the function name and argument list that will be sent to the chaincode and executed. (In our chaincode, this will execute the Init function.) Also note that the proposal contains the endorsement policy (Constants.TRANSACTION_ENDORSEMENT_POLICY) we set earlier, which requires a member from each of the four organizations to endorse a chaincode invocation. The proposal responses (one for each endorsing peer) returned by the orderer must be validated in the same way as in the installation stage. Using the result of the preceding channel.sendInstantiateProposal call, we must now build an instantiation transaction request and submit it to the orderer:

var proposalResponses = results[0];
var proposal = results[1];
var request = {
proposalResponses: proposalResponses,
proposal: proposal
};
channel.sendTransaction(request);

A successful response to channel.sendTransaction will allow our middleware to proceed on the basis that the instantiation was successfully submitted. This does not indicate, though, that the instantiation will successfully conclude with a commitment to the shared ledger; for that, our code will have to subscribe to events, and we will see how to do that later in this section.

Our script in createTradeApp.js triggers chaincode instantiation as follows:

var instantiateCC = require('./instantiate-chaincode.js'),
instantiateCC.instantiateOrUpgradeChaincode(
Constants.IMPORTER_ORG,
Constants.CHAINCODE_PATH,
Constants.CHAINCODE_VERSION,
'init',
['LumberInc', 'LumberBank', '100000', 'WoodenToys', 'ToyBank', '200000', 'UniversalFrieght', 'ForestryDepartment'],
false
);

The last parameter is set to false, indicating that an instantiation must be performed and not an upgrade. The first parameter (Constants.IMPORTER_ORG) indicates that the instantiation request must be submitted by a member (administrator in this context) of the importer’s organization.

If the instantiation was successful, the chaincode will be built in Docker containers, one corresponding to each endorsing peer, and deployed to receive requests on behalf of their peers. If you run docker ps -a, you should see something like this in addition to the ones created upon launching the network:

CONTAINER ID    IMAGE    COMMAND    CREATED    STATUS    PORTS    NAMES
b5fb71241f6d dev-peer0.regulatororg.trade.com-tradecc-v0-cbbb0581fb2b9f86d1fbd159e90f7448b256d2f7cc0e8ee68f90813b59d81bf5 "chaincode -peer.add..." About a minute ago Up About a minute dev-peer0.regulatororg.trade.com-tradecc-v0
077304fc60d8 dev-peer0.importerorg.trade.com-tradecc-v0-49020d3db2f1c0e3c00cf16d623eb1dddf7b649fee2e305c4d2c3eb5603a2a9f "chaincode -peer.add..." About a minute ago Up About a minute dev-peer0.importerorg.trade.com-tradecc-v0
8793002062d7 dev-peer0.carrierorg.trade.com-tradecc-v0-ec83c1904f90a76404e9218742a0fc3985f74e8961976c1898e0ea9a7a640ed2 "chaincode -peer.add..." About a minute ago Up About a minute dev-peer0.carrierorg.trade.com-tradecc-v0
9e5164bd8da1 dev-peer0.exporterorg.trade.com-tradecc-v0-dc2ed9ea732a90d6c5ffb0cd578dfb614e1ba14c2936b0ae785f30ea0f37da56 "chaincode -peer.add..." About a minute ago Up About a minute dev-peer0.exporterorg.trade.com-tradecc-v0
..................Content has been hidden....................

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