Skip to content

Commit

Permalink
fix: Some attestations are downloaded twice via the "getAllAttestatio…
Browse files Browse the repository at this point in the history
…ns" script
  • Loading branch information
alainncls committed Mar 18, 2024
1 parent 8502ea4 commit 58fb150
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
25 changes: 21 additions & 4 deletions sdk/examples/utils/countUniqueSubjects.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VeraxSdk } from "../../src/VeraxSdk";
import fs from "fs";
import path from "path";
import { VeraxSdk } from "../../src/VeraxSdk";

const BATCH_SIZE = 100000;

Expand All @@ -11,13 +11,13 @@ const fetchSubjectsFromFile = async (fileSuffix: number): Promise<string[]> => {

async function main() {
const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_MAINNET);
const attestationsNumber = await veraxSdk.utils.getAttestationIdCounter();
const filesNumber = Math.ceil(Number(attestationsNumber) / BATCH_SIZE);
const attestationNumber = await veraxSdk.utils.getAttestationIdCounter();
const filesNumber = Math.ceil(Number(attestationNumber) / BATCH_SIZE);

const allSubjects: string[][] = [];
const uniqueSubjects: Set<string> = new Set<string>();

for (let i = 0; i <= filesNumber; i++) {
for (let i = 0; i < filesNumber; i++) {
allSubjects.push(await fetchSubjectsFromFile(i));
}

Expand All @@ -30,6 +30,23 @@ async function main() {
}

console.log(`Unique subjects = ${uniqueSubjects.size}`);

const uniqueSubjectsArray = Array.from(uniqueSubjects);
const chunks = Math.ceil(uniqueSubjectsArray.length / BATCH_SIZE);

for (let i = 0; i < chunks; i++) {
const start = i * BATCH_SIZE;
const end = start + BATCH_SIZE;
const chunk = uniqueSubjectsArray.slice(start, end);

fs.writeFile(path.resolve(__dirname, `../../uniqueSubjects-${i}.txt`), JSON.stringify(chunk), function (err) {
if (err) {
return console.log(err);
}

console.log(`File uniqueSubjects-${i}.txt was saved!`);
});
}
}

// We recommend this pattern to be able to use async/await everywhere
Expand Down
14 changes: 6 additions & 8 deletions sdk/examples/utils/getAllAttestations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const fetchAllAttestations = async (batchNumber: number, veraxSdk: VeraxSdk) =>
let hasMoreResults = true;
const batch: string[] = [];

while (hasMoreResults && batch.length <= BATCH_SIZE) {
while (hasMoreResults && batch.length < BATCH_SIZE) {
console.log(`Query batch #${skip}`);
const matchingAttestations = await veraxSdk.attestation.findBy(1000, skip * 1000 + batchNumber * BATCH_SIZE);

Expand All @@ -29,15 +29,13 @@ const fetchAllAttestations = async (batchNumber: number, veraxSdk: VeraxSdk) =>

async function main() {
const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_MAINNET);
const attestationsNumber = await veraxSdk.utils.getAttestationIdCounter();
const batchesNumber = Math.ceil(Number(attestationsNumber) / BATCH_SIZE);
const attestationNumber = await veraxSdk.utils.getAttestationIdCounter();
const batchesNumber = Math.ceil(Number(attestationNumber) / BATCH_SIZE);

console.log(
`We expect ${batchesNumber} batches of ${BATCH_SIZE} attestations to get all ${attestationsNumber} attestations.`,
);
console.log(`Creating ${batchesNumber} batches of ${BATCH_SIZE} items to get all ${attestationNumber} attestations.`);

for (let i = 0; i <= batchesNumber; i++) {
console.log(`Attestations batch #${i}`);
for (let i = 8; i < batchesNumber; i++) {
console.log(`Attestation batch #${i}`);
const subjectsBatch = await fetchAllAttestations(i, veraxSdk);

fs.writeFile(path.resolve(__dirname, `../../allSubjects-${i}.txt`), JSON.stringify(subjectsBatch), function (err) {
Expand Down

0 comments on commit 58fb150

Please sign in to comment.