jam_bootstrap_service_common/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]
2#![cfg_attr(any(target_arch = "riscv32", target_arch = "riscv64"), no_main)]
3#![allow(clippy::unwrap_used)]
4
5extern crate alloc;
6
7use jam_types::*;
8use scale::{Decode, Encode};
9
10/// Bootstrap service instruction.
11///
12/// Used to form the work item payload for the JAM Bootstrap service.
13#[derive(Encode, Decode, Debug)]
14pub enum Instruction {
15 /// Create a new service.
16 CreateService {
17 /// Hash of the code of the new service.
18 code_hash: CodeHash,
19 /// Length of the code of the new service.
20 code_len: u64,
21 /// Minimum gas required for each work-item to be accumulated.
22 min_item_gas: UnsignedGas,
23 /// Minimum gas required for each incoming transfer.
24 min_memo_gas: UnsignedGas,
25 /// The balance to be transferred to the new service.
26 endowment: Balance,
27 /// The memo to be attached to the transfer.
28 memo: Memo,
29 },
30 /// Upgrade the code of the Bootstrap service.
31 Upgrade {
32 /// Hash of the code to be upgraded. This must already be in the service's preimage store.
33 code_hash: CodeHash,
34 /// Minimum gas required for each work-item to be accumulated.
35 min_item_gas: UnsignedGas,
36 /// Minimum gas required for each incoming transfer.
37 min_memo_gas: UnsignedGas,
38 },
39 /// Transfer funds to another service.
40 Transfer {
41 /// The destination service.
42 destination: ServiceId,
43 /// The amount to be transferred.
44 amount: Balance,
45 /// The amount of gas for the processing of the transfer by the destination service.
46 gas_limit: UnsignedGas,
47 /// The memo to give the destination service for the transfer.
48 memo: Memo,
49 },
50 /// Turn the current service into a zombie.
51 Zombify {
52 /// The service which will be able to eject the zombie.
53 ejector: ServiceId,
54 },
55 /// Destroy a zombie service with this as its ejector.
56 Eject {
57 /// The target service. This must have empty storage and exactly one preimage, whose hash
58 /// is `code_hash` and which must be unrequested and droppable.
59 target: ServiceId,
60 /// The code hash of the target service, which should be unrequested and droppable.
61 code_hash: CodeHash,
62 },
63 /// Delete some items from the storage of the service.
64 DeleteItems {
65 /// List of keys to be deleted.
66 storage_items: Vec<Vec<u8>>,
67 },
68 /// Request that preimage data be placed in the service's preimage store.
69 Solicit {
70 /// The hash of the data to solicit.
71 hash: AnyHash,
72 /// The length of the data to solicit.
73 len: u64,
74 },
75 /// Revoke request that preimage data be placed in the service's preimage store or drop
76 /// preimage data once they have sat without request for sufficiently long.
77 Forget {
78 /// The hash of the previously requested data.
79 hash: AnyHash,
80 /// The length of the previously requested data.
81 len: u64,
82 },
83 /// Look up preimage data.
84 Lookup {
85 /// Service ID which has the preimage data in its preimage store.
86 service: ServiceId,
87 /// The hash of the data.
88 hash: AnyHash,
89 },
90 /// Import segments from the JAM D3L.
91 Import {
92 /// The index and length pairs of each of the segments to be imported.
93 items: Vec<(
94 u64, // index
95 u64, // len
96 )>,
97 },
98 /// Export segments to the JAM D3L.
99 Export {
100 /// The blobs of data to be exported.
101 data: Vec<Vec<u8>>,
102 },
103 /// Reset the JAM privileged services.
104 Bless {
105 /// The manager service ID.
106 manager: ServiceId,
107 /// The assigner service ID.
108 assign: ServiceId,
109 /// The designator service ID.
110 designate: ServiceId,
111 /// The auto-accumulator service IDs, together with the baseline gas they get.
112 auto_acc: Vec<(ServiceId, UnsignedGas)>,
113 },
114 /// Assign the queue of a core.
115 Assign {
116 /// The index of the core to be assigned.
117 core: CoreIndex,
118 /// The authorization queue to assign.
119 queue: AuthQueue,
120 },
121 /// Designate the new validator key set.
122 Designate {
123 /// The keys of the new validator set.
124 keys: OpaqueValKeysets,
125 },
126 /// Specify a value for accumulate to return.
127 Yield {
128 /// The hash to be yielded.
129 hash: Hash,
130 },
131 /// Checkpoint the accumulation state.
132 Checkpoint,
133 /// Panic.
134 Panic,
135 #[doc(hidden)]
136 LookedUp { data: Vec<u8> },
137 #[doc(hidden)]
138 Imported { data: Vec<Vec<u8>> },
139 #[doc(hidden)]
140 Exported { count: u64 },
141 #[doc(hidden)]
142 RandomStorageRefine(test_key_vals::Input),
143 // Warning bad design, this can be too big for an accumulate state,
144 // and is just for testing (easy to reach size limits).
145 #[doc(hidden)]
146 RandomStorageAccumulate(Result<test_key_vals::Output, ()>),
147}
148
149impl From<Instruction> for WorkPayload {
150 fn from(i: Instruction) -> Self {
151 i.encode().into()
152 }
153}
154
155pub struct Instructions(pub Vec<Instruction>);
156impl From<Instructions> for WorkPayload {
157 fn from(instrs: Instructions) -> Self {
158 let mut payload = Vec::new();
159 for i in &instrs.0 {
160 i.encode_to(&mut payload);
161 }
162 payload.into()
163 }
164}
165
166pub mod test_key_vals {
167 use alloc::vec::Vec;
168 use rand::prelude::*;
169 use scale::{Decode, Encode};
170
171 #[derive(Encode, Decode, Debug)]
172 pub struct Input {
173 pub seed: u64,
174 pub nb_items: u8,
175 }
176
177 #[derive(Encode, Decode, Debug)]
178 pub struct Item {
179 pub key: [u8; 32],
180 }
181
182 #[derive(Encode, Decode, Debug)]
183 pub struct Output {
184 pub items: Vec<Item>,
185 }
186
187 pub fn generate_payload(input: Input) -> Result<Output, ()> {
188 let mut items: Vec<Item> = Default::default();
189 let mut rng = SmallRng::seed_from_u64(input.seed);
190 for _ in 0..input.nb_items {
191 let key: [u8; 32] = rng.random();
192 items.push(Item { key });
193 }
194 Ok(Output { items })
195 }
196}