Skip to content

Commit

Permalink
if resolving a legacy history entry from the dev contract, filter out…
Browse files Browse the repository at this point in the history
… the duplicate entries
  • Loading branch information
m0ar committed Sep 27, 2024
1 parent 5df1f0c commit a18bf8d
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/api/v2/resolvers/dpid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import parentLogger from "../../../logger.js";
import { CACHE_TTL_ANCHORED, CACHE_TTL_PENDING, DPID_ENV, getDpidAliasRegistry } from "../../../util/config.js";
import { ResolverError } from "../../../errors.js";
import { getCodexHistory, type HistoryQueryResult, type HistoryVersion } from "../queries/history.js";
import { BigNumber } from "ethers";
import { getFromCache, setToCache } from "../../../redis.js";
import type { DpidAliasRegistry } from "@desci-labs/desci-contracts/dist/typechain-types/DpidAliasRegistry.js";

Expand Down Expand Up @@ -152,7 +151,8 @@ export const resolveDpid = async (dpid: number, versionIx?: number): Promise<His
resolvedEntry = JSON.parse(fromCache);
}

const [owner, versions] = resolvedEntry;
const owner = resolvedEntry[0];
const versions = undupeIfLegacyDevHistory(resolvedEntry[1]);
const requestedVersion = versions[versionIx ?? versions.length - 1];

result = {
Expand All @@ -163,7 +163,7 @@ export const resolveDpid = async (dpid: number, versionIx?: number): Promise<His
versions: versions.map(([manifest, time]) => ({
// No CommitID available
version: "",
time: BigNumber.from(time).toNumber(),
time: time.toNumber(),
manifest,
})),
};
Expand All @@ -179,5 +179,23 @@ export const resolveDpid = async (dpid: number, versionIx?: number): Promise<His
};

type DpidErrorName = "RegistryContactFailed" | "CeramicContactFailed" | "LegacyLookupError" | "DpidNotFound";

export class DpidResolverError extends ResolverError<DpidErrorName> {}

type LegacyVersion = DpidAliasRegistry.LegacyVersionStructOutput;

const undupeIfLegacyDevHistory = (versions: LegacyVersion[]) => {
if (DPID_ENV !== "dev") {
return versions;
}

return versions.reduce((unduped, current) => {
if (unduped.length === 0 || !isLegacyDupe(current, unduped[unduped.length - 1])) {
unduped.push(current);
}
return unduped;
}, [] as LegacyVersion[]);
};

const isLegacyDupe = (a: LegacyVersion, b: LegacyVersion) => {
return a[0] === b[0] && a[1].toNumber() === b[1].toNumber();
};

0 comments on commit a18bf8d

Please sign in to comment.