spacejam_service/service/
work.rs

1//! Work package related stuffs
2
3use crate::{
4    ErasureRoot, ExportsRoot, Gas, OpaqueHash, ServiceId, Vec, WorkPackageHash,
5    service::RefineContext,
6};
7use serde::{Deserialize, Serialize};
8
9#[cfg(feature = "json")]
10use {crate::String, crate::service::RefineContextJson, spacejson::Json};
11
12/// Represents the specification of a work package.
13#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Default)]
14#[cfg_attr(feature = "json", derive(Json))]
15pub struct WorkPackageSpec {
16    /// (p) The hash
17    #[cfg_attr(feature = "json", json(hex))]
18    pub hash: WorkPackageHash,
19
20    /// (l) The length of the erasure bundle
21    pub length: u32,
22
23    /// (u) The erasure root
24    #[cfg_attr(feature = "json", json(hex))]
25    pub erasure_root: ErasureRoot,
26
27    /// (e) The exports root (segment root)
28    #[cfg_attr(feature = "json", json(hex))]
29    pub exports_root: ExportsRoot,
30
31    /// (n) The exports count
32    pub exports_count: u16,
33}
34
35/// Represents a work package in the system.
36///
37/// TODO: embed token and host to the authorizer?
38#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Default)]
39#[cfg_attr(feature = "json", derive(Json))]
40pub struct WorkPackage {
41    /// (h) The auth code host
42    pub auth_code_host: ServiceId,
43
44    /// (u) The auth code hash
45    #[cfg_attr(feature = "json", json(hex))]
46    pub auth_code_hash: OpaqueHash,
47
48    /// (c) The context
49    #[cfg_attr(feature = "json", json(nested))]
50    pub context: RefineContext,
51
52    /// (j) The authorization token
53    #[cfg_attr(feature = "json", json(hex))]
54    pub authorization: Vec<u8>,
55
56    /// (a) The authorizer
57    #[cfg_attr(feature = "json", json(hex))]
58    #[serde(alias = "authorizer_config")]
59    pub config: Vec<u8>,
60
61    /// (w) The items
62    #[cfg_attr(feature = "json", json(nested))]
63    pub items: Vec<WorkItem>,
64}
65
66/// Represents an individual work item within a work package.
67#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
68#[cfg_attr(feature = "json", derive(Json))]
69pub struct WorkItem {
70    /// (s) The service
71    pub service: ServiceId,
72
73    /// (h) The code hash
74    #[cfg_attr(feature = "json", json(hex))]
75    pub code_hash: OpaqueHash,
76
77    /// (g) The refine gas limit
78    pub refine_gas_limit: Gas,
79
80    /// (a) The accumulate gas limit
81    pub accumulate_gas_limit: Gas,
82
83    /// (e) The export count
84    ///
85    /// MAX=W_X=3072
86    pub export_count: u16,
87
88    /// (y) The payload
89    #[cfg_attr(feature = "json", json(hex))]
90    pub payload: Vec<u8>,
91
92    /// (i) The import segments
93    ///
94    /// MAX=W_M=3072
95    #[cfg_attr(feature = "json", json(nested))]
96    pub import_segments: Vec<ImportSpec>,
97
98    /// (x) The extrinsic
99    ///
100    /// MAX=T=128
101    #[cfg_attr(feature = "json", json(nested))]
102    pub extrinsic: Vec<ExtrinsicSpec>,
103}
104
105/// Represents an import specification for a work item.
106#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
107#[cfg_attr(feature = "json", derive(Json))]
108pub struct ImportSpec {
109    /// The tree root
110    #[cfg_attr(feature = "json", json(hex))]
111    pub tree_root: OpaqueHash,
112
113    /// The index
114    pub index: u16,
115}
116
117/// Represents an extrinsic specification for a work item.
118#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
119#[cfg_attr(feature = "json", derive(Json))]
120pub struct ExtrinsicSpec {
121    /// The hash
122    #[cfg_attr(feature = "json", json(hex))]
123    pub hash: OpaqueHash,
124
125    /// The length
126    pub len: u32,
127}
128
129#[cfg(feature = "blake2")]
130impl WorkPackage {
131    /// Compute the authorizer hash
132    ///
133    /// FIXME: shall we hash it after encoding?
134    pub fn authorizer_hash(&self) -> OpaqueHash {
135        crate::blake2b(&[self.auth_code_hash.as_ref(), &self.config].concat())
136    }
137}