use ic_platform::native::RequestStatus;
use ic_platform_protocol::management_snapshots::Status as CanisterStatusType;
use super::{Context, Operation, load, request_status, stop, upgrade};
use ic_blob_storage::protocol::ManagementView;
use sha2::{Digest, Sha256};
use std::path::Path;
pub(super) async fn admit(
context: &Context,
stop_record: &Path,
upgrade_record: &Path,
attempt: u64,
wasm: &[u8],
after_rejected_upgrade: bool,
) -> Result<(), String> {
let original = load(
context,
upgrade_record,
Operation::Upgrade { wasm, stop_record },
)?;
if original.attempt != attempt {
return Err("upgrade predecessor attempt mismatch".into());
}
upgrade::confirm_stop(context, stop_record, attempt).await?;
let outcome = request_status(context, &original).await?;
if after_rejected_upgrade {
if !matches!(outcome, RequestStatus::Rejected { .. }) {
return Err("recovery requires authenticated rejection of the original upgrade".into());
}
if stop::status(context, &context.wasm_hash).await? != CanisterStatusType::Stopped {
return Err("recovery requires the original host to remain stopped".into());
}
} else {
upgrade::observe(context, &original.id, outcome, wasm).await?;
}
Ok(())
}
pub(super) async fn acknowledge(
context: &Context,
id: &[u8; 32],
attempt: u64,
bytes: &[u8],
artifact: &[u8; 32],
) -> Result<String, String> {
stop::reply(bytes).map_err(|_| "invalid original start reply")?;
let view = inspect(context, artifact).await?;
if view.maintenance.attempt != attempt || view.maintenance.active {
return Err(
"reopened host does not match the current drained attempt; no activation approval"
.into(),
);
}
Ok(format!(
"Start observed: request_id={display_id} attempt={attempt} active=false; installed artifact and configuration matched; no activation performed.",
display_id = crate::overview::hex(id)
))
}
pub(super) fn artifact_hash(
context: &Context,
wasm: &[u8],
after_rejected_upgrade: bool,
) -> [u8; 32] {
if after_rejected_upgrade {
context.wasm_hash
} else {
Sha256::digest(wasm).into()
}
}
pub(super) async fn inspect(
context: &Context,
expected: &[u8; 32],
) -> Result<ManagementView, String> {
if stop::status(context, expected).await? != CanisterStatusType::Running {
return Err("reopened target is not running; preserve custody".into());
}
context.inspect_artifact(expected).await
}
pub(super) async fn observe(
context: &Context,
id: &[u8; 32],
attempt: u64,
original: RequestStatus,
artifact: &[u8; 32],
) -> Result<String, String> {
match original {
RequestStatus::Replied(reply) => acknowledge(context, id, attempt, &reply, artifact).await,
RequestStatus::Rejected { .. } => {
Err("original start rejected; preserve custody, no resubmission".into())
}
_ => Err(
"original start outcome unresolved; running state cannot replace its acknowledgment"
.into(),
),
}
}