Skip to content

[Governance] Formal Upgrade Framework with Version Tracking, Migration Hooks, and Rollback (Issue #16) - #36

Open
Jokay1997 wants to merge 6 commits into
EquipChain:mainfrom
Jokay1997:governance/upgrade-framework-16
Open

[Governance] Formal Upgrade Framework with Version Tracking, Migration Hooks, and Rollback (Issue #16)#36
Jokay1997 wants to merge 6 commits into
EquipChain:mainfrom
Jokay1997:governance/upgrade-framework-16

Conversation

@Jokay1997

@Jokay1997 Jokay1997 commented Jul 29, 2026

Copy link
Copy Markdown

Summary

Closes #16.

Implements a Formal Upgrade Framework for EquipChain smart contracts, providing version tracking, sequential migration hooks, two-phase upgrade approval, storage schema versioning, and emergency rollback. This replaces ad-hoc contract upgrades with a governed, auditable process enforced entirely on-chain.


Files Changed

File Type Description
\contracts/utility_contracts/src/upgrade_framework.rs\ ✨ New Full upgrade framework (version tracking, migrations, two-phase upgrade, rollback, schema versioning)
\contracts/utility_contracts/src/lib.rs\ 🔧 Modified Module declaration, new \DataKey\ variants, new \ContractError\ variants
\contracts/utility_contracts/src/nonce_sync_tests.rs\ 🔧 Modified Fixed missing \Vec/\String\ imports for CI
\contracts/utility_contracts/src/tariff_oracle_tests.rs\ 🔧 Modified Pre-existing CI fix
\contracts/utility_contracts/src/temporary_storage_tests.rs\ 🔧 Modified Pre-existing CI fix

Implementation Details

upgrade_framework.rs — New Module

Version Tracking

  • \ContractVersion\ stores the current (major, minor, patch)\ tuple in instance storage
  • \LastUpgradeLedger\ records the ledger sequence of the most recent upgrade
  • \VersionInfo(u32)\ stores per-version metadata (WASM hash, timestamp, executor) for full audit trail
  • \VersionCount\ tracks the total number of upgrades applied (append-only)

Migration Registry

  • A static \MIGRATIONS\ array maps \ rom_version → migration_fn\
  • On upgrade, migrations execute sequentially from the current version to the target version
  • Each migration function receives the \Env\ and can read/write storage to reshape data
  • A failed migration returns \MigrationFailed (error 122)\ and the upgrade is aborted without advancing the version

Two-Phase Upgrade
propose_upgrade → (multi-sig approval) → execute_upgrade

  • \propose_upgrade\ stores the target WASM hash and opens an approval window
  • Multi-sig signers approve via the existing approval mechanism
  • \execute_upgrade\ can only be called after the quorum is reached and the timelock has elapsed
  • \cancel_upgrade_proposal\ allows the proposer to retract before execution

Upgrade Delay Enforcement

  • \MIN_UPGRADE_INTERVAL\ (72 hours) enforced between consecutive non-emergency upgrades
  • \UpgradeDelayNotElapsed (error 119)\ returned if the interval has not passed
  • Emergency upgrades bypass the interval but require multi-sig authorization

Emergency Rollback


  • ollback_to_version(n)\ restores a previous WASM hash from \PreviousWasmHash(u32)\ storage
  • Each rollback point is single-use; a consumed rollback returns \RollbackAlreadyConsumed (error 121)\
  • A missing rollback point returns \RollbackPointNotFound (error 120)\
  • Emergency rollback still requires multi-sig authorization

Storage Schema Versioning

  • \StorageSchemaVersion(BytesN<32>)\ tracks the schema version for each \DataKey\ variant
  • Allows migrations to detect and upgrade stale data structures independently per key
  • Guards against version skew when reading storage written by an older contract version

lib.rs — Registry Updates

New \DataKey\ variants:

  • \ContractVersion\
  • \LastUpgradeLedger\
  • \VersionInfo(u32)\
  • \VersionCount\
  • \StorageSchemaVersion(BytesN<32>)\
  • \RollbackPoint(u32)\
  • \RollbackCount\
  • \PreviousWasmHash(u32)\

New \ContractError\ variants:

  • \UpgradeDelayNotElapsed = 119\
  • \RollbackPointNotFound = 120\
  • \RollbackAlreadyConsumed = 121\
  • \MigrationFailed = 122\

Testing

  • All pre-existing CI compilation errors on this branch resolved (missing imports, collapsible \if\ clippy warnings)
  • \cargo check --workspace\ and \cargo test --workspace\ pass

…tion Hooks, and Rollback

Closes EquipChain#16 - Formal Upgrade Framework with Version Tracking, Migration Hooks, and Rollback

- Add upgrade_framework.rs module with contract version tracking
- Implement version history with VersionInfo storage
- Add migration registry with static hook entries for version transitions
- Implement two-phase upgrade: propose, approve, execute with timelock
- Add emergency rollback with rollback point management
- Add storage schema versioning per data key
- Enforce MIN_UPGRADE_INTERVAL (72h) between non-emergency upgrades
- Add cancel_upgrade_proposal for proposer retraction
- Remove  (macro generates type directly)
- Fix collapsible  where sla_config is not Option
- Add missing Vec/String imports to nonce_sync_tests.rs
- Add cross-module  import to property_tests
- Add cross-module  import to tariff_property_tests
- Add missing Vec import to temporary_storage_tests.rs
Resolve cargo fmt --check failures on both CI jobs:
- Collapse env.storage().instance().get(&DataKey::ApprovedTokens) onto one line
  in require_approved_token() (was split across 3 lines, now fits under 100 chars)
- Wrap env.storage().instance() chains in approve_token(), revoke_token()
  starting at env. rather than env.storage() per rustfmt chain-call rules
- Wrap env.storage().instance().set() calls in approve_token() and revoke_token()
- Collapse get_token_info() single-expression body onto one line

All changes are pure formatting; no logic altered.
…elayNotElapsed, RollbackPointNotFound, RollbackAlreadyConsumed, MigrationFailed)

The upgrade_framework.rs module referenced ContractError::MigrationFailed
which was declared in the PR description but not added to the ContractError
enum. Also adds the other three Issue EquipChain#16 error codes:
  UpgradeDelayNotElapsed = 119
  RollbackPointNotFound  = 120
  RollbackAlreadyConsumed = 121
  MigrationFailed         = 122

Fixes cargo clippy --lib failure introduced by this PR.
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.

[Governance] Implement Formal Upgrade Framework with Version Tracking, Migration Hooks, and Rollback

2 participants