Skip to content

Commit

Permalink
FABN-1347 NodeSDK update low level (#59)
Browse files Browse the repository at this point in the history
Update fabric-common with changes to help with
the integration with the high level SDK

Signed-off-by: Bret Harrison <[email protected]>
  • Loading branch information
harrisob authored and andrew-coleman committed Dec 4, 2019
1 parent 804e680 commit ee4225f
Show file tree
Hide file tree
Showing 27 changed files with 839 additions and 231 deletions.
2 changes: 1 addition & 1 deletion build/tasks/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ gulp.task('test-fv-scenario', shell.task('npx nyc gulp run-test-fv-scenario'));

// run fv only with code coverage
// override the global nyc configuration
gulp.task('test-fv-only', shell.task('npx nyc --check-coverage --statements 53 --branches 42 --functions 53 --lines 53 gulp run-tape-e2e'));
gulp.task('test-fv-only', shell.task('npx nyc --check-coverage --statements 50 --branches 42 --functions 50 --lines 50 gulp run-tape-e2e'));

gulp.task('run-test-fv-scenario', (done) => {
const tasks = ['run-tape-e2e', 'docker-clean', 'run-test:cucumber', 'docker-clean', 'run-test:ts-cucumber'];
Expand Down
42 changes: 22 additions & 20 deletions fabric-common/lib/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,60 +82,62 @@ const Channel = class {

/**
* Gets an Endorsement instance for this channel.
* @param {string} chaincodeName
* @param {string} chaincodeId
*/
newEndorsement(chaincodeName = checkParameter('chaincodeName')) {
newEndorsement(chaincodeId = checkParameter('chaincodeId')) {
const method = `newEndorsement[${this.name}]`;
logger.debug(`${method} - start`);

return new Endorsement(chaincodeName, this);
return new Endorsement(chaincodeId, this);
}

/**
* Gets a Query instance for this channel.
* @param {string} chaincodeName
* @param {string} chaincodeId
*/
newQuery(chaincodeName = checkParameter('chaincodeName')) {
newQuery(chaincodeId = checkParameter('chaincodeId')) {
const method = `newQuery[${this.name}]`;
logger.debug(`${method} - start`);

return new Query(chaincodeName, this);
return new Query(chaincodeId, this);
}

/**
* Gets a Commit instance for this channel.
* @param {string} chaincodeName
* @param {string} chaincodeId
*/
newCommit(chaincodeName = checkParameter('chaincodeName')) {
newCommit(chaincodeId = checkParameter('chaincodeId')) {
const method = `newCommit[${this.name}]`;
logger.debug(`${method} - start`);

return new Commit(chaincodeName, this);
return new Commit(chaincodeId, this);
}

/**
* Returns a new {@link EventService} object on each call.
* Returns a new {@link EventService} instance
*
* @param {string} name - The name for this EventService
* @returns {EventService} The EventService instance
* @param {string} name - The name of this event service.
*/
newEventService(name = checkParameter('name')) {
const method = `newEventService[${this.name}]`;
logger.debug(`${method} - start`);
return new EventService(name, this);

const eventService = new EventService(name, this);

return eventService;
}

/**
* Returns a {@link DiscoveryService} instance with the given name.
* Will return a new instance.
* Returns a new {@link DiscoveryService} instance
*
* @param {string} name The name of this discovery instance.
* @returns {DiscoveryService} The discovery instance.
* @param {string} name - The name of this discovery service.
*/
newDiscoveryService(name = checkParameter('name')) {
const method = `newDiscovery[${this.name}]`;
logger.debug(`${method} - start - create new DiscoveryService name:${name} for channel:${this.name}`);
return new DiscoveryService(name, this);
const method = `newDiscoveryService[${this.name}]`;
logger.debug(`${method} - start`);
const discoveryService = new DiscoveryService(name, this);

return discoveryService;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion fabric-common/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ const Client = class {

/**
* Will return an array of {@link Endorser} instances that have been
* assigned to this channel instance. Include a MSPID to only return endorsers
* created by this client instance. Include a MSPID to only return endorsers
* in a specific organization.
*
* @param {string} [mspid] - Optional. The mspid of the endorsers to return
Expand Down
20 changes: 10 additions & 10 deletions fabric-common/lib/Commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class Commit extends Proposal {
/**
* Construct a Proposal object.
*
* @param {string} chaincodeName - The chaincode this proposal will execute
* @param {string} chaincodeId - The chaincode this proposal will execute
* @param {Channel} channel - The channel of this proposal
* @returns {Proposal} The Proposal instance.
*/
constructor(chaincodeName = checkParameter('chaincodeName'), channel = checkParameter('channel'), endorsement) {
super(chaincodeName, channel);
const method = `constructor[${chaincodeName}]`;
constructor(chaincodeId = checkParameter('chaincodeId'), channel = checkParameter('channel'), endorsement) {
super(chaincodeId, channel);
const method = `constructor[${chaincodeId}]`;
logger.debug('%s - start', method);
this.type = TYPE;
this._endorsement = endorsement;
Expand All @@ -53,7 +53,7 @@ class Commit extends Proposal {
* @returns {byte[]} The commits payload bytes to be signed.
*/
build(idContext = checkParameter('idContext'), request = {}) {
const method = `build[${this.chaincodeName}]`;
const method = `build[${this.chaincodeId}]`;
logger.debug('%s - start - %s', method, idContext.name);

if (request.endorsement) {
Expand Down Expand Up @@ -144,20 +144,20 @@ class Commit extends Proposal {
* @returns commit results
*/
async send(request = {}) {
const method = `send[${this.chaincodeName}]`;
const method = `send[${this.chaincodeId}]`;
logger.debug('%s - start', method);

const {handler, targets, requestTimeout} = request;

const envelope = this.getSignedEnvelope();

if (handler) {
logger.debug('%s - calling the handler');
logger.debug('%s - calling the handler', method);
const result = await handler.commit(envelope, request);

return result;
} else if (targets) {
logger.debug('%s - sending to the targets');
logger.debug('%s - sending to the targets', method);
const committers = this.channel.getTargetCommitters(targets);
let bad_result = {};
bad_result.status = 'UNKNOWN';
Expand All @@ -173,7 +173,7 @@ class Commit extends Proposal {

return bad_result;
} else {
throw Error('Missing targets parameter');
throw checkParameter('targets');
}
}

Expand All @@ -182,7 +182,7 @@ class Commit extends Proposal {
*/
toString() {

return `Commit: {chaincodeName: ${this.chaincodeName}, channel: ${this.channel.name}}`;
return `Commit: {chaincodeId: ${this.chaincodeId}, channel: ${this.channel.name}}`;
}
}

Expand Down
12 changes: 7 additions & 5 deletions fabric-common/lib/Discoverer.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,31 @@ class Discoverer extends ServiceEndpoint {
const send_timeout = setTimeout(() => {
clearTimeout(send_timeout);
logger.error(`${method} - timed out after:${rto}`);
return reject(new Error('REQUEST TIMEOUT'));
const return_error = new Error('REQUEST TIMEOUT');
this.getCharacteristics(return_error);
return reject(return_error);
}, rto);

this.service.discover(signedEnvelope, (err, response) => {
clearTimeout(send_timeout);
if (err) {
logger.debug(`${method} - Received discovery response from: ${this.endpoint.url} status: ${err}`);
if (err instanceof Error) {
err.peer = this.getCharacteristics();
this.getCharacteristics(err);
reject(err);
} else {
const return_error = new Error(err);
return_error.connection = this.getCharacteristics();
this.getCharacteristics(return_error);
reject(return_error);
}
} else {
if (response) {
logger.debug(`${method} - Received discovery response from peer "${this.endpoint.url}"`);
response.connection = this.getCharacteristics();
this.getCharacteristics(response);
resolve(response);
} else {
const return_error = new Error(`GRPC service failed to get a proper response from the peer ${this.endpoint.url}.`);
return_error.connection = this.getCharacteristics();
this.getCharacteristics(return_error);
logger.error(`${method} - rejecting with:${return_error}`);
reject(return_error);
}
Expand Down
18 changes: 11 additions & 7 deletions fabric-common/lib/DiscoveryService.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DiscoveryService extends ServiceAction {
this.discoveryResults = null;
this.asLocalhost = false;

this._current_target = null;
this.currentTarget = null;
this.targets = null; // will be used when targets are not provided

}
Expand All @@ -65,9 +65,13 @@ class DiscoveryService extends ServiceAction {
throw Error('targets parameter is not an array');
}

if (targets.length < 1) {
throw Error('No targets provided');
}

for (const discoverer of targets) {
if (discoverer.connected) {
logger.debug('%s - target is connected %s', method, discoverer.name);
if (discoverer.connected || discoverer.isConnectable()) {
logger.debug('%s - target is or could be connected %s', method, discoverer.name);
} else {
throw Error(`Discoverer ${discoverer.name} is not connected`);
}
Expand Down Expand Up @@ -286,7 +290,7 @@ class DiscoveryService extends ServiceAction {
logger.debug(`${method} - about to discover on ${target.endpoint.url}`);
try {
response = await target.sendDiscovery(signedEnvelope, this.requestTimeout);
this._current_target = target;
this.currentTarget = target;
break;
} catch (error) {
response = error;
Expand Down Expand Up @@ -610,7 +614,7 @@ class DiscoveryService extends ServiceAction {
const host_port = address.split(':');
const url = this._buildUrl(host_port[0], host_port[1]);
logger.debug(`${method} - create a new endorser ${url}`);
const peer = this.client.getEndorser(address, msp_id);
const peer = this.client.newEndorser(address, msp_id);
const end_point = this.client.newEndpoint(this._buildOptions(address, url, host_port[0], msp_id));
try {
logger.debug(`${method} - about to connect to endorser ${address} url:${url}`);
Expand Down Expand Up @@ -638,8 +642,8 @@ class DiscoveryService extends ServiceAction {
// discovery should also use TLS.
let protocol = null;
let isTLS = true;
if (this._current_target) {
isTLS = this._current_target.endpoint.isTLS();
if (this.currentTarget) {
isTLS = this.currentTarget.endpoint.isTLS();
}
protocol = isTLS ? 'grpcs' : 'grpc';
// but if not, use the following to override
Expand Down
12 changes: 6 additions & 6 deletions fabric-common/lib/Endorsement.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class Endorsement extends Proposal {
/**
* Construct a Proposal object.
*
* @param {string} chaincodeName - The chaincode this proposal will execute
* @param {string} chaincodeId - The chaincode this proposal will execute
* @param {Channel} channel - The channel of this proposal
* @returns {Proposal} The Proposal instance.
*/
constructor(chaincodeName = checkParameter('chaincodeName'), channel = checkParameter('channel')) {
super(chaincodeName, channel);
const method = `constructor[${chaincodeName}]`;
constructor(chaincodeId = checkParameter('chaincodeId'), channel = checkParameter('channel')) {
super(chaincodeId, channel);
const method = `constructor[${chaincodeId}]`;
logger.debug('%s - start', method);
this.type = TYPE;
}
Expand All @@ -43,15 +43,15 @@ class Endorsement extends Proposal {
const method = `newCommit[${this.name}]`;
logger.debug(`${method} - start`);

return new Commit(this.chaincodeName, this.channel, this);
return new Commit(this.chaincodeId, this.channel, this);
}

/**
* return a printable representation of this object
*/
toString() {

return `Endorsement: {chaincodeName: ${this.chaincodeName}, channel: ${this.channel.name}}`;
return `Endorsement: {chaincodeId: ${this.chaincodeId}, channel: ${this.channel.name}}`;
}
}

Expand Down
16 changes: 9 additions & 7 deletions fabric-common/lib/Endorser.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Endorser extends ServiceEndpoint {
*/
sendProposal(signedProposal, timeout) {
const method = `sendProposal[${this.name}]`;
logger.debug(`${method} - Start ----${this.name} ${this.endpoint.url}`);
logger.debug(`${method} - Start ----${this.name} ${this.endpoint.url} timeout:${timeout}`);

return new Promise((resolve, reject) => {
if (!signedProposal) {
Expand All @@ -77,36 +77,38 @@ class Endorser extends ServiceEndpoint {
const send_timeout = setTimeout(() => {
clearTimeout(send_timeout);
logger.error(`${method} - ${this.name} timed out after:${rto}`);
return reject(new Error('REQUEST TIMEOUT'));
const return_error = new Error('REQUEST TIMEOUT');
this.getCharacteristics(return_error);
return reject(return_error);
}, rto);

this.service.processProposal(signedProposal, (err, proposalResponse) => {
clearTimeout(send_timeout);
if (err) {
logger.error(`${method} - Received error response from: ${this.endpoint.url} error: ${err}`);
if (err instanceof Error) {
err.connection = this.getCharacteristics();
this.getCharacteristics(err);
reject(err);
} else {
const out_error = new Error(err);
out_error.connection = this.getCharacteristics();
this.getCharacteristics(out_error);
reject(out_error);
}
} else {
if (proposalResponse) {
logger.debug(`${method} - Received proposal response from peer "${this.endpoint.url}": status - ${proposalResponse.response && proposalResponse.response.status}`);
if (proposalResponse.response && proposalResponse.response.status) {
proposalResponse.connection = this.getCharacteristics();
this.getCharacteristics(proposalResponse);
resolve(proposalResponse);
} else {
const return_error = new Error(`GRPC service failed to get a proper response from the peer "${this.endpoint.url}".`);
return_error.connection = this.getCharacteristics();
this.getCharacteristics(return_error);
logger.error(`${method} - rejecting with:${return_error}`);
reject(return_error);
}
} else {
const return_error = new Error(`GRPC service got a null or undefined response from the peer "${this.endpoint.url}".`);
return_error.connection = this.getCharacteristics();
this.getCharacteristics(return_error);
logger.error(`${method} - rejecting with:${return_error}`);
reject(return_error);
}
Expand Down
11 changes: 10 additions & 1 deletion fabric-common/lib/EventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class EventListener {
/*
* Constructs a Event Listener
*
* @param {EventService} eventService - The EventService where this listener is registered
* @param {string} listenerType - a string to indicate the type of event registration
* "block", "tx", or "chaincode".
* @param {function} callback - Callback for event matches
Expand All @@ -43,7 +44,8 @@ class EventListener {
* @param {string} [chaincodeId] - optional - used to isolate chaincode events
* to a specific chaincode.
*/
constructor(listenerType = checkParameter('listenerType'), callback = checkParameter('callback'), options, event, chaincodeId) {
constructor(eventService = checkParameter('eventService'), listenerType = checkParameter('listenerType'), callback = checkParameter('callback'), options, event, chaincodeId) {
this.eventService = eventService;
this.type = TYPE;
this.listenerType = listenerType;
if (listenerType === EventListener.TX && !event) {
Expand Down Expand Up @@ -97,6 +99,13 @@ class EventListener {
}
}

/**
* Convenience method to for users to unregister this listener
*/
unregisterEventListener() {
this.eventService.unregisterEventListener(this);
}

toString() {
return `EventListener: { listenerType: ${this.listenerType}, startBlock: ${
this.startBlock}, endBlock: ${this.endBlock}, unregister: ${
Expand Down
Loading

0 comments on commit ee4225f

Please sign in to comment.