diff --git a/backend/src/event-handlers/rollup/compute-author-weights.js b/backend/src/event-handlers/rollup/compute-author-weights.js index 1997f8d..c9d8b3c 100644 --- a/backend/src/event-handlers/rollup/compute-author-weights.js +++ b/backend/src/event-handlers/rollup/compute-author-weights.js @@ -3,17 +3,14 @@ const read = require('../../util/forum/read'); const { matrixPools } = require('../../util/db'); const computeAuthorWeights = async (batchItems_) => { - console.log('computeAuthorWeights starting'); const weights = {}; await Promise.each(batchItems_, async (postId) => { const post = await read(postId); const matrixPool = await matrixPools.get(postId); - console.log('batch item', { postId, post, matrixPool }); - console.log('post.authors', post.authors); const { fee, result: { votePasses, quorumMet } } = matrixPool; post.authors.forEach(({ authorAddress, weightPPM }) => { - weights[authorAddress] = weights[authorAddress] ?? 0; if (votePasses && quorumMet) { + weights[authorAddress] = weights[authorAddress] ?? 0; // scale by matrix pool outcome and strength weights[authorAddress] += weightPPM * fee; } @@ -21,18 +18,17 @@ const computeAuthorWeights = async (batchItems_) => { // TODO: Propagation via references }); }); - console.log('weights', weights); // Rescale author weights so they sum to 1000000 const sumOfWeights = Object.values(weights).reduce((t, v) => t + v, 0); + if (!sumOfWeights) { + return []; + } const scaledWeights = Object.values(weights) .map((weight) => Math.floor((weight * 1000000) / sumOfWeights)); const sumOfScaledWeights = scaledWeights.reduce((t, v) => t + v, 0); scaledWeights[0] += 1000000 - sumOfScaledWeights; const authors = Object.keys(weights) .map((authorAddress, i) => ({ authorAddress, weightPPM: scaledWeights[i] })); - console.log('computeAuthorWeights result', { - sumOfWeights, scaledWeights, sumOfScaledWeights, authors, - }); return authors; }; diff --git a/backend/src/event-handlers/rollup/fetch-batch-items-info.js b/backend/src/event-handlers/rollup/fetch-batch-items-info.js new file mode 100644 index 0000000..352c304 --- /dev/null +++ b/backend/src/event-handlers/rollup/fetch-batch-items-info.js @@ -0,0 +1,13 @@ +const { rollup } = require('../../util/contracts'); + +const fetchBatchItemsInfo = async () => { + // Read from Rollup.items + const itemCount = await rollup.itemCount(); + const promises = []; + for (let i = 0; i < itemCount; i += 1) { + promises.push(rollup.items(i)); + } + return Promise.all(promises); +}; + +module.exports = fetchBatchItemsInfo; diff --git a/backend/src/event-handlers/rollup/matrix-pools/initiate-matrix-pools.js b/backend/src/event-handlers/rollup/matrix-pools/initiate-matrix-pools.js index 8876813..068e1ad 100644 --- a/backend/src/event-handlers/rollup/matrix-pools/initiate-matrix-pools.js +++ b/backend/src/event-handlers/rollup/matrix-pools/initiate-matrix-pools.js @@ -1,19 +1,9 @@ const Promise = require('bluebird'); -const { rollup } = require('../../../util/contracts'); const { matrixPools } = require('../../../util/db'); const read = require('../../../util/forum/read'); const initiateMatrixPool = require('./initiate'); const { addBatchItem, getBatchItems } = require('../batch-items'); - -const fetchBatchItemsInfo = async () => { - // Read from Rollup.items - const itemCount = await rollup.itemCount(); - const promises = []; - for (let i = 0; i < itemCount; i += 1) { - promises.push(rollup.items(i)); - } - return Promise.all(promises); -}; +const fetchBatchItemsInfo = require('../fetch-batch-items-info'); const initiateMatrixPools = async () => { const batchItemsInfo = await fetchBatchItemsInfo(); diff --git a/backend/src/event-handlers/rollup/submit-rollup.js b/backend/src/event-handlers/rollup/submit-rollup.js index 4635d60..f704d59 100644 --- a/backend/src/event-handlers/rollup/submit-rollup.js +++ b/backend/src/event-handlers/rollup/submit-rollup.js @@ -8,13 +8,28 @@ const computeAuthorWeights = require('./compute-author-weights'); const { wallet, rollup } = require('../../util/contracts'); const { sendMatrixEvent } = require('../../matrix-bot'); const { stakeRollupAvailability } = require('./utils'); +const fetchBatchItemsInfo = require('./fetch-batch-items-info'); const submitRollup = async () => { - const batchItems = getBatchItems(); + const availableBatchItems = getBatchItems(); + const batchItems = []; + const batchItemsInfo = await fetchBatchItemsInfo(); + for (let i = 0; i < batchItemsInfo.length; i += 1) { + const { postId } = batchItemsInfo[i]; + if (availableBatchItems.includes(postId)) { + batchItems.push(postId); + } else { + // Batch items have to be submitted in the correct order, with no gaps + break; + } + } if (!batchItems.length) { return { batchItems: [] }; } const authors = await computeAuthorWeights(batchItems); + if (!authors.length) { + return { batchItems: [] }; + } // TODO: Compute citations as aggregate of the citations of posts in the batch const citations = []; const content = `Batch of ${batchItems.length} items`;