Skip to content

Conversation

@dkuthoore
Copy link
Collaborator

@dkuthoore dkuthoore commented Oct 29, 2025

NOTE

Change base branch to main before merging, i built this branch off of dk/assignDeposit, assuming it would be merged first.

Summary

This PR merges the standalone nonces table into the vaults table by adding a NOT NULL DEFAULT 0 nonce column and enforcing that every vault always has at least one controller. All reads/writes of nonce now use vaults.nonce, and the legacy nonces table and its index are removed. API behavior is preserved, with stricter validation and clearer errors.

Changes

  • Schema and migrations

    • Add nonce INTEGER NOT NULL DEFAULT 0 to vaults.
    • Add CHECK constraint vaults_controllers_nonempty to ensure controllers are never empty.
    • Migration migrations/4_merge_nonces_into_vaults.sh:
      • Backfills vaults.nonce from legacy nonces if it exists.
      • Drops unique_lower_vault_nonces and the nonces table.
    • Update migrations/1_start.sh to remove creation of nonces and related index/lowercasing.
  • App logic

    • Controllers (src/controllers.ts):
      • /nonce/:vault GET now reads from vaults.nonce.
      • /nonce/:vault POST now updates vaults.nonce; returns 404 if the vault doesn’t exist.
      • Metrics vault count now uses SELECT COUNT(*) FROM vaults.
    • Proposer (src/proposer.ts):
      • Reads nonce from vaults.nonce.
      • Updates vaults.nonce only; no row creation. Logs a warning if the vault is missing.
    • Vault utilities (src/utils/vaults.ts):
      • updateVaultControllers rejects empty controller arrays.
      • removeControllerFromVault prevents removing the last controller; reverts the change and throws a descriptive error.
    • DB config (src/config/dbSettings.ts): removed nonces from REQUIRED_TABLES.
  • Setup scripts and tests

    • scripts/shared/db-setup.js: remove nonces table and index; add vaults.nonce with default 0; update drop/verify/log messages; keep balances lowercasing.
    • scripts/setup-db.js and scripts/setup-test-db.js: update help text to reflect vaults storing nonce.
    • test/helpers/testServer.ts: remove nonces from TRUNCATE list.
    • Integration tests (test/integration/vaults.db.test.ts):
      • Update test to assert that removing the last controller is rejected.
      • Add test for nonce default (0) and updating behavior.

Deployment notes

  • Run migrations/4_merge_nonces_into_vaults.sh :
    • Adds vaults.nonce, enforces non-empty controllers, backfills from nonces, then drops the legacy table and index.
  • Ensure no flows attempt to set nonce for non-existent vaults (now enforced with 404 / warning).

@dkuthoore dkuthoore marked this pull request as ready for review October 29, 2025 22:23
@dkuthoore dkuthoore requested a review from pemulis as a code owner October 29, 2025 22:23
@dkuthoore dkuthoore changed the title Dk/nonce vault2 Combine nonces and vaults table Oct 29, 2025
@KagemniKarimu
Copy link
Collaborator

Looks good to me! nice work.

@pemulis pemulis changed the base branch from dk/assignDeposit to main October 30, 2025 06:03
@pemulis pemulis merged commit f3b7bbc into main Oct 30, 2025
2 checks passed
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.

4 participants