1use super::call_frame::{CallFrameInit, CallFrameMessage};
2use crate::errors::*;
3use crate::internal_prelude::*;
4use crate::kernel::kernel_api::KernelInvocation;
5use crate::kernel::kernel_api::{KernelApi, KernelInternalApi};
6use crate::kernel::substate_io::SubstateDevice;
7use crate::track::interface::{IOAccess, NodeSubstates};
8use crate::track::*;
9use crate::transaction::ResourcesUsage;
10use radix_engine_interface::api::field_api::LockFlags;
11use radix_substate_store_interface::interface::SubstateDatabase;
12use radix_transactions::model::ExecutableTransaction;
13
14#[allow(clippy::len_without_is_empty)]
15pub trait CallFrameReferences {
16 fn global_references(&self) -> Vec<NodeId>;
17 fn direct_access_references(&self) -> Vec<NodeId>;
18 fn stable_transient_references(&self) -> Vec<NodeId>;
19
20 fn len(&self) -> usize;
21}
22
23#[derive(Debug)]
25pub enum CreateNodeEvent<'a> {
26 Start(&'a NodeId, &'a NodeSubstates),
27 IOAccess(&'a IOAccess),
28 End(&'a NodeId),
29}
30
31#[derive(Debug)]
32pub enum DropNodeEvent<'a> {
33 Start(&'a NodeId),
34 IOAccess(&'a IOAccess),
35 End(&'a NodeId, &'a NodeSubstates),
36}
37
38#[derive(Debug)]
39pub enum CheckReferenceEvent<'a> {
40 IOAccess(&'a IOAccess),
41}
42
43#[derive(Debug)]
44pub enum MoveModuleEvent<'a> {
45 IOAccess(&'a IOAccess),
46}
47
48#[derive(Debug)]
49pub enum OpenSubstateEvent<'a> {
50 Start {
51 node_id: &'a NodeId,
52 partition_num: &'a PartitionNumber,
53 substate_key: &'a SubstateKey,
54 flags: &'a LockFlags,
55 },
56 IOAccess(&'a IOAccess),
57 End {
58 handle: SubstateHandle,
59 node_id: &'a NodeId,
60 size: usize,
61 },
62}
63
64#[derive(Debug)]
65pub enum ReadSubstateEvent<'a> {
66 OnRead {
67 handle: SubstateHandle,
68 value: &'a IndexedScryptoValue,
69 device: SubstateDevice,
70 },
71 IOAccess(&'a IOAccess),
72}
73
74impl<'a> ReadSubstateEvent<'a> {
75 pub fn is_about_heap(&self) -> bool {
76 match self {
77 ReadSubstateEvent::OnRead { device, .. } => matches!(device, SubstateDevice::Heap),
78 ReadSubstateEvent::IOAccess(access) => match access {
79 IOAccess::ReadFromDb(_, _) => false,
80 IOAccess::ReadFromDbNotFound(_) => false,
81 IOAccess::TrackSubstateUpdated { .. } => false,
82 IOAccess::HeapSubstateUpdated { .. } => true,
83 },
84 }
85 }
86}
87
88#[derive(Debug)]
89pub enum WriteSubstateEvent<'a> {
90 Start {
91 handle: SubstateHandle,
92 value: &'a IndexedScryptoValue,
93 },
94 IOAccess(&'a IOAccess),
95}
96
97#[derive(Debug)]
98pub enum CloseSubstateEvent {
99 Start(SubstateHandle),
100}
101
102#[derive(Debug)]
103pub enum SetSubstateEvent<'a> {
104 Start(
105 &'a NodeId,
106 &'a PartitionNumber,
107 &'a SubstateKey,
108 &'a IndexedScryptoValue,
109 ),
110 IOAccess(&'a IOAccess),
111}
112
113#[derive(Debug)]
114pub enum RemoveSubstateEvent<'a> {
115 Start(&'a NodeId, &'a PartitionNumber, &'a SubstateKey),
116 IOAccess(&'a IOAccess),
117}
118
119#[derive(Debug)]
120pub enum ScanKeysEvent<'a> {
121 Start,
122 IOAccess(&'a IOAccess),
123}
124
125#[derive(Debug)]
126pub enum DrainSubstatesEvent<'a> {
127 Start(u32),
128 IOAccess(&'a IOAccess),
129}
130
131#[derive(Debug)]
132pub enum ScanSortedSubstatesEvent<'a> {
133 Start,
134 IOAccess(&'a IOAccess),
135}
136
137pub trait ExecutionReceipt {
139 fn set_resource_usage(&mut self, resources_usage: ResourcesUsage);
140}
141
142pub trait UniqueSeed {
145 fn unique_seed_for_id_allocator(&self) -> Hash;
146}
147
148impl UniqueSeed for ExecutableTransaction {
149 fn unique_seed_for_id_allocator(&self) -> Hash {
150 *self.unique_hash()
151 }
152}
153
154pub trait KernelTransactionExecutor: KernelCallbackObject {
155 type Init;
157 type Executable: UniqueSeed;
159 type ExecutionOutput;
161 type Receipt: ExecutionReceipt;
163
164 #[allow(clippy::type_complexity)]
166 fn init(
167 store: &mut impl CommitableSubstateStore,
168 executable: &Self::Executable,
169 init: Self::Init,
170 always_visible_global_nodes: &'static IndexSet<NodeId>,
171 ) -> Result<(Self, Vec<CallFrameInit<Self::CallFrameData>>), Self::Receipt>;
172
173 fn execute<Y: KernelApi<CallbackObject = Self>>(
175 api: &mut Y,
176 executable: &Self::Executable,
177 ) -> Result<Self::ExecutionOutput, RuntimeError>;
178
179 fn finalize(
181 &mut self,
182 executable: &Self::Executable,
183 store_commit_info: StoreCommitInfo,
184 ) -> Result<(), RuntimeError>;
185
186 fn create_receipt<S: SubstateDatabase>(
188 self,
189 track: Track<S>,
190 result: Result<Self::ExecutionOutput, TransactionExecutionError>,
191 ) -> Self::Receipt;
192}
193
194pub trait KernelCallbackObject: Sized {
196 type LockData: Default + Clone;
198 type CallFrameData: CallFrameReferences;
200
201 fn on_pin_node<Y: KernelInternalApi<System = Self>>(
203 node_id: &NodeId,
204 api: &mut Y,
205 ) -> Result<(), RuntimeError>;
206
207 fn on_create_node<Y: KernelInternalApi<System = Self>>(
209 event: CreateNodeEvent,
210 api: &mut Y,
211 ) -> Result<(), RuntimeError>;
212
213 fn on_drop_node<Y: KernelInternalApi<System = Self>>(
215 event: DropNodeEvent,
216 api: &mut Y,
217 ) -> Result<(), RuntimeError>;
218
219 fn on_move_module<Y: KernelInternalApi<System = Self>>(
221 event: MoveModuleEvent,
222 api: &mut Y,
223 ) -> Result<(), RuntimeError>;
224
225 fn on_open_substate<Y: KernelInternalApi<System = Self>>(
227 event: OpenSubstateEvent,
228 api: &mut Y,
229 ) -> Result<(), RuntimeError>;
230
231 fn on_close_substate<Y: KernelInternalApi<System = Self>>(
233 event: CloseSubstateEvent,
234 api: &mut Y,
235 ) -> Result<(), RuntimeError>;
236
237 fn on_read_substate<Y: KernelInternalApi<System = Self>>(
239 event: ReadSubstateEvent,
240 api: &mut Y,
241 ) -> Result<(), RuntimeError>;
242
243 fn on_write_substate<Y: KernelInternalApi<System = Self>>(
245 event: WriteSubstateEvent,
246 api: &mut Y,
247 ) -> Result<(), RuntimeError>;
248
249 fn on_set_substate<Y: KernelInternalApi<System = Self>>(
251 event: SetSubstateEvent,
252 api: &mut Y,
253 ) -> Result<(), RuntimeError>;
254
255 fn on_remove_substate<Y: KernelInternalApi<System = Self>>(
257 event: RemoveSubstateEvent,
258 api: &mut Y,
259 ) -> Result<(), RuntimeError>;
260
261 fn on_scan_keys<Y: KernelInternalApi<System = Self>>(
263 event: ScanKeysEvent,
264 api: &mut Y,
265 ) -> Result<(), RuntimeError>;
266
267 fn on_drain_substates<Y: KernelInternalApi<System = Self>>(
269 event: DrainSubstatesEvent,
270 api: &mut Y,
271 ) -> Result<(), RuntimeError>;
272
273 fn on_scan_sorted_substates<Y: KernelInternalApi<System = Self>>(
275 event: ScanSortedSubstatesEvent,
276 api: &mut Y,
277 ) -> Result<(), RuntimeError>;
278
279 fn before_invoke<Y: KernelApi<CallbackObject = Self>>(
281 invocation: &KernelInvocation<Self::CallFrameData>,
282 api: &mut Y,
283 ) -> Result<(), RuntimeError>;
284
285 fn on_execution_start<Y: KernelInternalApi<System = Self>>(
287 api: &mut Y,
288 ) -> Result<(), RuntimeError>;
289
290 fn invoke_upstream<Y: KernelApi<CallbackObject = Self>>(
292 args: &IndexedScryptoValue,
293 api: &mut Y,
294 ) -> Result<IndexedScryptoValue, RuntimeError>;
295
296 fn auto_drop<Y: KernelApi<CallbackObject = Self>>(
299 nodes: Vec<NodeId>,
300 api: &mut Y,
301 ) -> Result<(), RuntimeError>;
302
303 fn on_execution_finish<Y: KernelInternalApi<System = Self>>(
305 message: &CallFrameMessage,
306 api: &mut Y,
307 ) -> Result<(), RuntimeError>;
308
309 fn after_invoke<Y: KernelApi<CallbackObject = Self>>(
311 output: &IndexedScryptoValue,
312 api: &mut Y,
313 ) -> Result<(), RuntimeError>;
314
315 fn on_allocate_node_id<Y: KernelInternalApi<System = Self>>(
317 entity_type: EntityType,
318 api: &mut Y,
319 ) -> Result<(), RuntimeError>;
320
321 fn on_mark_substate_as_transient<Y: KernelInternalApi<System = Self>>(
323 node_id: &NodeId,
324 partition_number: &PartitionNumber,
325 substate_key: &SubstateKey,
326 api: &mut Y,
327 ) -> Result<(), RuntimeError>;
328
329 fn on_substate_lock_fault<Y: KernelApi<CallbackObject = Self>>(
331 node_id: NodeId,
332 partition_num: PartitionNumber,
333 offset: &SubstateKey,
334 api: &mut Y,
335 ) -> Result<bool, RuntimeError>;
336
337 fn on_drop_node_mut<Y: KernelApi<CallbackObject = Self>>(
339 node_id: &NodeId,
340 api: &mut Y,
341 ) -> Result<(), RuntimeError>;
342
343 fn on_get_stack_id<Y: KernelInternalApi<System = Self>>(
344 api: &mut Y,
345 ) -> Result<(), RuntimeError>;
346
347 fn on_switch_stack<Y: KernelInternalApi<System = Self>>(
348 api: &mut Y,
349 ) -> Result<(), RuntimeError>;
350
351 fn on_send_to_stack<Y: KernelInternalApi<System = Self>>(
352 value: &IndexedScryptoValue,
353 api: &mut Y,
354 ) -> Result<(), RuntimeError>;
355
356 fn on_set_call_frame_data<Y: KernelInternalApi<System = Self>>(
357 data: &Self::CallFrameData,
358 api: &mut Y,
359 ) -> Result<(), RuntimeError>;
360
361 fn on_get_owned_nodes<Y: KernelInternalApi<System = Self>>(
362 api: &mut Y,
363 ) -> Result<(), RuntimeError>;
364}