radix-engine 1.3.1

Reference implementation of Radix Engine, from the Radix DLT project.
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use crate::blueprints::package::*;
use crate::errors::{ApplicationError, RuntimeError};
use crate::internal_prelude::*;
use crate::kernel::kernel_api::{KernelNodeApi, KernelSubstateApi};
use crate::system::system_callback::*;
use crate::system::system_callback_api::SystemCallbackObject;
use crate::system::system_substates::KeyValueEntrySubstate;
use crate::vm::wasm::{ScryptoV1WasmValidator, WasmEngine};
use crate::vm::{NativeVm, NativeVmExtension, ScryptoVm};
use radix_engine_interface::api::field_api::LockFlags;
use radix_engine_interface::api::SystemApi;

use crate::vm::ScryptoVmVersion;

use super::wasm::DefaultWasmEngine;
use super::NoExtension;

pub type VmBootSubstate = VmBoot;

#[derive(Debug, Clone, PartialEq, Eq, Sbor, ScryptoSborAssertion)]
#[sbor_assert(backwards_compatible(cuttlefish = "FILE:vm_boot_substate_cuttlefish_schema.bin",))]
pub enum VmBoot {
    V1 { scrypto_version: u64 },
}

impl VmBoot {
    /// Loads vm boot from the database, or resolves a fallback.
    pub fn load(substate_db: &impl SubstateDatabase) -> Self {
        substate_db
            .get_substate(
                TRANSACTION_TRACKER,
                BOOT_LOADER_PARTITION,
                BootLoaderField::VmBoot,
            )
            .unwrap_or_else(Self::babylon_genesis)
    }

    pub fn latest() -> Self {
        Self::bottlenose()
    }

    pub fn bottlenose() -> Self {
        Self::V1 {
            scrypto_version: ScryptoVmVersion::V1_1.into(),
        }
    }

    pub fn babylon_genesis() -> Self {
        Self::V1 {
            scrypto_version: ScryptoVmVersion::V1_0.into(),
        }
    }
}

pub trait VmApi {
    fn get_scrypto_version(&self) -> ScryptoVmVersion;
}

impl VmApi for VmBoot {
    fn get_scrypto_version(&self) -> ScryptoVmVersion {
        match self {
            VmBoot::V1 { scrypto_version } => ScryptoVmVersion::try_from(*scrypto_version)
                .unwrap_or_else(|_| panic!("Unexpected scrypto version: {}", scrypto_version)),
        }
    }
}

/// This trait is intended to encapsulate the data and types required to
/// initalize the VMs in the engine.
///
/// The canonical implementation is [`VmModules`] - but we have this trait
/// as well so that functions can take an `&impl VmInitialize` parameter,
/// and we can avoid the proliferation of [`WasmEngine`] and
/// [`NativeVmExtension`] generics across the codebase.
pub trait VmInitialize {
    type WasmEngine: WasmEngine;
    type NativeVmExtension: NativeVmExtension;

    fn get_vm_extension(&self) -> Self::NativeVmExtension;

    fn get_scrypto_vm(&self) -> &ScryptoVm<Self::WasmEngine>;
}

pub struct VmModules<W: WasmEngine, E: NativeVmExtension> {
    pub scrypto_vm: ScryptoVm<W>,
    pub vm_extension: E,
}

impl<W: WasmEngine, E: NativeVmExtension> VmModules<W, E> {
    pub fn new(scrypto_vm: ScryptoVm<W>, vm_extension: E) -> Self {
        Self {
            scrypto_vm,
            vm_extension,
        }
    }
}

impl<E: NativeVmExtension> VmModules<DefaultWasmEngine, E> {
    pub fn default_with_extension(vm_extension: E) -> Self {
        Self {
            scrypto_vm: Default::default(),
            vm_extension,
        }
    }
}

pub type DefaultVmModules = VmModules<DefaultWasmEngine, NoExtension>;

impl Default for DefaultVmModules {
    fn default() -> Self {
        Self {
            scrypto_vm: ScryptoVm::default(),
            vm_extension: NoExtension,
        }
    }
}

impl<W: WasmEngine, E: NativeVmExtension> VmInitialize for VmModules<W, E> {
    type WasmEngine = W;
    type NativeVmExtension = E;

    fn get_vm_extension(&self) -> Self::NativeVmExtension {
        self.vm_extension.clone()
    }

    fn get_scrypto_vm(&self) -> &ScryptoVm<Self::WasmEngine> {
        &self.scrypto_vm
    }
}

pub struct VmInit<'g, W: WasmEngine, E: NativeVmExtension> {
    pub scrypto_vm: &'g ScryptoVm<W>,
    pub native_vm_extension: E,
    pub vm_boot: VmBoot,
}

impl<'g, W: WasmEngine, E: NativeVmExtension> InitializationParameters for VmInit<'g, W, E> {
    type For = Vm<'g, W, E>;
}

impl<'g, W: WasmEngine, E: NativeVmExtension> VmInit<'g, W, E> {
    pub fn load(
        substate_db: &impl SubstateDatabase,
        vm_modules: &'g impl VmInitialize<WasmEngine = W, NativeVmExtension = E>,
    ) -> Self {
        Self {
            scrypto_vm: vm_modules.get_scrypto_vm(),
            native_vm_extension: vm_modules.get_vm_extension(),
            vm_boot: VmBoot::load(substate_db),
        }
    }
}

pub struct Vm<'g, W: WasmEngine, E: NativeVmExtension> {
    pub scrypto_vm: &'g ScryptoVm<W>,
    pub native_vm: NativeVm<E>,
    pub vm_boot: VmBoot,
}

impl<'g, W: WasmEngine + 'g, E: NativeVmExtension> SystemCallbackObject for Vm<'g, W, E> {
    type Init = VmInit<'g, W, E>;

    fn init(vm_init: VmInit<'g, W, E>) -> Result<Self, BootloadingError> {
        Ok(Self {
            scrypto_vm: vm_init.scrypto_vm,
            native_vm: NativeVm::new_with_extension(vm_init.native_vm_extension),
            vm_boot: vm_init.vm_boot,
        })
    }

    fn invoke<
        Y: SystemApi<RuntimeError>
            + SystemBasedKernelInternalApi<SystemCallback = Self>
            + KernelNodeApi
            + KernelSubstateApi<SystemLockData>,
    >(
        address: &PackageAddress,
        export: PackageExport,
        input: &IndexedScryptoValue,
        api: &mut Y,
    ) -> Result<IndexedScryptoValue, RuntimeError> {
        let vm_type = {
            let handle = api.kernel_open_substate_with_default(
                address.as_node_id(),
                MAIN_BASE_PARTITION
                    .at_offset(PACKAGE_VM_TYPE_PARTITION_OFFSET)
                    .unwrap(),
                &SubstateKey::Map(scrypto_encode(&export.code_hash).unwrap()),
                LockFlags::read_only(),
                Some(|| {
                    let kv_entry = KeyValueEntrySubstate::<()>::default();
                    IndexedScryptoValue::from_typed(&kv_entry)
                }),
                SystemLockData::default(),
            )?;
            let vm_type = api.kernel_read_substate(handle)?;
            let vm_type: PackageCodeVmTypeEntrySubstate = vm_type.as_typed().unwrap();
            api.kernel_close_substate(handle)?;
            vm_type
                .into_value()
                .unwrap_or_else(|| panic!("Vm type not found: {:?}", export))
        };

        let vm_api = api.kernel_get_system().callback.vm_boot.clone();

        let output = match vm_type.fully_update_and_into_latest_version().vm_type {
            VmType::Native => {
                let original_code = {
                    let handle = api.kernel_open_substate_with_default(
                        address.as_node_id(),
                        MAIN_BASE_PARTITION
                            .at_offset(PACKAGE_ORIGINAL_CODE_PARTITION_OFFSET)
                            .unwrap(),
                        &SubstateKey::Map(scrypto_encode(&export.code_hash).unwrap()),
                        LockFlags::read_only(),
                        Some(|| {
                            let kv_entry = KeyValueEntrySubstate::<()>::default();
                            IndexedScryptoValue::from_typed(&kv_entry)
                        }),
                        SystemLockData::default(),
                    )?;
                    let original_code = api.kernel_read_substate(handle)?;
                    let original_code: PackageCodeOriginalCodeEntrySubstate =
                        original_code.as_typed().unwrap();
                    api.kernel_close_substate(handle)?;
                    original_code
                        .into_value()
                        .unwrap_or_else(|| panic!("Original code not found: {:?}", export))
                };

                let mut vm_instance = api.kernel_get_system().callback.native_vm.create_instance(
                    address,
                    &original_code.fully_update_and_into_latest_version().code,
                )?;
                let output =
                    { vm_instance.invoke(export.export_name.as_str(), input, api, &vm_api)? };

                output
            }
            VmType::ScryptoV1 => {
                let instrumented_code = {
                    let handle = api.kernel_open_substate_with_default(
                        address.as_node_id(),
                        MAIN_BASE_PARTITION
                            .at_offset(PACKAGE_INSTRUMENTED_CODE_PARTITION_OFFSET)
                            .unwrap(),
                        &SubstateKey::Map(scrypto_encode(&export.code_hash).unwrap()),
                        LockFlags::read_only(),
                        Some(|| {
                            let kv_entry = KeyValueEntrySubstate::<()>::default();
                            IndexedScryptoValue::from_typed(&kv_entry)
                        }),
                        SystemLockData::default(),
                    )?;
                    let instrumented_code = api.kernel_read_substate(handle)?;
                    let instrumented_code: PackageCodeInstrumentedCodeEntrySubstate =
                        instrumented_code.as_typed().unwrap();
                    api.kernel_close_substate(handle)?;
                    instrumented_code
                        .into_value()
                        .unwrap_or_else(|| panic!("Instrumented code not found: {:?}", export))
                        .fully_update_and_into_latest_version()
                };

                let mut scrypto_vm_instance = {
                    api.kernel_get_system().callback.scrypto_vm.create_instance(
                        address,
                        export.code_hash,
                        &instrumented_code.instrumented_code,
                    )
                };

                api.consume_cost_units(ClientCostingEntry::PrepareWasmCode {
                    size: instrumented_code.instrumented_code.len(),
                })?;

                let output = {
                    scrypto_vm_instance.invoke(export.export_name.as_str(), input, api, &vm_api)?
                };

                output
            }
        };

        Ok(output)
    }
}

pub trait VmInvoke {
    // TODO: Remove KernelNodeAPI + KernelSubstateAPI from api, unify with VmApi
    fn invoke<
        Y: SystemApi<RuntimeError>
            + KernelNodeApi
            + KernelSubstateApi<SystemLockData>
            + SystemBasedKernelInternalApi,
        V: VmApi,
    >(
        &mut self,
        export_name: &str,
        input: &IndexedScryptoValue,
        api: &mut Y,
        vm_api: &V,
    ) -> Result<IndexedScryptoValue, RuntimeError>;
}

pub struct VmPackageValidation;

impl VmPackageValidation {
    pub fn validate<V: VmApi>(
        definition: &PackageDefinition,
        vm_type: VmType,
        code: &[u8],
        vm_api: &V,
    ) -> Result<Option<Vec<u8>>, RuntimeError> {
        match vm_type {
            VmType::Native => Ok(None),
            VmType::ScryptoV1 => {
                let version = vm_api.get_scrypto_version();

                // Validate WASM
                let instrumented_code = ScryptoV1WasmValidator::new(version)
                    .validate(code, definition.blueprints.values())
                    .map_err(|e| {
                        RuntimeError::ApplicationError(ApplicationError::PackageError(
                            PackageError::InvalidWasm(e),
                        ))
                    })?
                    .0;

                for BlueprintDefinitionInit {
                    is_transient,
                    blueprint_type,
                    feature_set,
                    schema:
                        BlueprintSchemaInit {
                            generics,
                            state:
                                BlueprintStateSchemaInit {
                                    collections,
                                    fields,
                                },
                            functions,
                            hooks,
                            ..
                        },
                    ..
                } in definition.blueprints.values()
                {
                    match blueprint_type {
                        BlueprintType::Outer => {}
                        BlueprintType::Inner { .. } => {
                            return Err(RuntimeError::ApplicationError(
                                ApplicationError::PackageError(PackageError::WasmUnsupported(
                                    "Inner blueprints not supported".to_string(),
                                )),
                            ));
                        }
                    }

                    if !feature_set.is_empty() {
                        return Err(RuntimeError::ApplicationError(
                            ApplicationError::PackageError(PackageError::WasmUnsupported(
                                "Feature set not supported".to_string(),
                            )),
                        ));
                    }

                    if !collections.is_empty() {
                        return Err(RuntimeError::ApplicationError(
                            ApplicationError::PackageError(PackageError::WasmUnsupported(
                                "Static collections not supported".to_string(),
                            )),
                        ));
                    }

                    if fields.len() > 1 {
                        return Err(RuntimeError::ApplicationError(
                            ApplicationError::PackageError(PackageError::WasmUnsupported(
                                "More than 1 substate field not supported".to_string(),
                            )),
                        ));
                    }

                    for field in fields {
                        match &field.condition {
                            Condition::Always => {}
                            _ => {
                                return Err(RuntimeError::ApplicationError(
                                    ApplicationError::PackageError(PackageError::WasmUnsupported(
                                        "Conditional fields are not supported".to_string(),
                                    )),
                                ));
                            }
                        }

                        match field.transience {
                            FieldTransience::NotTransient => {}
                            _ => {
                                return Err(RuntimeError::ApplicationError(
                                    ApplicationError::PackageError(PackageError::WasmUnsupported(
                                        "Transient fields are not supported".to_string(),
                                    )),
                                ));
                            }
                        }
                    }

                    if !hooks.hooks.is_empty() {
                        return Err(RuntimeError::ApplicationError(
                            ApplicationError::PackageError(PackageError::WasmUnsupported(
                                "Hooks not supported".to_string(),
                            )),
                        ));
                    }

                    for (_name, schema) in &functions.functions {
                        if let Some(info) = &schema.receiver {
                            if info.ref_types != RefTypes::NORMAL {
                                return Err(RuntimeError::ApplicationError(
                                    ApplicationError::PackageError(PackageError::WasmUnsupported(
                                        "Irregular ref types not supported".to_string(),
                                    )),
                                ));
                            }
                        }
                    }

                    if !generics.is_empty() {
                        return Err(RuntimeError::ApplicationError(
                            ApplicationError::PackageError(PackageError::WasmUnsupported(
                                "Generics not supported".to_string(),
                            )),
                        ));
                    }

                    if *is_transient {
                        return Err(RuntimeError::ApplicationError(
                            ApplicationError::PackageError(PackageError::WasmUnsupported(
                                "Transient blueprints not supported".to_string(),
                            )),
                        ));
                    }
                }
                Ok(Some(instrumented_code))
            }
        }
    }
}