feat(migration): add dry-run mode and rollback/recovery safeguards - #21
Merged
Meshmulla merged 1 commit intoJul 24, 2026
Conversation
migration.rs and version_migration_helper.rs could validate a forward-only version bump, but had no way to preview a migration without mutating state, no guard against skipping a major version, and no way to recover if a migration got stuck mid-flight - begin_migration even had its target version marked as reserved/unused rather than actually validated (stellar-kracken#11). - validate_upgrade now takes a `force: bool` and rejects both downgrades and major-version skips (e.g. 1.x -> 3.x) unless forced, matching the "migrate sequentially" rule already documented in VERSION_MIGRATION_POLICY.md. Applied to both migration.rs's MigrationHelper and version_migration_helper.rs's EnhancedMigrationHelper for consistency between the two. - begin_migration now actually calls validate_upgrade against the current version (closing the TODO that was already in the code), so a bad target is rejected up front instead of only at complete_migration. - New dry_run_migration: runs the same authorization, mutual-exclusion, and version-policy checks begin_migration would, plus caller-supplied storage-layout errors/warnings, and returns a DryRunReport with every problem found - no storage writes. - New cancel_migration: clears a stuck in-progress flag left by a begin_migration that never reached complete_migration, replacing the previous "manually clear if system crashed" non-solution. - VERSION_MIGRATION_POLICY.md updated throughout (rules, API reference, error list, events, troubleshooting, and a new "Recovering From a Partial Migration" section) so the documented policy matches what the code now enforces. Tests: soroban/tests/version_migration_tests.rs covers a valid migration, version-skip and downgrade rejected (and allowed when forced), a dry-run that reports issues without mutating state, and cancel_migration unsticking a stuck migration. migration.rs gains its own test module (previously untested) covering the same forward/downgrade/skip/force cases plus its version/history storage helpers. Closes stellar-kracken#11
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
How i resolved this issue
Contract upgrades are one of the riskiest operations this workspace performs.
migration.rsandversion_migration_helper.rscould validate that a version bump was forward-only, but there was no way to preview a migration without mutating state, no guard against skipping an entire major version, and no way to recover if a migration got stuck mid-flight.begin_migrationeven had its target-version parameter marked with// Reserved for wiring into validate_upgrade in a future change— it accepted a target version but never actually checked it (#11).Approach
validate_upgradegains aforce: boolparameter and now rejects both downgrades and version skips (jumping more than one MAJOR version, e.g.1.x -> 3.x) unlessforce = true. This directly enforces the "migrate sequentially" / "skip versions not recommended" rule that was already written inVERSION_MIGRATION_POLICY.mdbut not enforced in code. Applied to bothmigration.rs's simplerMigrationHelperandversion_migration_helper.rs's fullerEnhancedMigrationHelper, so the two files agree.begin_migrationnow actually validates its target version by callingvalidate_upgradeagainst the current stored version — closing the literal TODO comment that was in the code. A bad target is now rejected up front instead of only being caught later atcomplete_migration.dry_run_migration: runs the same authorization, mutual-exclusion, and version-policy checksbegin_migrationwould (plus any caller-supplied storage-layouterrors/warnings, mirroring the existingvalidate_stateshape), and returns aDryRunReportlisting every problem found — without writing anything to storage.cancel_migration: the actual recovery path when a migration is begun but never completes (the off-chain migration logic fails or crashes betweenbegin_migrationandcomplete_migration). Clears the stuck mutual-exclusion flag so a fresh migration can proceed. This replaces the previous "may need to manually clear flag if system crashed" non-solution in the docs with real, tested code.complete_migrationgains a matchingforce: boolso a migration that was legitimately begun as a forced downgrade/skip isn't rejected again at completion.VERSION_MIGRATION_POLICY.mdupdated throughout: migration rules, the API reference for every changed/new function, theMigrationErrorlist, the events list, the troubleshooting section, and a new "Recovering From a Partial Migration" walkthrough — so the documented policy matches what the code now enforces, per the issue's explicit ask.Scope note:
migration.rs'sMigrationHelperisn't wired into any contract entry point and had zero test coverage before this change, so I added a small test module for it (forward/downgrade/skip/force cases plus its version/history storage helpers) rather than leaving the two files' behavior inconsistent.How this was tested
soroban/tests/version_migration_tests.rs: updated all existing call sites for the new signatures, and added coverage for — a valid forward migration, version-skip rejected and allowed-when-forced (both atvalidate_upgradeandbegin_migrationlevel), downgrade rejected and allowed-when-forced, a dry-run that reports issues without mutating version/history/in-progress state, dry-run reporting an unauthorized caller plus caller-supplied storage errors together, andcancel_migrationunsticking a stuck migration (plus its own unauthorized/nothing-to-cancel error cases).soroban/src/migration.rs: new inline test module covering the same forward/downgrade/skip/force matrix plus version get/set and history round-trips.cargo fmt --all -- --check— passcargo clippy --all-targets --all-features -- -D warnings— passcargo build --release --target wasm32-unknown-unknown— passcargo test(full workspace) — 517 lib tests + 30 migration integration tests (up from 506 and 19), 0 failedCloses #11