Skip to content

Commit

Permalink
reworked EventQueue purging to better handle resyncing feathers to ex…
Browse files Browse the repository at this point in the history
…isting contract data
  • Loading branch information
ewingrj committed Oct 7, 2017
1 parent 6aeb1c5 commit 4967234
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
1 change: 0 additions & 1 deletion src/blockchain/EventQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class EventQueue {
return queued.splice(0, 1)[ 0 ]() // remove first function from list and run it
.then(() => {
console.log('returned from purge');
return this.purge(txHash);
});
}

Expand Down
62 changes: 30 additions & 32 deletions src/blockchain/Pledges.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { pledgePaymentStatus } from "./helpers";
import { pledgePaymentStatus } from './helpers';
import { hexToNumber } from 'web3-utils';

const BreakSignal = () => {
const ReProcessEvent = () => {
};

class Pledges {
Expand All @@ -21,25 +21,35 @@ class Pledges {
const { from, to, amount } = event.returnValues;
const txHash = event.transactionHash;

this._getBlockTimestamp(event.blockNumber)
.then(ts => {
const processEvent = () => {
if (from === '0') return this._newDonation(to, amount, ts, txHash)
.then(() => this.queue.purge(txHash));
const processEvent = (retry = false) => {
return this._getBlockTimestamp(event.blockNumber)
.then(ts => {
if (from === '0') return this._newDonation(to, amount, ts, txHash, retry)
.then(() => this.queue.purge(txHash))
.catch((err) => {
if (err instanceof ReProcessEvent) {
// this is really only useful when instant mining. Other then that, the donation should always be
// created before the tx was mined.
setTimeout(() => processEvent(true), 5000);
return;
}

console.error('_newDonation error ->', err);
});

return this._transfer(from, to, amount, ts, txHash)
.then(() => this.queue.purge(txHash));
};
});
};

if (hexToNumber(event.transactionLogIndex) > 0) {
this.queue.add(
event.transactionHash,
processEvent
);
} else {
return processEvent();
}
});
if (hexToNumber(event.transactionLogIndex) > 0) {
this.queue.add(
event.transactionHash,
processEvent
);
} else {
return processEvent();
}
}

_newDonation(pledgeId, amount, ts, txHash, retry = false) {
Expand Down Expand Up @@ -71,23 +81,11 @@ class Pledges {

// this is really only useful when instant mining. Other then that, the donation should always be
// created before the tx was mined.
setTimeout(() => this._newDonation(pledgeId, amount, ts, txHash, true), 5000);
throw new BreakSignal();
// setTimeout(() => this._newDonation(pledgeId, amount, ts, txHash, true), 5000);
throw new ReProcessEvent();
}

return donations.patch(donation._id, mutation);
})
.catch((err) => {
if (err instanceof BreakSignal) return;
if (err.name === 'NotFound') {
// most likely the from pledgeAdmin hasn't been registered yet.
// this can happen b/c when donating in liquidPledging, if the giverId === 0, the donate method will create a
// giver. Thus the tx will emit 3 events. AddGiver, and 2 x Transfer. Since these are processed asyncrounously
// calling pledgeAdmins.get(from) could result in a 404 as the AddGiver event hasn't finished processing
setTimeout(() => this._newDonation(pledgeId, amount, ts, txHash, true), 5000);
return;
}
console.error(err); // eslint-disable-line no-console
});

}
Expand Down Expand Up @@ -115,7 +113,7 @@ class Pledges {
if (filteredDonationsByAmount.length > 0) return filteredDonationsByAmount[ 0 ];

// this is probably a split which happened outside of the ui
throw new Error('unable to determine what donations entity to update', from, to, amount, ts, txHash);
throw new Error(`unable to determine what donations entity to update -> from: ${from}, to: ${to}, amount: ${amount}, ts: ${ts}, txHash: ${txHash}`);
});
};

Expand Down

0 comments on commit 4967234

Please sign in to comment.