-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: As a user, I want to count unique attested subjects (#466)
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { VeraxSdk } from "../../src/VeraxSdk"; | ||
|
||
const BATCH_SIZE = 100000; | ||
|
||
const fetchSubjectsFromFile = async (fileSuffix: number): Promise<string[]> => { | ||
console.log(`Reading file #${fileSuffix}...`); | ||
return JSON.parse(fs.readFileSync(path.resolve(__dirname, `../../allSubjects-${fileSuffix}.txt`), "utf8")); | ||
}; | ||
|
||
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 allSubjects: string[][] = []; | ||
const uniqueSubjects: Set<string> = new Set<string>(); | ||
|
||
for (let i = 0; i <= filesNumber; i++) { | ||
allSubjects.push(await fetchSubjectsFromFile(i)); | ||
} | ||
|
||
console.log(`Total subjects = ${allSubjects.reduce((total, subjects) => total + subjects.length, 0)}`); | ||
|
||
for (const array of allSubjects) { | ||
for (const subject of array) { | ||
uniqueSubjects.add(subject); | ||
} | ||
} | ||
|
||
console.log(`Unique subjects = ${uniqueSubjects.size}`); | ||
} | ||
|
||
// We recommend this pattern to be able to use async/await everywhere | ||
// and properly handle errors. | ||
main().catch((error) => { | ||
console.error(error); | ||
process.exitCode = 1; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { VeraxSdk } from "../../src/VeraxSdk"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
const BATCH_SIZE = 100000; | ||
|
||
const fetchAllAttestations = async (batchNumber: number, veraxSdk: VeraxSdk) => { | ||
let skip = 0; | ||
let hasMoreResults = true; | ||
const batch: string[] = []; | ||
|
||
while (hasMoreResults && batch.length <= BATCH_SIZE) { | ||
console.log(`Query batch #${skip}`); | ||
const matchingAttestations = await veraxSdk.attestation.findBy(1000, skip * 1000 + batchNumber * BATCH_SIZE); | ||
|
||
if (matchingAttestations.length === 0) { | ||
hasMoreResults = false; | ||
continue; | ||
} | ||
|
||
const subjects = matchingAttestations.map((attestation) => attestation.subject); | ||
|
||
batch.push(...subjects); | ||
skip++; | ||
} | ||
|
||
return batch; | ||
}; | ||
|
||
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); | ||
|
||
console.log( | ||
`We expect ${batchesNumber} batches of ${BATCH_SIZE} attestations to get all ${attestationsNumber} attestations.`, | ||
); | ||
|
||
for (let i = 0; i <= batchesNumber; i++) { | ||
console.log(`Attestations batch #${i}`); | ||
const subjectsBatch = await fetchAllAttestations(i, veraxSdk); | ||
|
||
fs.writeFile(path.resolve(__dirname, `../../allSubjects-${i}.txt`), JSON.stringify(subjectsBatch), function (err) { | ||
if (err) { | ||
return console.log(err); | ||
} | ||
|
||
console.log(`File #${i} was saved!`); | ||
}); | ||
} | ||
} | ||
|
||
// We recommend this pattern to be able to use async/await everywhere | ||
// and properly handle errors. | ||
main().catch((error) => { | ||
console.error(error); | ||
process.exitCode = 1; | ||
}); |