Skip to content

Commit 904190c

Browse files
committed
Handle missing direct-read registry objects
1 parent 07274a5 commit 904190c

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

src/demo/tokenhost-wrapper.mjs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ export function createTokenHostRegistryDirectReadAdapter({
155155
readContract(reads.receiptPayer),
156156
]);
157157
const object = normalizeRegistryStorageObject(objectResult);
158-
const normalizedObjectId = object.objectId === "0" ? decimalString(objectId) : object.objectId;
158+
if (isMissingRegistryObject(object)) return null;
159+
const normalizedObjectId = object.objectId;
159160

160161
return {
161162
objectId: normalizedObjectId,
@@ -186,7 +187,9 @@ export function createTokenHostRegistryDirectReadAdapter({
186187
),
187188
});
188189
const ids = Array.from(page.values, decimalString);
189-
const objects = await mapWithConcurrency(ids, maxDetailConcurrency, readObjectDetails);
190+
const objects = (
191+
await mapWithConcurrency(ids, maxDetailConcurrency, readObjectDetails)
192+
).filter(Boolean);
190193

191194
return {
192195
sourceOfTruth: DIRECT_READ_SOURCE,
@@ -384,6 +387,7 @@ export function createTokenHostRegistryDirectReadAdapter({
384387

385388
async function mergeObjectDetail(model, objectId) {
386389
const row = await readObjectDetails(objectId);
390+
if (!row) return;
387391
model.objects[row.objectId] = row.object;
388392
model.copyReceipts[row.objectId] = row.copyReceipts;
389393
model.receiptPayers[row.objectId] = row.receiptPayer;
@@ -398,6 +402,7 @@ export function createTokenHostRegistryDirectReadAdapter({
398402
includeTerminal: options.includeTerminal ?? includeTerminal,
399403
});
400404
for (const row of result.objects) {
405+
if (!row) continue;
401406
model.objects[row.objectId] = row.object;
402407
model.copyReceipts[row.objectId] = row.copyReceipts;
403408
model.receiptPayers[row.objectId] = row.receiptPayer;
@@ -763,6 +768,10 @@ function currentUnixSeconds() {
763768
return Math.floor(Date.now() / 1000);
764769
}
765770

771+
function isMissingRegistryObject(object) {
772+
return object.objectId === "0";
773+
}
774+
766775
function decimalString(value) {
767776
return BigInt(value).toString();
768777
}

test/tokenhost-direct-reads.test.mjs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ test("Token Host direct read adapter honors admin route hints", async () => {
160160
"getCopyReceipts",
161161
"receiptPayer",
162162
]);
163+
164+
calls.length = 0;
165+
const missingObject = await api.handle({
166+
method: "GET",
167+
path: "/admin/storage/objects/999",
168+
headers: {},
169+
});
170+
assert.equal(missingObject.status, 404);
171+
assert.equal(missingObject.body.error.code, "admin_object_not_found");
172+
assert.deepEqual(calls.map((call) => call.functionName), [
173+
"getStorageObject",
174+
"getCopyReceipts",
175+
"receiptPayer",
176+
]);
163177
});
164178

165179
test("Token Host direct read adapter defaults admin time for coordinator expiry checks", async () => {
@@ -313,7 +327,7 @@ function createRegistryFixtureClient({ staleActiveCursor, calls } = {}) {
313327
case "listRelayerAddresses":
314328
return offsetPage([RELAYER], args[0], args[1]);
315329
case "getStorageObject":
316-
return objects.get(String(args[0]));
330+
return objects.get(String(args[0])) ?? zeroStorageObject();
317331
case "getAccountUsage":
318332
return usage.get(args[0]);
319333
case "getCopyReceipts":
@@ -381,6 +395,31 @@ function offsetPage(values, offset, limit) {
381395
return values.slice(Number(offset), Number(offset) + Number(limit));
382396
}
383397

398+
function zeroStorageObject() {
399+
return {
400+
objectId: 0n,
401+
accountId: hex32("00"),
402+
user: ZERO_ADDRESS,
403+
idempotencyKey: hex32("00"),
404+
contentHash: hex32("00"),
405+
metadataHash: hex32("00"),
406+
pieceCidHash: hex32("00"),
407+
size: 0n,
408+
requestedCopies: 0,
409+
completedCopies: 0,
410+
withCDN: false,
411+
maxCost: 0n,
412+
reservedCost: 0n,
413+
actualCost: 0n,
414+
status: 0,
415+
coordinator: ZERO_ADDRESS,
416+
requestExpiresAt: 0n,
417+
createdAt: 0n,
418+
updatedAt: 0n,
419+
receiptHash: hex32("00"),
420+
};
421+
}
422+
384423
function hex32(byte) {
385424
return `0x${byte.padStart(64, "0")}`;
386425
}

0 commit comments

Comments
 (0)