linera_sdk/abis/
controller.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use async_graphql::{scalar, Request, Response, SimpleObject};
5use linera_sdk_derive::GraphQLMutationRootInCrate;
6use serde::{Deserialize, Serialize};
7
8use crate::linera_base_types::{
9    AccountOwner, ApplicationId, ChainId, ContractAbi, DataBlobHash, ServiceAbi,
10};
11
12pub struct ControllerAbi;
13
14impl ContractAbi for ControllerAbi {
15    type Operation = Operation;
16    type Response = ();
17}
18
19impl ServiceAbi for ControllerAbi {
20    type Query = Request;
21    type QueryResponse = Response;
22}
23
24/// Service are identified by the blob ID of the description.
25pub type ManagedServiceId = DataBlobHash;
26
27#[derive(Debug, Deserialize, Serialize, GraphQLMutationRootInCrate)]
28pub enum Operation {
29    /// Worker commands
30    ExecuteWorkerCommand {
31        owner: AccountOwner,
32        command: WorkerCommand,
33    },
34    /// Execute a controller command
35    ExecuteControllerCommand {
36        admin: AccountOwner,
37        command: ControllerCommand,
38    },
39}
40
41/// A worker command
42#[derive(Clone, Debug, Deserialize, Serialize)]
43pub enum WorkerCommand {
44    /// Executed by workers to register themselves.
45    RegisterWorker { capabilities: Vec<String> },
46    /// Executed by workers to de-register themselves.
47    DeregisterWorker,
48}
49
50scalar!(WorkerCommand);
51
52/// A controller command
53#[derive(Clone, Debug, Deserialize, Serialize)]
54pub enum ControllerCommand {
55    /// Set the admin owners.
56    SetAdmins { admins: Option<Vec<AccountOwner>> },
57    /// Remove a worker. (This should not usually happen, but some workers may be broken
58    /// and need to be cleaned up.)
59    RemoveWorker { worker_id: ChainId },
60    /// Update the state of a particular service to be running on the specific workers.
61    UpdateService {
62        service_id: ManagedServiceId,
63        workers: Vec<ChainId>,
64    },
65    /// Remove a service from the map entirely.
66    RemoveService { service_id: ManagedServiceId },
67    /// Set the states of all services at once, possibly removing some of them.
68    UpdateAllServices {
69        services: Vec<(ManagedServiceId, Vec<ChainId>)>,
70    },
71}
72
73scalar!(ControllerCommand);
74
75/// The description of a service worker.
76#[derive(Clone, Debug, Serialize, Deserialize, SimpleObject)]
77pub struct Worker {
78    /// The address used by the worker.
79    pub owner: AccountOwner,
80    /// Some tags denoting the capabilities of this worker. Each capability has a value
81    /// that the worker will read from its local environment and pass to the applications.
82    pub capabilities: Vec<String>,
83}
84
85/// The description of a service managed by the controller.
86#[derive(Clone, Debug, Serialize, Deserialize)]
87pub struct ManagedService {
88    /// The application ID running the service (e.g. pm-engine)
89    pub application_id: ApplicationId,
90    /// The role assumed by this service within the application (e.g. engine, event,
91    /// market-maker).
92    pub name: String,
93    /// The chain on which the service is run. Note that this is different from the worker
94    /// chains which typically only run the controller application for managing the worker
95    /// itself.
96    pub chain_id: ChainId,
97    /// The required capabilities for a worker to be useful (e.g. some API key).
98    /// Concretely, the worker will read its environment variable
99    pub requirements: Vec<String>,
100}
101
102scalar!(ManagedService);
103
104/// The local state of a worker.
105// This is used to facilitate service queries.
106#[derive(Clone, Debug, Deserialize, Serialize)]
107pub struct LocalWorkerState {
108    /// The description of this worker as we registered it.
109    pub local_worker: Option<Worker>,
110    /// The services currently running locally.
111    pub local_services: Vec<ManagedService>,
112    /// The chains currently followed locally (besides ours and the active service
113    /// chains).
114    pub local_chains: Vec<ChainId>,
115}
116
117scalar!(LocalWorkerState);