use super::controller;
use canic::sharding::{
AssignInstance, AssignShard, Key, LIMITS, LookupInstance, LookupShard, Placement,
};
use canic_management::sharding;
use canic_management_protocol::{
Refusal,
stored::{self, ShardPool},
};
use ic_platform_runtime as runtime;
#[runtime::update(
runtime = runtime,
name = "configure_shards",
guard = controller,
decode = approval::<ShardPool, {stored::CONFIGURE_SHARDS}>,
encode = configured::<{stored::CONFIGURE_SHARDS}>,
max_bytes = 65536,
on_error = fail,
)]
fn configure(input: ShardPool) -> Result<(), Refusal> {
sharding::configure(input)
}
#[runtime::update(
runtime = runtime,
name = "configure_instances",
guard = controller,
decode = approval::<stored::InstancePool, {stored::CONFIGURE_INSTANCES}>,
encode = configured::<{stored::CONFIGURE_INSTANCES}>,
max_bytes = 65536,
on_error = fail,
)]
fn configure_instances(input: stored::InstancePool) -> Result<(), Refusal> {
sharding::configure_instances(input)
}
#[runtime::update(runtime = runtime, name = "configure_workers", guard = controller,
decode = approval::<stored::InstancePool, {stored::CONFIGURE_WORKERS}>,
encode = configured::<{stored::CONFIGURE_WORKERS}>, max_bytes = 65536, on_error = fail)]
fn configure_workers(input: stored::InstancePool) -> Result<(), Refusal> {
sharding::configure_workers(input)
}
#[canic::update(contract = canic::sharding::AssignWorker, guard = sharding::worker_hub)]
fn assign_worker(ordinal: u8) -> Placement {
sharding::worker(ordinal, true)
}
#[canic::update(contract = canic::sharding::LookupWorker, guard = sharding::worker_hub)]
fn lookup_worker(ordinal: u8) -> Placement {
sharding::worker(ordinal, false)
}
#[runtime::update(
runtime = runtime,
name = "configure_capacity",
guard = controller,
decode = approval::<stored::SubnetCapacity, {stored::CONFIGURE_CAPACITY}>,
encode = configured::<{stored::CONFIGURE_CAPACITY}>,
max_bytes = 65536,
on_error = fail,
)]
fn configure_capacity(input: stored::SubnetCapacity) -> Result<(), Refusal> {
sharding::configure_capacity(input)
}
#[runtime::update(
runtime = runtime,
name = "increase_pool",
guard = controller,
decode = approval::<stored::IncreasePool, {stored::INCREASE_POOL}>,
encode = configured::<{stored::INCREASE_POOL}>,
max_bytes = 65536,
on_error = fail,
)]
fn increase_pool(input: stored::IncreasePool) -> Result<(), Refusal> {
sharding::increase_pool(input)
}
#[canic::update(contract = AssignShard, guard = hub)]
fn assign_shard(key: Key) -> Placement {
sharding::assign(key)
}
#[canic::update(contract = LookupShard, guard = hub)]
fn lookup_shard(key: Key) -> Placement {
sharding::lookup(&key)
}
#[canic::update(contract = AssignInstance, guard = sharding::project_hub)]
fn assign_instance(key: Key) -> Placement {
sharding::assign_instance(key)
}
#[canic::update(contract = LookupInstance, guard = sharding::project_hub)]
fn lookup_instance(key: Key) -> Placement {
sharding::lookup_instance(&key)
}
fn hub() -> Result<(), &'static str> {
sharding::hub()
}
fn frame(method: u64, direction: ic_wire::Direction) -> ic_wire::Encoding {
ic_wire::Encoding::Frame { method, direction }
}
fn approval<T: serde::de::DeserializeOwned, const METHOD: u64>(
b: &[u8],
) -> Result<T, ic_wire::Error> {
ic_wire::decode(
b,
frame(METHOD, ic_wire::Direction::Request),
stored::LIMITS,
)
}
fn configured<const METHOD: u64>(value: Result<(), Refusal>) -> Result<Vec<u8>, ic_wire::Error> {
ic_wire::encode(&value, frame(METHOD, ic_wire::Direction::Response), LIMITS)
}
fn fail(error: impl std::fmt::Debug) -> ! {
runtime::system::trap(&format!("shard placement: {error:?}"))
}
#[cfg(feature = "role-issuer")]
#[canic::update(contract = canic::topology::attestation::PrepareRoleKey, guard = controller)]
async fn prepare_role_key() -> Result<ic_wire::Bytes<32>, String> {
canic_management::role_issuer::prepare().await
}
#[cfg(feature = "role-issuer")]
#[canic::update(contract = canic::topology::attestation::Issue)]
fn issue_role(
receiver: ic_platform_types::Principal,
) -> Result<canic::topology::attestation::Proof, String> {
canic_management::role_issuer::issue(receiver)
}
#[canic::update(contract = canic::sharding::InspectShard, guard = controller)]
fn inspect_shard(
shard: ic_platform_types::Principal,
) -> Result<canic::sharding::ShardStatus, String> {
sharding::inspect_shard(shard)
}
#[canic::update(contract = canic::sharding::BeginShardDrain, guard = controller)]
fn begin_shard_drain(target: canic::sharding::ShardTarget) -> Result<u32, String> {
sharding::begin_shard_drain(&target)
}
#[canic::update(contract = canic::sharding::ReleaseShard, guard = hub)]
fn release_shard((key, target): (Key, canic::sharding::ShardTarget)) -> Result<(), String> {
sharding::release_shard(&key, &target)
}
#[cfg(feature = "role-issuer")]
#[canic::update(
contract = canic::topology::attestation::CloseRoleIssuance,
guard = controller,
)]
fn close_role_issuance(
(subject, generation): (ic_platform_types::Principal, ic_wire::Bytes<32>),
) -> Result<u64, String> {
canic_management::role_issuer::close(subject, &generation)
}
#[cfg(feature = "role-issuer")]
#[canic::query(guard = controller)]
fn retirement_status(
(subject, generation): (ic_platform_types::Principal, ic_wire::Bytes<32>),
) -> Result<(u64, u32), String> {
canic_management::role_issuer::retirement_status(subject, &generation)
}
#[canic::update(contract = canic::sharding::standby::RefillStandby, guard = controller)]
async fn refill_standby(
input: canic::sharding::standby::Refill,
) -> Result<canic::sharding::standby::Asset, String> {
sharding::standby::refill(input).await
}
#[canic::update(contract = canic::sharding::standby::InspectStandby, guard = controller)]
fn inspect_standby() -> Vec<(
canic::sharding::standby::Refill,
canic::sharding::standby::Asset,
)> {
sharding::standby::inspect()
}
#[canic::update(contract = canic::sharding::standby::ConfirmRelease, guard = controller)]
async fn confirm_worker_release(target: canic::sharding::ShardTarget) -> Result<(), String> {
sharding::standby::confirm_release(target).await
}