Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:OriginTrail/ot-node into docker
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Lelicanin committed Jul 20, 2018
2 parents 643fc84 + a288576 commit d5c0bcc
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 127 deletions.
4 changes: 2 additions & 2 deletions modules/Blockchain/Ethereum/Transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class Transactions {
const { transaction, future } = args;
try {
const delta = (Date.now() - this.lastTransactionTime);
if (delta < 5000) {
await sleep.sleep(5000);
if (delta < 2000) {
await sleep.sleep(2000);
}
const result = await this._sendTransaction(transaction);
if (result.status === '0x0') {
Expand Down
4 changes: 2 additions & 2 deletions modules/Blockchain/Ethereum/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Ethereum {

const importIdHash = Utilities.sha3(importId);

this.log.notify('Writing root hash to blockchain');
this.log.notify(`Writing root hash to blockchain for import ${importId}`);
return this.transactions.queueTransaction(this.otContractAbi, 'addFingerPrint', [importId, importIdHash, rootHash], options);
}

Expand Down Expand Up @@ -1040,7 +1040,7 @@ class Ethereum {
* Get replication modifier
*/
async getReplicationModifier() {
this.log.trace('Get replication modifier ... ');
this.log.trace('get replication modifier from blockchain');
return this.biddingContract.methods.replication_modifier().call({
from: this.config.wallet_address,
});
Expand Down
2 changes: 1 addition & 1 deletion modules/Challenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Challenger {
payload, challenge.dh_id,
async (error, response) => {
if (error) {
log.warn(`challenge-request: failed to get answer. Error: ${error}.`);
log.warn(`failed to get challenge answer from ${challenge.dh_id}. ${error}.`);
return;
}

Expand Down
92 changes: 46 additions & 46 deletions modules/DCService.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class DCService {
this.remoteControl.initializingOffer(importId);

const profileBalance =
new BN((await this.blockchain.getProfile(config.node_wallet)).balance, 10);
new BN((await this.blockchain.getProfile(config.node_wallet)).balance, 10);

const replicationModifier = await this.blockchain.getReplicationModifier();

Expand All @@ -165,7 +165,7 @@ class DCService {
dhWallets,
dhIds,
).then(async () => {
this.log.info('Offer written to blockchain. Started bidding phase.');
this.log.important(`Offer ${importId} written to blockchain. Started bidding phase.`);
this.remoteControl.biddingStarted(importId);
offer.status = 'STARTED';
await offer.save({ fields: ['status'] });
Expand All @@ -188,38 +188,41 @@ class DCService {
return;
}

this.log.trace('Started choosing phase.');
this.remoteControl.biddingComplete(importId);
this.remoteControl.choosingBids(importId);

offer.status = 'FINALIZING';
offer.save({ fields: ['status'] });
this.chooseBids(offer.id, totalEscrowTime).then(() => {
this.blockchain.subscribeToEvent('OfferFinalized', offer.import_id)
.then(() => {
const errorMsg = `Offer for import ${offer.import_id} finalized`;
offer.status = 'FINALIZED';
this.remoteControl.bidChosen(importId);
this.remoteControl.offerFinalized(`Offer for import ${offer.import_id} finalized`, importId);
offer.message = errorMsg;
offer.save({ fields: ['status', 'message'] });
this.log.info(errorMsg);
}).catch((error) => {
const errorMsg = `Failed to get offer for import ${offer.import_id}). ${error}.`;
offer.status = 'FAILED';
offer.message = errorMsg;
offer.save({ fields: ['status', 'message'] });
this.log.error(errorMsg);
this.remoteControl.dcErrorHandling(errorMsg);
});
}).catch((err) => {
const errorMsg = `Failed to choose bids. ${err}`;
offer.status = 'FAILED';
offer.message = errorMsg;
offer.save({ fields: ['status', 'message'] });
this.log.error(errorMsg);
this.remoteControl.dcErrorHandling(errorMsg);
});
setTimeout(() => {
this.log.trace('Started choosing phase.');
this.remoteControl.biddingComplete(importId);
this.remoteControl.choosingBids(importId);

offer.status = 'FINALIZING';
offer.save({ fields: ['status'] });

this.chooseBids(offer.id, totalEscrowTime).then(() => {
this.blockchain.subscribeToEvent('OfferFinalized', offer.import_id)
.then(() => {
const errorMsg = `Offer for import ${offer.import_id} finalized`;
offer.status = 'FINALIZED';
this.remoteControl.bidChosen(importId);
this.remoteControl.offerFinalized(`Offer for import ${offer.import_id} finalized`, importId);
offer.message = errorMsg;
offer.save({ fields: ['status', 'message'] });
this.log.info(errorMsg);
}).catch((error) => {
const errorMsg = `Failed to get offer for import ${offer.import_id}). ${error}.`;
offer.status = 'FAILED';
offer.message = errorMsg;
offer.save({ fields: ['status', 'message'] });
this.log.error(errorMsg);
this.remoteControl.dcErrorHandling(errorMsg);
});
}).catch((err) => {
const errorMsg = `Failed to choose bids. ${err}`;
offer.status = 'FAILED';
offer.message = errorMsg;
offer.save({ fields: ['status', 'message'] });
this.log.error(errorMsg);
this.remoteControl.dcErrorHandling(errorMsg);
});
}, 30000);
});
}).catch((err) => {
const errorMsg = `Failed to create offer. ${err}.`;
Expand Down Expand Up @@ -264,25 +267,19 @@ class DCService {
/**
* Chose DHs
* @param offerId Offer identifier
* @param totalEscrowTime Total escrow time
* @param totalEscrowTime Total escrow time
*/
chooseBids(offerId, totalEscrowTime) {
return new Promise((resolve, reject) => {
Models.offers.findOne({ where: { id: offerId } }).then((offerModel) => {
const offer = offerModel.get({ plain: true });
this.log.info(`Choose bids for offer ID ${offerId}, import ID ${offer.import_id}.`);
this.blockchain.increaseApproval(offer.max_token_amount * offer.replication_number)
this.blockchain.chooseBids(offer.import_id)
.then(() => {
this.blockchain.chooseBids(offer.import_id)
.then(() => {
this.log.info(`Bids chosen for offer ID ${offerId}, import ID ${offer.import_id}.`);
resolve();
}).catch((err) => {
this.log.warn(`Failed call choose bids for offer ID ${offerId}, import ID ${offer.import_id}. ${err}`);
reject(err);
});
this.log.info(`Bids chosen for offer ID ${offerId}, import ID ${offer.import_id}.`);
resolve();
}).catch((err) => {
this.log.warn(`Failed to increase allowance. ${JSON.stringify(err)}`);
this.log.warn(`Failed call choose bids for offer ID ${offerId}, import ID ${offer.import_id}. ${err}`);
reject(err);
});
}).catch((err) => {
Expand Down Expand Up @@ -370,7 +367,10 @@ class DCService {
importId,
kadWallet,
);
this.log.important('Data successfully verified, preparing to start challenges');
this.log.important(`Holding data for offer ${importId} and contact ${kadWallet} successfully verified. Challenges taking place...`);

replicatedData.status = 'ACTIVE';
await replicatedData.save({ fields: ['status'] });

await this.network.kademlia().sendVerifyImportResponse({
status: 'success',
Expand Down
29 changes: 17 additions & 12 deletions modules/DHService.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ class DHService {
predeterminedBid,
) {
try {
dcNodeId = dcNodeId.substring(2, 42);
const dcContact = await this.network.kademlia().getContact(dcNodeId, true);
if (dcContact == null || dcContact.hostname == null) {
// wait until peers are synced
return;
}

// Check if mine offer and if so ignore it.
const offerModel = await Models.offers.findOne({ where: { import_id: importId } });
if (offerModel) {
const offer = offerModel.get({ plain: true });
this.log.trace(`Mine offer (ID ${offer.data_hash}). Offer ignored`);
return;
}

dcNodeId = dcNodeId.substring(2, 42);
const dcContact = await this.network.kademlia().getContact(dcNodeId, true);
if (dcContact == null || dcContact.hostname == null) {
this.log.trace(`Unknown DC contact ${dcNodeId} for import ${importId}. Offer ignored.`);
return;
}
this.log.info(`New offer has been created by ${dcNodeId}. Offer ID ${importId}.`);

const distanceParams = await this.blockchain.getDistanceParameters(importId);

Expand All @@ -71,7 +71,7 @@ class DHService {
const k = distanceParams[4];
const numNodes = distanceParams[5];

if (this.amIClose(k, numNodes, dataHash, nodeHash, 20000)) {
if (this.amIClose(k, numNodes, dataHash, nodeHash, 10000)) {
this.log.notify('Close enough to take bid');
} else {
this.log.notify('Not close enough to take bid');
Expand Down Expand Up @@ -180,8 +180,13 @@ class DHService {
}

if (!predeterminedBid) {
await this.blockchain.addBid(importId, this.config.identity);
bidEvent = await this.blockchain.subscribeToEvent('AddedBid', importId);
try {
await this.blockchain.addBid(importId, this.config.identity);
bidEvent = await this.blockchain.subscribeToEvent('AddedBid', importId);
} catch (err) {
this.log.info('Bid not added, your bid was probably too late and the offer has been closed');
return;
}
} else {
const myBidIndex = await this.blockchain.getBidIndex(
importId,
Expand Down Expand Up @@ -249,7 +254,7 @@ class DHService {
},
bid.dc_id, (err) => {
if (err) {
this.log.warn(`Failed to send replication request ${err}`);
this.log.warn(`Failed to send replication request to ${bid.dc_id}. ${err}`);
// TODO Cancel bid here.
this.remoteControl.replicationReqestFailed(`Failed to send replication request ${err}`);
}
Expand Down
6 changes: 2 additions & 4 deletions modules/DataReplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,20 @@ class DataReplication {
* @return object response
*/
async sendPayload(data) {
this.log.info('Entering sendPayload');

const currentUnixTime = Date.now();
const options = {
dh_wallet: config.dh_wallet,
import_id: data.import_id,
amount: data.vertices.length + data.edges.length,
start_time: currentUnixTime,
total_time: 10 * 60000,
total_time: parseInt(config.total_escrow_time_in_milliseconds, 10), // TODO introduce BN
};

ImportUtilities.sort(data.vertices);

// TODO: Move test generation outside sendPayload(.
const tests = Challenge.generateTests(
data.contact, options.import_id.toString(), 10,
data.contact, options.import_id.toString(), 20,
options.start_time, options.start_time + options.total_time,
32, data.vertices,
);
Expand Down
Loading

0 comments on commit d5c0bcc

Please sign in to comment.