Skip to content

Commit

Permalink
migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
mraszyk committed Jan 14, 2025
1 parent a5cb5ee commit a52e0b6
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 8 deletions.
8 changes: 4 additions & 4 deletions core/upgrader/impl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::model::{DisasterRecovery, LogEntry};
use crate::model::{DisasterRecovery, DisasterRecoveryV0, LogEntry};
use crate::services::insert_logs;
use crate::upgrade::{
CheckController, Upgrade, Upgrader, WithAuthorization, WithBackground, WithLogs, WithStart,
Expand Down Expand Up @@ -152,10 +152,10 @@ fn post_upgrade() {
// if a principal can be parsed out of memory with OLD_MEMORY_ID_TARGET_CANISTER_ID
// then we need to perform stable memory migration
if let Ok(target_canister) = serde_cbor::from_slice::<Principal>(&target_canister_bytes.0) {
let old_disaster_recovery: StableValue<DisasterRecovery> = StableValue::init(
let old_disaster_recovery: StableValue<DisasterRecoveryV0> = StableValue::init(
old_memory_manager.get(MemoryId::new(OLD_MEMORY_ID_DISASTER_RECOVERY)),
);
let disaster_recovery: DisasterRecovery =
let disaster_recovery: DisasterRecoveryV0 =
old_disaster_recovery.get(&()).unwrap_or_default();

let old_logs: StableBTreeMap<Timestamp, LogEntry, Memory> =
Expand All @@ -168,7 +168,7 @@ fn post_upgrade() {

let state = State {
target_canister,
disaster_recovery,
disaster_recovery: disaster_recovery.into(),
stable_memory_version: STABLE_MEMORY_VERSION,
};
set_state(state);
Expand Down
44 changes: 42 additions & 2 deletions core/upgrader/impl/src/mappers/disaster_recovery.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::model::{
RequestDisasterRecoveryInstallCodeLog, RequestDisasterRecoveryOperationLog,
DisasterRecovery, DisasterRecoveryV0, RequestDisasterRecoveryInstallCodeLog,
RequestDisasterRecoveryOperationLog, StationRecoveryRequest,
StationRecoveryRequestInstallCodeOperation,
StationRecoveryRequestInstallCodeOperationFootprint, StationRecoveryRequestOperation,
StationRecoveryRequestOperationFootprint,
StationRecoveryRequestOperationFootprint, StationRecoveryRequestV0,
};
use orbit_essentials::utils::sha256_hash;

Expand Down Expand Up @@ -78,3 +79,42 @@ impl From<&StationRecoveryRequestOperation> for upgrader_api::StationRecoveryReq
}
}
}

// legacy types

impl From<StationRecoveryRequestV0> for StationRecoveryRequest {
fn from(request: StationRecoveryRequestV0) -> Self {
Self {
user_id: request.user_id,
operation: StationRecoveryRequestOperation::InstallCode(
StationRecoveryRequestInstallCodeOperation {
install_mode: request.install_mode,
wasm_module: request.wasm_module,
wasm_module_extra_chunks: request.wasm_module_extra_chunks,
wasm_sha256: request.wasm_sha256,
arg: request.arg,
arg_sha256: request.arg_sha256,
},
),
submitted_at: request.submitted_at,
}
}
}

impl From<DisasterRecoveryV0> for DisasterRecovery {
fn from(disaster_recovery: DisasterRecoveryV0) -> Self {
Self {
accounts: disaster_recovery.accounts,
multi_asset_accounts: disaster_recovery.multi_asset_accounts,
assets: disaster_recovery.assets,
committee: disaster_recovery.committee,
recovery_requests: disaster_recovery
.recovery_requests
.into_iter()
.map(|request| request.into())
.collect(),
recovery_status: disaster_recovery.recovery_status,
last_recovery_result: disaster_recovery.last_recovery_result,
}
}
}
56 changes: 56 additions & 0 deletions core/upgrader/impl/src/model/disaster_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,62 @@ impl From<DisasterRecovery> for upgrader_api::GetDisasterRecoveryStateResponse {
}
}

// legacy types

#[storable]
#[derive(Clone, Debug)]
pub struct StationRecoveryRequestV0 {
/// The user ID of the station.
pub user_id: UUID,
/// The wasm module to be installed.
#[serde(with = "serde_bytes")]
pub wasm_module: Vec<u8>,
/// Optional extra chunks of the wasm module to be installed.
pub wasm_module_extra_chunks: Option<WasmModuleExtraChunks>,
/// The SHA-256 hash of the wasm module.
pub wasm_sha256: Vec<u8>,
/// The install mode: upgrade or reinstall.
pub install_mode: InstallMode,
/// The install arguments.
#[serde(with = "serde_bytes")]
pub arg: Vec<u8>,
/// The SHA-256 hash of the install arguments.
pub arg_sha256: Vec<u8>,
/// Time in nanoseconds since the UNIX epoch when the request was submitted.
pub submitted_at: Timestamp,
}

#[storable]
#[derive(Clone, Debug)]
pub struct DisasterRecoveryV0 {
pub accounts: Vec<Account>,

#[serde(default)]
pub multi_asset_accounts: Vec<MultiAssetAccount>,
#[serde(default)]
pub assets: Vec<Asset>,

pub committee: Option<DisasterRecoveryCommittee>,

pub recovery_requests: Vec<StationRecoveryRequestV0>,
pub recovery_status: RecoveryStatus,
pub last_recovery_result: Option<RecoveryResult>,
}

impl Default for DisasterRecoveryV0 {
fn default() -> Self {
DisasterRecoveryV0 {
accounts: vec![],
multi_asset_accounts: vec![],
assets: vec![],
committee: None,
recovery_requests: vec![],
recovery_status: RecoveryStatus::Idle,
last_recovery_result: None,
}
}
}

#[cfg(test)]
pub mod tests {
use candid::Principal;
Expand Down
2 changes: 1 addition & 1 deletion core/upgrader/impl/src/model/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl std::fmt::Display for RequestDisasterRecoveryOperationLog {
RequestDisasterRecoveryOperationLog::InstallCode(install_code) => {
write!(
f,
"InstallCode with mode{}, wasm hash {}, and arg hash {}",
"InstallCode with mode {}, wasm hash {}, and arg hash {}",
install_code.install_mode, install_code.wasm_sha256, install_code.arg_sha256
)
}
Expand Down
26 changes: 25 additions & 1 deletion tests/integration/src/upgrader_test_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,30 @@ impl<'a> UpgraderDataGenerator<'a> {
);
let logs =
get_all_upgrader_logs(self.env, &self.upgrader_id, &self.some_committee_member());
assert_eq!(logs, self.logs);
assert_eq!(logs.len(), self.logs.len());
for (i, log) in logs.iter().enumerate() {
assert_eq!(log.time, self.logs[i].time);
assert_eq!(log.entry_type, self.logs[i].entry_type);
// we made a breaking change to the log message format
if log.message != self.logs[i].message {
assert!(
log.message
.contains("requested disaster recovery with wasm hash")
|| log
.message
.contains("Disaster recovery successfully initiated to")
);
assert!(
self.logs[i]
.message
.contains("requested disaster recovery with operation")
|| self.logs[i]
.message
.contains("Disaster recovery successfully initiated with operation")
);
} else {
assert_eq!(log.data_json, self.logs[i].data_json);
}
}
}
}

0 comments on commit a52e0b6

Please sign in to comment.