-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(syncers): add swarm stamp syncer
- Loading branch information
Showing
12 changed files
with
625 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@blobscan/syncers": minor | ||
--- | ||
|
||
Added swarm stamp syncer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import type { AxiosResponse } from "axios"; | ||
import { AxiosError } from "axios"; | ||
import axios from "axios"; | ||
|
||
import { prisma } from "@blobscan/db"; | ||
|
||
import { BaseSyncer } from "../BaseSyncer"; | ||
import type { CommonSyncerConfig } from "../BaseSyncer"; | ||
import { SwarmNodeError } from "../errors"; | ||
|
||
type BatchData = { | ||
batchID: string; | ||
batchTTL: number; | ||
}; | ||
|
||
export interface SwarmStampSyncerConfig extends CommonSyncerConfig { | ||
beeEndpoint: string; | ||
batchId: string; | ||
} | ||
|
||
export class SwarmStampSyncer extends BaseSyncer { | ||
constructor({ | ||
cronPattern, | ||
redisUriOrConnection, | ||
batchId, | ||
beeEndpoint, | ||
}: SwarmStampSyncerConfig) { | ||
const name = "swarm-stamp"; | ||
super({ | ||
name, | ||
cronPattern, | ||
redisUriOrConnection, | ||
syncerFn: async () => { | ||
let response: AxiosResponse<BatchData>; | ||
|
||
try { | ||
const url = `${beeEndpoint}/stamps/${batchId}`; | ||
|
||
response = await axios.get<BatchData>(url); | ||
} catch (err) { | ||
let cause; | ||
|
||
if (err instanceof AxiosError) { | ||
cause = new SwarmNodeError(err); | ||
} | ||
|
||
throw new Error(`Failed to fetch stamp batch "${batchId}"`, { | ||
cause, | ||
}); | ||
} | ||
|
||
const { batchTTL } = response.data; | ||
|
||
await prisma.blobStoragesState.upsert({ | ||
create: { | ||
swarmDataId: batchId, | ||
swarmDataTTL: batchTTL, | ||
}, | ||
update: { | ||
swarmDataTTL: batchTTL, | ||
updatedAt: new Date(), | ||
}, | ||
where: { | ||
id: 1, | ||
swarmDataId: batchId, | ||
}, | ||
}); | ||
|
||
this.logger.info(`Swarm stamp data with batch ID "${batchId}" updated`); | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* eslint-disable @typescript-eslint/no-misused-promises */ | ||
import { http, HttpResponse } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
import { beforeAll, beforeEach, describe, expect, it } from "vitest"; | ||
|
||
import type { BlobStoragesState } from "@blobscan/db"; | ||
import { prisma } from "@blobscan/db"; | ||
import { fixtures, testValidError } from "@blobscan/test"; | ||
|
||
import type { SwarmStampSyncerConfig } from "../src/syncers/SwarmStampSyncer"; | ||
import { SwarmStampSyncer } from "../src/syncers/SwarmStampSyncer"; | ||
|
||
const BEE_ENDPOINT = process.env.BEE_ENDPOINT ?? "http://localhost:1633"; | ||
|
||
class SwarmStampSyncerMock extends SwarmStampSyncer { | ||
constructor({ batchId, cronPattern }: Partial<SwarmStampSyncerConfig> = {}) { | ||
super({ | ||
redisUriOrConnection: process.env.REDIS_URI ?? "", | ||
cronPattern: cronPattern ?? "* * * * *", | ||
batchId: batchId ?? process.env.SWARM_BATCH_ID ?? "", | ||
beeEndpoint: BEE_ENDPOINT, | ||
}); | ||
} | ||
|
||
getQueue() { | ||
return this.queue; | ||
} | ||
|
||
getWorkerProcessor() { | ||
return this.syncerFn; | ||
} | ||
} | ||
|
||
describe("SwarmStampSyncer", () => { | ||
const expectedBatchId = fixtures.blobStoragesState[0]?.swarmDataId as string; | ||
const expectedBatchTTL = 1000; | ||
|
||
let swarmStampSyncer: SwarmStampSyncerMock; | ||
|
||
beforeAll(() => { | ||
const baseUrl = `${BEE_ENDPOINT}/stamps`; | ||
const server = setupServer( | ||
...[ | ||
http.get(`${baseUrl}/:batchId`, ({ request }) => { | ||
const batchId = request.url.split("/").pop(); | ||
|
||
if (!batchId || batchId.length !== 64) { | ||
return HttpResponse.json( | ||
{ | ||
code: 400, | ||
message: "invalid path params", | ||
reasons: [ | ||
{ | ||
field: "batch_id", | ||
error: "odd length hex string", | ||
}, | ||
], | ||
}, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
if (batchId !== expectedBatchId) { | ||
return HttpResponse.json( | ||
{ | ||
code: 404, | ||
message: "issuer does not exist", | ||
}, | ||
{ status: 404 } | ||
); | ||
} | ||
|
||
return HttpResponse.json( | ||
{ | ||
batchID: expectedBatchId, | ||
batchTTL: expectedBatchTTL, | ||
}, | ||
{ | ||
status: 200, | ||
} | ||
); | ||
}), | ||
] | ||
); | ||
|
||
server.listen(); | ||
|
||
return () => { | ||
server.close(); | ||
}; | ||
}); | ||
|
||
beforeEach(() => { | ||
swarmStampSyncer = new SwarmStampSyncerMock(); | ||
|
||
return async () => { | ||
await swarmStampSyncer.close(); | ||
}; | ||
}); | ||
|
||
describe("when creating a new swarm batch data row in the db", async () => { | ||
let blobStorageState: BlobStoragesState | null = null; | ||
|
||
beforeEach(async () => { | ||
await prisma.blobStoragesState.deleteMany(); | ||
|
||
const workerProcessor = swarmStampSyncer.getWorkerProcessor(); | ||
|
||
await workerProcessor().catch((err) => console.log(err)); | ||
|
||
blobStorageState = await prisma.blobStoragesState.findFirst(); | ||
}); | ||
|
||
it("should create it with the correct swarm stamp batch ID", async () => { | ||
expect(blobStorageState?.swarmDataId).toBe(process.env.SWARM_BATCH_ID); | ||
}); | ||
|
||
it("should create it with the correct batch TTL", async () => { | ||
expect(blobStorageState?.swarmDataTTL).toBe(expectedBatchTTL); | ||
}); | ||
}); | ||
|
||
it("should update the batch TTl", async () => { | ||
await prisma.blobStoragesState.update({ | ||
data: { | ||
swarmDataTTL: 99999, | ||
}, | ||
where: { | ||
id: 1, | ||
}, | ||
}); | ||
|
||
const workerProcessor = swarmStampSyncer.getWorkerProcessor(); | ||
await workerProcessor(); | ||
|
||
const blobStorageState = await prisma.blobStoragesState.findFirst(); | ||
|
||
expect(blobStorageState?.swarmDataTTL).toBe(expectedBatchTTL); | ||
}); | ||
|
||
testValidError( | ||
"should fail when trying to fetch a non-existing batch", | ||
async () => { | ||
const failingSwarmStampSyncer = new SwarmStampSyncerMock({ | ||
batchId: | ||
"6b538866048cfb6e9e1d06805374c51572c11219d2d550c03e6e277366cb0371", | ||
}); | ||
const failingWorkerProcessor = | ||
failingSwarmStampSyncer.getWorkerProcessor(); | ||
|
||
await failingWorkerProcessor().finally(async () => { | ||
await failingSwarmStampSyncer.close(); | ||
}); | ||
}, | ||
Error, | ||
{ | ||
checkCause: true, | ||
} | ||
); | ||
|
||
testValidError( | ||
"should fail when trying to fetch an invalid batch", | ||
async () => { | ||
const failingSwarmStampSyncer = new SwarmStampSyncerMock({ | ||
batchId: "invalid-batch", | ||
}); | ||
const failingWorkerProcessor = | ||
failingSwarmStampSyncer.getWorkerProcessor(); | ||
|
||
await failingWorkerProcessor().finally(async () => { | ||
await failingSwarmStampSyncer.close(); | ||
}); | ||
}, | ||
Error, | ||
{ | ||
checkCause: true, | ||
} | ||
); | ||
}); |
Empty file.
9 changes: 9 additions & 0 deletions
9
packages/syncers/test/__snapshots__/SwarmStampSyncer.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`SwarmStampSyncer > should fail when trying to fetch a non-existing batch 1`] = `"Failed to fetch stamp batch \\"6b538866048cfb6e9e1d06805374c51572c11219d2d550c03e6e277366cb0371\\""`; | ||
|
||
exports[`SwarmStampSyncer > should fail when trying to fetch a non-existing batch 2`] = `[SwarmNodeError: issuer does not exist]`; | ||
|
||
exports[`SwarmStampSyncer > should fail when trying to fetch an invalid batch 1`] = `"Failed to fetch stamp batch \\"invalid-batch\\""`; | ||
|
||
exports[`SwarmStampSyncer > should fail when trying to fetch an invalid batch 2`] = `[SwarmNodeError: invalid path params]`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { setupServer } from "msw/node"; | ||
|
||
export function createServer(handlers: Parameters<typeof setupServer>[0][]) { | ||
const server = setupServer(...handlers); | ||
|
||
server.listen(); | ||
|
||
return () => { | ||
server.close(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.