diff --git a/backend/src/event-handlers/rollup.js b/backend/src/event-handlers/rollup.js index 71f971b..35e108d 100644 --- a/backend/src/event-handlers/rollup.js +++ b/backend/src/event-handlers/rollup.js @@ -24,19 +24,6 @@ const availabilityStakeDuration = ROLLUP_AVAILABILITY_STAKE_DURATION || 600; let batchWorker; let batchItems; -const resetBatch = async () => { - batchItems = []; - // Read from Rollup.items - const itemCount = await rollup.itemCount(); - const promises = []; - for (let i = 0; i < itemCount; i += 1) { - promises.push(rollup.items(i)); - } - batchItems = await Promise.all(promises); - await applicationData.put('batchItems', batchItems); - return batchItems; -}; - const stakeRollupAvailability = async () => { const currentRep = await dao.balanceOf(await wallet.getAddress()); if (currentRep) { @@ -186,20 +173,74 @@ const validatePost = async ({ const valid = await validateWorkEvidence(sender, post); const stake = { amount: currentRep, account: await wallet.getAddress(), inFavor: valid }; sendMatrixEvent('io.dgov.pool.stake', { postId, amount: currentRep, inFavor: valid }); - console.log('matrixPool', { + const matrixPool = { postId, roomId, eventId, ...params, stakes: [stake], - }); - await matrixPools.put(postId, { + }; + console.log('matrixPool', matrixPool); + await matrixPools.put(postId, matrixPool); +}; + +const initiateMatrixPool = async (postId, post, sender, fee) => { + const duration = 20; + const quorum = [1, 3]; + const winRatio = [1, 2]; + const params = { + sender, + fee: Number(fee), + duration, + quorum, + winRatio, + }; + + console.log('sending matrix pool start event'); + const { roomId, eventId } = await sendMatrixEvent('io.dgov.pool.start', { postId, - roomId, - eventId, ...params, - stakes: [stake], }); + console.log('sent matrix pool start event'); + // Register our own stake and send a message + await validatePost({ + sender, post, postId, roomId, eventId, ...params, + }); + + // Since we're assuming responsibility as the batch worker, + // set a timeout to evaulate the outcome + setTimeout(() => evaluateMatrixPoolOutcome(postId), duration * 1000); +}; + +const resetBatch = async () => { + batchItems = []; + // Read from Rollup.items + const itemCount = await rollup.itemCount(); + const promises = []; + for (let i = 0; i < itemCount; i += 1) { + promises.push(rollup.items(i)); + } + const batchItemsInfo = await Promise.all(promises); + batchItems = batchItemsInfo.map((x) => x.postId); + await applicationData.put('batchItems', batchItems); + + // Make sure there's a matrix pool for each batch item. + // If there's not, then let's start one. + await Promise.each(batchItemsInfo, async ({ postId, sender, fee }) => { + let post; + try { + post = await read(postId); + } catch (e) { + console.error(`Post ID ${postId} not found`); + return; + } + try { + await matrixPools.get(postId); + } catch (e) { + await initiateMatrixPool(postId, post, sender, fee); + } + }); + return batchItems; }; const start = async () => { @@ -254,17 +295,6 @@ const start = async () => { // If we are the batch worker or there is no batch worker, initiate a matrix pool if (batchWorker === await wallet.getAddress() || batchWorker === '0x0000000000000000000000000000000000000000') { - const duration = 20; - const quorum = [1, 3]; - const winRatio = [1, 2]; - const params = { - sender, - fee: Number(fee), - duration, - quorum, - winRatio, - }; - let post; try { post = await read(postId); @@ -280,23 +310,10 @@ const start = async () => { console.log(`Matrix pool start event has already been sent for postId ${postId}`); } catch (e) { if (e.status === 404) { - console.log('sending matrix pool start event'); - const { roomId, eventId } = await sendMatrixEvent('io.dgov.pool.start', { - postId, - ...params, - }); - console.log('sent matrix pool start event'); - // Register our own stake and send a message - await validatePost({ - sender, post, postId, roomId, eventId, ...params, - }); + await initiateMatrixPool(postId, post, sender, fee); } else { throw e; } - - // Since we're assuming responsibility as the batch worker, - // set a timeout to evaulate the outcome - setTimeout(() => evaluateMatrixPoolOutcome(postId), duration * 1000); } } });