Fixes for matrix pool batching
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 31s Details

This commit is contained in:
Ladd Hoffman 2024-05-03 20:43:58 -05:00
parent f9f8fc8f5f
commit 981dda97b1
16 changed files with 135 additions and 106 deletions

View File

@ -1,14 +1,14 @@
{
"localhost": {
"DAO": "0x1d63FDe5B461106729fE1e5e38A02fc68C518Af5",
"Work1": "0xB8f0cd092979F273b752FDa060F82BF2745f192e",
"Onboarding": "0x8F00038542C87A5eAf18d5938B7723bF2A04A4e4",
"Proposals": "0x6c18eb38b7450F8DaE5A5928A40fcA3952493Ee4",
"Rollup": "0x57BDFFf79108E5198dec6268A6BFFD8B62ECfA38",
"Work2": "0x42b79f8d8408c36aD4347ab72f826684440a7a8F",
"Reputation": "0x8d914D38dD301FC4606f5aa9fEcF8A76389020d3",
"Forum": "0x050C420Cc4995B41217Eba1B54B82Fd5687e9139",
"Bench": "0xfe58B9EB03F75A603de1B286584f5E9532ab8fB5"
"DAO": "0x3734B0944ea37694E85AEF60D5b256d19EDA04be",
"Work1": "0x8BDA04936887cF11263B87185E4D19e8158c6296",
"Onboarding": "0x8688E736D0D72161db4D25f68EF7d0EE4856ba19",
"Proposals": "0x3287061aDCeE36C1aae420a06E4a5EaE865Fe3ce",
"Rollup": "0x71cb20D63576a0Fa4F620a2E96C73F82848B09e1",
"Work2": "0x76Dfe9F47f06112a1b78960bf37d87CfbB6D6133",
"Reputation": "0xEAefe601Aad7422307B99be65bbE005aeA966012",
"Forum": "0x79e365342329560e8420d7a0f016633d7640cB18",
"Bench": "0xC0f00E5915F9abE6476858fD1961EAf79395ea64"
},
"sepolia": {
"DAO": "0x02dC6871d89fA6a16be2e875C63d81761CF459A8",

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -5,8 +5,9 @@ const {
appState,
proposalEventIds,
} = require('../util/db');
const { submitRollup } = require('./rollup');
const submitRollup = require('./rollup/submit-rollup');
const { resetBatchItems } = require('./rollup/batch-items');
const { initiateMatrixPools } = require('./rollup/matrix-pools/initiate-matrix-pools');
const {
BOT_INSTANCE_ID,
@ -71,6 +72,7 @@ const handleCommand = async (client, roomId, event) => {
if (instanceId === BOT_INSTANCE_ID) {
console.log('!resetBatch');
const batchItems = await resetBatchItems();
await initiateMatrixPools();
await client.replyText(roomId, event, `Reset batch, now contains ${batchItems.length} items`);
}
}

View File

@ -1,7 +1,4 @@
const { rollup } = require('../../util/contracts');
const { applicationData, matrixPools } = require('../../util/db');
const read = require('../../util/forum/read');
const { initiateMatrixPool } = require('./matrix-pools/initiate');
const { applicationData } = require('../../util/db');
let batchItems;
@ -16,8 +13,10 @@ const initializeBatchItems = async () => {
const getBatchItems = () => batchItems;
const addBatchItem = async (postId) => {
batchItems.push(postId);
await applicationData.put('batchItems', batchItems);
if (!batchItems.includes(postId)) {
batchItems.push(postId);
await applicationData.put('batchItems', batchItems);
}
};
const clearBatchItems = async (itemsToClear) => {
@ -27,32 +26,7 @@ const clearBatchItems = async (itemsToClear) => {
const resetBatchItems = 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;
};

View File

@ -1,3 +1,4 @@
const Promise = require('bluebird');
const read = require('../../util/forum/read');
const { matrixPools } = require('../../util/db');

View File

@ -1,7 +1,7 @@
const { isEqual } = require('lodash');
const { registerDecider } = require('../validation-pools');
const { registerMatrixEventHandler, sendMatrixText } = require('../../matrix-bot');
const { registerMatrixEventHandler, sendMatrixText, sendMatrixEvent } = require('../../matrix-bot');
const { matrixPools, matrixUserToAuthorAddress, applicationData } = require('../../util/db');
const {
rollup, wallet,
@ -9,12 +9,14 @@ const {
const read = require('../../util/forum/read');
const { availabilityStakeDuration } = require('./config');
const {
stakeRollupAvailability, getBatchPostAuthorWeights, authorsMatch, validatePost,
stakeRollupAvailability, authorsMatch, validatePost,
} = require('./utils');
const computeMatrixPoolResult = require('./compute-matrix-pool-result');
const computeMatrixPoolResult = require('./matrix-pools/compute-result');
const { initializeBatchItems } = require('./batch-items');
const { getCurrentBatchWorker, initializeBatchWorker } = require('./batch-worker');
const { initiateMatrixPool } = require('./matrix-pools/initiate');
const initiateMatrixPool = require('./matrix-pools/initiate');
const { initiateMatrixPools } = require('./matrix-pools/initiate-matrix-pools');
const computeAuthorWeights = require('./compute-author-weights');
let batchItems;
@ -32,7 +34,7 @@ const start = async () => {
// Our task here is to check whether the posted result agrees with our own computations
let expectedAuthors;
try {
expectedAuthors = await getBatchPostAuthorWeights(post.embeddedData.batchItems);
expectedAuthors = await computeAuthorWeights(post.embeddedData.batchItems);
} catch (e) {
console.error('Error calculating batch post author weights', e);
return null;
@ -52,6 +54,9 @@ const start = async () => {
await stakeRollupAvailability();
setInterval(stakeRollupAvailability, availabilityStakeDuration * 1000);
// Initiate any matrix pools that haven't already occurred
await initiateMatrixPools();
/// `sender` is the address that called Rollup.addItem on chain, i.e. the Work2 contract.
rollup.on('BatchItemAdded', async (postId, sender, fee) => {
// If we are the batch worker or there is no batch worker, initiate a matrix pool
@ -98,9 +103,17 @@ const start = async () => {
console.error(`Post ID ${postId} not found`);
break;
}
await validatePost({
sender, post, postId, roomId, eventId, ...params,
});
// Register our own stake and send a message
const { amount, inFavor } = await validatePost(sender, post);
sendMatrixEvent('io.dgov.pool.stake', { postId, amount, inFavor });
const matrixPool = {
postId,
roomId,
eventId,
...params,
stakes: [{ amount, inFavor, account: await wallet.getAddress() }],
};
await matrixPools.put(postId, matrixPool);
break;
}
case 'io.dgov.pool.stake': {
@ -165,7 +178,7 @@ const start = async () => {
batchPostIds.push(batchPostId);
await applicationData.put('batchPostIds', batchPostIds);
// Compare batch worker's result with ours to verify
const expectedAuthors = await getBatchPostAuthorWeights(batchItems_);
const expectedAuthors = await computeAuthorWeights(batchItems_);
if (!authorsMatch(authors, expectedAuthors)) {
sendMatrixText(`Unexpected result for batch post ${batchPostId}`);
}

View File

@ -1,6 +1,6 @@
const {
dao,
} = require('../../util/contracts');
} = require('../../../util/contracts');
const computeMatrixPoolResult = async (matrixPool) => {
// This should already contain all the info we need to evaluate the outcome

View File

@ -3,7 +3,7 @@ const { wallet } = require('../../../util/contracts');
const { matrixPools } = require('../../../util/db');
const { addBatchItem, getBatchItems } = require('../batch-items');
const { getCurrentBatchWorker } = require('../batch-worker');
const computeMatrixPoolResult = require('../compute-matrix-pool-result');
const computeMatrixPoolResult = require('./compute-result');
const { rollupBatchSize } = require('../config');
const submitRollup = require('../submit-rollup');
const { stakeRollupAvailability } = require('../utils');

View File

@ -0,0 +1,45 @@
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 initiateMatrixPools = async () => {
const batchItemsInfo = await fetchBatchItemsInfo();
// 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 {
const matrixPool = await matrixPools.get(postId);
if (matrixPool.result) {
await addBatchItem(postId);
}
} catch (e) {
// TODO: It's possible we missed messages about pools that have already occurred.
await initiateMatrixPool(postId, post, sender, fee);
}
});
console.log('batch items count:', getBatchItems().length);
};
module.exports = {
initiateMatrixPools,
};

View File

@ -1,6 +1,8 @@
const { sendMatrixEvent } = require('../../../matrix-bot');
const { validatePost } = require('../utils');
const evaluateMatrixPoolOutcome = require('./evaluate');
const { matrixPools } = require('../../../util/db');
const { wallet } = require('../../../util/contracts');
const initiateMatrixPool = async (postId, post, sender, fee) => {
const duration = 20;
@ -21,9 +23,16 @@ const initiateMatrixPool = async (postId, post, sender, fee) => {
});
console.log('sent matrix pool start event');
// Register our own stake and send a message
await validatePost({
sender, post, postId, roomId, eventId, ...params,
});
const { amount, inFavor } = await validatePost(sender, post);
sendMatrixEvent('io.dgov.pool.stake', { postId, amount, inFavor });
const matrixPool = {
postId,
roomId,
eventId,
...params,
stakes: [{ amount, inFavor, account: await wallet.getAddress() }],
};
await matrixPools.put(postId, matrixPool);
// Since we're assuming responsibility as the batch worker,
// set a timeout to evaulate the outcome
@ -35,6 +44,4 @@ const initiateMatrixPool = async (postId, post, sender, fee) => {
);
};
module.exports = {
initiateMatrixPool,
};
module.exports = initiateMatrixPool;

View File

@ -1,10 +1,8 @@
const { sendMatrixEvent } = require('../../matrix-bot');
const callWithRetry = require('../../util/call-with-retry');
const {
rollup, wallet, dao,
work2,
} = require('../../util/contracts');
const { matrixPools } = require('../../util/db');
const { availabilityStakeDuration } = require('./config');
const stakeRollupAvailability = async () => {
@ -36,22 +34,11 @@ const validateWorkEvidence = async (sender, post) => {
return valid;
};
const validatePost = async ({
sender, post, postId, roomId, eventId, ...params
}) => {
const validatePost = async (sender, post) => {
const currentRep = Number(await dao.balanceOf(await wallet.getAddress()));
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 });
const matrixPool = {
postId,
roomId,
eventId,
...params,
stakes: [stake],
};
console.log('matrixPool', matrixPool);
await matrixPools.put(postId, matrixPool);
const stake = { amount: currentRep, inFavor: valid };
return stake;
};
module.exports = {

View File

@ -1,14 +1,14 @@
{
"localhost": {
"DAO": "0x1d63FDe5B461106729fE1e5e38A02fc68C518Af5",
"Work1": "0xB8f0cd092979F273b752FDa060F82BF2745f192e",
"Onboarding": "0x8F00038542C87A5eAf18d5938B7723bF2A04A4e4",
"Proposals": "0x6c18eb38b7450F8DaE5A5928A40fcA3952493Ee4",
"Rollup": "0x57BDFFf79108E5198dec6268A6BFFD8B62ECfA38",
"Work2": "0x42b79f8d8408c36aD4347ab72f826684440a7a8F",
"Reputation": "0x8d914D38dD301FC4606f5aa9fEcF8A76389020d3",
"Forum": "0x050C420Cc4995B41217Eba1B54B82Fd5687e9139",
"Bench": "0xfe58B9EB03F75A603de1B286584f5E9532ab8fB5"
"DAO": "0x3734B0944ea37694E85AEF60D5b256d19EDA04be",
"Work1": "0x8BDA04936887cF11263B87185E4D19e8158c6296",
"Onboarding": "0x8688E736D0D72161db4D25f68EF7d0EE4856ba19",
"Proposals": "0x3287061aDCeE36C1aae420a06E4a5EaE865Fe3ce",
"Rollup": "0x71cb20D63576a0Fa4F620a2E96C73F82848B09e1",
"Work2": "0x76Dfe9F47f06112a1b78960bf37d87CfbB6D6133",
"Reputation": "0xEAefe601Aad7422307B99be65bbE005aeA966012",
"Forum": "0x79e365342329560e8420d7a0f016633d7640cB18",
"Bench": "0xC0f00E5915F9abE6476858fD1961EAf79395ea64"
},
"sepolia": {
"DAO": "0x02dC6871d89fA6a16be2e875C63d81761CF459A8",

View File

@ -1,14 +1,14 @@
{
"localhost": {
"DAO": "0x1d63FDe5B461106729fE1e5e38A02fc68C518Af5",
"Work1": "0xB8f0cd092979F273b752FDa060F82BF2745f192e",
"Onboarding": "0x8F00038542C87A5eAf18d5938B7723bF2A04A4e4",
"Proposals": "0x6c18eb38b7450F8DaE5A5928A40fcA3952493Ee4",
"Rollup": "0x57BDFFf79108E5198dec6268A6BFFD8B62ECfA38",
"Work2": "0x42b79f8d8408c36aD4347ab72f826684440a7a8F",
"Reputation": "0x8d914D38dD301FC4606f5aa9fEcF8A76389020d3",
"Forum": "0x050C420Cc4995B41217Eba1B54B82Fd5687e9139",
"Bench": "0xfe58B9EB03F75A603de1B286584f5E9532ab8fB5"
"DAO": "0x3734B0944ea37694E85AEF60D5b256d19EDA04be",
"Work1": "0x8BDA04936887cF11263B87185E4D19e8158c6296",
"Onboarding": "0x8688E736D0D72161db4D25f68EF7d0EE4856ba19",
"Proposals": "0x3287061aDCeE36C1aae420a06E4a5EaE865Fe3ce",
"Rollup": "0x71cb20D63576a0Fa4F620a2E96C73F82848B09e1",
"Work2": "0x76Dfe9F47f06112a1b78960bf37d87CfbB6D6133",
"Reputation": "0xEAefe601Aad7422307B99be65bbE005aeA966012",
"Forum": "0x79e365342329560e8420d7a0f016633d7640cB18",
"Bench": "0xC0f00E5915F9abE6476858fD1961EAf79395ea64"
},
"sepolia": {
"DAO": "0x02dC6871d89fA6a16be2e875C63d81761CF459A8",

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long