Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(upgrader): introduce disaster recovery request operation #461

Merged
merged 7 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions core/upgrader/api/spec.did
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ type SetDisasterRecoveryAccountsAndAssetsInput = record {

// Request to trigger disaster recovery. Requests are stored in the Upgrader
// canister, and when at least `quorum` of the committee members
// agree on the exact module, args, and install mode, the request is processed.
// agree on the exact disaster recovery input, the request is processed.
// Requests older than 1 week will be discarded.
type RequestDisasterRecoveryInput = record {
type RequestDisasterRecoveryInstallCodeInput = record {
// The wasm module to be installed.
module : blob;
// Additional wasm module chunks to append to the wasm module.
Expand All @@ -174,6 +174,9 @@ type RequestDisasterRecoveryInput = record {
// The install mode: Install, Upgrade, or Reinstall.
install_mode : InstallMode;
};
type RequestDisasterRecoveryInput = variant {
InstallCode : RequestDisasterRecoveryInstallCodeInput;
};

type InstallMode = variant {
// Install the wasm module.
Expand Down Expand Up @@ -226,16 +229,25 @@ type GetLogsResult = variant {
Err : Error;
};

// Request to recover the station.
type StationRecoveryRequest = record {
// The requester user id.
user_id : text;
// The sha of the wasm module to be installed.
wasm_sha256 : blob;
type StationRecoveryRequestInstallCodeOperation = record {
// The install mode: Install, Upgrade, or Reinstall.
install_mode : InstallMode;
// The sha of the wasm module to be installed.
wasm_sha256 : blob;
// The argument to be passed to the install function.
arg : blob;
};

type StationRecoveryRequestOperation = variant {
InstallCode : StationRecoveryRequestInstallCodeOperation;
};

// Request to recover the station.
type StationRecoveryRequest = record {
// The requester user id.
user_id : text;
// The disaster recovery operation.
operation : StationRecoveryRequestOperation;
// The request submission timestamp.
submitted_at : text;
};
Expand Down
28 changes: 22 additions & 6 deletions core/upgrader/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub enum InstallMode {
}

#[derive(Clone, Debug, CandidType, Deserialize)]
pub struct RequestDisasterRecoveryInput {
pub struct RequestDisasterRecoveryInstallCodeInput {
#[serde(with = "serde_bytes")]
pub module: Vec<u8>,
pub module_extra_chunks: Option<WasmModuleExtraChunks>,
Expand All @@ -165,6 +165,11 @@ pub struct RequestDisasterRecoveryInput {
pub install_mode: InstallMode,
}

#[derive(Clone, Debug, CandidType, Deserialize)]
pub enum RequestDisasterRecoveryInput {
InstallCode(RequestDisasterRecoveryInstallCodeInput),
}

#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct PaginationInput {
pub offset: Option<u64>,
Expand Down Expand Up @@ -198,15 +203,26 @@ pub enum TriggerUpgradeResponse {
}

#[derive(Clone, Debug, CandidType, Deserialize, PartialEq, Eq)]
pub struct StationRecoveryRequest {
/// The user ID of the station.
pub user_id: UuidDTO,
/// The SHA-256 hash of the wasm module.
pub wasm_sha256: Vec<u8>,
pub struct StationRecoveryRequestInstallCodeOperation {
/// The install mode: upgrade or reinstall.
pub install_mode: InstallMode,
/// The SHA-256 hash of the wasm module.
pub wasm_sha256: Vec<u8>,
/// The install arguments.
pub arg: Vec<u8>,
}

#[derive(Clone, Debug, CandidType, Deserialize, PartialEq, Eq)]
pub enum StationRecoveryRequestOperation {
InstallCode(StationRecoveryRequestInstallCodeOperation),
}

#[derive(Clone, Debug, CandidType, Deserialize, PartialEq, Eq)]
pub struct StationRecoveryRequest {
/// The user ID of the station.
pub user_id: UuidDTO,
/// The disaster recovery operation.
pub operation: StationRecoveryRequestOperation,
/// Time in nanoseconds since the UNIX epoch when the request was submitted.
pub submitted_at: TimestampRfc3339,
}
Expand Down
9 changes: 5 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 @@ -28,6 +28,7 @@ pub use orbit_essentials::cdk::mocks as upgrader_ic_cdk;

pub mod controllers;
pub mod errors;
pub mod mappers;
pub mod model;
pub mod services;
pub mod upgrade;
Expand Down Expand Up @@ -151,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 @@ -167,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
120 changes: 120 additions & 0 deletions core/upgrader/impl/src/mappers/disaster_recovery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use crate::model::{
DisasterRecovery, DisasterRecoveryV0, RequestDisasterRecoveryInstallCodeLog,
RequestDisasterRecoveryOperationLog, StationRecoveryRequest,
StationRecoveryRequestInstallCodeOperation,
StationRecoveryRequestInstallCodeOperationFootprint, StationRecoveryRequestOperation,
StationRecoveryRequestOperationFootprint, StationRecoveryRequestV0,
};
use orbit_essentials::utils::sha256_hash;

impl From<upgrader_api::RequestDisasterRecoveryInput> for StationRecoveryRequestOperation {
fn from(request: upgrader_api::RequestDisasterRecoveryInput) -> Self {
match request {
upgrader_api::RequestDisasterRecoveryInput::InstallCode(install_code) => {
let wasm_sha256 =
if let Some(ref module_extra_chunks) = install_code.module_extra_chunks {
module_extra_chunks.wasm_module_hash.clone()
} else {
sha256_hash(&install_code.module)
};
StationRecoveryRequestOperation::InstallCode(
StationRecoveryRequestInstallCodeOperation {
install_mode: install_code.install_mode.into(),
wasm_module: install_code.module,
wasm_module_extra_chunks: install_code.module_extra_chunks,
wasm_sha256,
arg_sha256: sha256_hash(&install_code.arg),
arg: install_code.arg,
},
)
}
}
}
}

impl From<&StationRecoveryRequestOperation> for StationRecoveryRequestOperationFootprint {
fn from(operation: &StationRecoveryRequestOperation) -> Self {
match operation {
StationRecoveryRequestOperation::InstallCode(ref install_code) => {
StationRecoveryRequestOperationFootprint::InstallCode(
StationRecoveryRequestInstallCodeOperationFootprint {
install_mode: install_code.install_mode,
wasm_sha256: install_code.wasm_sha256.clone(),
arg_sha256: install_code.arg_sha256.clone(),
},
)
}
}
}
}

impl From<&StationRecoveryRequestOperation> for RequestDisasterRecoveryOperationLog {
fn from(operation: &StationRecoveryRequestOperation) -> Self {
match operation {
StationRecoveryRequestOperation::InstallCode(ref install_code) => {
RequestDisasterRecoveryOperationLog::InstallCode(
RequestDisasterRecoveryInstallCodeLog {
install_mode: install_code.install_mode.to_string(),
wasm_sha256: hex::encode(&install_code.wasm_sha256),
arg_sha256: hex::encode(&install_code.arg_sha256),
},
)
}
}
}
}

impl From<&StationRecoveryRequestOperation> for upgrader_api::StationRecoveryRequestOperation {
fn from(operation: &StationRecoveryRequestOperation) -> Self {
match operation {
StationRecoveryRequestOperation::InstallCode(ref install_code) => {
upgrader_api::StationRecoveryRequestOperation::InstallCode(
upgrader_api::StationRecoveryRequestInstallCodeOperation {
install_mode: install_code.install_mode.into(),
wasm_sha256: install_code.wasm_sha256.clone(),
arg: install_code.arg.clone(),
},
)
}
}
}
}

// 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,
}
}
}
1 change: 1 addition & 0 deletions core/upgrader/impl/src/mappers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod disaster_recovery;
Loading
Loading