Skip to content

refactor(contracts): standardize error codes via shared trivela-contract-errors crate - #1395

Merged
joelpeace48-cell merged 3 commits into
FinesseStudioLab:mainfrom
gideononiru:refactor/contract-error-codes-1192
Sep 25, 2026
Merged

joelpeace48-cell merged 3 commits into
FinesseStudioLab:mainfrom
gideononiru:refactor/contract-error-codes-1192

Conversation

@gideononiru

@gideononiru gideononiru commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a shared trivela-contract-errors crate with structured numeric error-code ranges, and makes every workspace contract conform to it. After this, an Error(Contract, #N) identifies both the failing contract and the reason without extra context.

Error-code ranges

Range Contract Status
1–99 rewards unchanged (1–64 in use)
100–199 campaign unchanged (100–126 in use)
200–299 nullifier registry renumbered from 1–3
300–399 badges reserved (crate excluded from workspace)
400–499 voting reserved (crate excluded from workspace)
500–899 future contracts reserved
900–999 shared / cross-contract reserved

Changes

  1. New crate contracts/errors (trivela-contract-errors, no_std, no Soroban dependency), added to the workspace:
    • range constants plus a ContractDomain enum with range(), contains() and name();
    • domain_of(code): a const fn that resolves which contract owns a code;
    • assert_codes_in_domain(domain, codes): a test helper;
    • its own tests: ranges are ordered and disjoint, lookups hit the boundaries correctly, and out-of-range codes panic.
  2. Nullifier registry: AlreadySpent, Unauthorized and NotInitialized move from 1/2/3 to 200/201/202. The old values collided with the rewards contract's Overflow, InsufficientBalance and Unauthorized (1/2/3), so an Error(Contract, #2) was ambiguous. No backend or frontend code maps nullifier codes. frontend/src/lib/contractErrors.js and the backend mappings only cover rewards and campaign, so nothing off-chain changes.
  3. Conformance tests: error_codes_test.rs in rewards, campaign and nullifiers lists every Error variant and asserts each one is in its range. They're generated from the enums: 69, 27 and 3 variants. The crate is a dev-dependency only, so contract WASM size and ABI are unaffected.
  4. Cargo.lock records the new crate (additive only).

Backward compatibility

  • Rewards and campaign codes are unchanged. They're deployed and mapped client-side, and renumbering them would break existing clients.
  • Nullifier codes change. If a nullifier registry is already deployed anywhere, clients matching on the raw numbers 1–3 need updating. As far as I can find, none do in this repo.
  • rewards::merkle::MerkleVerifyError (1–2) is an internal Result type used in tests, not a contract-returned error, so it's left as is.

Not verified (raised early on request)

no_std crate reserving numeric error-code ranges per contract
(rewards 1-99, campaign 100-199, nullifiers 200-299, badges/voting
reserved, shared 900-999) with domain_of() lookup and an
assert_codes_in_domain() test helper. Added to the workspace.

Refs FinesseStudioLab#1192
…anges

Nullifier registry codes 1-3 collided with rewards 1-3; renumber to
200-202. Rewards and campaign codes are unchanged (already deployed and
mapped client-side). Each contract gets an error_codes_test asserting
every Error variant stays inside its reserved range.

Closes FinesseStudioLab#1192
@drips-wave

drips-wave Bot commented Sep 25, 2026

Copy link
Copy Markdown

@gideononiru Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@joelpeace48-cell
joelpeace48-cell merged commit d0f7dec into FinesseStudioLab:main Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment