radix_engine/blueprints/transaction_processor/
package.rs

1use crate::errors::ApplicationError;
2use crate::errors::RuntimeError;
3use crate::internal_prelude::*;
4use crate::kernel::kernel_api::{KernelNodeApi, KernelSubstateApi};
5use crate::system::system_callback::SystemLockData;
6use radix_blueprint_schema_init::{
7    BlueprintEventSchemaInit, BlueprintFunctionsSchemaInit, BlueprintSchemaInit,
8    BlueprintStateSchemaInit, FunctionSchemaInit, TypeRef,
9};
10use radix_engine_interface::api::SystemApi;
11use radix_engine_interface::blueprints::package::{
12    AuthConfig, BlueprintDefinitionInit, BlueprintType, FunctionAuth, MethodAuthTemplate,
13    PackageDefinition,
14};
15use radix_engine_interface::blueprints::transaction_processor::*;
16
17use super::TransactionProcessorBlueprint;
18use super::TransactionProcessorV1MinorVersion;
19
20pub struct TransactionProcessorNativePackage;
21
22impl TransactionProcessorNativePackage {
23    pub fn definition() -> PackageDefinition {
24        let mut aggregator = TypeAggregator::<ScryptoCustomTypeKind>::new();
25
26        let fields = Vec::new();
27
28        let mut functions = index_map_new();
29        functions.insert(
30            TRANSACTION_PROCESSOR_RUN_IDENT.to_string(),
31            FunctionSchemaInit {
32                receiver: None,
33                input: TypeRef::Static(
34                    aggregator.add_child_type_and_descendents::<TransactionProcessorRunInput>(),
35                ),
36                output: TypeRef::Static(
37                    aggregator.add_child_type_and_descendents::<TransactionProcessorRunOutput>(),
38                ),
39                export: TRANSACTION_PROCESSOR_RUN_IDENT.to_string(),
40            },
41        );
42
43        let schema = generate_full_schema(aggregator);
44        let blueprints = indexmap!(
45            TRANSACTION_PROCESSOR_BLUEPRINT.to_string() => BlueprintDefinitionInit {
46                blueprint_type: BlueprintType::default(),
47                is_transient: true,
48                feature_set: indexset!(),
49                dependencies: indexset!(),
50                schema: BlueprintSchemaInit {
51                    generics: vec![],
52                    schema,
53                    state: BlueprintStateSchemaInit {
54                        fields,
55                        collections: vec![],
56                    },
57                    functions: BlueprintFunctionsSchemaInit {
58                        functions,
59                    },
60                    events: BlueprintEventSchemaInit::default(),
61                    types: BlueprintTypeSchemaInit::default(),
62                    hooks: BlueprintHooksInit::default(),
63                },
64                royalty_config: PackageRoyaltyConfig::default(),
65                auth_config: AuthConfig {
66                    // Only allow the root call frame to call any function in transaction processor.
67                    // This is a safety precaution to reduce surface area of attack. This may be removed
68                    // if/when the transaction processor is verified to be safe.
69                    function_auth: FunctionAuth::RootOnly,
70                    method_auth: MethodAuthTemplate::AllowAll,
71                },
72            }
73        );
74
75        PackageDefinition { blueprints }
76    }
77
78    pub fn invoke_export<
79        Y: SystemApi<RuntimeError> + KernelNodeApi + KernelSubstateApi<SystemLockData>,
80    >(
81        export_name: &str,
82        input: &IndexedScryptoValue,
83        version: TransactionProcessorV1MinorVersion,
84        api: &mut Y,
85    ) -> Result<IndexedScryptoValue, RuntimeError> {
86        match export_name {
87            TRANSACTION_PROCESSOR_RUN_IDENT => {
88                let input: TransactionProcessorRunInput = input.as_typed().map_err(|e| {
89                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
90                })?;
91
92                let rtn = TransactionProcessorBlueprint::run(
93                    input.manifest_encoded_instructions,
94                    input.global_address_reservations,
95                    input.references,
96                    input.blobs,
97                    version,
98                    api,
99                )?;
100
101                Ok(IndexedScryptoValue::from_typed(&rtn))
102            }
103            _ => Err(RuntimeError::ApplicationError(
104                ApplicationError::ExportDoesNotExist(export_name.to_string()),
105            )),
106        }
107    }
108}