Skip to content

Commit

Permalink
feat: Reset nonces on server start, add endpoint (#366)
Browse files Browse the repository at this point in the history
* feat: Reset nonces on server start

* add route
  • Loading branch information
arcoraven authored Jan 10, 2024
1 parent 710bb5d commit d2a989e
Show file tree
Hide file tree
Showing 5 changed files with 75 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 resetBackendWalletNonces = 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",
},
});
},
});
};
2 changes: 2 additions & 0 deletions src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import { revokeRelayer } from "./relayer/revoke";

// System
import { getAllTransactions } from "./backend-wallet/getTransactions";
import { resetBackendWalletNonces } from "./backend-wallet/resetNonces";
import { sendTransactionBatch } from "./backend-wallet/sendTransactionBatch";
import { withdraw } from "./backend-wallet/withdraw";
import { home } from "./home";
Expand All @@ -118,6 +119,7 @@ export const withRoutes = async (fastify: FastifyInstance) => {
await fastify.register(signTransaction);
await fastify.register(signMessage);
await fastify.register(getAllTransactions);
await fastify.register(resetBackendWalletNonces);

// Configuration
await fastify.register(getWalletsConfiguration);
Expand Down
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 d2a989e

Please sign in to comment.