Joining a channel

Now that tradechannel has been created, our four peers, one in each organization, must be joined to the channel, a step that initializes the ledger on each node and prepares the peer to run chaincode and transactions on it. For this, we will need to reuse the client handle created in the previous step or instantiate one using a similar sequence of operations. In addition, we must instantiate a handle to the channel, register the orderer, and obtain the genesis block (implicitly sent to the orderer in the creation step using the channel configuration), as indicated by the following code snippets from the joinChannel function in join-channel.js:

var channel = client.newChannel(channel_name);
channel.addOrderer(
client.newOrderer(
ORGS.orderer.url,
{
'pem': caroots,
'ssl-target-name-override': ORGS.orderer['server-hostname']
}
)
);
tx_id = client.newTransactionID();
let request = { txId : tx_id };
return channel.getGenesisBlock(request);

The transaction ID argument is optional in the preceding getGenesisBlock call. Now, for each organization, we must obtain a handle to an administrator user who will then submit a channel joining request for the peer belonging to that organization:

return ClientUtils.getSubmitter(client, true /* get peer org admin */, org);
for (let key in ORGS[org])
if (ORGS[org].hasOwnProperty(key)) {
if (key.indexOf('peer') === 0) {
data = fs.readFileSync(path.join(__dirname, ORGS[org][key]['tls_cacerts']));
targets.push(
client.newPeer(
ORGS[org][key].requests,
{
pem: Buffer.from(data).toString(),
'ssl-target-name-override': ORGS[org][key]['server-hostname']
}
)
);
}
}
}
tx_id = client.newTransactionID();
let request = {
targets : targets,
block : genesis_block,
txId : tx_id
};
let sendPromise = channel.joinChannel(request, 40000);

As in the channel creation process, the getSubmitter function associates the signing identity of an administrator of a particular organization with the client object before submitting the channel join request. This request contains the genesis block as well as the configuration of every peer in that organization (loaded from the attributes containing the peer prefix within each organization in config.json, as you can see in the above code.)

A generous wait time of 40 seconds is indicated above as this process can take a while to complete. This join process needs to be executed independently by an administrator in each organization; hence, the function joinChannel(<org-name>) is called 4 times in sequence the main function processJoinChannel, which is called in our test script in createTradeApp.js as follows:

var joinChannel = require('./join-channel.js'),
joinChannel.processJoinChannel();
In a typical production network, each organization will independently run the join process, but only for its peers. The orchestration code (processJoinChannel in join-channel.js) that we use in our repository is meant for convenience and testing.
..................Content has been hidden....................

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