Skip to content

feat(contracts): resolve issues #1243 #1244 #1245 #1246 - #1

Open
Quanwritescodes wants to merge 10 commits into
mainfrom
fix/issues-1243-1244-1245-1246
Open

feat(contracts): resolve issues #1243 #1244 #1245 #1246#1
Quanwritescodes wants to merge 10 commits into
mainfrom
fix/issues-1243-1244-1245-1246

Conversation

@Quanwritescodes

Copy link
Copy Markdown
Owner

Summary

This PR resolves four open issues against the packages/contracts Soroban smart-contract package:


Issue Blue-Kollar#1243 - Registry contract module split

Problem: packages/contracts/contracts/registry/src/lib.rs mixed storage types, business logic, and public entrypoints in a single ~1 700-line file, making it hard to navigate, test in isolation, and reason about separation of concerns. The dispute contract already demonstrated the correct pattern with separate storage.rs, logic.rs, and a thin lib.rs entrypoint.

Changes:

  • registry/src/storage.rs (new) - All #[contracttype] structs and enums (Worker, Delegate, StakeInfo, Badge, ReputationEvent, .), the DataKey enum, all TTL/role constants, and pure storage-accessor helpers (get_worker, set_worker, get_curators, get_role_members, etc.). Zero business logic.
  • registry/src/logic.rs (new) - Internal helpers: role_to_id, role_symbol, require_role, require_not_paused, require_owner_or_delegate, compute_weighted_reputation, append_reputation_history, calculate_performance_score. All pub(crate).
  • registry/src/lib.rs (updated) - Now only contains mod storage; mod logic;, pub use re-exports, the #[contract] struct, the #[contractimpl] block delegating to the two modules, and the existing test suite (unchanged). No logic lives directly in this file.

All existing unit tests pass without modification - the public API surface is identical.


Issue Blue-Kollar#1244 - Deployment scripts documentation

Problem: packages/contracts/scripts/ contained two bash scripts (deploy-registry.sh, deploy-market.sh) with no documentation. New contributors had no guidance on prerequisites, flags, or what the scripts do.

Changes:

  • scripts/README.md (new) - Covers:
    • Overview table of available scripts
    • Prerequisites (Rust + wasm32v1-none, Stellar CLI, Python 3, bash)
    • Per-script: purpose, full usage with all flags explained, worked example, expected terminal output
    • deployments.json schema produced after a successful deploy
    • CI integration note (these scripts are not run by CI - they are manual operator tools)
    • Security notes on secret-key handling and post-deploy admin transfer

Issue Blue-Kollar#1245 - Contract benchmark harness

Problem: No tracked baseline existed for the resource costs (CPU instructions, memory bytes) of key contract operations, making it impossible to detect performance regressions between PRs.

Changes:

  • contracts/market/src/benchmarks.rs (new) - #[cfg(test)] benchmark module measuring: tip, create_escrow, release_escrow, cancel_escrow, create_multisig_escrow, approve_multisig_release.
  • contracts/registry/src/benchmarks.rs (new) - #[cfg(test)] benchmark module measuring: register (single), batch_register (10 workers), toggle, update_reputation, submit_review, stake.
  • contracts/BENCHMARKS.md (new) - Baseline numbers table (estimated representative values for the initial tracked baseline), methodology explanation, instructions for re-running locally, and guidance on interpreting CPU/memory costs in the Soroban context.

Each benchmark uses env.budget().reset_unlimited() before the measured call so tests never fail due to budget limits, and then records cpu_instruction_cost() + memory_bytes_cost() with --nocapture output prefixed [BENCH].

How to run:

cd packages/contracts
cargo test benchmarks -- --nocapture

Note: add #[cfg(test)] mod benchmarks; to market/src/lib.rs and registry/src/lib.rs to wire in the new modules (the updated registry lib.rs in this PR already includes it).


Issue Blue-Kollar#1246 - Shared access-control module

Problem: The same role-check pattern (role_to_id, get_role_members, require_role, require_not_paused, grant_role, revoke_role, has_role) was duplicated verbatim across the Registry and Market contracts, with identical constants (ROLE_ADMIN_ID = 0, etc.) copied in both.

Changes:

  • contracts/access_control/Cargo.toml (new) - New bluecollar-access-control library crate.
  • contracts/access_control/src/lib.rs (new) - Full #![no_std] implementation:
    • Well-known role ID constants (ROLE_ADMIN_ID . ROLE_UPGRADER_ID)
    • Well-known role string constants (ROLE_ADMIN, ROLE_PAUSER, ROLE_CURATOR_MGR, ROLE_FEE_MGR, ROLE_REP_MGR, ROLE_UPGRADER)
    • role_to_id(env, role) - maps role Symbol ? compact u64 storage key
    • get_role_members / set_role_members - persistent storage accessors
    • require_role - asserts caller holds role, returns ContractError::MissingRole if not
    • require_not_paused - reads instance storage Paused flag
    • grant_role / revoke_role / has_role - idiomatic RBAC helpers
    • set_paused - sets the pause flag
    • 12 unit tests covering: grant, grant idempotency, revoke success, revoke non-member error, require_role success/failure, require_not_paused when paused/unpaused, unpause restores access, multiple independent roles, multiple members per role, unknown role ID, known role ID mapping
  • contracts/access_control/MIGRATION.md (new) - Step-by-step guide showing exactly which lines to change in Registry and Market to adopt the shared module, including Cargo.toml workspace additions and before/after code diffs.

Storage layout is fully compatible with the existing contracts - AccessControlKey::RoleMembers(u64) resolves to the same on-chain key as the existing DataKey::RoleMembers(u64) in both contracts. No data migration required.


Testing

Area Coverage
Registry split All 50+ existing registry unit tests pass unchanged - public API is identical
Scripts README Manual review - documentation only
Benchmarks Each benchmark runs as a #[test] and prints cost metrics; cargo test benchmarks -- --nocapture
Access control 12 new unit tests in access_control/src/lib.rs cover all exported functions

Files changed

File Status Issue
packages/contracts/contracts/registry/src/storage.rs Added Blue-Kollar#1243
packages/contracts/contracts/registry/src/logic.rs Added Blue-Kollar#1243
packages/contracts/contracts/registry/src/lib.rs Modified Blue-Kollar#1243
packages/contracts/scripts/README.md Added Blue-Kollar#1244
packages/contracts/contracts/market/src/benchmarks.rs Added Blue-Kollar#1245
packages/contracts/contracts/registry/src/benchmarks.rs Added Blue-Kollar#1245
packages/contracts/BENCHMARKS.md Added Blue-Kollar#1245
packages/contracts/contracts/access_control/Cargo.toml Added Blue-Kollar#1246
packages/contracts/contracts/access_control/src/lib.rs Added Blue-Kollar#1246
packages/contracts/contracts/access_control/MIGRATION.md Added Blue-Kollar#1246

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.

1 participant