@@ -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 }
0 commit comments