use canic::sharding::{ASSIGN, ASSIGN_INSTANCE, Key, LIMITS, LOOKUP, LOOKUP_INSTANCE, 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_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)
}
#[runtime::update(
runtime = runtime,
name = "assign_shard",
guard = hub,
decode = decode::<ASSIGN>,
encode = encode::<ASSIGN>,
max_bytes = 1024,
on_error = fail,
)]
fn assign(key: Key) -> Placement {
sharding::assign(key)
}
#[runtime::update(
runtime = runtime,
name = "lookup_shard",
guard = hub,
decode = decode::<LOOKUP>,
encode = encode::<LOOKUP>,
max_bytes = 1024,
on_error = fail,
)]
fn lookup(key: Key) -> Placement {
sharding::lookup(&key)
}
#[runtime::update(
runtime = runtime,
name = "assign_instance",
guard = sharding::project_hub,
decode = decode::<ASSIGN_INSTANCE>,
encode = encode::<ASSIGN_INSTANCE>,
max_bytes = 1024,
on_error = fail,
)]
fn assign_instance(key: Key) -> Placement {
sharding::assign_instance(key)
}
#[runtime::update(
runtime = runtime,
name = "lookup_instance",
guard = sharding::project_hub,
decode = decode::<LOOKUP_INSTANCE>,
encode = encode::<LOOKUP_INSTANCE>,
max_bytes = 1024,
on_error = fail,
)]
fn lookup_instance(key: Key) -> Placement {
sharding::lookup_instance(&key)
}
fn hub() -> Result<(), &'static str> {
sharding::hub()
}
fn controller() -> Result<(), &'static str> {
runtime::system::is_controller(&runtime::system::caller())
.then_some(())
.ok_or("controller required")
}
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 decode<const METHOD: u64>(b: &[u8]) -> Result<Key, ic_wire::Error> {
ic_wire::decode(b, frame(METHOD, ic_wire::Direction::Request), LIMITS)
}
#[allow(clippy::needless_pass_by_value)]
fn encode<const METHOD: u64>(value: Placement) -> 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(guard = super::directory_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<u16, 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(guard = super::directory_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 = super::directory_controller)]
fn retirement_status(
(subject, generation): (ic_platform_types::Principal, ic_wire::Bytes<32>),
) -> Result<(u64, u16), String> {
canic_management::role_issuer::retirement_status(subject, &generation)
}