Skip to content

[Feature 07] Storage & Idempotency #26

@onlyhyde

Description

@onlyhyde

Overview

Implement persistent storage for transaction records and idempotency keys using SQLite.

Implementation Details

Files:

  • internal/storage/interface.go - Storage interface
  • internal/storage/sqlite.go - SQLite implementation
  • internal/middleware/idempotency.go - Idempotency middleware

Storage Interface

type Storage interface {
    // Transactions
    CreateTransaction(ctx, tx *TransactionRecord) error
    UpdateTransaction(ctx, id string, updates map[string]interface{}) error
    GetTransaction(ctx, id string) (*TransactionRecord, error)
    GetTransactionByHash(ctx, txHash string) (*TransactionRecord, error)
    ListTransactions(ctx, filters map[string]interface{}, limit, offset int) ([]*TransactionRecord, error)

    // Idempotency
    GetIdempotencyKey(ctx, key string) (*IdempotencyRecord, error)
    SetIdempotencyKey(ctx, record *IdempotencyRecord) error
    CleanupExpiredIdempotencyKeys(ctx) error

    // Lifecycle
    Close() error
    Migrate(ctx) error
}

Data Models

TransactionRecord:

  • ID, Network, TxHash, From, To, Amount, Asset
  • Status (pending, confirmed, failed)
  • CreatedAt, ConfirmedAt, ErrorMessage

IdempotencyRecord:

  • Key, Response, CreatedAt, ExpiresAt

SQLite Setup

  • WAL mode for better concurrency
  • Parameterized queries (prevent SQL injection)
  • Indexes on tx_hash, status, expires_at

Idempotency Middleware

  • Check Idempotency-Key header on POST requests
  • Return cached response if key exists
  • Store response after successful processing
  • Set X-Idempotency-Replayed: true header on replay

Acceptance Criteria

  • Storage interface defined
  • SQLite implementation with migrations
  • Parameterized queries (no SQL injection)
  • Idempotency middleware
  • Cleanup job for expired keys
  • Unit tests for storage operations
  • Integration tests

Dependencies

Branch

feature/storage

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions