corevm-engine 0.1.28

CoreVM engine that drives program execution either on the builder or CoreVM service side
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use crate::hash_encoded;
use alloc::{borrow::Cow, vec::Vec};
use codec::{Decode, DecodeAll, Encode};
use corevm_host::{
	fs, CoreVmInstruction, CoreVmOutput, PageInfo, PageNum, Range, RangeSet, StorageKey, VmSpec,
	VmState,
};
use jam_types::{
	AccumulateItem, Balance, Hash, Memo, SegmentTreeRoot, ServiceId, SignedGas, Slot, UnsignedGas,
	WorkError, WorkOutput,
};
use log::debug;

mod messages;

const ALL_STORAGE_KEYS_WITHOUT_PARAMS: &[StorageKey] = {
	use StorageKey::*;
	&[
		Gas,
		StateHash,
		VmSpec,
		VideoMode,
		AudioMode,
		ExecEnvRef,
		Owner,
		StoredPages,
		IncomingServiceMessages,
		OutgoingServiceMessages,
	]
};

const _: () = assert!(ALL_STORAGE_KEYS_WITHOUT_PARAMS.len() == StorageKey::COUNT_WITHOUT_PARAMS);

/// Storage keys that should be removed on VM reset.
const REMOVE_ON_RESET_KEYS: &[StorageKey] = {
	use StorageKey::*;
	&[VmSpec, VideoMode, AudioMode, StoredPages, IncomingServiceMessages, OutgoingServiceMessages]
};

/// Storage keys that should _not_ be removed on VM reset.
const DO_NOT_REMOVE_ON_RESET_KEYS: &[StorageKey] = {
	use StorageKey::*;
	&[Gas, ExecEnvRef, StateHash, Owner]
};

const _: () = assert!(
	REMOVE_ON_RESET_KEYS.len() + DO_NOT_REMOVE_ON_RESET_KEYS.len() ==
		StorageKey::COUNT_WITHOUT_PARAMS
);

/// Operations that [`AccumulateEngine`] uses during accumulation.
///
/// Each operation corresponds to a JAM host-call.
pub trait AccumulateOps {
	/// Implemenatation-specific error type.
	type Error: core::fmt::Debug;

	/// Read value under the specified key from the storage of the specified service.
	fn get(&self, id: ServiceId, key: &StorageKey) -> Option<Cow<'_, [u8]>>;

	/// Write value under the specified key to the storage.
	fn set(&mut self, key: StorageKey, value: Cow<'_, [u8]>) -> Result<(), Self::Error>;

	/// Remove value under the specified key from the storage of the service being accumulated.
	///
	/// Returns `true` if the value was present in the storage before the removal.
	fn remove(&mut self, key: &StorageKey) -> bool;

	/// Same as [`set`](Self::set) but encodes the provided value using JAM codec.
	fn set_typed(&mut self, key: StorageKey, value: &impl Encode) -> Result<(), Self::Error> {
		value.using_encoded(|bytes| self.set(key, bytes.into()))
	}

	/// Same as [`get`](Self::get) but decodes the retrieved value using JAM codec.
	fn get_typed<T: Decode>(
		&self,
		id: ServiceId,
		key: &StorageKey,
	) -> Result<Option<T>, codec::Error> {
		self.get(id, key).map(|bytes| T::decode_all(&mut bytes.as_ref())).transpose()
	}

	/// Get min. memo gas of the specified service.
	fn min_memo_gas(&self, service_id: ServiceId) -> Option<UnsignedGas>;

	/// Transfer data and/or funds to another service.
	fn transfer(
		&self,
		destination: ServiceId,
		amount: Balance,
		gas_limit: UnsignedGas,
		memo: &Memo,
	) -> Result<(), Self::Error>;

	/// Same as [`transfer`](Self::transfer) but encodes the provided memo using JAM codec.
	///
	/// Panics if the encoded value doesn't fit into [`Memo`].
	fn transfer_typed(
		&self,
		destination: ServiceId,
		amount: Balance,
		gas_limit: UnsignedGas,
		memo: &impl Encode,
	) -> Result<(), Self::Error> {
		let mut raw_memo = Memo::zero();
		memo.encode_to(&mut SliceOutput(&mut raw_memo[..]));
		self.transfer(destination, amount, gas_limit, &raw_memo)
	}

	/// Reset the privileged services.
	fn bless<'a>(
		&mut self,
		manager: ServiceId,
		assigner: ServiceId,
		designator: ServiceId,
		registrar: ServiceId,
		always_acc: impl IntoIterator<Item = &'a (ServiceId, UnsignedGas)>,
	);

	/// Zombify the service.
	fn zombify(&mut self, ejector: ServiceId);
}

impl<A: AccumulateOps + ?Sized> AccumulateOps for &mut A {
	type Error = A::Error;

	fn get(&self, service_id: ServiceId, key: &StorageKey) -> Option<Cow<'_, [u8]>> {
		AccumulateOps::get(*self, service_id, key)
	}

	fn set(&mut self, key: StorageKey, value: Cow<'_, [u8]>) -> Result<(), Self::Error> {
		AccumulateOps::set(*self, key, value)
	}

	fn remove(&mut self, key: &StorageKey) -> bool {
		AccumulateOps::remove(*self, key)
	}

	fn min_memo_gas(&self, service_id: ServiceId) -> Option<UnsignedGas> {
		AccumulateOps::min_memo_gas(*self, service_id)
	}

	fn transfer(
		&self,
		destination: ServiceId,
		amount: Balance,
		gas_limit: UnsignedGas,
		memo: &Memo,
	) -> Result<(), Self::Error> {
		AccumulateOps::transfer(*self, destination, amount, gas_limit, memo)
	}

	fn bless<'a>(
		&mut self,
		manager: ServiceId,
		assigner: ServiceId,
		designator: ServiceId,
		registrar: ServiceId,
		always_acc: impl IntoIterator<Item = &'a (ServiceId, UnsignedGas)>,
	) {
		AccumulateOps::bless(*self, manager, assigner, designator, registrar, always_acc);
	}

	fn zombify(&mut self, ejector: ServiceId) {
		AccumulateOps::zombify(*self, ejector);
	}
}

/// An engine that drives execution of `accumulate` and `on_transfer` CoreVM service entry points.
pub struct AccumulateEngine<A: AccumulateOps> {
	ops: A,
	/// The current slot.
	slot: Slot,
	/// Id of the service that is being accumulated.
	service_id: ServiceId,
}

impl<A: AccumulateOps> AccumulateEngine<A> {
	/// Create new engine with the specified accumulation API implementation.
	pub fn new(ops: A, slot: Slot, service_id: ServiceId) -> Self {
		Self { ops, slot, service_id }
	}

	/// Accumulate the specified items.
	///
	/// Returns accumulation result for each item.
	pub fn run(
		&mut self,
		items: &[AccumulateItem],
	) -> Vec<Result<(), AccumulationError<A::Error>>> {
		items
			.iter()
			.map(|item| match item {
				AccumulateItem::WorkItem(item) =>
					self.accumulate(item.result.as_ref(), item.exports_root),
				AccumulateItem::Transfer(item) => self.transfer(item.source, item.memo),
			})
			.collect()
	}

	/// Get the underlying accumulation API implementation.
	pub fn into_inner(self) -> A {
		self.ops
	}

	fn accumulate(
		&mut self,
		result: Result<&WorkOutput, &WorkError>,
		exports_root: SegmentTreeRoot,
	) -> Result<(), AccumulationError<A::Error>> {
		use AccumulationError::*;
		let output = result.map_err(|e| Work(e.clone()))?;
		let output = CoreVmOutput::decode_all(&mut &output[..])?;

		if self.get_typed::<fs::BlockRef>(&StorageKey::ExecEnvRef)? != Some(output.exec_ref) {
			return Err(ExecEnvRefMismatch);
		}

		// Update VM state hash.
		if self.get_typed::<Hash>(&StorageKey::StateHash)? != Some(output.old_hash) {
			return Err(PriorStateMismatch);
		}
		self.set_typed(StorageKey::StateHash, &output.new_hash)?;
		let mut stored_pages =
			self.get_typed::<RangeSet>(&StorageKey::StoredPages)?.unwrap_or_default();
		// Check imported pages' hashes.
		for (page, our_hash) in output.touched_imported_pages.iter() {
			let key = StorageKey::PageInfo(*page);
			let their_hash = self.get_typed::<PageInfo>(&key)?.ok_or(NoSuchPage(*page))?.hash;
			if &their_hash != our_hash {
				return Err(PageHashMismatch);
			}
		}
		// Update memory pages' metadata.
		let mut export_index = 0;
		for (page, hash) in output.updated_pages.iter() {
			let key = StorageKey::PageInfo(*page);
			if hash == &[0; 32] {
				// The page was freed.
				self.get_typed::<PageInfo>(&key)?.ok_or(NoSuchPage(*page))?;
				stored_pages.remove(&Range::new(page.0, page.0 + 1));
				self.ops.remove(&key);
			} else {
				stored_pages.insert(Range::new(page.0, page.0 + 1));
				let info = PageInfo { hash: *hash, exports_root, export_index };
				self.set_typed(key, &info)?;
				export_index += 1;
			}
		}
		self.set_typed(StorageKey::StoredPages, &stored_pages)?;
		// Update video mode.
		if let Some(ref mode) = output.vm_state.video {
			self.set_typed(StorageKey::VideoMode, mode)?;
		}
		// Update audio mode.
		if let Some(ref mode) = output.vm_state.audio {
			self.set_typed(StorageKey::AudioMode, mode)?;
		}
		self.remove_processed_incoming_service_messages(&output.processed_service_messages)?;
		self.send_new_outgoing_service_messages_and_remove_old(output.outgoing_messages)?;
		// Store VM output specification.
		let spec = VmSpec { exports_root, output: output.vm_output, state: output.vm_state };
		self.set_typed(StorageKey::VmSpec, &spec)?;

		Ok(())
	}

	fn transfer(
		&mut self,
		source: ServiceId,
		memo: Memo,
	) -> Result<(), AccumulationError<A::Error>> {
		use AccumulationError::*;
		let instr = CoreVmInstruction::decode(&mut &memo[..])?;
		let owner = self.get_typed::<ServiceId>(&StorageKey::Owner)?;
		if let Some(owner) = owner {
			if owner != source {
				return Err(NotTheOwner(owner, source));
			}
		}
		match instr {
			CoreVmInstruction::Reset { gas, exec_ref } => {
				if owner.is_none() {
					debug!("Setting owner to {source:x}");
					self.set_typed(StorageKey::Owner, &source)?;
				}
				self.reset(gas, exec_ref)?;
			},
			CoreVmInstruction::SetOwner(owner) => {
				debug!("Setting owner to {owner:x}");
				self.set_typed(StorageKey::Owner, &owner)?;
			},
			CoreVmInstruction::PushServiceMessage(message) => {
				self.push_incoming_service_message(message)?;
			},
			CoreVmInstruction::Destroy { ejector } => {
				self.destroy()?;
				self.ops.bless(ejector, ejector, ejector, ejector, []);
				self.ops.zombify(ejector);
			},
		}
		Ok(())
	}

	/// Reset the VM.
	///
	/// Sets `ExecEnv` and `Gas`; resets everything else.
	fn reset(
		&mut self,
		gas: SignedGas,
		exec_ref: fs::BlockRef,
	) -> Result<(), AccumulationError<A::Error>> {
		debug!("Resetting VM: gas = {gas}, exec = {exec_ref}");
		let state_hash = hash_encoded(VmState::initial());
		self.set_typed(StorageKey::Gas, &gas)?;
		self.set_typed(StorageKey::ExecEnvRef, &exec_ref)?;
		self.set_typed(StorageKey::StateHash, &state_hash)?;
		self.remove_memory_pages()?;
		self.remove_all_incoming_service_messages()?;
		self.remove_all_outgoing_service_messages()?;
		// We should remove every key except the ones that we've overwritten above and except
		// Owner.
		for key in REMOVE_ON_RESET_KEYS {
			self.ops.remove(key);
		}
		Ok(())
	}

	/// Destroy the VM.
	///
	/// Removes everything from the storage.
	fn destroy(&mut self) -> Result<(), AccumulationError<A::Error>> {
		self.remove_memory_pages()?;
		self.remove_all_incoming_service_messages()?;
		self.remove_all_outgoing_service_messages()?;
		for key in ALL_STORAGE_KEYS_WITHOUT_PARAMS.iter() {
			self.ops.remove(key);
		}
		Ok(())
	}

	fn remove_memory_pages(&mut self) -> Result<(), AccumulationError<A::Error>> {
		let stored_pages =
			self.get_typed::<RangeSet>(&StorageKey::StoredPages)?.unwrap_or_default();
		for range in stored_pages.as_slice().iter() {
			for page in range.start..range.end {
				self.ops.remove(&StorageKey::PageInfo(PageNum(page)));
			}
		}
		Ok(())
	}

	fn set_typed(
		&mut self,
		key: StorageKey,
		value: &impl Encode,
	) -> Result<(), AccumulationError<A::Error>> {
		self.ops.set_typed(key, value).map_err(AccumulationError::Api)
	}

	fn get_typed<T: Decode>(&self, key: &StorageKey) -> Result<Option<T>, codec::Error> {
		self.ops.get_typed::<T>(self.service_id, key)
	}
}

/// Accumulation error.
#[derive(Debug, thiserror::Error)]
pub enum AccumulationError<E: core::fmt::Debug> {
	#[error("Accumulation API error: {0:?}")]
	Api(E),
	#[error("Work error: {0:?}")]
	Work(WorkError),
	#[error("JAM codec error: {0:?}")]
	Codec(codec::Error),
	#[error("Prior VM state mismatch")]
	PriorStateMismatch,
	#[error("Page {0} not found in storage")]
	NoSuchPage(PageNum),
	#[error("Page hash mismatch")]
	PageHashMismatch,
	#[error("ExecEnvRef mismatch")]
	ExecEnvRefMismatch,
	#[error("Processed message doesn't match the message in the queue")]
	MessageMismatch,
	#[error("Invalid message")]
	InvalidMessage,
	#[error("Unknown service {0:x}")]
	UnknownService(ServiceId),
	#[error("Transfer source is not the owner of the service: actual owner {0:x}, transfer source {1:x}")]
	NotTheOwner(ServiceId, ServiceId),
}

impl<E: core::fmt::Debug> From<codec::Error> for AccumulationError<E> {
	fn from(e: codec::Error) -> Self {
		Self::Codec(e)
	}
}

struct SliceOutput<'a>(&'a mut [u8]);

impl codec::Output for SliceOutput<'_> {
	fn write(&mut self, src: &[u8]) {
		let (dst, rest) = core::mem::take(&mut self.0).split_at_mut(src.len());
		dst.copy_from_slice(src);
		self.0 = rest;
	}
}