Skip to content

Commit

Permalink
Merge pull request #506 from DemocracyEarth/search
Browse files Browse the repository at this point in the history
Fixes syncing issue on cache
  • Loading branch information
santisiri authored Feb 17, 2020
2 parents 55973bf + de223ca commit 06eb7f9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 85 deletions.
53 changes: 0 additions & 53 deletions imports/api/server/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,59 +336,6 @@ Meteor.methods({
return count;
},

/**
* @summary updates the period of the posts
* @param {Date} lastTimestamp with last sync signature
* @return {Number} total count.
*/
sync(lastTimestamp) {
check(lastTimestamp, Date);
const feed = Contracts.find({ $or: [{ period: 'COMPLETE' }, { period: 'VOTING' }, { period: 'GRACE' }, { period: 'QUEUE' }, { period: 'PROCESS' }] }).fetch();

log(`{ method: 'sync', feed.length: '${feed.length}', lastTimestamp: '${lastTimestamp}' }`);

let newPeriod;
let queueEnd;
for (let i = 0; i < feed.length; i += 1) {
newPeriod = feed[i].period;
switch (feed[i].period) {
case 'PROCESS':
case 'COMPLETE':
if (lastTimestamp >= feed[i].closing.graceCalendar && feed[i].processed) {
if (!feed[i].aborted) {
newPeriod = 'COMPLETE';
}
if (feed[i].didPass) {
newPeriod = 'PASSED';
} else if (feed[i].aborted) {
newPeriod = 'ABORTED';
}
}
break;
case 'GRACE':
if ((lastTimestamp > feed[i].closing.graceCalendar) && !feed[i].processed) {
newPeriod = 'PROCESS';
}
break;
case 'VOTING':
if (lastTimestamp > feed[i].closing.calendar && !feed[i].processed) {
newPeriod = 'GRACE';
}
break;
case 'QUEUE':
default:
queueEnd = parseInt(feed[i].timestamp.getTime() + feed[i].closing.periodDuration, 10);
if (lastTimestamp > queueEnd && !feed[i].processed) {
newPeriod = 'VOTING';
}
break;
}
if (newPeriod !== feed[i].period) {
Contracts.update({ _id: feed[i]._id }, { $set: { period: newPeriod } });
}
}
},

/**
* @summary get block timme
* @param {object} collective where to persist blocktime
Expand Down
23 changes: 0 additions & 23 deletions imports/startup/both/modules/metamask.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,28 +758,6 @@ const _getBlockHeight = async () => {
return height;
};

/**
* @summary get the last timestamp from the last block
*/
const _getLastTimestamp = async () => {
if (_web3()) {
if (!Session.get('blockTimes')) {
await sync();
}
const blockTimes = Session.get('blockTimes');
if (blockTimes && blockTimes.length > 0) {
const timestamp = _.pluck(_.where(blockTimes, { collectiveId: defaults.ROOT }), 'timestamp');
Meteor.call('sync', new Date(timestamp[0]), (error) => {
if (error) {
console.log(error);
}
});
return timestamp;
}
}
return undefined;
};

/**
* @summary does a web3 login without privacy mode;
*/
Expand Down Expand Up @@ -926,7 +904,6 @@ export const setupWeb3 = _web3;
export const syncBlockchain = _syncBlockchain;
export const hideLogin = _hideLogin;
export const getBlockHeight = _getBlockHeight;
export const getLastTimestamp = _getLastTimestamp;
export const verifyCoinVote = _verifyCoinVote;
export const submitVote = _submitVote;
export const hasRightToVote = _hasRightToVote;
1 change: 0 additions & 1 deletion imports/ui/templates/layout/sidebar/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ const _adapt = (list) => {
*/
const _showSidebar = () => {
const percentage = sidebarPercentage();
console.log(`showSidebar`);
if (!Meteor.Device.isPhone()) {
if ($(window).width() < gui.MOBILE_MAX_WIDTH) {
$('.navbar').css('left', 0);
Expand Down
2 changes: 0 additions & 2 deletions imports/ui/templates/widgets/feed/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { here } from '/lib/utils';
import { gui } from '/lib/const';
import { Contracts } from '/imports/api/contracts/Contracts';
import { toggleSidebar } from '/imports/ui/modules/menu';
import { getLastTimestamp } from '/imports/startup/both/modules/metamask.js';

import '/imports/ui/templates/widgets/feed/feed.html';
import '/imports/ui/templates/widgets/feed/feedItem.js';
Expand Down Expand Up @@ -248,7 +247,6 @@ Template.feed.onRendered(function () {
}
}
}
await getLastTimestamp();
});
});

Expand Down
58 changes: 57 additions & 1 deletion lib/dao.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Meteor } from 'meteor/meteor';
import { Collectives } from '/imports/api/collectives/Collectives';

import { getEvents, syncDAOGuilds, getBlockHeight } from '/lib/web3';
import { getEvents, syncDAOGuilds, getBlockHeight, getLastTimestamp } from '/lib/web3';
import { log, defaults } from '/lib/const';
import { Contracts } from '/imports/api/contracts/Contracts';
import { Tokens } from '/imports/api/tokens/tokens';
Expand Down Expand Up @@ -167,11 +167,66 @@ const _refreshDAOs = async () => {
}
};


/**
* @summary updates the period of the posts
* @return {Number} total count.
*/
const _updateProposalPeriods = async () => {
const feed = Contracts.find({ $or: [{ period: 'COMPLETE' }, { period: 'VOTING' }, { period: 'GRACE' }, { period: 'QUEUE' }, { period: 'PROCESS' }] }).fetch();

const lastTimestamp = await getLastTimestamp();
log(`[dao] Updating period for ${feed.length} proposals with timestamp ${lastTimestamp}...`);

let newPeriod;
let queueEnd;
for (let i = 0; i < feed.length; i += 1) {
newPeriod = feed[i].period;
switch (feed[i].period) {
case 'PROCESS':
case 'COMPLETE':
if (lastTimestamp >= feed[i].closing.graceCalendar && feed[i].processed) {
if (!feed[i].aborted) {
newPeriod = 'COMPLETE';
}
if (feed[i].didPass) {
newPeriod = 'PASSED';
} else if (feed[i].aborted) {
newPeriod = 'ABORTED';
}
}
break;
case 'GRACE':
if ((lastTimestamp > feed[i].closing.graceCalendar) && !feed[i].processed) {
newPeriod = 'PROCESS';
}
break;
case 'VOTING':
if (lastTimestamp > feed[i].closing.calendar && !feed[i].processed) {
newPeriod = 'GRACE';
}
break;
case 'QUEUE':
default:
queueEnd = parseInt(feed[i].timestamp.getTime() + feed[i].closing.periodDuration, 10);
if (lastTimestamp > queueEnd && !feed[i].processed) {
newPeriod = 'VOTING';
}
break;
}
if (newPeriod !== feed[i].period) {
Contracts.update({ _id: feed[i]._id }, { $set: { period: newPeriod } });
}
}
};

/**
* @summary batch refresh instructions.
*/
const _refresh = async () => {
await _refreshDAOs();
await _updateProposalPeriods();
await _computeDAOStats();
await syncDAOGuilds();
};

Expand Down Expand Up @@ -213,5 +268,6 @@ if (Meteor.isServer) {
}

export const computeDAOStats = _computeDAOStats;
export const updateProposalPeriods = _updateProposalPeriods;
export const refreshDAOs = _refreshDAOs;
export const refresh = _refresh;
6 changes: 1 addition & 5 deletions lib/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import { BigNumber } from 'bignumber.js';
import { migrateAddress, getContractObject, getTransactionObject, parseContent, getFinality } from '/lib/interpreter';

import { log, defaults } from '/lib/const';
import { computeDAOStats } from '/lib/dao';

import erc20 from 'human-standard-token-abi';
import { Math } from 'core-js';


const Web3 = require('web3');

Expand Down Expand Up @@ -950,7 +947,6 @@ const _getEvents = async (smartContract, collectiveId, fromBlock, toBlock) => {
log(`[web3] Log for ${smartContract.publicAddress} has a length of ${events.length} events.`);
const state = await _getState(smartContract);
await _writeEvents(events, smartContract, state, collectiveId);
computeDAOStats();
} else {
log('[web3] No new events found.');
}
Expand All @@ -974,7 +970,7 @@ const _getBlockHeight = async () => {
});
}
return false;
}
},
);
}
return height;
Expand Down

0 comments on commit 06eb7f9

Please sign in to comment.