spacejam_service/api/
vm.rs

1//! Virtual machine shared types
2
3use crate::{
4    BTreeMap, BandersnatchPublic, BlsPublic, Ed25519Public, EntropyBuffer, Gas, OpaqueHash,
5    ServiceId, String, TimeSlot, ValidatorMetadata, Vec,
6    service::{Privileges, ServiceAccount, WorkPackage},
7    vm::{DeferredTransfer, Operand},
8};
9use serde::{Deserialize, Serialize};
10#[cfg(feature = "json")]
11use spacejson::Json;
12
13/// Data of validators
14pub type ValidatorsData = [ValidatorData; 6];
15
16/// The accounts type
17pub type Accounts = BTreeMap<ServiceId, ServiceAccount>;
18
19/// Arguments for the authorize invocation
20#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
21pub struct AuthorizeArgs {
22    /// (p) the work package
23    pub package: WorkPackage,
24    /// (c) The core index
25    pub core_idx: u16,
26    /// (δ) accounts for historical lookup
27    pub accounts: Accounts,
28    /// (N_t) timeslot for the current operation
29    pub timeslot: TimeSlot,
30}
31
32/// Arguments for the refine invocation
33#[derive(Serialize, Deserialize)]
34pub struct RefineArgs {
35    /// (c) the core index
36    pub core: u16,
37    /// (i) the work item index
38    pub index: usize,
39    /// (p) the work package
40    pub package: WorkPackage,
41    /// (r) the authorizer output
42    pub auth_output: Vec<u8>,
43    /// (ī) all work items' import segments
44    pub all_imports: Vec<Vec<Segment>>,
45    /// (ς) export segment offset
46    pub export_offset: u16,
47    /// (δ) accounts for historical lookup
48    pub accounts: Accounts,
49    /// (N_t) timeslot for the current operation
50    pub timeslot: TimeSlot,
51}
52
53/// A segment of the import segments
54#[derive(Serialize, Deserialize)]
55pub struct Segment(#[serde(with = "codec::bytes")] pub [u8; 4104]);
56
57/// Arguments for the accumulate invocation
58#[derive(Serialize, Deserialize)]
59pub struct AccumulateArgs {
60    /// (U) The state context
61    pub context: AccumulateState,
62    /// (N_t)  timeslot for the current accumulation
63    pub timeslot: TimeSlot,
64    /// (N_s)  the service id of the caller
65    pub service: ServiceId,
66    /// (N_g)  the gas limit for the current operation
67    pub gas: Gas,
68    /// (O)  the accumulation operands
69    pub operands: Vec<Operand>,
70}
71
72/// State for the accumulate invocation
73#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
74pub struct AccumulateState {
75    /// d (δ) The accounts
76    pub accounts: Accounts,
77
78    /// i (ι) The upcoming validators
79    pub validators: ValidatorsData,
80
81    /// p (φ) The authorization queue
82    pub authorization: [Vec<OpaqueHash>; crate::CORES_COUNT],
83
84    /// a (χ) The privileged service indices
85    pub privileges: Privileges,
86
87    /// (η) The entropy
88    pub entropy: EntropyBuffer,
89}
90
91/// The accumulated result
92#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
93pub struct Accumulated {
94    /// (o) The state context
95    pub context: AccumulateState,
96
97    /// (t) The timeslot for the current accumulation
98    pub transfers: Vec<DeferredTransfer>,
99
100    /// (b) The output hash of the accumulation
101    pub hash: Option<OpaqueHash>,
102
103    /// (u) The gas used
104    pub gas: Gas,
105
106    /// (_e) The reason for the accumulation
107    pub reason: Reason,
108}
109
110/// Represents the ValidatorData structure from ASN.1
111#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy)]
112#[cfg_attr(feature = "json", derive(Json))]
113pub struct ValidatorData {
114    /// The bandersnatch public key
115    #[cfg_attr(feature = "json", json(hex))]
116    pub bandersnatch: BandersnatchPublic,
117    /// The ed25519 public key
118    #[cfg_attr(feature = "json", json(hex))]
119    pub ed25519: Ed25519Public,
120    /// The bls public key
121    #[cfg_attr(feature = "json", json(hex))]
122    #[serde(with = "codec::bytes")]
123    pub bls: BlsPublic,
124    /// The metadata
125    #[cfg_attr(feature = "json", json(hex))]
126    #[serde(with = "codec::bytes")]
127    pub metadata: ValidatorMetadata,
128}
129
130/// The program exit reason.
131///
132/// As defined per the graypaper (A.2)
133#[derive(Debug, Default, PartialEq, Eq, Clone, Serialize, Deserialize)]
134pub enum Reason {
135    /// The program has halted.
136    Halt,
137
138    /// The program has panicked.
139    Panic(String),
140
141    /// The invocation completed with a page fault.
142    Fault {
143        /// The page number
144        page: u32,
145    },
146
147    /// The status is unknown.
148    HostCall(u32),
149
150    /// The program has run out of gas.
151    OOG,
152
153    /// The program is still running.
154    #[default]
155    Continue,
156}