Skip to content

Commit

Permalink
OriginTrail Gemini release v4.0.2
Browse files Browse the repository at this point in the history
OriginTrail Gemini release v4.0.2
  • Loading branch information
djordjekovac authored Jan 14, 2020
2 parents 11195e9 + 3dd4be3 commit 7903234
Show file tree
Hide file tree
Showing 26 changed files with 5,907 additions and 5,387 deletions.
59 changes: 59 additions & 0 deletions migrations/202010171600123-update-offers-add-replication-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.addColumn(
'offers',
'number_of_replications',
{
type: Sequelize.INTEGER,
},
);

await queryInterface.addColumn(
'offers',
'number_of_verified_replications',
{
type: Sequelize.INTEGER,
},
);

await queryInterface.addColumn(
'offers',
'trac_in_eth_used_for_price_calculation',
{
type: Sequelize.STRING,
},
);

await queryInterface.addColumn(
'offers',
'gas_price_used_for_price_calculation',
{
type: Sequelize.STRING,
},
);

await queryInterface.addColumn(
'offers',
'price_factor_used_for_price_calculation',
{
type: Sequelize.INTEGER,
},
);

return queryInterface.addColumn(
'offers',
'offer_finalize_transaction_hash',
{
type: Sequelize.STRING(128),
},
);
},
down: async (queryInterface) => {
await queryInterface.removeColumn('offers', 'number_of_replications');
await queryInterface.removeColumn('offers', 'number_of_verified_replications');
await queryInterface.removeColumn('offers', 'trac_in_eth_used_for_price_calculation');
await queryInterface.removeColumn('offers', 'gas_price_used_for_price_calculation');
await queryInterface.removeColumn('offers', 'price_factor_used_for_price_calculation');
return queryInterface.removeColumn('offers', 'offer_finalize_transaction_hash');
},
};
6 changes: 6 additions & 0 deletions models/offers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ module.exports = (sequelize, DataTypes) => {
global_status: DataTypes.STRING,
message: DataTypes.STRING,
transaction_hash: DataTypes.STRING(128),
number_of_replications: DataTypes.INTEGER,
number_of_verified_replications: DataTypes.INTEGER,
trac_in_eth_used_for_price_calculation: DataTypes.STRING,
gas_price_used_for_price_calculation: DataTypes.STRING,
price_factor_used_for_price_calculation: DataTypes.INTEGER,
offer_finalize_transaction_hash: DataTypes.STRING(128),
}, {});
offers.associate = (models) => {
// associations can be defined here
Expand Down
17 changes: 13 additions & 4 deletions modules/Blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,20 @@ class Blockchain {
* @param challengerIdentity - DC identity
* @param proofData - answer
* @param leafIndex - the number of the block in the lowest level of the merkle tree
* @param urgent - Whether max gas price should or not
* @return {Promise<void>}
*/
async completeLitigation(offerId, holderIdentity, challengerIdentity, proofData, leafIndex) {
async completeLitigation(
offerId,
holderIdentity,
challengerIdentity,
proofData,
leafIndex,
urgent,
) {
return this.blockchain.completeLitigation(
offerId, holderIdentity,
challengerIdentity, proofData, leafIndex,
challengerIdentity, proofData, leafIndex, urgent,
);
}

Expand All @@ -166,10 +174,11 @@ class Blockchain {
* @param offerId
* @param holderIdentity
* @param answer
* @param urgent - Whether maximum gas price should be used
* @return {Promise<any>}
*/
answerLitigation(offerId, holderIdentity, answer) {
return this.blockchain.answerLitigation(offerId, holderIdentity, answer);
answerLitigation(offerId, holderIdentity, answer, urgent) {
return this.blockchain.answerLitigation(offerId, holderIdentity, answer, urgent);
}

/**
Expand Down
17 changes: 13 additions & 4 deletions modules/Blockchain/Ethereum/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,11 @@ class Ethereum {
* @param offerId - Offer ID
* @param holderIdentity - DH identity
* @param answer - Litigation answer
* @param urgent - Whether maximum gas price should be used
* @return {Promise<any>}
*/
async answerLitigation(offerId, holderIdentity, answer) {
const gasPrice = await this.getGasPrice();
async answerLitigation(offerId, holderIdentity, answer, urgent) {
const gasPrice = await this.getGasPrice(urgent);
const options = {
gasLimit: this.web3.utils.toHex(this.config.gas_limit),
gasPrice: this.web3.utils.toHex(gasPrice),
Expand Down Expand Up @@ -1350,10 +1351,18 @@ class Ethereum {
* @param challengerIdentity - DC identity
* @param proofData - answer
* @param leafIndex - the number of the block in the lowest level of the merkle tree
* @param urgent - Whether max gas price should be used or not
* @return {Promise<void>}
*/
async completeLitigation(offerId, holderIdentity, challengerIdentity, proofData, leafIndex) {
const gasPrice = await this.getGasPrice();
async completeLitigation(
offerId,
holderIdentity,
challengerIdentity,
proofData,
leafIndex,
urgent,
) {
const gasPrice = await this.getGasPrice(urgent);
const options = {
gasLimit: this.web3.utils.toHex(this.config.gas_limit),
gasPrice: this.web3.utils.toHex(gasPrice),
Expand Down
32 changes: 30 additions & 2 deletions modules/command/command-executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class CommandExecutor {
}, transaction);

command.data = handler.unpack(command.data);
const result = await handler.execute(command, transaction);
let result = await handler.execute(command, transaction);
if (result.repeat) {
await CommandExecutor._update(command, {
status: STATUS.repeating,
Expand All @@ -148,6 +148,13 @@ class CommandExecutor {
return Command.repeat();
}

if (result.retry) {
result = await this._handleRetry(command, handler);
if (result.retry) {
return result;
}
}

const children = result.commands.map((c) => {
c.parent_id = command.id;
return c;
Expand All @@ -162,7 +169,7 @@ class CommandExecutor {
};
}, command.transactional);

if (!result.repeat) {
if (!result.repeat && !result.retry) {
if (this.verboseLoggingEnabled) {
this.logger.trace(`Command ${command.name} and ID ${command.id} processed.`);
}
Expand Down Expand Up @@ -239,6 +246,27 @@ class CommandExecutor {
}
}


/**
* Handles command retry
* @param command
* @param handler
* @return {Promise<void>}
* @private
*/
async _handleRetry(command, handler) {
if (command.retries > 0) {
command.data = handler.pack(command.data);
await CommandExecutor._update(command, {
status: STATUS.pending,
retries: command.retries - 1,
});
await this.add(command, command.delay ? command.delay : 0, false);
return Command.retry();
}
return Command.empty();
}

/**
* Handles command error
* @param command
Expand Down
11 changes: 11 additions & 0 deletions modules/command/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ class Command {
repeat: true,
};
}


/**
* Returns retry info
* @returns {{retry: boolean, commands: Array}}
*/
static retry() {
return {
retry: true,
};
}
}

module.exports = Command;
1 change: 1 addition & 0 deletions modules/command/dc/dc-litigation-complete-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class DCLitigationCompleteCommand extends Command {
dcIdentity,
answer,
challenge.test_index,
true,
);
return {
commands: [
Expand Down
7 changes: 5 additions & 2 deletions modules/command/dc/dc-offer-choose-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,18 @@ class DCOfferChooseCommand extends Command {
* @param err
*/
async recover(command, err) {
const { internalOfferId } = command.data;
const { internalOfferId, handler_id } = command.data;
const offer = await models.offers.findOne({ where: { id: internalOfferId } });
offer.status = 'FAILED';
offer.global_status = 'FAILED';
offer.message = err.message;
offer.message = `Failed to choose holders. Error message: ${err.message}`;
await offer.save({ fields: ['status', 'message', 'global_status'] });
this.remoteControl.offerUpdate({
id: internalOfferId,
});
models.handler_ids.update({
status: 'FAILED',
}, { where: { handler_id } });
await this.replicationService.cleanup(offer.id);
return Command.empty();
}
Expand Down
6 changes: 4 additions & 2 deletions modules/command/dc/dc-offer-create-bc-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class DCOfferCreateBcCommand extends Command {
* @param err
*/
async recover(command, err) {
const { internalOfferId } = command.data;
const { internalOfferId, handler_id } = command.data;
const offer = await Models.offers.findOne({ where: { id: internalOfferId } });
offer.status = 'FAILED';
offer.global_status = 'FAILED';
Expand All @@ -94,7 +94,9 @@ class DCOfferCreateBcCommand extends Command {
this.remoteControl.offerUpdate({
id: internalOfferId,
});

Models.handler_ids.update({
status: 'FAILED',
}, { where: { handler_id } });
await this.replicationService.cleanup(offer.id);
return Command.empty();
}
Expand Down
5 changes: 4 additions & 1 deletion modules/command/dc/dc-offer-create-db-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class DCOfferCreateDbCommand extends Command {
* @param err
*/
async recover(command, err) {
const { internalOfferId } = command.data;
const { internalOfferId, handler_id } = command.data;
const offer = await models.offers.findOne({ where: { id: internalOfferId } });
offer.status = 'FAILED';
offer.global_status = 'FAILED';
Expand All @@ -71,6 +71,9 @@ class DCOfferCreateDbCommand extends Command {
this.remoteControl.offerUpdate({
id: internalOfferId,
});
models.handler_ids.update({
status: 'FAILED',
}, { where: { handler_id } });
await this.replicationService.cleanup(offer.id);
return Command.empty();
}
Expand Down
12 changes: 9 additions & 3 deletions modules/command/dc/dc-offer-finalize-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ class DCOfferFinalizeCommand extends Command {
where: { handler_id },
},
);

let result;
try {
await this.blockchain.finalizeOffer(
result = await this.blockchain.finalizeOffer(
Utilities.normalizeHex(this.config.erc725Identity),
offerId,
new BN(solution.shift, 10),
Expand All @@ -90,7 +90,9 @@ class DCOfferFinalizeCommand extends Command {
}
throw error;
}

const offer = await Models.offers.findOne({ where: { offer_id: offerId } });
offer.offer_finalize_transaction_hash = result.transactionHash;
await offer.save({ fields: ['offer_finalize_transaction_hash'] });
return {
commands: [
{
Expand All @@ -112,6 +114,7 @@ class DCOfferFinalizeCommand extends Command {
const {
offerId,
solution,
handler_id,
} = command.data;

const offer = await Models.offers.findOne({ where: { offer_id: offerId } });
Expand Down Expand Up @@ -165,6 +168,9 @@ class DCOfferFinalizeCommand extends Command {
this.remoteControl.offerUpdate({
offer_id: offerId,
});
Models.handler_ids.update({
status: 'FAILED',
}, { where: { handler_id } });

return Command.empty();
}
Expand Down
40 changes: 36 additions & 4 deletions modules/command/dc/dc-offer-finalized-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,30 @@ class DcOfferFinalizedCommand extends Command {
where: { handler_id },
},
);

const replications = await Models.replicated_data.count({
where: {
offer_id: offerId,
status: {
[Op.in]: ['STARTED', 'VERIFIED'],
},
},
});
const verifiedReplications = await Models.replicated_data.count({
where: {
offer_id: offerId,
status: {
[Op.in]: ['VERIFIED'],
},
},
});

const offer = await Models.offers.findOne({ where: { offer_id: offerId } });
offer.status = 'FINALIZED';
offer.global_status = 'ACTIVE';
offer.number_of_replications = replications;
offer.number_of_verified_replications = verifiedReplications;
offer.message = 'Offer has been finalized. Offer is now active.';
await offer.save({ fields: ['status', 'message', 'global_status'] });
await offer.save({ fields: ['status', 'message', 'global_status', 'number_of_replications', 'number_of_verified_replications'] });
this.remoteControl.offerUpdate({
offer_id: offerId,
});
Expand Down Expand Up @@ -163,7 +180,20 @@ class DcOfferFinalizedCommand extends Command {
* @param command
*/
async expired(command) {
const { offerId } = command.data;
return this.invalidateOffer(command);
}

/**
* Recover system from failure
* @param command
* @param err
*/
async recover(command) {
return this.invalidateOffer(command);
}

async invalidateOffer(command) {
const { offerId, handler_id } = command.data;
this.logger.notify(`Offer ${offerId} has not been finalized.`);

const offer = await Models.offers.findOne({ where: { offer_id: offerId } });
Expand All @@ -174,7 +204,9 @@ class DcOfferFinalizedCommand extends Command {
this.remoteControl.offerUpdate({
offer_id: offerId,
});

Models.handler_ids.update({
status: 'FAILED',
}, { where: { handler_id } });
await this.replicationService.cleanup(offer.id);
return Command.empty();
}
Expand Down
Loading

0 comments on commit 7903234

Please sign in to comment.