Skip to content

Commit

Permalink
Merge pull request #33 from desci-labs/m0ar/filter-dev-registry-dupes
Browse files Browse the repository at this point in the history
Filter out dupes when resolving history through legacy mappings
  • Loading branch information
m0ar authored Oct 1, 2024
2 parents 5df1f0c + e9090b6 commit 782321e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
ARG NODE_VERSION
FROM node:${NODE_VERSION}-alpine3.20 AS base
FROM node:20.13.1-alpine3.20 AS base
RUN apk update && apk add --no-cache bash dumb-init

WORKDIR /usr/src/app
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"type": "module",
"scripts": {
"build": "tsc",
"docker:build": "docker build --build-arg NODE_VERSION=$(< .nvmrc) .",
"watch": "tsc --watch",
"start": "node dist/index.js",
"test": "PINO_LOG_LEVEL=silent PORT=5600 vitest --config vitest.config.ts",
Expand Down
32 changes: 29 additions & 3 deletions src/api/v2/resolvers/dpid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ 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";
import { BigNumber } from "ethers";

const MODULE_PATH = "/api/v2/resolvers/codex" as const;
const logger = parentLogger.child({
Expand Down Expand Up @@ -152,7 +152,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,6 +164,8 @@ export const resolveDpid = async (dpid: number, versionIx?: number): Promise<His
versions: versions.map(([manifest, time]) => ({
// No CommitID available
version: "",
// When restored from redis, the BigNumber is deserialised as a regular object
// Ethers can instantiate the class from that format
time: BigNumber.from(time).toNumber(),
manifest,
})),
Expand All @@ -179,5 +182,28 @@ 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 = (
[aCid, aTimeBn]: LegacyVersion,
[bCid, bTimeBn]: LegacyVersion
): Boolean => {
const cidIsEqual = aCid === bCid;
const timeIsEqual = aTimeBn.toNumber() === bTimeBn.toNumber();
return cidIsEqual && timeIsEqual;
};
3 changes: 2 additions & 1 deletion test/v2/resolvers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ describe("dPID", { timeout: 10_000 }, function () {
.expect(200)
.expect((res) =>
expect(res.body.manifest).toEqual(
"bafkreih5koqw5nvxucidlihwfslknj674oeuroclit74rkaqpe4mq6xuka",
// fourth published CID
"bafkreibn3jhdlsdsonv25t7i2bwtrbkl3jzwjbnnwylpeih3jmmzdhsfmi",
),
);
});
Expand Down

0 comments on commit 782321e

Please sign in to comment.