Skip to content

Latest commit

 

History

History
67 lines (53 loc) · 1.93 KB

File metadata and controls

67 lines (53 loc) · 1.93 KB

Contract Versioning Implementation

Summary

Added a basic versioning mechanism to the AidEscrow contract to prepare for future upgrades.

Changes Made

1. Storage Key Addition (src/lib.rs)

  • Added KEY_VERSION constant for storing version in instance storage

2. Initialize Function Update (src/lib.rs)

  • Modified init() to set version 1 on contract initialization

3. New Functions (src/lib.rs)

  • get_version(): Returns the current contract version (defaults to 0 if not set)
  • migrate(env: Env, new_version: u32): Admin-only function to migrate between versions
    • Requires admin authentication
    • Structured with match statement for version-specific migrations
    • Currently no-op but ready for future data transformations

4. Tests (tests/versioning.rs)

Three comprehensive tests added:

  • test_version_set_on_init: Verifies version is set to 1 on initialization
  • test_migrate_admin_only: Confirms only admin can call migrate function
  • test_migrate_version_progression: Tests version can be incremented through migrations

Test Results

All tests pass (19 total):

  • 5 core flow tests
  • 11 integration tests
  • 3 versioning tests

Build Status

✅ Contract compiles successfully for wasm32-unknown-unknown target ✅ All existing functionality preserved ✅ No breaking changes

Usage Example

// Initialize contract with version 1
client.init(&admin);
assert_eq!(client.get_version(), 1);

// Migrate to version 2 (admin only)
client.migrate(&2);
assert_eq!(client.get_version(), 2);

Future Migration Pattern

When adding version-specific migrations, update the match statement in migrate():

match (current_version, new_version) {
    (1, 2) => {
        // Add v1 -> v2 migration logic here
        // e.g., transform storage structures, add new keys, etc.
    }
    (2, 3) => {
        // Add v2 -> v3 migration logic here
    }
    _ => {
        // No-op for other transitions
    }
}