use canic::guard::require_controller;
use canic::sharding::LIMITS;
use canic::sharding::Placement;
use canic_management::sharding;
use canic_management_protocol::{Refusal, stored};
use ic_platform_runtime as runtime;
#[canic::update(contract = stored::ApproveChildCreation, guard = require_controller)]
fn approve_child_creation(grant: stored::ChildCreationGrant) -> Result<(), Refusal> {
use stored::ChildCreationGrant;
match grant {
ChildCreationGrant::Shared(a) => sharding::configure(a),
ChildCreationGrant::Dedicated(a) => sharding::configure_instances(a),
ChildCreationGrant::Worker(a) => sharding::configure_workers(a),
ChildCreationGrant::PerParent(a) => sharding::configure_singleton(a),
}
}
canic::__canic_inspect!(wire, "configure_subnet_capacity", update, stored::CONFIGURE_SUBNET_CAPACITY, stored::SubnetCapacity, Result<(), Refusal>, stored::LIMITS, LIMITS);
#[runtime::update(
runtime = runtime,
name = "configure_subnet_capacity",
guard = require_controller,
decode = approval::<stored::SubnetCapacity, {stored::CONFIGURE_SUBNET_CAPACITY}>,
encode = configured::<{stored::CONFIGURE_SUBNET_CAPACITY}>,
max_bytes = 65536,
on_error = fail,
)]
fn configure_subnet_capacity(input: stored::SubnetCapacity) -> Result<(), Refusal> {
sharding::configure_subnet_capacity(input)
}
canic::__canic_inspect!(wire, "increase_pool_limit", update, stored::INCREASE_POOL_LIMIT, stored::IncreasePoolLimit, Result<(), Refusal>, stored::LIMITS, LIMITS);
#[runtime::update(
runtime = runtime,
name = "increase_pool_limit",
guard = require_controller,
decode = approval::<stored::IncreasePoolLimit, {stored::INCREASE_POOL_LIMIT}>,
encode = configured::<{stored::INCREASE_POOL_LIMIT}>,
max_bytes = 65536,
on_error = fail,
)]
fn increase_pool_limit(input: stored::IncreasePoolLimit) -> Result<(), Refusal> {
sharding::increase_pool_limit(input)
}
#[canic::update(contract = canic::sharding::LookupPlacement)]
fn lookup_placement(request: canic::sharding::Lookup) -> Placement {
use canic::sharding::Lookup;
match request {
Lookup::Instance(key) => sharding::lookup_instance(&key),
Lookup::Worker(pool, ordinal) => sharding::lookup_worker(&pool, ordinal),
}
}
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!("child placement: {error:?}"))
}
#[canic::update(contract = canic::topology::attestation::PrepareRoleKey, guard = require_controller)]
async fn prepare_role_key() -> Result<ic_wire::Bytes<32>, canic::topology::attestation::RoleError> {
canic_management::role_issuer::prepare().await
}
#[canic::update(contract = canic::topology::attestation::Issue)]
fn issue_role_proof(
receiver: ic_platform_types::Principal,
) -> Result<canic::topology::attestation::Proof, canic::topology::attestation::RoleError> {
canic_management::role_issuer::issue(receiver)
}
#[canic::update(contract = canic::sharding::ReleaseChildAllocation)]
fn release_child_allocation(
(child, allocation): (ic_platform_types::Principal, canic::sharding::Allocation),
) -> Result<(), canic::sharding::ReleaseError> {
sharding::release_child_allocation(child, allocation)
}
#[canic::update(
contract = canic::topology::attestation::CloseRoleIssuance,
guard = require_controller,
)]
fn close_role_issuance(
(subject, generation): (ic_platform_types::Principal, ic_wire::Bytes<32>),
) -> Result<u64, canic::topology::attestation::RoleError> {
canic_management::role_issuer::close(subject, &generation)
}
#[canic::query(guard = require_controller)]
fn retirement_status(
(subject, generation): (ic_platform_types::Principal, ic_wire::Bytes<32>),
) -> Result<(u64, u32), canic::topology::attestation::RoleError> {
canic_management::role_issuer::retirement_status(subject, &generation)
}
#[canic::update(contract = canic::sharding::standby::RefillStandby, guard = require_controller)]
async fn refill_standby(
input: canic::sharding::standby::Refill,
) -> Result<canic::sharding::standby::Asset, canic::sharding::standby::ReserveError> {
sharding::standby::refill(input).await
}
#[canic::update(contract = canic::sharding::standby::ObserveStandby, guard = require_controller)]
fn observe_standby() -> Vec<(
canic::sharding::standby::Source,
canic::sharding::standby::Asset,
)> {
sharding::standby::inspect()
}
#[canic::update(contract = canic::sharding::standby::ConfirmRelease, guard = require_controller)]
async fn confirm_worker_release(
target: canic::sharding::ShardTarget,
) -> Result<(), canic::sharding::standby::ReserveError> {
sharding::standby::confirm_release(target).await
}
#[canic::update(contract = canic::sharding::standby::ResetWorker, guard = require_controller)]
async fn reset_worker(
(target, deadline): (canic::sharding::ShardTarget, u64),
) -> Result<canic::sharding::standby::ResetPhase, canic::sharding::standby::ReserveError> {
let admit_receiver = crate::auth::require_no_worker_grant;
sharding::standby::reset(target, deadline, admit_receiver).await
}
#[canic::update(contract = stored::RetireReleasedChild, guard = require_controller)]
async fn retire_released_child(
input: stored::Stage<stored::ChildRetirement>,
) -> Result<stored::RetirementPhase, canic::sharding::standby::ReserveError> {
let grant = crate::auth::require_no_worker_grant;
sharding::standby::retire(input, grant).await
}
#[canic::update(contract = stored::ConfigureStandby, guard = require_controller)]
fn configure_standby(
policy: stored::StandbyPolicy,
) -> Result<(), canic::sharding::standby::ReserveError> {
for text in &policy.imports {
crate::auth::require_no_worker_grant(text.principal.parse().map_err(|_| {
canic::sharding::standby::ReserveError::InvalidApproval(text.principal.clone())
})?)
.map_err(canic::sharding::standby::ReserveError::CustodyOccupied)?;
}
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, canic::sharding::standby::ReserveError> {
sharding::standby::policy::import(id).await
}
#[canic::update(contract = canic::sharding::standby::ExportStandby, guard = require_controller)]
async fn export_standby(
input: (
ic_platform_types::Principal,
ic_platform_types::Principal,
u64,
),
) -> Result<canic::sharding::standby::TransferPhase, canic::sharding::standby::ReserveError> {
sharding::standby::transfer::export(input.0, input.1, input.2).await
}
#[canic::update(contract = canic::sharding::standby::ObserveStandbyHandoff, guard = sharding::standby::policy::import_caller)]
fn observe_standby_handoff(
id: ic_platform_types::Principal,
) -> Result<canic::sharding::standby::Asset, canic::sharding::standby::ReserveError> {
sharding::standby::policy::handoff_status(id)
}
#[canic::query(contract = stored::ReadStandbyStatus)]
fn standby_status() -> Option<stored::SpareCounts> {
sharding::standby::policy::spare_counts()
}
#[canic::update(contract = stored::ConfigureChildFunding, guard = require_controller)]
fn configure_child_funding(
input: stored::ChildFunding,
) -> Result<stored::ChildFundingStatus, stored::FundingError> {
sharding::funding::configure(input)
}
#[canic::update(contract = stored::StopChildFunding, guard = require_controller)]
fn stop_child_funding(
child: ic_platform_types::Principal,
) -> Result<stored::ChildFundingStatus, stored::FundingError> {
sharding::funding::stop(child)
}
#[canic::update(contract = canic::sharding::payment::AcquirePlacement)]
async fn acquire_placement(
input: (canic::sharding::payment::Demand, u128),
) -> Result<canic::sharding::payment::Payment, canic::sharding::payment::Refusal> {
sharding::payment::acquire_placement(input.0, input.1).await
}
#[canic::update(contract = canic::funding::ObserveChildFunding)]
fn observe_child_funding(_: ()) -> Option<canic::funding::FundingReceipts> {
sharding::funding::receipts()
}