Skip to content

Feature/indexer chain sync - #1732

Open
chidii wants to merge 2 commits into
Liquifact:mainfrom
chidii:feature/indexer-chain-sync
Open

chidii wants to merge 2 commits into
Liquifact:mainfrom
chidii:feature/indexer-chain-sync

Conversation

@chidii

@chidii chidii commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Closes #1671

Summary

Adds src/services/chain-sync.js — a service that maps decoded escrow events (from src/indexer/decode.js) to idempotent invoice-store mutations and emits the same webhook events the API routes already emit. The indexer wires it in so POST /api/internal/indexer/run now applies events to invoice state, not just stores them.

  • src/services/chain-sync.js (new) — applyEvents(events, opts) sorts events by ledger, then dispatches on type:
    • funded → records the contribution via store.fund(), emits invoice.funding_received
    • funding_target_reached → emits invoice.funded (the status is already flipped by store.fund() when the final contribution arrives)
    • settled → store.settle(), emits invoice.settled
    • refunded / funding_cancelled → store.cancel(), emits invoice.cancelled
    • Dedupes by event id (module-level Set, exported resetAppliedIdsForTests() for tests)
    • Ignores events for unknown contracts, malformed data, duplicate events, and state conflicts
    • Audit every applied event with actor: "indexer", action: "invoice.chain_sync", outcome: "success"
  • src/indexer/escrow-indexer.js — after store.append([decoded]), calls chainSync.applyEvents([decoded]) and merges applied / ignored into the run summary. runOnce now returns applied and ignored alongside the existing ingested / skipped. src/routes/indexerTrigger.js passes the extended shape through unchanged, so POST /api/internal/indexer/run now reports applied/ignored counts.

Acceptance criteria

  • Replaying the same events changes nothing — applyEvents dedupes by event id; test full replay of the same event list is a no-op asserts applied: 0 on the second call.
  • Funded events create contributions only for linked invoices — events whose contractId doesn't match any invoice.escrowContractId are ignored (unknownContract).
  • Status transitions follow the allowed state graph — funded rejects when terminal; settled rejects when already settled/cancelled; refunded / funding_cancelled reject when settled.
  • Audit entries use actor "indexer" — asserted in settled flips status, emits invoice.settled, and writes an audit entry with actor=indexer.
  • Trigger response reports applied and ignored counts — runOnce returns { ingested, skipped, applied, ignored, ... }, passed through by src/routes/indexerTrigger.js.

Test output

npx jest --runInBand tests/chain-sync.test.js

PASS  tests/chain-sync.test.js
  chain-sync.applyEvents
    ✓ funded for a linked invoice records the contribution and returns applied:1
    ✓ duplicate event is ignored (idempotent)
    ✓ unknown contract is ignored
    ✓ malformed funded data is ignored (no investor or amount)
    ✓ funded against a settled invoice is ignored (state conflict)
    ✓ funding_target_reached flips status to funded and emits invoice.funded
    ✓ funding_target_reached on an unfunded invoice is ignored
    ✓ settled flips status, emits invoice.settled, and writes an audit entry with actor=indexer
    ✓ settled twice — second is duplicate
    ✓ refunded flips status to cancelled and emits invoice.cancelled
    ✓ funding_cancelled flips status to cancelled
    ✓ out-of-order ledgers: settled at ledger 50 before funded at ledger 10 — settled is applied, funded then ignored as state conflict
    ✓ full replay of the same event list is a no-op
    ✓ unknown event type is ignored with a specific reason

Test Suites: 1 passed, 1 total
Tests:       14 passed, 14 total
Snapshots:   0 total
Time:        2.382 s

Files changed

 src/indexer/escrow-indexer.js  |  15 ++--
 src/services/chain-sync.js     | 182 ++++++++++++++++++++++++++++++++
 tests/chain-sync.test.js       | 214 ++++++++++++++++++++++++++++++++
 3 files changed, 411 insertions(+), 1 deletion(-)

Note on the upstream mutatingLimiter regression

upstream/main at 9ac9e4a (merge of #1729) has ReferenceError: mutatingLimiter is not defined at src/routes/invoices.js:253 — the mutatingLimiter export from src/middleware/rate-limit.js:396 isn't imported in that file. That regression blocks every test that loads src/app.js.

This PR's test file does not load src/app.js — it requires src/services/chain-sync and src/data/store directly — so npx jest tests/chain-sync.test.js runs green regardless. The existing tests/indexerTrigger.test.js mocks runOnce, so it is unaffected by the extended response shape.

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.

Apply indexed escrow events to invoice state through a sync service

1 participant