Skip to content

Commit

Permalink
Fix CI workloads and enhance error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Klenik <[email protected]>
  • Loading branch information
aklenik committed Apr 1, 2020
1 parent 2ddd4f1 commit e0bc58d
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 51 deletions.
29 changes: 20 additions & 9 deletions packages/caliper-core/lib/worker/client/message-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class MessageHandler {
if (error) {
// send(to, type, data)
context.messenger.send(['orchestrator'], type, {error: error.toString()});
logger.error(`Handled unsuccessful "init" message for worker ${context.workerId}, with error: ${error.toString()}`);
logger.error(`Handled unsuccessful "init" message for worker ${context.workerId}, with error: ${error.stack}`);
} else {
context.workerClient = new CaliperLocalClient(context.adapter, context.workerId, context.messenger);
context.messenger.send(['orchestrator'], type, {});
Expand Down Expand Up @@ -117,7 +117,7 @@ class MessageHandler {
if (error) {
// send(to, type, data)
context.messenger.send(['orchestrator'], type, {error: error.toString()});
logger.error(`Handled unsuccessful "prepare" message for worker ${context.workerId} and test round ${message.testRound} with error ${error.toString()}`);
logger.error(`Handled unsuccessful "prepare" message for worker ${context.workerId} and test round ${message.testRound} with error ${error.stack}`);
} else {
context.messenger.send(['orchestrator'], type, {});
logger.info(`Handled successful "prepare" message for worker ${context.workerId} and test round ${message.testRound}`);
Expand All @@ -139,12 +139,19 @@ class MessageHandler {
* Called after processing the "test" message.
* @param {object} context The context/state of the message handler.
* @param {object} message The message object.
* @param {object} error An error conditioning message
*/
static async afterTest(context, message) {
await CaliperUtils.sleep(200);
static async afterTest(context, message, error) {
const type = 'testResult';
context.messenger.send(['orchestrator'], type, context.testResult);
logger.info(`Handled "test" message for worker ${context.workerId}`);

if (error) {
// send(to, type, data)
context.messenger.send(['orchestrator'], type, {error: error.toString()});
logger.error(`Handled unsuccessful "test" message for worker ${context.workerId} and test round ${message.testRound} with error ${error.stack}`);
} else {
context.messenger.send(['orchestrator'], type, context.testResult);
logger.info(`Handled successful "test" message for worker ${context.workerId} and test round ${message.testRound}`);
}
}

/**
Expand Down Expand Up @@ -216,9 +223,13 @@ class MessageHandler {
break;
}
case 'test': {
await context.beforeTestHandler(context, message);
context.testResult = await context.testHandler(context, message);
await context.afterTestHandler(context, message);
try {
await context.beforeTestHandler(context, message);
context.testResult = await context.testHandler(context, message);
await context.afterTestHandler(context, message, undefined);
} catch (err) {
await context.afterTestHandler(context, message, err);
}

break;
}
Expand Down
80 changes: 49 additions & 31 deletions packages/caliper-ethereum/lib/ethereum.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,42 +201,60 @@ class Ethereum extends BlockchainInterface {
let status = new TxStatus();
let params = {from: context.fromAddress};
let contractInfo = context.contracts[contractID];
try {
context.engine.submitCallback(1);
let receipt = null;
let methodType = 'send';
if (methodCall.isView) {
methodType = 'call';
} else if (context.nonces && (typeof context.nonces[context.fromAddress] !== 'undefined')) {
let nonce = context.nonces[context.fromAddress];
context.nonces[context.fromAddress] = nonce + 1;
params.nonce = nonce;

context.engine.submitCallback(1);
let receipt = null;
let methodType = 'send';
if (methodCall.isView) {
methodType = 'call';
} else if (context.nonces && (typeof context.nonces[context.fromAddress] !== 'undefined')) {
let nonce = context.nonces[context.fromAddress];
context.nonces[context.fromAddress] = nonce + 1;
params.nonce = nonce;
}

const onFailure = (err) => {
status.SetStatusFail();
logger.error('Failed tx on ' + contractID + ' calling method ' + methodCall.verb + ' nonce ' + params.nonce);
logger.error(err);
};

const onSuccess = (rec) => {
status.SetID(rec.transactionHash);
status.SetResult(rec);
status.SetVerification(true);
status.SetStatusSuccess();
};

if (methodCall.args) {
if (contractInfo.gas && contractInfo.gas[methodCall.verb]) {
params.gas = contractInfo.gas[methodCall.verb];
} else if (contractInfo.estimateGas) {
params.gas = 1000 + await contractInfo.contract.methods[methodCall.verb](...methodCall.args).estimateGas();
}
if (methodCall.args) {
if (contractInfo.gas && contractInfo.gas[methodCall.verb]) {
params.gas = contractInfo.gas[methodCall.verb];
} else if (contractInfo.estimateGas) {
params.gas = 1000 + await contractInfo.contract.methods[methodCall.verb](...methodCall.args).estimateGas();
}

try {
receipt = await contractInfo.contract.methods[methodCall.verb](...methodCall.args)[methodType](params);
} else {
if (contractInfo.gas && contractInfo.gas[methodCall.verb]) {
params.gas = contractInfo.gas[methodCall.verb];
} else if (contractInfo.estimateGas) {
params.gas = 1000 + await contractInfo.contract.methods[methodCall.verb].estimateGas(params);
}
onSuccess(receipt);
} catch (err) {
onFailure(err);
}
} else {
if (contractInfo.gas && contractInfo.gas[methodCall.verb]) {
params.gas = contractInfo.gas[methodCall.verb];
} else if (contractInfo.estimateGas) {
params.gas = 1000 + await contractInfo.contract.methods[methodCall.verb].estimateGas(params);
}

try {
receipt = await contractInfo.contract.methods[methodCall.verb]()[methodType](params);
onSuccess(receipt);
} catch (err) {
onFailure(err);
}
status.SetID(receipt.transactionHash);
status.SetResult(receipt);
status.SetVerification(true);
status.SetStatusSuccess();
} catch (err) {
status.SetStatusFail();
logger.error('Failed tx on ' + contractID + ' calling method ' + methodCall.verb + ' nonce ' + params.nonce);
logger.error(err);
}
return Promise.resolve(status);

return status;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/caliper-sawtooth/lib/sawtooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class Sawtooth extends BlockchainInterface {
* @return {Promise} The return promise.
*/
getContext(name, args) {
let config = require(this.configPath);
let config = require(configPath);
let context = config.sawtooth.context;
if(typeof context === 'undefined') {
let validatorUrl = config.sawtooth.network.validator.url;
Expand Down
4 changes: 2 additions & 2 deletions packages/caliper-tests-integration/besu_tests/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ function generateWorkload() {
let acc_id = generateAccount();
account_array.push(acc_id);

if (bc.bcType === 'fabric') {
if (bc.getType() === 'fabric') {
workload.push({
chaincodeFunction: 'open',
chaincodeArguments: [acc_id, initMoney.toString()],
});
} else if (bc.bcType === 'ethereum') {
} else if (bc.getType() === 'ethereum') {
workload.push({
verb: 'open',
args: [acc_id, initMoney]
Expand Down
2 changes: 1 addition & 1 deletion packages/caliper-tests-integration/besu_tests/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports.init = function(blockchain, context, args) {
module.exports.run = function() {
const acc = account_array[Math.floor(Math.random()*(account_array.length))];

if (bc.bcType === 'fabric') {
if (bc.getType() === 'fabric') {
let args = {
chaincodeFunction: 'query',
chaincodeArguments: [acc],
Expand Down
4 changes: 2 additions & 2 deletions packages/caliper-tests-integration/besu_tests/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ module.exports.run = function () {
const account2 = account_array[Math.floor(Math.random() * (account_array.length))];
let args;

if (bc.bcType === 'fabric') {
if (bc.getType() === 'fabric') {
args = {
chaincodeFunction: 'transfer',
chaincodeArguments: [account1, account2, initmoney.toString()],
};
} else if (bc.bcType === 'ethereum') {
} else if (bc.getType() === 'ethereum') {
args = {
verb: 'transfer',
args: [account1, account2, initmoney]
Expand Down
4 changes: 2 additions & 2 deletions packages/caliper-tests-integration/ethereum_tests/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ function generateWorkload() {
let acc_id = generateAccount();
account_array.push(acc_id);

if (bc.bcType === 'fabric') {
if (bc.getType() === 'fabric') {
workload.push({
chaincodeFunction: 'open',
chaincodeArguments: [acc_id, initMoney.toString()],
});
} else if (bc.bcType === 'ethereum') {
} else if (bc.getType() === 'ethereum') {
workload.push({
verb: 'open',
args: [acc_id, initMoney]
Expand Down
2 changes: 1 addition & 1 deletion packages/caliper-tests-integration/ethereum_tests/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports.init = function(blockchain, context, args) {
module.exports.run = function() {
const acc = account_array[Math.floor(Math.random()*(account_array.length))];

if (bc.bcType === 'fabric') {
if (bc.getType() === 'fabric') {
let args = {
chaincodeFunction: 'query',
chaincodeArguments: [acc],
Expand Down
4 changes: 2 additions & 2 deletions packages/caliper-tests-integration/ethereum_tests/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ module.exports.run = function () {
const account2 = account_array[Math.floor(Math.random() * (account_array.length))];
let args;

if (bc.bcType === 'fabric') {
if (bc.getType() === 'fabric') {
args = {
chaincodeFunction: 'transfer',
chaincodeArguments: [account1, account2, initmoney.toString()],
};
} else if (bc.bcType === 'ethereum') {
} else if (bc.getType() === 'ethereum') {
args = {
verb: 'transfer',
args: [account1, account2, initmoney]
Expand Down

0 comments on commit e0bc58d

Please sign in to comment.