2
2
pub mod asset_certification;
3
3
pub mod evidence;
4
4
pub mod state_machine;
5
+ pub mod state_trait;
5
6
pub mod types;
6
7
mod url_decode;
7
8
@@ -15,6 +16,7 @@ use crate::{
15
16
StreamingCallbackToken ,
16
17
} ,
17
18
state_machine:: { AssetDetails , CertifiedTree , EncodedAsset , State } ,
19
+ state_trait:: AssetCanisterStateTrait ,
18
20
types:: * ,
19
21
} ;
20
22
use asset_certification:: types:: { certification:: AssetKey , rc_bytes:: RcBytes } ;
@@ -29,7 +31,7 @@ use std::cell::RefCell;
29
31
pub static SUPPORTED_CERTIFICATE_VERSIONS : [ u8 ; 3 ] = * b"1,2" ;
30
32
31
33
thread_local ! {
32
- static STATE : RefCell <State > = RefCell :: new( State :: default ( ) ) ;
34
+ static STATE : RefCell <Box < dyn AssetCanisterStateTrait >> = RefCell :: new( Box :: new ( State :: default ( ) ) ) ;
33
35
}
34
36
35
37
#[ query]
@@ -422,6 +424,14 @@ fn is_controller() -> Result<(), String> {
422
424
}
423
425
}
424
426
427
+ pub fn init_with_state ( args : Option < AssetCanisterArgs > , state : Box < dyn AssetCanisterStateTrait > ) {
428
+ STATE . with ( |s| {
429
+ * s. borrow_mut ( ) = state;
430
+ } ) ;
431
+
432
+ init ( args) ;
433
+ }
434
+
425
435
pub fn init ( args : Option < AssetCanisterArgs > ) {
426
436
STATE . with ( |s| {
427
437
let mut s = s. borrow_mut ( ) ;
@@ -443,7 +453,7 @@ pub fn init(args: Option<AssetCanisterArgs>) {
443
453
}
444
454
445
455
pub fn pre_upgrade ( ) -> StableState {
446
- STATE . with ( |s| s. take ( ) . into ( ) )
456
+ STATE . with ( |s| s. borrow ( ) . as_ref ( ) . to_stable_state ( ) )
447
457
}
448
458
449
459
pub fn post_upgrade ( stable_state : StableState , args : Option < AssetCanisterArgs > ) {
@@ -453,7 +463,7 @@ pub fn post_upgrade(stable_state: StableState, args: Option<AssetCanisterArgs>)
453
463
} ) ;
454
464
455
465
STATE . with ( |s| {
456
- * s. borrow_mut ( ) = State :: from ( stable_state) ;
466
+ * s. borrow_mut ( ) = Box :: new ( State :: from ( stable_state) ) ;
457
467
set_certified_data ( & s. borrow ( ) . root_hash ( ) ) ;
458
468
if let Some ( set_permissions) = set_permissions {
459
469
s. borrow_mut ( ) . set_permissions ( set_permissions) ;
0 commit comments