Now we are ready to carry out the upgrade process, which will require two steps:
- The installation of the new version of chaincode on the network peers
- The upgrade of the chaincode and endorsement policy on the channel
The code to perform these steps can be found in middleware/upgrade-chaincode.js and simply involves calling functions we have already implemented (see Chapter 5, Exposing Network Assets and Transactions). The following code snippet shows what we need to do for installation:
var Constants = require('./constants.js'); var installCC = require('./install-chaincode.js'); Constants.networkConfig = './config_upgrade.json'; Constants.TRANSACTION_ENDORSEMENT_POLICY = Constants.ALL_FIVE_ORG_MEMBERS; installCC.installChaincode(Constants.CHAINCODE_UPGRADE_PATH, Constants.CHAINCODE_UPGRADE_VERSION, Constants);
Note in the preceding code that the 5-organization network configuration is used and so is the 5-organization endorsement policy. The new path and version of the chaincode are set in constants.js as follows:
var CHAINCODE_UPGRADE_PATH = 'github.com/trade_workflow_v1'; var CHAINCODE_UPGRADE_VERSION = 'v1';
The path is relative to the chaincode/src folder in the repository, as the GOPATH is temporarily set to wherever the chaincode/ folder has been copied to (see constants.js and install-chaincode.js). The version is set to v1 as opposed to the initiation version, which was v0.
Triggering the upgrade is the next step, which is almost identical to the instantiation step from the developer's perspective:
var instantiateCC = require('./instantiate-chaincode.js'); instantiateCC.instantiateOrUpgradeChaincode( Constants.IMPORTER_ORG, Constants.CHAINCODE_UPGRADE_PATH, Constants.CHAINCODE_UPGRADE_VERSION, 'init', [], true, Constants );
As we can see preceding, we exercise the option of leaving the ledger state as it currently stands by passing an empty argument's list. In the function instantiateOrUpgradeChaincode in instantiate-chaincode.js, after a proposal is built, channel.sendUpgradeProposal(request, 300000) is called instead of channel.sendInstantiateProposal(request, 300000) to send the request to the orderer. As in the case of instantiation, we register event listeners to tell us whether the request succeeded.
To push the chaincode upgrade, run:
node upgrade-chaincode.js
To test the new chaincode, run:
node five-org-trade-scenario.js
This will run a sequence of trade operations (invocations and queries on the chaincode) involving the various parties from the request of a trade to the final payment for delivery of a shipment.