use crate::{
dto::template::{
TemplateChunkResponse, TemplateChunkSetInfoResponse, TemplateManifestInput,
WasmStoreCatalogEntryResponse, WasmStoreStatusResponse,
},
ids::{TemplateId, TemplateVersion, WasmStoreBinding},
ops::storage::{state::subnet::SubnetStateOps, template::TemplateChunkedOps},
};
use candid::CandidType;
use canic_core::__control_plane_core as cp_core;
use cp_core::{InternalError, InternalErrorOrigin, cdk::types::Principal, protocol};
use super::super::call_store_result;
#[derive(CandidType)]
pub(super) struct TemplateChunkInputRef<'a> {
pub template_id: &'a TemplateId,
pub version: &'a TemplateVersion,
pub chunk_index: u32,
pub bytes: &'a [u8],
}
pub(super) async fn store_catalog(
store_pid: Principal,
) -> Result<Vec<WasmStoreCatalogEntryResponse>, InternalError> {
call_store_result(store_pid, protocol::CANIC_WASM_STORE_CATALOG, ()).await
}
pub(super) async fn store_chunk_set_info(
store_pid: Principal,
template_id: &TemplateId,
version: &TemplateVersion,
) -> Result<TemplateChunkSetInfoResponse, InternalError> {
call_store_result(
store_pid,
protocol::CANIC_WASM_STORE_INFO,
(
template_id.as_str().to_string(),
version.as_str().to_string(),
),
)
.await
}
pub(super) async fn store_status(
store_pid: Principal,
) -> Result<WasmStoreStatusResponse, InternalError> {
call_store_result(store_pid, protocol::CANIC_WASM_STORE_STATUS, ()).await
}
pub(super) async fn store_stage_manifest(
store_pid: Principal,
request: TemplateManifestInput,
) -> Result<(), InternalError> {
call_store_result(
store_pid,
protocol::CANIC_WASM_STORE_STAGE_MANIFEST,
(request,),
)
.await
}
pub(super) async fn store_prepare_gc(store_pid: Principal) -> Result<(), InternalError> {
call_store_result(store_pid, protocol::CANIC_WASM_STORE_PREPARE_GC, ()).await
}
pub(super) async fn store_begin_gc(store_pid: Principal) -> Result<(), InternalError> {
call_store_result(store_pid, protocol::CANIC_WASM_STORE_BEGIN_GC, ()).await
}
pub(super) async fn store_complete_gc(store_pid: Principal) -> Result<(), InternalError> {
call_store_result(store_pid, protocol::CANIC_WASM_STORE_COMPLETE_GC, ()).await
}
pub(super) async fn store_chunk(
store_pid: Principal,
template_id: &TemplateId,
version: &TemplateVersion,
chunk_index: u32,
) -> Result<Vec<u8>, InternalError> {
let response: TemplateChunkResponse = call_store_result(
store_pid,
protocol::CANIC_WASM_STORE_CHUNK,
(
template_id.as_str().to_string(),
version.as_str().to_string(),
chunk_index,
),
)
.await?;
Ok(response.bytes)
}
pub(super) fn store_binding_for_pid(
store_pid: Principal,
) -> Result<WasmStoreBinding, InternalError> {
SubnetStateOps::wasm_store_binding_for_pid(store_pid).ok_or_else(|| {
InternalError::workflow(
InternalErrorOrigin::Workflow,
format!("wasm store {store_pid} is not registered"),
)
})
}
pub(super) fn local_chunk(
template_id: &TemplateId,
version: &TemplateVersion,
chunk_index: u32,
) -> Result<Vec<u8>, InternalError> {
let response = TemplateChunkedOps::chunk_response(template_id, version, chunk_index)?;
Ok(response.bytes)
}