Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
fix(records): avoid setting records with invalid contarctTxIds in the…
Browse files Browse the repository at this point in the history
… cache
  • Loading branch information
dtfiedler committed Mar 22, 2024
1 parent c8e03fc commit c865cc4
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export async function evaluateArNSNames() {
);

// create a map of the contract records for O(1) lookup
const contractRecordMap: Record<ContractTxId, Record<string, ANTRecord>> = {};
const contractRecordMap: Record<
ContractTxId,
{ owner: string; records: Record<string, ANTRecord> }
> = {};
// TODO: wrap this in p-limit to avoid overloading the node process
await Promise.all(
[...contractTxIds].map(async (contractTxId) => {
Expand All @@ -65,7 +68,10 @@ export async function evaluateArNSNames() {
});

if (Object.keys(antRecords).length) {
contractRecordMap[contractTxId] = antRecords;
contractRecordMap[contractTxId] = {
owner: await antContract.getOwner(),
records: antRecords,
};
}
}),
);
Expand All @@ -74,30 +80,33 @@ export async function evaluateArNSNames() {
contractCount: Object.keys(contractRecordMap).length,
});

// filter out any records associated with an invalid contract
const validArNSRecords = Object.entries(apexRecords).filter(
([_, record]) => record.contractTxId in contractRecordMap,
);

// now go through all the record names and assign them to the resolved tx ids
for (const [apexName, apexRecordData] of Object.entries(apexRecords)) {
const contractRecords = contractRecordMap[apexRecordData.contractTxId];
const ant = new ANT({ contractTxId: apexRecordData.contractTxId });
for (const [apexName, apexRecordData] of validArNSRecords) {
const antData = contractRecordMap[apexRecordData.contractTxId];
// TODO: current complexity is O(n^2) - we can do better by flattening records above before this loop
for (const [antName, antRecordData] of Object.entries(contractRecords)) {
for (const [antName, antRecordData] of Object.entries(antData.records)) {
const resolvedRecordObj = {
ttlSeconds: antRecordData.ttlSeconds,
txId: antRecordData.transactionId,
contractTxId: apexRecordData.contractTxId,
type: apexRecordData.type,
owner: await ant.getOwner(),
owner: antData.owner,
...(apexRecordData.type === 'lease' && {
endTimestamp: apexRecordData.endTimestamp,
}),
};
const resolvedRecordBuffer = Buffer.from(
JSON.stringify(resolvedRecordObj),
);
if (antName === '@') {
cache.set(apexName, resolvedRecordBuffer);
} else {
cache.set(`${antName}_${apexName}`, resolvedRecordBuffer);
}
const cacheKey: string =
antName === '@' ? apexName : `${antName}_${apexName}`;
cache.set(cacheKey, resolvedRecordBuffer);
cache.set(cacheKey, resolvedRecordBuffer);
}
}
log.info('Successfully evaluated arns names', {
Expand Down

0 comments on commit c865cc4

Please sign in to comment.