attempt to fix batch ordering
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 40s Details

This commit is contained in:
Ladd Hoffman 2024-05-04 19:52:44 -05:00
parent 0617fea89e
commit 1e1686619d
4 changed files with 34 additions and 20 deletions

View File

@ -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;
};

View File

@ -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;

View File

@ -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();

View File

@ -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`;