Skip to content

[Governance] Implement Upgrade Framework (Issue #16) and Multi-Sig Enhancements (Issue #18) - #35

Closed
Jokay1997 wants to merge 15 commits into
EquipChain:mainfrom
Jokay1997:governance/upgrade-framework-multisig-16-18
Closed

[Governance] Implement Upgrade Framework (Issue #16) and Multi-Sig Enhancements (Issue #18)#35
Jokay1997 wants to merge 15 commits into
EquipChain:mainfrom
Jokay1997:governance/upgrade-framework-multisig-16-18

Conversation

@Jokay1997

Copy link
Copy Markdown

Summary

This PR implements two governance-related issues assigned to @Jokay1997:

Closes #16 - Formal Upgrade Framework with Version Tracking, Migration Hooks, and Rollback
Closes #18 - Multi-Sig Proposal Expiry, Quorum, Vote Weighting, and Nonce Sync Safety


Issue #16: Upgrade Framework

Changes Made

  • New module: contracts/utility_contracts/src/upgrade_framework.rs
  • New DataKey variants: ContractVersion, LastUpgradeLedger, VersionInfo, VersionCount, StorageSchemaVersion, RollbackPoint, RollbackCount, PreviousWasmHash
  • New ContractError variants: UpgradeDelayNotElapsed, RollbackPointNotFound, RollbackAlreadyConsumed, MigrationFailed

Features Implemented

  1. Version Tracking - Stores current contract version and last upgrade ledger timestamp
  2. Version History - Maintains history of all deployed versions with WASM hashes
  3. Migration Registry - Static array of MigrationEntry mapping from_version → migrate_fn
  4. Two-Phase Upgrade - propose_upgrade → approve → execute_upgrade_proposal with multi-sig
  5. Upgrade Delay - MIN_UPGRADE_INTERVAL (72 hours) enforced between non-emergency upgrades
  6. Emergency Rollback - rollback_to_version() with multi-sig authorization and require_auth() verification
  7. Cancel Proposal - cancel_upgrade_proposal() for proposer to retract flawed proposals
  8. Storage Schema Versioning - Per-key schema version tracking
  9. Migration Execution - execute_migrations() iterates through migration registry

Issue #18: Multi-Sig Enhancements

Changes Made

  • New module: contracts/utility_contracts/src/governance.rs
  • New DataKey variants: GovernanceConfig, GovernanceProposal, GovernanceProposalCounter, GovernanceVote, PendingNonceLock
  • Modified: contracts/utility_contracts/src/nonce_sync.rs (pending nonce lock)
  • New ContractError variants: QuorumNotSatisfied, GovernanceDisabled, NonceLockActive

Features Implemented

  1. Proposal Expiry - Proposals expire after configurable duration; votes rejected on expired proposals
  2. Quorum Mechanism - Absolute min_quorum voter count required before execution regardless of approval
  3. Vote Weighting - VoteWeightProvider supports token-based, reputation-based, or 1-person-1-vote
  4. Timelock - Configurable delay after approval threshold is reached before execution
  5. Cancel Proposal - Only proposer can cancel; emits ProposalCancelledEvent with reason
  6. Proposal Status Query - get_proposal_status() returns ProposalStatus enum (Pending, Active, Approved, Ready, Executed, Expired, Cancelled)
  7. Nonce Sync Race Condition Fix - Added PendingNonceLock in nonce_sync.rs that prevents concurrent nonce updates for the same device

Verification

  • cargo check --workspace passes with zero errors
  • Pre-existing SLA config pattern match issue fixed in lib.rs
  • Pre-existing utility_contract::Client unresolved import commented out

Files Changed

File Changes
contracts/utility_contracts/src/upgrade_framework.rs New - 540 lines
contracts/utility_contracts/src/governance.rs New - 620 lines
contracts/utility_contracts/src/nonce_sync.rs Modified - pending nonce lock added
contracts/utility_contracts/src/lib.rs DataKey, ContractError, module declarations + 2 pre-existing fixes

…d Multi-Sig Enhancements (Issue EquipChain#18)

Issue EquipChain#16: Formal Upgrade Framework with Version Tracking, Migration Hooks, and Rollback
- Add upgrade_framework.rs module with contract version tracking, version history
- Implement migration registry with static hook entries
- Add two-phase upgrade with multi-sig approval and timelock
- Add emergency rollback with multi-sig authorization
- Enforce MIN_UPGRADE_INTERVAL (72h) between non-emergency upgrades
- Add cancel_upgrade_proposal for proposer retraction
- Add storage schema versioning per data key

Issue EquipChain#18: Multi-Sig Proposal Expiry, Quorum, Vote Weighting, and Nonce Sync Safety
- Add governance.rs module with full proposal lifecycle (create, vote, execute)
- Implement proposal expiry with configurable duration
- Add absolute quorum (min_voters) requirement
- Add vote weighting support (token-based, reputation, 1-person-1-vote)
- Add configurable timelock after approval threshold is reached
- Add cancel_proposal with reason (proposer only)
- Add proposal status query returning detailed status enum
- Fix nonce_sync.rs race condition with pending_nonce lock
@KarenZita01

Copy link
Copy Markdown
Member

Workflow tests failed. Fix it

- Run rustfmt on governance.rs, lib.rs, upgrade_framework.rs
- Fix all formatting diffs detected by cargo fmt --check
- Ensures CI formatting check passes
- Restore pub use utility_contract::Client as UtilityContractClient
- This type is used by 114+ test files across the codebase
- Removing it caused 253 cascading compilation errors in CI
- Change pub use utility_contract::Client to pub use crate::utility_contract::Client
- This ensures proper module resolution with the #[contract] macro expansion
- Add #[cfg(any(test, feature = testutils))] to pub use
- WASM target compilation no longer tries to resolve the macro-generated module
- Test code still gets the client type via cfg(test) activation
- WASM target: stub struct (for clippy library checks only)
- Native target: real import from macro-generated module (for tests)
- Tests always compile natively, so they get the real client type
…macro provides it

The #[contract] macro on UtilityContract already generates the UtilityContractClient
type at the crate root. Both the pub use re-export AND the cfg-gated stub struct
were creating duplicate definitions. Removing both resolves the CI compilation failure.
- Collapse if meter.sla_config_set { if condition {...} } into single if with &&
- Fixes CI clippy -D warnings failure
- buffer_tests.rs has explicit use crate::{...} imports and was missing UtilityContractClient
- The #[contract] macro generates this type at the crate root
- Fixes CI cargo test compilation failure for utility_contracts
- nonce_sync_tests.rs: Add Vec, String imports, fix create_test_heartbeat visibility
- tariff_oracle_tests.rs: Add create_test_schedule cross-module import
- temporary_storage_tests.rs: Add Vec import
- Restore #[cfg(any(test, feature = testutils))] pub use crate::utility_contract::Client as UtilityContractClient
- This was originally in the codebase but was accidentally removed
- cfg gate prevents E0428 duplicate on WASM (where #[contract] macro also generates it)
- Combined with import fixes in 3 other test files, CI should pass
…lient re-export

- Changed #[cfg(any(test, feature = "testutils"))] to #[cfg(test)]
- The testutils feature is not defined in Cargo.toml, causing WASM clippy failure
- #[cfg(test)] is sufficient: active only for native test compilation
…ient

- Original code used
- Adding  prefix broke the path resolution
- Keep #[cfg(test)] gate to prevent E0428 on WASM
- Original code on main had
- The E0428 WASM error was from a removed WASM stub, not this line
- No cfg gate needed - matches main branch exactly
diff --git a/contracts/utility_contracts/src/lib.rs b/contracts/utility_contracts/src/lib.rs
…irectly

The  +  macros on UtilityContract generate
UtilityContractClient directly at the crate root for all targets. The
pub use re-export from  module path doesn't resolve
on native CI compilation because the macro doesn't always generate a
separate module. buffer_tests.rs already has an explicit import.
@Jokay1997

Copy link
Copy Markdown
Author

Split into separate PRs for each issue: #36 for Issue #16 (Upgrade Framework) and #37 for Issue #18 (Multi-Sig Enhancements).

@Jokay1997 Jokay1997 closed this Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants