-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Implement BasicProverHandler - Add unit and e2e tests Change-Id: I14ede2d6fe426b292a5511bbdad5e516e3c4b082 Signed-off-by: Wenjian Qiao <[email protected]>
- Loading branch information
1 parent
81d32ad
commit 1dcaf55
Showing
7 changed files
with
299 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* Base class for prover handling | ||
* @class | ||
*/ | ||
class ProverHandler { | ||
|
||
/** | ||
* @typedef {Object} ProverHandlerParameters | ||
* @property {Object} request - {@link ChaincodeInvokeRequest} | ||
* @property {Object} signed_proposal - the encoded protobuf "SignedProposal" | ||
* created by the sendTransactionProposal method before calling the | ||
* handler. Will be the object to be proved by the target peers. | ||
* @property {Number} timeout - the timeout setting passed on sendTransactionProposal | ||
* method. | ||
*/ | ||
|
||
/** | ||
* This method will process the request object to calculate the target peers. | ||
* Once the targets have been determined, use the channel to send the token | ||
* transaction to the targets one at a time. After a target peer returns a response, | ||
* this method will skip the rest of targets and return the response to the caller. | ||
* | ||
* @param {ProverHandlerParameters} params - A {@link ProverHandlerParameters} | ||
* that contains enough information to determine the targets and contains | ||
* a {@link ChaincodeInvokeRequest} to be sent using the included channel | ||
* with the {@link Channel} 'sendTransactionProposal' method. | ||
* @returns {Promise} A Promise for the {@link ProposalResponseObject}, the | ||
* same results as calling the {@link Channel#sendTransactionProposal} | ||
* method directly. | ||
*/ | ||
processCommand(params) { | ||
if (params) { | ||
throw new Error('The "processCommand" method must be implemented'); | ||
} | ||
throw new Error('The "processCommand" method must be implemented'); | ||
} | ||
|
||
/** | ||
* This method will be called by the channel when the channel is initialized. | ||
*/ | ||
initialize() { | ||
throw new Error('The "initialize" method must be implemented'); | ||
} | ||
|
||
/** | ||
* This static method will be called by the channel to create an instance of | ||
* this handler. It will be passed the channel object this handler is working | ||
* with. | ||
*/ | ||
static create(channel) { | ||
if (channel) { | ||
throw new Error('The "create" method must be implemented'); | ||
} | ||
throw new Error('The "create" method must be implemented'); | ||
} | ||
} | ||
|
||
module.exports = ProverHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
Copyright 2019 IBM All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const token_utils = require('../token-utils'); | ||
const utils = require('../utils'); | ||
const Constants = require('../Constants.js'); | ||
const ProverHandler = require('../ProverHandler'); | ||
const logger = utils.getLogger('BasicProverHandler'); | ||
|
||
|
||
/** | ||
* This is an implementation of the [ProverHandler]{@link ProverHandler} API. | ||
* It will send a token request to a Prover peer one at a time from a provided | ||
* list or a list currently assigned to the channel. | ||
* | ||
* @class | ||
* @extends ProverHandler | ||
*/ | ||
class BasicProverHandler extends ProverHandler { | ||
|
||
/** | ||
* constructor | ||
* | ||
* @param {Channel} channel - The channel for this handler. | ||
*/ | ||
constructor(channel) { | ||
super(); | ||
this._channel = channel; | ||
} | ||
|
||
/** | ||
* Factory method to create an instance of a Proverter handler. | ||
* | ||
* @param {Channel} channel - the channel instance that this Prover | ||
* handler will be servicing. | ||
* @returns {BasicProverHandler} The instance of the handler | ||
*/ | ||
static create(channel) { | ||
return new BasicProverHandler(channel); | ||
} | ||
|
||
initialize() { | ||
logger.debug('initialize - start'); | ||
} | ||
|
||
async processCommand(params) { | ||
const method = 'processCommand'; | ||
logger.debug('%s - start', method); | ||
|
||
|
||
let errorMsg = null; | ||
if (!params) { | ||
errorMsg = 'Missing all required input parameters'; | ||
} else if (!params.request) { | ||
errorMsg = 'Missing "request" input parameter'; | ||
} else if (!params.signed_command) { | ||
errorMsg = 'Missing "signed_command" input parameter'; | ||
} | ||
|
||
if (!params.request.txId) { | ||
errorMsg = 'Missing "txId" parameter in the token request'; | ||
} | ||
|
||
if (errorMsg) { | ||
logger.error('Prover Handler error:' + errorMsg); | ||
throw new Error(errorMsg); | ||
} | ||
|
||
let timeout = utils.getConfigSetting('request-timeout'); | ||
if (params.timeout) { | ||
timeout = params.timeout; | ||
} | ||
|
||
// Prover peers are not integrated with discovery service yet, | ||
// so this handler uses request.targets or gets all prover peers if not defined. | ||
|
||
let targetPeers; | ||
|
||
if (!params.request.targets) { | ||
logger.debug('%s - running prover handler without provided targets', method); | ||
// find all prover peers added to this channel | ||
targetPeers = this._channel._getTargets(undefined, Constants.NetworkConfig.PROVER_PEER_ROLE); | ||
} else { | ||
logger.debug('%s - running prover handler with provided targets', method); | ||
// convert any names into peer objects | ||
targetPeers = this._channel._getTargets(params.request.targets, Constants.NetworkConfig.PROVER_PEER_ROLE); | ||
} | ||
|
||
// send to one of the peers; if failed, send to next peer | ||
const responses = await token_utils.sendTokenCommandToPeer(targetPeers, params.signed_command, timeout); | ||
return responses; | ||
|
||
} | ||
} | ||
|
||
module.exports = BasicProverHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const ProverHandler = require('../lib/ProverHandler'); | ||
|
||
const chai = require('chai'); | ||
chai.should(); | ||
|
||
describe('ProverHandler', () => { | ||
let providerHandler; | ||
|
||
beforeEach(() => { | ||
providerHandler = new ProverHandler(); | ||
}); | ||
|
||
describe('#processCommand', () => { | ||
|
||
it('should throw', () => { | ||
(() => { | ||
providerHandler.initialize(); | ||
}).should.throw('The "initialize" method must be implemented'); | ||
}); | ||
|
||
it('should throw when params are given', () => { | ||
(() => { | ||
providerHandler.processCommand('params'); | ||
}).should.throw('The "processCommand" method must be implemented'); | ||
}); | ||
|
||
it('should throw when params are not given', () => { | ||
(() => { | ||
providerHandler.processCommand(); | ||
}).should.throw('The "processCommand" method must be implemented'); | ||
}); | ||
}); | ||
|
||
describe('#initialize', () => { | ||
it('should throw', () => { | ||
(() => { | ||
providerHandler.initialize(); | ||
}).should.throw('The "initialize" method must be implemented'); | ||
}); | ||
}); | ||
|
||
describe('create', () => { | ||
it('should throw when params are given', () => { | ||
(() => { | ||
ProverHandler.create('channel'); | ||
}).should.throw('The "create" method must be implemented'); | ||
}); | ||
|
||
it('should throw when params are not given', () => { | ||
(() => { | ||
ProverHandler.create(); | ||
}).should.throw('The "create" method must be implemented'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters