Skip to content

Commit

Permalink
feat: Reset nonces on server start
Browse files Browse the repository at this point in the history
  • Loading branch information
arcoraven committed Jan 9, 2024
1 parent 77d3cf8 commit cea3caf
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/db/wallets/deleteAllWalletNonces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { PrismaTransaction } from "../../schema/prisma";
import { getPrismaWithPostgresTx } from "../client";

interface DeleteAllWalletNoncesParams {
pgtx?: PrismaTransaction;
}

/**
* Delete the cached value of all backend wallet nonces.
* This is useful to require all backend wallets to resync their
* nonce on the next txn.
*/
export const deleteAllWalletNonces = async ({
pgtx,
}: DeleteAllWalletNoncesParams) => {
const prisma = getPrismaWithPostgresTx(pgtx);
await prisma.walletNonce.deleteMany({});
};
8 changes: 7 additions & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fastify, { FastifyInstance } from "fastify";
import * as fs from "fs";
import path from "path";
import { URL } from "url";
import { deleteAllWalletNonces } from "../db/wallets/deleteAllWalletNonces";
import { env } from "../utils/env";
import { logger } from "../utils/logger";
import { withAuth } from "./middleware/auth";
Expand All @@ -25,8 +26,12 @@ interface HttpsObject {
}

const main = async () => {
let httpsObject: HttpsObject | undefined = undefined;
// Reset any server state that is safe to reset.
// This allows the server to start in a predictable state.
await deleteAllWalletNonces({});

// Enables the server to run on https://localhost:PORT, if ENABLE_HTTPS is provided.
let httpsObject: HttpsObject | undefined = undefined;
if (env.ENABLE_HTTPS) {
httpsObject = {
https: {
Expand All @@ -37,6 +42,7 @@ const main = async () => {
};
}

// Start the server with middleware.
const server: FastifyInstance = fastify({
disableRequestLogging: true,
...(env.ENABLE_HTTPS ? httpsObject : {}),
Expand Down
46 changes: 46 additions & 0 deletions src/server/routes/backend-wallet/resetNonces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { deleteAllWalletNonces } from "../../../db/wallets/deleteAllWalletNonces";
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";

const responseSchema = Type.Object({
result: Type.Object({
status: Type.String(),
}),
});

responseSchema.example = {
result: {
status: "success",
},
};

export const resetBackendWallet = async (fastify: FastifyInstance) => {
fastify.route<{
Reply: Static<typeof responseSchema>;
}>({
method: "POST",
url: "/backend-wallet/reset-nonces",
schema: {
summary: "Reset all nonces",
description:
"Reset nonces for all backend wallets. This is for debugging purposes and does not impact held tokens.",
tags: ["Backend Wallet"],
operationId: "resetNonces",
response: {
...standardResponseSchema,
[StatusCodes.OK]: responseSchema,
},
},
handler: async (req, reply) => {
await deleteAllWalletNonces({});

reply.status(StatusCodes.OK).send({
result: {
status: "success",
},
});
},
});
};
4 changes: 2 additions & 2 deletions src/server/schemas/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export const walletAuthSchema = Type.Object({
export const walletParamSchema = Type.Object({
chain: Type.String({
examples: ["mumbai"],
description: "Chain ID name",
description: "Chain name",
}),
walletAddress: Type.String({
examples: ["0x..."],
description: "Wallet Address",
description: "Backend wallet address",
}),
});

Expand Down

0 comments on commit cea3caf

Please sign in to comment.