-
Notifications
You must be signed in to change notification settings - Fork 5
REFACTOR: utxo worker gasless endpoints test #511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GabrielTozatti
wants to merge
9
commits into
tzt/feat/utxo-worker-schema-service
Choose a base branch
from
tzt/feat/utxo-worker-gasless-endpoints-test
base: tzt/feat/utxo-worker-schema-service
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
323e64e
feat: add unit tests for gaslessUtxos collection
GabrielTozatti 42a97ec
feat: add base HTTP controller and routes structure
GabrielTozatti 4a4b1f0
fix: remove console.log and add basic Express middleware
GabrielTozatti 0889af8
feat: implement POST /worker/gasless/reserve endpoint
GabrielTozatti ef6444f
feat: implement GET /worker/gasless/pool/stats endpoint
GabrielTozatti 4b1ee52
fix: address code review critical and important issues
GabrielTozatti a8b253a
Merge branch 'tzt/feat/utxo-pool-unit-tests' into tzt/feat/utxo-worke…
GabrielTozatti 13897f1
refactor: update and add complete flow tests for gasless endpoints
GabrielTozatti be6e77b
fix: address code review important and suggestion issues
GabrielTozatti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 @@ | ||
| import { Application } from "express"; | ||
| import gaslessRouter from "./modules/gasless/routes"; | ||
| import { handleErrors } from "./middlewares/handleErrors"; | ||
|
|
||
| export const setupRoutes = (app: Application): void => { | ||
| app.use("/worker/gasless", gaslessRouter); | ||
|
|
||
| app.use(handleErrors); | ||
| }; |
This file contains hidden or 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,22 @@ | ||
| import { Request, Response, NextFunction } from "express"; | ||
|
|
||
| export class AppError extends Error { | ||
| constructor(public readonly statusCode: number, message: string) { | ||
| super(message); | ||
| this.name = "AppError"; | ||
| } | ||
| } | ||
|
|
||
| export const handleErrors = ( | ||
| err: Error, | ||
| _req: Request, | ||
| res: Response, | ||
| _next: NextFunction | ||
| ): void => { | ||
| if (err instanceof AppError) { | ||
| res.status(err.statusCode).json({ error: err.message }); | ||
| return; | ||
| } | ||
|
|
||
| res.status(500).json({ error: "Internal server error" }); | ||
| }; |
This file contains hidden or 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,76 @@ | ||
| import { Request, Response, NextFunction } from "express"; | ||
| import { MongoDatabase } from "@/clients/mongoClient"; | ||
| import { gaslessUtxosCollection } from "@/queues/gaslessUtxos"; | ||
| import { COLLECTION_GASLESS_UTXOS } from "@/queues/gaslessUtxos/constants"; | ||
| import { GaslessUtxo } from "@/queues/gaslessUtxos/types"; | ||
| import { AppError } from "@/http/middlewares/handleErrors"; | ||
|
|
||
| export class GaslessController { | ||
| static async reserve( | ||
| req: Request, | ||
| res: Response, | ||
| next: NextFunction | ||
| ): Promise<void> { | ||
| try { | ||
| const { accountId, estimatedMaxFee } = req.body; | ||
|
|
||
| if (estimatedMaxFee === undefined || estimatedMaxFee === null) { | ||
| throw new AppError(400, "estimatedMaxFee is required"); | ||
| } | ||
|
|
||
| if (typeof estimatedMaxFee !== "number" || estimatedMaxFee <= 0) { | ||
| throw new AppError(400, "estimatedMaxFee must be a positive number"); | ||
| } | ||
|
|
||
| if ( | ||
| accountId !== undefined && | ||
| (typeof accountId !== "string" || accountId.trim().length === 0) | ||
| ) { | ||
| throw new AppError(400, "accountId must be a non-empty string"); | ||
| } | ||
|
|
||
| const db = await MongoDatabase.connect(); | ||
| const utxos = gaslessUtxosCollection( | ||
| db.getCollection<GaslessUtxo>(COLLECTION_GASLESS_UTXOS) | ||
| ); | ||
|
|
||
| const utxo = await utxos.reserve({ | ||
| reservedBy: accountId ?? "anonymous", | ||
| estimatedMaxFee, | ||
| }); | ||
|
|
||
| if (!utxo) { | ||
GabrielTozatti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new AppError(503, "Pool exhausted - no UTXOs available"); | ||
| } | ||
|
|
||
| res.status(200).json({ | ||
| utxoId: utxo.utxoId, | ||
| txId: utxo.txId, | ||
| outputIndex: utxo.outputIndex, | ||
| amount: utxo.amount, | ||
| owner: utxo.owner, | ||
| }); | ||
| } catch (err) { | ||
| next(err); | ||
| } | ||
| } | ||
|
|
||
| static async stats( | ||
| _req: Request, | ||
| res: Response, | ||
| next: NextFunction | ||
| ): Promise<void> { | ||
| try { | ||
| const db = await MongoDatabase.connect(); | ||
| const utxos = gaslessUtxosCollection( | ||
| db.getCollection<GaslessUtxo>(COLLECTION_GASLESS_UTXOS) | ||
| ); | ||
|
|
||
| const stats = await utxos.getStats(); | ||
|
|
||
| res.status(200).json(stats); | ||
| } catch (err) { | ||
| next(err); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 @@ | ||
| import { Router } from "express"; | ||
| import { GaslessController } from "@/http/modules/gasless/controller"; | ||
|
|
||
| const gaslessRouter = Router(); | ||
|
|
||
| gaslessRouter.post("/reserve", GaslessController.reserve); | ||
| gaslessRouter.get("/pool/stats", GaslessController.stats); | ||
|
|
||
| export default gaslessRouter; |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| export const COLLECTION_GASLESS_UTXOS = "gasless_utxos"; | ||
| export const DEFAULT_TTL_SECONDS = 60 * 60; | ||
| export const CLEANUP_INTERVAL_MS = 60 * 1000; | ||
| export const RESERVE_TTLS_MS = 5 * 60 * 1000; | ||
| export const SAFETY_MARGIN_PERCENT = 150; |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.