Skip to content

The service has no database — anchors, liquidity and settlements live in in-memory Maps and are destroyed on every restart #220

Description

@Jagadeeshftw

Priority: High  ·  Area: Persistence / architecture  ·  Est. effort: 12–16 h

📌 Problem

package.json lists exactly three runtime dependencies:

compression, cors, express

There is no database driver, no ORM, no migration tool. Every repository under src/repositories/anchorRepository.ts, liquidityRepository.ts, settlementRepository.ts — is backed by inMemoryRepository.ts, and settlementRepository.ts:13 keeps its secondary index as private readonly anchorIndex: Map<string, Set<number>> = new Map().

Consequences:

  • Every restart wipes all state. A deploy, crash, or scale event destroys the settlement history, the registered anchors and all liquidity records.
  • Horizontal scaling is impossible. Two replicas hold two disjoint datasets; which one answers a request decides what the client sees.
  • Unbounded growth. Nothing evicts, so the process grows until it is restarted — which is also the only thing that clears it.

Settlements are financial records. A service that forgets them on restart cannot be operated.

🎯 Design decision required

Post your proposed schema and approach in a comment on this issue before writing code, and wait for maintainer agreement. This is too large to land unsolicited. State and defend:

  1. Store choice. Postgres is the conventional answer for relational financial records; argue it against the alternatives rather than assuming it.
  2. Migration tooling and how migrations run in CI, in development, and on deploy.
  3. Interface preservation. The repository classes have method signatures that src/services/*.ts depend on. Keeping them stable means no service code changes — confirm that is achievable, and list any signature you must change.
  4. Sequencing. All three repositories in one PR, or one at a time behind the existing interfaces? Argue which is reviewable.
  5. Testing without a live database. The suite currently runs with no external dependency. How do you preserve that — an in-memory implementation retained for tests behind the same interface, or a containerised database in CI?

🧩 Requirements and context

  • src/services/anchorService.ts, liquidityService.ts, settlementService.ts and quoteService.ts should require no changes. If one does, justify it.
  • Schema must include the indexes the current code relies on — settlementRepository.ts's anchor index is the obvious one; find the rest by reading the query methods.
  • Concurrency matters: two simultaneous settlements against the same liquidity must not both succeed if only one can be funded. State how your schema and transactions prevent that.
  • All 42 existing test files must pass.
  • Do not change API response shapes.
  • Configuration must fail fast with a clear message when the database is unreachable — src/config.ts is the place.

🛠️ Suggested execution

  1. Post the schema and approach; wait for agreement.
  2. Add the driver and migration tooling; write migrations.
  3. Implement one repository behind its existing interface; keep the in-memory one for tests if that is your choice.
  4. Prove services are untouched.
  5. Add a concurrency test for the double-spend case.

✅ Acceptance criteria

  • The schema and approach were agreed on this issue before implementation.
  • At least one repository is database-backed, with migrations committed.
  • No file under src/services/ changed, or every change is justified.
  • Indexes backing existing query methods exist in the schema.
  • A test proves two concurrent settlements cannot both consume the same liquidity.
  • All 42 test files pass, with no requirement for a live database if that was your stated approach.
  • Startup fails fast and clearly when the database is unreachable.
  • API response shapes are unchanged.

🚫 Out of scope

  • Changing the public API surface.
  • Migrating all three repositories, if you argued for sequencing.
  • Adding caching.

🧪 Verification

npm ci
npm run lint
npm run build
npm test

📤 What your PR must include

  1. A link to the agreed schema comment.
  2. Your store, tooling, sequencing and test-strategy decisions with reasoning.
  3. The concurrency test.
  4. Confirmation services are unchanged.
  5. Closes #<n> or Part of #<n>.

🔒 Security notes

Settlement records are the audit trail for value movement. Storing them only in process memory means there is no durable record of what the system did — a restart erases the evidence of every settlement executed before it. Beyond availability, that is an integrity and accountability failure: no reconstruction, no dispute resolution, and no way to detect after the fact that something went wrong.

📋 Guidelines

  • Minimum 95% test coverage on changed lines
  • Clear documentation
  • Timeframe: 96 hours from assignment
  • One logical change per commit; no merge commits

💬 Join our community

Working on this, or want to sanity-check your approach before you start? Come and ask — the maintainers are there and happy to help.

Telegram: https://t.me/Grainlify

Activity

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

Metadata

Metadata

Assignees

Labels

GrantFox OSSGrantFox open-source programMaybe RewardedGrantFox: potentially rewarded contributionThird CampaignGrantFox third campaign issueenhancementNew feature or improvementpriority:highHigh difficulty / architectural or cross-cutting

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions