Skip to content

feat(contract): add authorized upgrade path with storage-layout versioning and migration (#41) - #64

Closed
huangji6693-max wants to merge 1 commit into
trustflow-protocol:mainfrom
huangji6693-max:feat/upgradeable-contract-storage-migration-41
Closed

huangji6693-max wants to merge 1 commit into
trustflow-protocol:mainfrom
huangji6693-max:feat/upgradeable-contract-storage-migration-41

Conversation

@huangji6693-max

Copy link
Copy Markdown

Summary

Adds an admin-authorized contract upgrade path (upgrade) alongside explicit storage-layout schema versioning (DataKey::StorageVersion) and a state migration routine (migrate) that safely transforms legacy v1 escrow and dispute records to the current v2 schema.

Closes #41

Changes

  • Storage-Layout Versioning:
    • Added STORAGE_VERSION_V1 = 1 and CURRENT_STORAGE_VERSION = 2 constants and DataKey::StorageVersion (set on initialize, queried via get_storage_version).
  • Authorized In-Place WASM Upgrade:
    • Added upgrade(env, caller, new_wasm_hash) entrypoint restricted to the contract Admin, invoking env.deployer().update_current_contract_wasm(new_wasm_hash) and emitting a ContractUpgraded event ((symbol_short!("contract"), symbol_short!("upgraded"))).
  • State Migration Routine (migrate):
    • Added legacy schema types (MilestoneV1, EscrowRecordV1, DisputeRecordV1) and DataKey::LegacyEscrowV1(u64) / DataKey::LegacyDisputeV1(u64).
    • Implemented admin-only migrate(env, caller) -> Result<u32, TrustFlowError> that guards against redundant runs (TrustFlowError::AlreadyAtLatestVersion), backfills missing FeeBps/Treasury defaults, transforms v1 escrows (MilestoneV1 -> Milestone with release_time: 0) and missing EscrowFeeConfig snapshots, transforms v1 disputes with commit/reveal deadlines, bumps persistent TTLs, updates DataKey::StorageVersion to CURRENT_STORAGE_VERSION, and emits a StorageMigrated event ((symbol_short!("storage"), symbol_short!("migrated"))).
  • Unit Tests:
    • Added tests covering version initialization, unauthorized upgrade/migrate rejection, AlreadyAtLatestVersion guard, and end-to-end v1 -> v2 escrow + dispute migration followed by milestone tranche release.

Verification

  • [MEASURED] cargo test --package trustflow: 68 passed; 0 failed
  • [MEASURED] cargo build --target wasm32-unknown-unknown --release --package trustflow: succeeded
  • [MEASURED] cargo fmt --all --check & cargo clippy --all-features --workspace --lib: passed

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.

Upgradeable Contract with Storage-Layout Versioning & Migration

2 participants