Skip to content

Commit 6fd7a51

Browse files
authored
fix migration harness Issue (#64)
* fix migration harness * fix(migration): init missing storage defaults and add regression test; fix test unwrap * fix the unwrap issue.
1 parent 1f2c9e1 commit 6fd7a51

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

apexchainx_calculator/src/lib.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,71 @@ impl SLACalculatorContract {
555555
Ok(())
556556
}
557557

558+
// Initialise any storage keys that may be missing from older schema
559+
// versions. This is intentionally conservative: only set a value when
560+
// the key is absent so migration is idempotent and does not overwrite
561+
// existing state.
562+
fn init_missing_storage_defaults(env: &Env) {
563+
let inst = env.storage().instance();
564+
565+
if !inst.has(&PAUSED_KEY) {
566+
inst.set(&PAUSED_KEY, &false);
567+
}
568+
569+
if !inst.has(&STATS_KEY) {
570+
inst.set(
571+
&STATS_KEY,
572+
&SLAStats {
573+
total_calculations: 0,
574+
total_violations: 0,
575+
total_rewards: 0,
576+
total_penalties: 0,
577+
},
578+
);
579+
}
580+
581+
if !inst.has(&HISTORY_KEY) {
582+
inst.set(&HISTORY_KEY, &Vec::<SLAResult>::new(env));
583+
}
584+
585+
if !inst.has(&CONFIG_KEY) {
586+
let mut configs = Map::<Symbol, SLAConfig>::new(env);
587+
configs.set(
588+
symbol_short!("critical"),
589+
SLAConfig {
590+
threshold_minutes: 15,
591+
penalty_per_minute: 100,
592+
reward_base: 750,
593+
},
594+
);
595+
configs.set(
596+
symbol_short!("high"),
597+
SLAConfig {
598+
threshold_minutes: 30,
599+
penalty_per_minute: 50,
600+
reward_base: 750,
601+
},
602+
);
603+
configs.set(
604+
symbol_short!("medium"),
605+
SLAConfig {
606+
threshold_minutes: 60,
607+
penalty_per_minute: 25,
608+
reward_base: 750,
609+
},
610+
);
611+
configs.set(
612+
symbol_short!("low"),
613+
SLAConfig {
614+
threshold_minutes: 120,
615+
penalty_per_minute: 10,
616+
reward_base: 600,
617+
},
618+
);
619+
inst.set(&CONFIG_KEY, &configs);
620+
}
621+
}
622+
558623
// -------------------------------------------------------------------
559624
// #61 – Storage migration harness
560625
// -------------------------------------------------------------------
@@ -602,6 +667,11 @@ impl SLACalculatorContract {
602667

603668
// v0 → v1: stamp the version; all other fields were set by initialize
604669
if current == 0 {
670+
// Ensure any storage keys that might be missing from older
671+
// deployments are initialised to deterministic defaults before
672+
// we mark the storage version as migrated. This codifies the
673+
// contract: migration arms must initialise newly-added keys.
674+
Self::init_missing_storage_defaults(&env);
605675
env.storage().instance().set(&STORAGE_VERSION_KEY, &1u32);
606676
current = 1;
607677
}

apexchainx_calculator/src/tests.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3288,6 +3288,39 @@ fn test_get_migration_state_after_migrate_shows_no_migration_needed() {
32883288
assert!(!info.needs_migration);
32893289
}
32903290

3291+
#[test]
3292+
fn test_migrate_initialises_missing_fields() {
3293+
// Simulate a frozen older snapshot that lacks keys added in later
3294+
// schemas, then run migrate and verify deterministic defaults are set.
3295+
let env = Env::default();
3296+
let cid = env.register_contract(None, SLACalculatorContract);
3297+
let client = SLACalculatorContractClient::new(&env, &cid);
3298+
let admin = soroban_sdk::Address::generate(&env);
3299+
let op = soroban_sdk::Address::generate(&env);
3300+
client.initialize(&admin, &op);
3301+
3302+
// Remove some keys to emulate an older schema state and force stored
3303+
// version to 0 so migrate will exercise the v0->v1 path.
3304+
env.as_contract(&cid, || {
3305+
env.storage().instance().remove(&CONFIG_KEY);
3306+
env.storage().instance().remove(&STATS_KEY);
3307+
env.storage().instance().set(&STORAGE_VERSION_KEY, &0u32);
3308+
});
3309+
3310+
// Run migration as admin
3311+
client.migrate(&admin);
3312+
3313+
// After migrate the keys should exist again and the stored version
3314+
// should match the binary's STORAGE_VERSION.
3315+
let version = client.get_storage_version();
3316+
assert_eq!(version, STORAGE_VERSION);
3317+
3318+
env.as_contract(&cid, || {
3319+
assert!(env.storage().instance().has(&CONFIG_KEY));
3320+
assert!(env.storage().instance().has(&STATS_KEY));
3321+
});
3322+
}
3323+
32913324
// ============================================================
32923325
// SC-011 – Latest result by outage (issue #131) – additional coverage
32933326
// ============================================================

0 commit comments

Comments
 (0)