radix_transactions/model/v1/
system_manifest_v1.rs

1use super::*;
2use crate::internal_prelude::*;
3
4//=================================================================================
5// NOTE:
6// This isn't actually embedded as a model - it's just a useful model which we use
7// in eg the manifest builder
8//=================================================================================
9
10/// Can be built with a [`SystemV1ManifestBuilder`]
11#[derive(Debug, Clone, Default, PartialEq, Eq, ManifestSbor, ScryptoDescribe)]
12pub struct SystemTransactionManifestV1 {
13    pub instructions: Vec<InstructionV1>,
14    pub blobs: IndexMap<Hash, Vec<u8>>,
15    pub preallocated_addresses: Vec<PreAllocatedAddress>,
16    pub object_names: ManifestObjectNames,
17}
18
19impl ReadableManifestBase for SystemTransactionManifestV1 {
20    fn is_subintent(&self) -> bool {
21        false
22    }
23
24    fn get_blobs<'a>(&'a self) -> impl Iterator<Item = (&'a Hash, &'a Vec<u8>)> {
25        self.blobs.iter()
26    }
27
28    fn get_preallocated_addresses(&self) -> &[PreAllocatedAddress] {
29        &self.preallocated_addresses
30    }
31
32    fn get_known_object_names_ref(&self) -> ManifestObjectNamesRef {
33        self.object_names.as_ref()
34    }
35}
36
37impl TypedReadableManifest for SystemTransactionManifestV1 {
38    type Instruction = InstructionV1;
39
40    fn get_typed_instructions(&self) -> &[Self::Instruction] {
41        &self.instructions
42    }
43}
44
45impl BuildableManifest for SystemTransactionManifestV1 {
46    fn add_instruction(&mut self, instruction: Self::Instruction) {
47        self.instructions.push(instruction)
48    }
49
50    fn add_blob(&mut self, hash: Hash, content: Vec<u8>) {
51        self.blobs.insert(hash, content);
52    }
53
54    fn set_names(&mut self, names: KnownManifestObjectNames) {
55        self.object_names = names.into()
56    }
57
58    fn add_preallocated_address(
59        &mut self,
60        preallocated: PreAllocatedAddress,
61    ) -> Result<(), ManifestBuildError> {
62        self.preallocated_addresses.push(preallocated);
63        Ok(())
64    }
65
66    fn preallocation_count(&self) -> usize {
67        self.preallocated_addresses.len()
68    }
69
70    fn default_test_execution_config_type(&self) -> DefaultTestExecutionConfigType {
71        DefaultTestExecutionConfigType::System
72    }
73
74    fn into_executable_with_proofs(
75        self,
76        nonce: u32,
77        initial_proofs: BTreeSet<NonFungibleGlobalId>,
78        validator: &TransactionValidator,
79    ) -> Result<ExecutableTransaction, String> {
80        let unique_hash = hash(format!("System txn: {}", nonce));
81        self.into_transaction(unique_hash)
82            .with_proofs_ref(initial_proofs)
83            .into_executable(&validator)
84            .map_err(|err| format!("Could not prepare: {err:?}"))
85    }
86}
87
88impl BuildableManifestSupportingPreallocatedAddresses for SystemTransactionManifestV1 {}
89
90impl SystemTransactionManifestV1 {
91    pub fn from_transaction(transaction: &SystemTransactionV1) -> Self {
92        Self {
93            instructions: transaction.instructions.clone().into(),
94            blobs: transaction.blobs.clone().into(),
95            preallocated_addresses: transaction.pre_allocated_addresses.clone(),
96            object_names: ManifestObjectNames::Unknown,
97        }
98    }
99
100    pub fn into_transaction(self, unique_hash: Hash) -> SystemTransactionV1 {
101        SystemTransactionV1 {
102            instructions: self.instructions.into(),
103            blobs: self.blobs.into(),
104            pre_allocated_addresses: self.preallocated_addresses,
105            hash_for_execution: unique_hash,
106        }
107    }
108
109    pub fn into_transaction_with_proofs(
110        self,
111        unique_hash: Hash,
112        initial_proofs: BTreeSet<NonFungibleGlobalId>,
113    ) -> SystemTransactionV1WithProofs<'static> {
114        self.into_transaction(unique_hash)
115            .with_proofs(initial_proofs)
116    }
117}