fabric-common: How to use the endorsement, query, and commit handlers

This tutorial illustrates the use of the handlers by the Hyperledger Fabric Node.js Client as of 1.3.

For more information on:

The following assumes an understanding of the Hyperledger Fabric network (orderers and peers), and of Node application development, including the use of the Javascript promise and async await.

Overview

The fabric-common provides the ability for custom code that will handle the endorsement process and the submitting of endorsements to the orderer. There are three plug points defined, the Endorsement#send, the Query#send, and the Commit#send. The fabric-common will pass control to the handler to complete the processing. The custom code may decide to retry, try another end point, or use discovery to help complete the task.

Service Handler

fabric-common includes a handler called DiscoveryHandler. The included handler was designed to be used with the Hyperledger Fabric's discovery service. The discovery service will provide the peers and orderers required for endorsing and committing transactions automatically.

A custom service handler should extend the ServiceHandler and be passed to the send method of the service in the options object attribute handler.

The following shows how to get a discovery handler from the discover service and then use that handler on the endorsement call.

	// connect to the peer with discover service
	await discoverer.connect(peer1_endpoint);
	// use the endorsement to build the discovery request
	const endorsement = channel.newEndorsement(chaincode_name);
	discovery.build(idx, {endorsement: endorsement});
	discovery.sign(idx);
	// discovery results will be based on the chaincode of the endorsement
	const discovery_results = await discovery.send({targets: [discoverer], asLocalhost: true});
	testUtil.logMsg('\nDiscovery test 1 results :: ' + JSON.stringify(discovery_results));

	// input to the build a proposal request
	const build_proposal_request = {
		args: ['createCar', '2000', 'GMC', 'Savana', 'grey', 'Jones']
	};

	endorsement.build(idx, build_proposal_request);
	endorsement.sign(idx);

	const handler = discovery.newHandler();

	// do not specify 'targets', use a handler instead
	const  endorse_request = {
		handler: handler,
		requestTimeout: 30000
	};

	const endorse_results = await endorsement.send(endorse_request);

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.