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(request: (String, u8)) -> Placement {
sharding::worker(&request.0, request.1, true)
}
#[canic::update(contract = canic::sharding::LookupWorker, guard = sharding::worker_hub)]
fn lookup_worker(request: (String, u8)) -> Placement {
sharding::worker(&request.0, request.1, 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(input: (String, Key)) -> Placement {
sharding::assign(&input.0, input.1)
}
#[canic::update(contract = LookupShard, guard = hub)]
fn lookup_shard(input: (String, Key)) -> Placement {
sharding::lookup(&input.0, &input.1)
}
#[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::Source,
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
}
#[canic::update(contract = canic::sharding::standby::ResetWorker, guard = controller)]
async fn reset_worker(
(target, deadline): (canic::sharding::ShardTarget, u64),
) -> Result<canic::sharding::standby::ResetPhase, String> {
#[cfg(feature = "auth")]
let admit_receiver = crate::auth::require_no_worker_grant;
#[cfg(not(feature = "auth"))]
let admit_receiver = |_| Ok(());
sharding::standby::reset(target, deadline, admit_receiver).await
}
#[cfg(feature = "role-issuer")]
#[canic::update(contract = stored::RetireShard, guard = controller)]
async fn retire_shard(
input: stored::Stage<stored::ShardRetirement>,
) -> Result<stored::RetirementPhase, String> {
#[cfg(feature = "auth")]
let grant = crate::auth::require_no_worker_grant;
#[cfg(not(feature = "auth"))]
let grant = |_| Ok(());
sharding::standby::retire(input, grant).await
}
#[canic::update(contract = stored::ConfigureStandby, guard = controller)]
fn configure_standby(policy: stored::StandbyPolicy) -> Result<(), String> {
#[cfg(feature = "auth")]
for text in &policy.imports {
crate::auth::require_no_worker_grant(
text.principal
.parse()
.map_err(|_| "invalid import principal")?,
)?;
}
sharding::standby::policy::configure(policy)
}
#[canic::update(contract = canic::sharding::standby::ImportStandby, guard = sharding::standby::policy::import_caller)]
async fn import_standby(
id: ic_platform_types::Principal,
) -> Result<canic::sharding::standby::Asset, String> {
sharding::standby::policy::import(id).await
}
#[canic::update(contract = canic::sharding::standby::ExportStandby, guard = controller)]
async fn export_standby(
input: (
ic_platform_types::Principal,
ic_platform_types::Principal,
u64,
),
) -> Result<canic::sharding::standby::TransferPhase, String> {
sharding::standby::transfer::export(input.0, input.1, input.2).await
}
#[canic::update(contract = canic::sharding::standby::HandoffStatus, guard = sharding::standby::policy::import_caller)]
fn standby_handoff_status(
id: ic_platform_types::Principal,
) -> Result<canic::sharding::standby::Asset, String> {
sharding::standby::policy::handoff_status(id)
}
#[canic::query(contract = stored::ReadSpareCounts)]
fn spare_counts() -> Option<stored::SpareCounts> {
sharding::standby::policy::spare_counts()
}
#[canic::update(contract = stored::ConfigureChildFunding, guard = controller)]
fn configure_child_funding(
input: stored::ChildFunding,
) -> Result<stored::ChildFundingStatus, String> {
sharding::funding::configure(input)
}
#[canic::update(contract = stored::StopChildFunding, guard = controller)]
fn stop_child_funding(
child: ic_platform_types::Principal,
) -> Result<stored::ChildFundingStatus, String> {
sharding::funding::stop(child)
}
#[canic::update(contract = canic::sharding::payment::FundPlacement)]
async fn fund_placement(
input: (canic::sharding::payment::Demand, u128),
) -> Result<canic::sharding::payment::Payment, String> {
sharding::payment::fund(input.0, input.1).await
}