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
use crate::blueprints::access_controller::v1::*;
use crate::blueprints::access_controller::v2::*;
use crate::blueprints::account::{AccountBlueprintBottlenoseExtension, AccountNativePackage};
use crate::blueprints::consensus_manager::{
    ConsensusManagerNativePackage, ConsensusManagerSecondsPrecisionNativeCode,
};
use crate::blueprints::identity::IdentityNativePackage;
use crate::blueprints::locker::LockerNativePackage;
use crate::blueprints::package::PackageNativePackage;
use crate::blueprints::pool::v1::package::*;
use crate::blueprints::resource::ResourceNativePackage;
use crate::blueprints::test_utils::TestUtilsNativePackage;
use crate::blueprints::transaction_processor::{
    TransactionProcessorNativePackage, TransactionProcessorV1MinorVersion,
};
use crate::blueprints::transaction_tracker::TransactionTrackerNativePackage;
use crate::errors::{NativeRuntimeError, RuntimeError, VmError};
use crate::internal_prelude::*;
use crate::kernel::kernel_api::{KernelNodeApi, KernelSubstateApi};
use crate::object_modules::metadata::MetadataNativePackage;
use crate::object_modules::role_assignment::*;
use crate::object_modules::royalty::RoyaltyNativePackage;
use crate::system::system_callback::SystemLockData;
use crate::vm::{VmApi, VmInvoke};
use radix_engine_interface::api::ClientApi;
use radix_engine_interface::blueprints::package::*;
use radix_engine_profiling_derive::trace_resources;

#[derive(Clone)]
pub struct NativeVm<E: NativeVmExtension> {
    extension: E,
}

impl<E: NativeVmExtension> NativeVm<E> {
    pub fn new_with_extension(extension: E) -> Self {
        Self { extension }
    }

    pub fn create_instance(
        &self,
        package_address: &PackageAddress,
        code: &[u8],
    ) -> Result<NativeVmInstance<E::Instance>, RuntimeError> {
        if let Some(custom_invoke) = self.extension.try_create_instance(code) {
            return Ok(NativeVmInstance::Extension(custom_invoke));
        }

        let code: [u8; 8] = match code.try_into() {
            Ok(code) => code,
            // It should be impossible for us to get to this point here. The code argument is
            // provided by the Vm after it reads the `PackageCodeOriginalCodeEntrySubstate`. Thus,
            // if the code-id at this point is invalid for the native-vm, then this means that the
            // database has been corrupted. We could safely panic here, however, we're choosing to
            // keep the `Err` here for safety.
            Err(..) => {
                return Err(RuntimeError::VmError(VmError::Native(
                    NativeRuntimeError::InvalidCodeId,
                )));
            }
        };
        let native_package_code_id = u64::from_be_bytes(code);
        let instance = NativeVmInstance::Native {
            package_address: *package_address,
            native_package_code_id,
        };

        Ok(instance)
    }
}

pub enum NativeVmInstance<I: VmInvoke> {
    Native {
        // Used by profiling
        #[allow(dead_code)]
        package_address: PackageAddress,
        native_package_code_id: u64,
    },
    Extension(I),
}

impl<I: VmInvoke> NativeVmInstance<I> {
    // Used by profiling
    #[allow(dead_code)]
    pub fn package_address(&self) -> PackageAddress {
        match self {
            NativeVmInstance::Native {
                package_address, ..
            } => package_address.clone(),
            _ => panic!("Profiling with NativeVmExtension is not supported."),
        }
    }
}

impl<I: VmInvoke> VmInvoke for NativeVmInstance<I> {
    #[trace_resources(log=self.package_address().is_native_package(), log=self.package_address().to_hex(), log=export_name)]
    fn invoke<Y, V>(
        &mut self,
        export_name: &str,
        input: &IndexedScryptoValue,
        api: &mut Y,
        vm_api: &V,
    ) -> Result<IndexedScryptoValue, RuntimeError>
    where
        Y: ClientApi<RuntimeError> + KernelNodeApi + KernelSubstateApi<SystemLockData>,
        V: VmApi,
    {
        #[allow(unused_mut)]
        let mut func = || match self {
            NativeVmInstance::Extension(e) => e.invoke(export_name, input, api, vm_api),
            NativeVmInstance::Native {
                native_package_code_id,
                package_address,
            } => {
                api.consume_cost_units(ClientCostingEntry::RunNativeCode {
                    package_address: package_address,
                    export_name: export_name,
                    input_size: input.len(),
                })?;

                let code_id = NativeCodeId::from_repr(*native_package_code_id).ok_or(
                    RuntimeError::VmError(VmError::Native(NativeRuntimeError::InvalidCodeId)),
                )?;

                match code_id {
                    NativeCodeId::PackageCode1 => PackageNativePackage::invoke_export(
                        export_name,
                        input,
                        PackageV1MinorVersion::Zero,
                        api,
                        vm_api,
                    ),
                    NativeCodeId::PackageCode2 => PackageNativePackage::invoke_export(
                        export_name,
                        input,
                        PackageV1MinorVersion::One,
                        api,
                        vm_api,
                    ),
                    NativeCodeId::ResourceCode1 => {
                        ResourceNativePackage::invoke_export(export_name, input, api)
                    }
                    NativeCodeId::ConsensusManagerCode1 => {
                        ConsensusManagerNativePackage::invoke_export(export_name, input, api)
                    }
                    NativeCodeId::ConsensusManagerCode2 => {
                        ConsensusManagerSecondsPrecisionNativeCode::invoke_export(
                            export_name,
                            input,
                            api,
                        )
                    }
                    NativeCodeId::IdentityCode1 => {
                        IdentityNativePackage::invoke_export(export_name, input, api)
                    }
                    NativeCodeId::AccountCode1 => {
                        AccountNativePackage::invoke_export(export_name, input, api)
                    }
                    NativeCodeId::AccountCode2 => {
                        AccountBlueprintBottlenoseExtension::invoke_export(export_name, input, api)
                    }
                    NativeCodeId::AccessControllerCode1 => {
                        AccessControllerV1NativePackage::invoke_export(export_name, input, api)
                    }
                    NativeCodeId::AccessControllerCode2 => {
                        AccessControllerV2NativePackage::invoke_export(export_name, input, api)
                    }
                    NativeCodeId::TransactionProcessorCode1 => {
                        TransactionProcessorNativePackage::invoke_export(
                            export_name,
                            input,
                            TransactionProcessorV1MinorVersion::Zero,
                            api,
                        )
                    }
                    NativeCodeId::TransactionProcessorCode2 => {
                        TransactionProcessorNativePackage::invoke_export(
                            export_name,
                            input,
                            TransactionProcessorV1MinorVersion::One,
                            api,
                        )
                    }
                    NativeCodeId::MetadataCode1 => {
                        MetadataNativePackage::invoke_export(export_name, input, api)
                    }
                    NativeCodeId::RoyaltyCode1 => {
                        RoyaltyNativePackage::invoke_export(export_name, input, api)
                    }
                    NativeCodeId::RoleAssignmentCode1 => {
                        RoleAssignmentNativePackage::invoke_export(export_name, input, api)
                    }
                    NativeCodeId::RoleAssignmentCode2 => {
                        RoleAssignmentBottlenoseExtension::invoke_export(export_name, input, api)
                    }
                    NativeCodeId::PoolCode1 => PoolNativePackage::invoke_export(
                        export_name,
                        input,
                        PoolV1MinorVersion::Zero,
                        api,
                    ),
                    NativeCodeId::PoolCode2 => PoolNativePackage::invoke_export(
                        export_name,
                        input,
                        PoolV1MinorVersion::One,
                        api,
                    ),
                    NativeCodeId::TransactionTrackerCode1 => {
                        TransactionTrackerNativePackage::invoke_export(export_name, input, api)
                    }
                    NativeCodeId::TestUtilsCode1 => {
                        TestUtilsNativePackage::invoke_export(export_name, input, api)
                    }
                    NativeCodeId::LockerCode1 => {
                        LockerNativePackage::invoke_export(export_name, input, api)
                    }
                }
            }
        };

        // Note: we can't unwind if we're compiling for no-std. See:
        // https://github.com/rust-lang/rfcs/issues/2810
        {
            #[cfg(feature = "std")]
            {
                match std::panic::catch_unwind(std::panic::AssertUnwindSafe(func)) {
                    Ok(rtn) => rtn,
                    Err(cause) => {
                        let message = if let Some(s) = cause.downcast_ref::<&'static str>() {
                            (*s).to_string()
                        } else if let Some(s) = cause.downcast_ref::<String>() {
                            s.clone()
                        } else {
                            "Unknown panic!".to_string()
                        };
                        Err(RuntimeError::VmError(VmError::Native(
                            NativeRuntimeError::Trap {
                                export_name: export_name.to_owned(),
                                input: input.as_scrypto_value().clone(),
                                error: message,
                            },
                        )))
                    }
                }
            }

            #[cfg(not(feature = "std"))]
            func()
        }
    }
}

pub trait NativeVmExtension: Clone {
    type Instance: VmInvoke + Clone;

    fn try_create_instance(&self, code: &[u8]) -> Option<Self::Instance>;
}

#[derive(Clone)]
pub struct NoExtension;
impl NativeVmExtension for NoExtension {
    type Instance = NullVmInvoke;
    fn try_create_instance(&self, _code: &[u8]) -> Option<Self::Instance> {
        None
    }
}

pub type DefaultNativeVm = NativeVm<NoExtension>;

impl DefaultNativeVm {
    pub fn new() -> Self {
        NativeVm::new_with_extension(NoExtension)
    }
}

#[derive(Clone)]
pub struct NullVmInvoke;

impl VmInvoke for NullVmInvoke {
    fn invoke<Y, V>(
        &mut self,
        _export_name: &str,
        _input: &IndexedScryptoValue,
        _api: &mut Y,
        _vm_api: &V,
    ) -> Result<IndexedScryptoValue, RuntimeError>
    where
        Y: ClientApi<RuntimeError> + KernelNodeApi + KernelSubstateApi<SystemLockData>,
        V: VmApi,
    {
        panic!("Invocation was called on null VmInvoke");
    }
}

#[derive(Clone)]
pub struct OverridePackageCode<C: VmInvoke + Clone> {
    custom_package_code_id: u64,
    custom_invoke: C,
}

impl<C: VmInvoke + Clone> OverridePackageCode<C> {
    pub fn new(custom_package_code_id: u64, custom_invoke: C) -> Self {
        Self {
            custom_package_code_id,
            custom_invoke,
        }
    }
}

impl<C: VmInvoke + Clone> NativeVmExtension for OverridePackageCode<C> {
    type Instance = C;

    fn try_create_instance(&self, code: &[u8]) -> Option<C> {
        let code_id = {
            let code: [u8; 8] = match code.try_into() {
                Ok(code) => code,
                Err(..) => return None,
            };
            u64::from_be_bytes(code)
        };

        if self.custom_package_code_id == code_id {
            Some(self.custom_invoke.clone())
        } else {
            None
        }
    }
}