Skip to content

Commit

Permalink
Run decrypt in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
luistorres committed May 20, 2024
1 parent 7d07f54 commit 7d7d5b2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 76 deletions.
26 changes: 17 additions & 9 deletions packages/web-app/app/_server/idos/idos-grantee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,23 @@ export class idOSGrantee {
}

async getSharedCredentialContentDecrypted(dataId: string): Promise<string> {
const credentialCopy = await this.fetchSharedCredentialFromIdos<{
content: string;
encryption_public_key: string;
}>(dataId);

return await this.noncedBox.decrypt(
credentialCopy.content,
credentialCopy.encryption_public_key,
);
try {
const credentialCopy = await this.fetchSharedCredentialFromIdos<{
content: string;
encryption_public_key: string;
}>(dataId);

return await this.noncedBox.decrypt(
credentialCopy.content,
credentialCopy.encryption_public_key,
);
} catch (error) {
console.log(
'Error fetching or decrypting shared credential from idos for dataId:',
dataId,
);
throw error;
}
}

async isValidCredential(credential: any): Promise<boolean> {
Expand Down
99 changes: 32 additions & 67 deletions packages/web-app/app/_server/idos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,53 +63,27 @@ const userFilter = async (grantee: idOSGrantee, userAddress: string) => {
return null;
};

const populateLists = async (addresses: string[], grantee: idOSGrantee) => {
const filterApplicants = async (addresses: string[], grantee: idOSGrantee) => {
const allowed: string[] = [];
const notAllowed: string[] = [];
const batches = Math.ceil(addresses.length / 2);
const batches = Math.ceil(addresses.length / 10);

// run in batches of 10
for (let i = 0; i < batches; i++) {
console.log(
'%c==>BATCH',
'color: green; background: yellow; font-size: 20px',
i,
);
console.log('==>BATCH', i);

const batch = addresses.slice(i * 2, (i + 1) * 2);
console.log(
'%c==>',
'color: green; background: yellow; font-size: 20px',
'MANDOU',
let batch = addresses.slice(i * 10, (i + 1) * 10);
let promises = batch.map(
async (address): Promise<string | null> =>
new Promise((resolve) => resolve(userFilter(grantee, address))),
);

const promises = batch.map(async (address): Promise<string | null> => {
return new Promise(() => userFilter(grantee, address));
});

const results = await Promise.all(promises);

console.log(
'%c==>',
'color: green; background: yellow; font-size: 20px',
'CHEGOU',
);
let results = await Promise.all(promises);

results.forEach((result, index) => {
console.log(
'%c==>RESULT',
'color: green; background: yellow; font-size: 20px',
result,
);

if (result !== null) {
allowed.push(result);
} else {
console.log(
'%c==>FAILED',
'color: green; background: yellow; font-size: 20px',
result,
);

notAllowed.push(batch[index]);
}
});
Expand All @@ -118,6 +92,21 @@ const populateLists = async (addresses: string[], grantee: idOSGrantee) => {
return { allowed, notAllowed };
};

const retryFailed = async (
allowed: string[],
notAllowed: string[],
grantee: idOSGrantee,
) => {
for (const address of notAllowed) {
const result = await userFilter(grantee, address);
if (result !== null) {
allowed.push(result);
}
}

notAllowed = notAllowed.filter((address) => !allowed.includes(address));
};

export const getAllowedProjectApplicants = async (projectAddress: string) => {
try {
const applicantsResult = await getProjectApplicants(projectAddress);
Expand All @@ -131,43 +120,19 @@ export const getAllowedProjectApplicants = async (projectAddress: string) => {
? undefined
: 'x44250024a9bf9599ad7c3fcdb220d2100357dbf263014485174a1ae3',
});
// number of batches of 5 addresses
let { allowed, notAllowed } = await populateLists(addresses, grantee);

console.log(
'%c==>',
'color: green; background: yellow; font-size: 20px',
'DEU MERDA',
);

// for (const address of addresses) {
// const result = await userFilter(grantee, address);
// if (result !== null) {
// allowed.push(result);
// } else {
// notAllowed.push(address);
// }
// }

let retry = 5;
// first run to populate the lists
let { allowed, notAllowed } = await filterApplicants(addresses, grantee);
// retry 2 times failed addresses
let retry = 2;
while (retry > 0 && notAllowed.length > 0) {
for (const address of notAllowed) {
const result = await userFilter(grantee, address);
if (result !== null) {
allowed.push(result);
} else {
console.log(
'%c==>Failed Retry',
'color: green; background: yellow; font-size: 20px',
address,
);
}
}

notAllowed = notAllowed.filter((address) => !allowed.includes(address));
retryFailed(allowed, notAllowed, grantee);
retry--;
}

console.log('==>', 'NOT ALLOWED:');
console.log(notAllowed);

return allowed as string[];
} catch (error) {
console.error(error);
Expand Down

0 comments on commit 7d7d5b2

Please sign in to comment.