Skip to content

[TEST] Spike: initialize cap example#827

Draft
MiguelLZPF wants to merge 2 commits intodevelopmentfrom
spike/initialize_cap_example
Draft

[TEST] Spike: initialize cap example#827
MiguelLZPF wants to merge 2 commits intodevelopmentfrom
spike/initialize_cap_example

Conversation

@MiguelLZPF
Copy link
Contributor

Description

Type of change

  • Bug fix 🐞
  • New feature ✨
  • Breaking change 💥
  • Documentation update 📖
  • Refactor 🔧

Testing

Node version:

  • 20
  • 22
  • 24

Checklist

  • Style Guidelines followed ✅
  • Documentation Updated 📚
  • Linters - No New Warnings ⚠️
  • Local Tests Pass ✅
  • Effective Tests Added ✔️
  • No reduction of Coverage

…ion-gated logic

Implement a comprehensive facet reinitialization framework for the Diamond pattern
that enables safe upgrades and reinitializations with version-based gating.

Core Infrastructure Changes:
- Added FacetVersionsStorageWrapper.sol: Centralized versioned storage for all
  facets using single bytes32 storage slot with mapping(bytes32 => uint64)
- Added _FACET_VERSIONS_STORAGE_POSITION to storagePositions.sol
- Added facetKeys.sol with _CAP_FACET_KEY constant (keccak256('ats.facet.cap'))

Cap Facet Implementation:
- Updated ICap.sol: Added CapInitParams struct with maxSupply and partitionCap
- Updated CapStorageWrapper1.sol: Refactored _initialize_Cap with version logic
  - Fresh deploy (v0 → v1): Runs V1 block, stores data, sets version to 1
  - Migration (initialized=true, v=0): Treats as V1, updates version only
  - Already at V1: Reverts AlreadyAtLatestVersion to prevent re-initialization
  - Extensible for future versions: Infrastructure ready for v2, v3+ blocks
- Updated Cap.sol: Added conditional access control
  - Fresh deploy: Anyone can call (Factory deployment before roles assigned)
  - Upgrade: Requires DEFAULT_ADMIN_ROLE for security
  - Version check in wrapper handles idempotency
- Updated Factory.sol: Uses new CapInitParams struct for initialize_Cap call
- Updated Internals.sol: Updated _initialize_Cap abstract signature

Test Coverage:
- Created facetVersions.test.ts (5 tests): Version tracking, migration, errors
- Created capReinitialization.test.ts (10 tests): Deploy, upgrades, admin checks
- Updated cap.test.ts: Adjusted for new CapInitParams struct
- All 1606 tests passing

Benefits:
- Safe facet upgrades with version tracking
- Backward compatibility through migration handling
- Future-proof initialization infrastructure for additional versions
- Proper separation of concerns between storage and access control

Signed-off-by: Miguel_LZPF <miguel.carpena@io.builders>
Add deinitialize_Cap function to enable rollback from version N to N-1,
undoing storage changes made during version upgrades.

Infrastructure changes:
- Add InvalidRollbackTarget and CannotRollbackBelowMinVersion errors
- Add FacetVersionRolledBack event to FacetVersionsStorageWrapper

Cap facet implementation:
- Add _CAP_MIN_VERSION constant (floor at V1, cannot rollback below)
- Add _deinitialize_Cap internal function with reverse cumulative pattern
- Add deinitialize_Cap external function with DEFAULT_ADMIN_ROLE access
- Register new selector in CapFacetBase getStaticFunctionSelectors

Signed-off-by: Miguel_LZPF <miguel.carpena@io.builders>
@MiguelLZPF MiguelLZPF self-assigned this Feb 3, 2026
@hedera-eng-infrastructure
Copy link

hedera-eng-infrastructure commented Feb 3, 2026

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

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.

2 participants