Skip to content

feat: create schema and service for managing UTXOs#504

Open
GabrielTozatti wants to merge 2 commits intostagingfrom
tzt/feat/utxo-worker-schema-service
Open

feat: create schema and service for managing UTXOs#504
GabrielTozatti wants to merge 2 commits intostagingfrom
tzt/feat/utxo-worker-schema-service

Conversation

@GabrielTozatti
Copy link
Collaborator

Description

Create schema and service for managing gasless UTXOs in the Worker. This includes the collection, interface, and all required atomic methods.

Summary

  • Created gasless_utxos collection following Worker standards
  • Implemented GaslessUtxo interface
  • Implemented methods: findAvailable(), reserve(), release(), markSpent(), getStats()
  • Ensured atomic operations using findOneAndUpdate
  • Added typings and validations for UTXO fields

Checklist

  • I reviewed my PR code before submitting
  • I ensured that the implementation is working correctly and did not impact other parts of the app
  • I mentioned the PR link in the task

@GabrielTozatti GabrielTozatti requested a review from guimroque March 9, 2026 17:06
): Promise<GaslessUtxo | null> => {
const { reservedBy, ttlSeconds = DEFAULT_TTL_SECONDS } = options;

return collection.findOneAndUpdate(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 IMPORTANT: Missing TTL cleanup mechanism

Problem: Reserved UTXOs with expired TTL will remain stuck in 'reserved' status forever, causing resource leaks. The reservedAt timestamp is set but never used for cleanup.

Suggestion: Add a cleanup method that finds expired reservations: { status: 'reserved', reservedAt: { $lt: new Date(Date.now() - ttlSeconds * 1000) } } and sets them back to 'available'.

utxoId: string,
spentTxHash: string
): Promise<GaslessUtxo | null> => {
return collection.findOneAndUpdate(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 IMPORTANT: Missing input validation

Problem: utxoId and spentTxHash parameters are not validated. Empty strings or invalid formats could cause issues.

Suggestion: Add validation: if (!utxoId?.trim() || !spentTxHash?.trim()) throw new Error('Invalid parameters');

});

export * from './types';
export * from './constants'; No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 SUGGESTION: Missing newline at end of file

Problem: File should end with a newline character for consistency.

Suggestion: Add a newline at the end of the file.

Copy link
Member

@guimroque guimroque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

Summary

This PR creates a well-structured UTXO management service for gasless transactions with proper atomic operations and clear separation of concerns. The implementation follows Worker standards and includes comprehensive CRUD operations.

Strengths

  • Atomic operations using findOneAndUpdate prevent race conditions
  • Clean separation of concerns with utils functions
  • Proper TypeScript interfaces and exports
  • Comprehensive stats aggregation functionality

Issues

  • 0 critical, 2 important, 1 suggestion

Copy link
Member

@guimroque guimroque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! ✅

Previous issues have been fixed. Code approved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants