Skip to content

Commit c06fa0d

Browse files
authored
refactor: rename all instances of maxRealtimeDistance to maxWorstCaseDistance (namehash#1415)
1 parent fae127e commit c06fa0d

6 files changed

Lines changed: 61 additions & 67 deletions

File tree

apps/ensapi/src/handlers/amirealtime-api.test.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { factory } from "@/lib/hono-factory";
1010
import * as middleware from "@/middleware/indexing-status.middleware";
1111

12-
import amIRealtimeApi, { AMIREALTIME_DEFAULT_MAX_REALTIME_DISTANCE } from "./amirealtime-api"; // adjust import path as needed
12+
import amIRealtimeApi, { AMIREALTIME_DEFAULT_MAX_WORST_CASE_DISTANCE } from "./amirealtime-api"; // adjust import path as needed
1313

1414
vi.mock("@/middleware/indexing-status.middleware", () => ({
1515
indexingStatusMiddleware: vi.fn(),
@@ -59,52 +59,52 @@ describe("amirealtime-api", () => {
5959

6060
describe("GET /amirealtime", () => {
6161
describe("request", () => {
62-
it("should accept valid maxRealtimeDistance query param", async () => {
62+
it("should accept valid maxWorstCaseDistance query param", async () => {
6363
// Arrange: set `indexingStatus` context var
6464
arrangeMockedIndexingStatusMiddleware({ now });
6565

6666
// Act
67-
const response = await app.request("http://localhost/amirealtime?maxRealtimeDistance=300");
67+
const response = await app.request("http://localhost/amirealtime?maxWorstCaseDistance=300");
6868
const responseJson = await response.json();
6969

7070
// Assert
7171
expect(response.status).toBe(200);
7272
expect(responseJson).toMatchObject({
73-
maxRealtimeDistance: 300,
73+
maxWorstCaseDistance: 300,
7474
});
7575
});
7676

77-
it("should accept valid maxRealtimeDistance query param (set to `0`)", async () => {
77+
it("should accept valid maxWorstCaseDistance query param (set to `0`)", async () => {
7878
// Arrange: set `indexingStatus` context var
7979
arrangeMockedIndexingStatusMiddleware({ now, slowestChainIndexingCursor: now });
8080

8181
// Act
82-
const response = await app.request("http://localhost/amirealtime?maxRealtimeDistance=0");
82+
const response = await app.request("http://localhost/amirealtime?maxWorstCaseDistance=0");
8383
const responseJson = await response.json();
8484

8585
// Assert
8686
expect(response.status).toBe(200);
8787
expect(responseJson).toMatchObject({
88-
maxRealtimeDistance: 0,
88+
maxWorstCaseDistance: 0,
8989
});
9090
});
9191

92-
it("should use default maxRealtimeDistance when unset", async () => {
92+
it("should use default maxWorstCaseDistance when unset", async () => {
9393
// Arrange: set `indexingStatus` context var
9494
arrangeMockedIndexingStatusMiddleware({ now });
9595

9696
// Act
97-
const response = await app.request("http://localhost/amirealtime?maxRealtimeDistance");
97+
const response = await app.request("http://localhost/amirealtime?maxWorstCaseDistance");
9898
const responseJson = await response.json();
9999

100100
// Assert
101101
expect(response.status).toBe(200);
102102
expect(responseJson).toMatchObject({
103-
maxRealtimeDistance: AMIREALTIME_DEFAULT_MAX_REALTIME_DISTANCE,
103+
maxWorstCaseDistance: AMIREALTIME_DEFAULT_MAX_WORST_CASE_DISTANCE,
104104
});
105105
});
106106

107-
it("should use default maxRealtimeDistance when not provided", async () => {
107+
it("should use default maxWorstCaseDistance when not provided", async () => {
108108
// Arrange: set `indexingStatus` context var
109109
arrangeMockedIndexingStatusMiddleware({ now });
110110

@@ -115,89 +115,89 @@ describe("amirealtime-api", () => {
115115
// Assert
116116
expect(response.status).toBe(200);
117117
expect(responseJson).toMatchObject({
118-
maxRealtimeDistance: AMIREALTIME_DEFAULT_MAX_REALTIME_DISTANCE,
118+
maxWorstCaseDistance: AMIREALTIME_DEFAULT_MAX_WORST_CASE_DISTANCE,
119119
});
120120
});
121121

122-
it("should reject invalid maxRealtimeDistance (negative number)", async () => {
122+
it("should reject invalid maxWorstCaseDistance (negative number)", async () => {
123123
// Arrange: set `indexingStatus` context var
124124
arrangeMockedIndexingStatusMiddleware({ now });
125125

126126
// Act
127-
const response = await app.request("http://localhost/amirealtime?maxRealtimeDistance=-1");
127+
const response = await app.request("http://localhost/amirealtime?maxWorstCaseDistance=-1");
128128

129129
// Assert
130130
expect(response.status).toBe(400);
131131
await expect(response.text()).resolves.toMatch(
132-
/maxRealtimeDistance query param must be a non-negative integer/,
132+
/maxWorstCaseDistance query param must be a non-negative integer/,
133133
);
134134
});
135135

136-
it("should reject invalid maxRealtimeDistance (not a number)", async () => {
136+
it("should reject invalid maxWorstCaseDistance (not a number)", async () => {
137137
// Arrange: set `indexingStatus` context var
138138
arrangeMockedIndexingStatusMiddleware({ now });
139139

140140
// Act
141141
const response = await app.request(
142-
"http://localhost/amirealtime?maxRealtimeDistance=invalid",
142+
"http://localhost/amirealtime?maxWorstCaseDistance=invalid",
143143
);
144144

145145
// Assert
146146
expect(response.status).toBe(400);
147147
await expect(response.text()).resolves.toMatch(
148-
/maxRealtimeDistance query param must be a number/,
148+
/maxWorstCaseDistance query param must be a number/,
149149
);
150150
});
151151
});
152152

153153
describe("response", () => {
154-
it("should return 200 when worstCaseDistance is below maxRealtimeDistance", async () => {
154+
it("should return 200 when worstCaseDistance is below maxWorstCaseDistance", async () => {
155155
// Arrange: set `indexingStatus` context var
156156
arrangeMockedIndexingStatusMiddleware({ now, slowestChainIndexingCursor: now - 9 });
157157

158158
// Act
159-
const response = await app.request("http://localhost/amirealtime?maxRealtimeDistance=10");
159+
const response = await app.request("http://localhost/amirealtime?maxWorstCaseDistance=10");
160160
const responseJson = await response.json();
161161

162162
// Assert
163163
expect(response.status).toBe(200);
164164
expect(responseJson).toMatchObject({
165-
maxRealtimeDistance: 10,
165+
maxWorstCaseDistance: 10,
166166
slowestChainIndexingCursor: 1766123720,
167167
worstCaseDistance: 9,
168168
});
169169
});
170170

171-
it("should return 200 when worstCaseDistance equals maxRealtimeDistance", async () => {
171+
it("should return 200 when worstCaseDistance equals maxWorstCaseDistance", async () => {
172172
// Arrange: set `indexingStatus` context var
173173
arrangeMockedIndexingStatusMiddleware({ now, slowestChainIndexingCursor: now - 10 });
174174

175175
// Act
176-
const response = await app.request("http://localhost/amirealtime?maxRealtimeDistance=10");
176+
const response = await app.request("http://localhost/amirealtime?maxWorstCaseDistance=10");
177177
const responseJson = await response.json();
178178

179179
// Assert
180180
expect(response.status).toBe(200);
181181
expect(responseJson).toMatchObject({
182-
maxRealtimeDistance: 10,
182+
maxWorstCaseDistance: 10,
183183
slowestChainIndexingCursor: 1766123719,
184184
worstCaseDistance: 10,
185185
});
186186
});
187187

188-
it("should return 503 when worstCaseDistance exceeds maxRealtimeDistance", async () => {
188+
it("should return 503 when worstCaseDistance exceeds maxWorstCaseDistance", async () => {
189189
// Arrange: set `indexingStatus` context var
190190
arrangeMockedIndexingStatusMiddleware({ now, slowestChainIndexingCursor: now - 11 });
191191

192192
// Act
193-
const response = await app.request("http://localhost/amirealtime?maxRealtimeDistance=10");
193+
const response = await app.request("http://localhost/amirealtime?maxWorstCaseDistance=10");
194194
const responseJson = await response.json();
195195

196196
// Assert
197197
expect(response.status).toBe(503);
198198
expect(responseJson).toHaveProperty("message");
199199
expect(responseJson.message).toMatch(
200-
/Indexing Status 'worstCaseDistance' must be below or equal to the requested 'maxRealtimeDistance'; worstCaseDistance = 11; maxRealtimeDistance = 10/,
200+
/Indexing Status 'worstCaseDistance' must be below or equal to the requested 'maxWorstCaseDistance'; worstCaseDistance = 11; maxWorstCaseDistance = 10/,
201201
);
202202
});
203203

@@ -210,14 +210,14 @@ describe("amirealtime-api", () => {
210210
});
211211

212212
// Act
213-
const response = await app.request("http://localhost/amirealtime?maxRealtimeDistance=10");
213+
const response = await app.request("http://localhost/amirealtime?maxWorstCaseDistance=10");
214214
const responseJson = await response.json();
215215

216216
// Assert
217-
expect(response.status).toBe(500);
217+
expect(response.status).toBe(503);
218218
expect(responseJson).toHaveProperty("message");
219219
expect(responseJson.message).toMatch(
220-
/Indexing Status has to be resolved successfully before 'maxRealtimeDistance' can be applied./,
220+
/Indexing Status has to be resolved successfully before 'maxWorstCaseDistance' can be applied./,
221221
);
222222
});
223223
});

apps/ensapi/src/handlers/amirealtime-api.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,54 @@ import { factory } from "@/lib/hono-factory";
1111

1212
const app = factory.createApp();
1313

14-
// Set default `maxRealtimeDistance` for `GET /amirealtime` endpoint to one minute.
15-
export const AMIREALTIME_DEFAULT_MAX_REALTIME_DISTANCE: Duration = minutesToSeconds(1);
14+
// Set default `maxWorstCaseDistance` for `GET /amirealtime` endpoint to one minute.
15+
export const AMIREALTIME_DEFAULT_MAX_WORST_CASE_DISTANCE: Duration = minutesToSeconds(1);
1616

1717
// allow performance monitoring clients to read HTTP Status for the provided
18-
// `maxRealtimeDistance` param
18+
// `maxWorstCaseDistance` param
1919
app.get(
2020
"/",
2121
validate(
2222
"query",
2323
z.object({
24-
maxRealtimeDistance: params.queryParam
24+
maxWorstCaseDistance: params.queryParam
2525
.optional()
26-
.default(AMIREALTIME_DEFAULT_MAX_REALTIME_DISTANCE)
27-
.pipe(makeDurationSchema("maxRealtimeDistance query param")),
26+
.default(AMIREALTIME_DEFAULT_MAX_WORST_CASE_DISTANCE)
27+
.pipe(makeDurationSchema("maxWorstCaseDistance query param")),
2828
}),
2929
),
3030
async (c) => {
3131
// context must be set by the required middleware
3232
if (c.var.indexingStatus === undefined) {
33-
throw new Error(`Invariant(amirealtime): indexingStatusMiddleware required.`);
33+
throw new Error(`Invariant(amirealtime-api): indexingStatusMiddleware required.`);
3434
}
3535

36+
// return 503 response error with details on prerequisite being unavailable
3637
if (c.var.indexingStatus instanceof Error) {
3738
return errorResponse(
3839
c,
39-
`Invariant(amirealtime): Indexing Status has to be resolved successfully before 'maxRealtimeDistance' can be applied.`,
40+
`Invariant(amirealtime-api): Indexing Status has to be resolved successfully before 'maxWorstCaseDistance' can be applied.`,
41+
503,
4042
);
4143
}
4244

43-
const { maxRealtimeDistance } = c.req.valid("query");
45+
const { maxWorstCaseDistance } = c.req.valid("query");
4446
const { worstCaseDistance, snapshot } = c.var.indexingStatus;
4547
const { slowestChainIndexingCursor } = snapshot;
4648

4749
// return 503 response error with details on
48-
// requested `maxRealtimeDistance` vs. actual `worstCaseDistance`
49-
if (worstCaseDistance > maxRealtimeDistance) {
50+
// requested `maxWorstCaseDistance` vs. actual `worstCaseDistance`
51+
if (worstCaseDistance > maxWorstCaseDistance) {
5052
return errorResponse(
5153
c,
52-
`Indexing Status 'worstCaseDistance' must be below or equal to the requested 'maxRealtimeDistance'; worstCaseDistance = ${worstCaseDistance}; maxRealtimeDistance = ${maxRealtimeDistance}`,
54+
`Indexing Status 'worstCaseDistance' must be below or equal to the requested 'maxWorstCaseDistance'; worstCaseDistance = ${worstCaseDistance}; maxWorstCaseDistance = ${maxWorstCaseDistance}`,
5355
503,
5456
);
5557
}
5658

57-
// return 200 response OK with current details on `maxRealtimeDistance`,
59+
// return 200 response OK with current details on `maxWorstCaseDistance`,
5860
// `slowestChainIndexingCursor`, and `worstCaseDistance`
59-
return c.json({ maxRealtimeDistance, slowestChainIndexingCursor, worstCaseDistance });
61+
return c.json({ maxWorstCaseDistance, slowestChainIndexingCursor, worstCaseDistance });
6062
},
6163
);
6264

apps/ensapi/src/handlers/ensnode-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ app.get("/indexing-status", async (c) => {
3535
serializeIndexingStatusResponse({
3636
responseCode: IndexingStatusResponseCodes.Error,
3737
} satisfies IndexingStatusResponseError),
38-
500,
38+
503,
3939
);
4040
}
4141

apps/ensapi/src/middleware/is-realtime.middleware.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { makeLogger } from "@/lib/logger";
88
*/
99
export type IsRealtimeMiddlewareVariables = { isRealtime: boolean };
1010

11-
export const makeIsRealtimeMiddleware = (scope: string, maxRealtimeDistance: Duration) => {
11+
export const makeIsRealtimeMiddleware = (scope: string, maxWorstCaseDistance: Duration) => {
1212
const logger = makeLogger(scope);
1313

1414
let hasLoggedIndexingStatusError = false;
@@ -24,7 +24,7 @@ export const makeIsRealtimeMiddleware = (scope: string, maxRealtimeDistance: Dur
2424
// no indexing status available in context
2525
if (!hasLoggedIndexingStatusError) {
2626
logger.warn(
27-
`ENSIndexer is NOT guaranteed to be within ${maxRealtimeDistance} seconds of realtime. Current indexing status has not been successfully fetched by this ENSApi instance yet and is therefore unknown to this ENSApi instance because: ${c.var.indexingStatus.message}.`,
27+
`ENSIndexer is NOT guaranteed to be within ${maxWorstCaseDistance} seconds of realtime. Current indexing status has not been successfully fetched by this ENSApi instance yet and is therefore unknown to this ENSApi instance because: ${c.var.indexingStatus.message}.`,
2828
);
2929

3030
hasLoggedIndexingStatusError = true;
@@ -35,16 +35,16 @@ export const makeIsRealtimeMiddleware = (scope: string, maxRealtimeDistance: Dur
3535
}
3636

3737
// determine if we're within the max worst-case distance to qualify as "realtime".
38-
const isRealtime = c.var.indexingStatus.worstCaseDistance <= maxRealtimeDistance;
38+
const isRealtime = c.var.indexingStatus.worstCaseDistance <= maxWorstCaseDistance;
3939

4040
if (lastLoggedIsRealtime !== isRealtime) {
4141
if (isRealtime) {
4242
logger.info(
43-
`ENSIndexer is guaranteed to be within ${maxRealtimeDistance} seconds of realtime.`,
43+
`ENSIndexer is guaranteed to be within ${maxWorstCaseDistance} seconds of realtime`,
4444
);
4545
} else {
4646
logger.warn(
47-
`ENSIndexer is NOT guaranteed to be within ${maxRealtimeDistance} seconds of realtime. (Worst Case distance: ${c.var.indexingStatus.worstCaseDistance} seconds > ${maxRealtimeDistance} seconds).`,
47+
`ENSIndexer is NOT guaranteed to be within ${maxWorstCaseDistance} seconds of realtime. (Worst Case distance: ${c.var.indexingStatus.worstCaseDistance} seconds > ${maxWorstCaseDistance} seconds).`,
4848
);
4949
}
5050

docs/ensnode.io/src/content/docs/docs/usage/api.mdx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,18 @@ This allows your client to programmatically determine whether the ENSNode
216216
instance is sufficiently synchronized for your use case before proceeding with
217217
operations that depend on the latest onchain data.
218218

219-
#### Verification endpoint
219+
#### Simplified Monitoring API
220220

221-
For improved developer experience, you may want to use `GET /amirealtime`
222-
endpoint, which supports an optional `maxRealtimeDistance` parameter.
221+
For simplified monitoring, you may want to use `GET /amirealtime` endpoint,
222+
which supports an optional `maxWorstCaseDistance` parameter (for which default
223+
value is `AMIREALTIME_DEFAULT_MAX_WORST_CASE_DISTANCE`).
223224

224-
The `maxRealtimeDistance` parameter accepts a duration value in seconds,
225-
representing the max allowed distance from the current "tip" of all indexed chains.
225+
The `maxWorstCaseDistance` parameter accepts a duration value in seconds,
226+
representing the max worst case distance from the current "tip" of all indexed chains.
226227
When you include this parameter it influences the HTTP response code as follows:
227228

228-
- **Success (200 OK)**: The latest indexed block of each chain is within the requested distance from realtime.
229-
- **Service Unavailable (503)**: The latest indexed block of each chain is NOT within the requested distance from realtime.
229+
- **Success (200 OK)**: The latest indexed block of each chain is guaranteed within the requested distance from realtime.
230+
- **Service Unavailable (503)**: The latest indexed block of each chain is NOT guaranteed within the requested distance from realtime.
230231

231232

232233
## Registrar Actions API

packages/ensnode-sdk/README.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,10 @@ console.log(config);
169169

170170
#### Indexing Status API
171171

172-
##### `indexingStatus(options)`
172+
##### `indexingStatus()`
173173

174174
Fetches the ENSNode's multichain indexing status.
175175

176-
- `options`: (optional) additional options
177-
- `maxRealtimeDistance`: (optional) The max allowed distance in seconds between the latest indexed block of the slowest indexed chain and the current time. Setting this parameter influences the HTTP response code:
178-
- Success (200 OK): The latest indexed block of each chain is within the requested distance from realtime
179-
- Service Unavailable (503): The latest indexed block of each chain is NOT within the requested distance from realtime
180176
- Returns: `IndexingStatusResponse` - The indexing status data for all indexed chains
181177
- Throws: Error if the request fails or the ENSNode API returns an error response
182178

@@ -185,11 +181,6 @@ Fetches the ENSNode's multichain indexing status.
185181
const status = await client.indexingStatus();
186182
console.log(status);
187183
// Returns indexing status for all indexed chains
188-
189-
// Check if omnichain indexing is within 60 seconds of realtime
190-
const status = await client.indexingStatus({ maxRealtimeDistance: 60 });
191-
console.log(status);
192-
// Returns indexing status, throws if not within 60 seconds of realtime
193184
```
194185

195186
### Configuration

0 commit comments

Comments
 (0)