Skip to content

Commit 48b8ddc

Browse files
committed
[ic-certificed-assets] Allow custom asset management
1 parent 5084480 commit 48b8ddc

File tree

4 files changed

+405
-279
lines changed

4 files changed

+405
-279
lines changed

src/canisters/frontend/ic-certified-assets/src/lib.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
pub mod asset_certification;
33
pub mod evidence;
44
pub mod state_machine;
5+
pub mod state_trait;
56
pub mod types;
67
mod url_decode;
78

@@ -15,6 +16,7 @@ use crate::{
1516
StreamingCallbackToken,
1617
},
1718
state_machine::{AssetDetails, CertifiedTree, EncodedAsset, State},
19+
state_trait::AssetCanisterStateTrait,
1820
types::*,
1921
};
2022
use asset_certification::types::{certification::AssetKey, rc_bytes::RcBytes};
@@ -29,7 +31,7 @@ use std::cell::RefCell;
2931
pub static SUPPORTED_CERTIFICATE_VERSIONS: [u8; 3] = *b"1,2";
3032

3133
thread_local! {
32-
static STATE: RefCell<State> = RefCell::new(State::default());
34+
static STATE: RefCell<Box< dyn AssetCanisterStateTrait>> = RefCell::new(Box::new(State::default()));
3335
}
3436

3537
#[query]
@@ -422,6 +424,14 @@ fn is_controller() -> Result<(), String> {
422424
}
423425
}
424426

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+
425435
pub fn init(args: Option<AssetCanisterArgs>) {
426436
STATE.with(|s| {
427437
let mut s = s.borrow_mut();
@@ -443,7 +453,7 @@ pub fn init(args: Option<AssetCanisterArgs>) {
443453
}
444454

445455
pub fn pre_upgrade() -> StableState {
446-
STATE.with(|s| s.take().into())
456+
STATE.with(|s| s.borrow().as_ref().to_stable_state())
447457
}
448458

449459
pub fn post_upgrade(stable_state: StableState, args: Option<AssetCanisterArgs>) {
@@ -453,7 +463,7 @@ pub fn post_upgrade(stable_state: StableState, args: Option<AssetCanisterArgs>)
453463
});
454464

455465
STATE.with(|s| {
456-
*s.borrow_mut() = State::from(stable_state);
466+
*s.borrow_mut() = Box::new(State::from(stable_state));
457467
set_certified_data(&s.borrow().root_hash());
458468
if let Some(set_permissions) = set_permissions {
459469
s.borrow_mut().set_permissions(set_permissions);

0 commit comments

Comments
 (0)