radix_transactions/model/v2/
transaction_manifest_v2.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 [`ManifestV2Builder`]
11#[derive(Debug, Clone, Default, PartialEq, Eq, ManifestSbor, ScryptoDescribe)]
12pub struct TransactionManifestV2 {
13    pub instructions: Vec<InstructionV2>,
14    pub blobs: IndexMap<Hash, Vec<u8>>,
15    pub children: IndexSet<ChildSubintentSpecifier>,
16    pub object_names: ManifestObjectNames,
17}
18
19impl ReadableManifestBase for TransactionManifestV2 {
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_child_subintent_hashes<'a>(
29        &'a self,
30    ) -> impl ExactSizeIterator<Item = &'a ChildSubintentSpecifier> {
31        self.children.iter()
32    }
33
34    fn get_known_object_names_ref(&self) -> ManifestObjectNamesRef {
35        self.object_names.as_ref()
36    }
37}
38
39impl TypedReadableManifest for TransactionManifestV2 {
40    type Instruction = InstructionV2;
41
42    fn get_typed_instructions(&self) -> &[Self::Instruction] {
43        &self.instructions
44    }
45}
46
47impl BuildableManifest for TransactionManifestV2 {
48    fn add_instruction(&mut self, instruction: Self::Instruction) {
49        self.instructions.push(instruction)
50    }
51
52    fn add_blob(&mut self, hash: Hash, content: Vec<u8>) {
53        self.blobs.insert(hash, content);
54    }
55
56    fn set_names(&mut self, names: KnownManifestObjectNames) {
57        self.object_names = names.into()
58    }
59
60    fn add_child_subintent(&mut self, hash: SubintentHash) -> Result<(), ManifestBuildError> {
61        if !self.children.insert(ChildSubintentSpecifier { hash }) {
62            return Err(ManifestBuildError::DuplicateChildSubintentHash);
63        }
64        Ok(())
65    }
66
67    fn default_test_execution_config_type(&self) -> DefaultTestExecutionConfigType {
68        DefaultTestExecutionConfigType::Test
69    }
70
71    fn into_executable_with_proofs(
72        self,
73        nonce: u32,
74        initial_proofs: BTreeSet<NonFungibleGlobalId>,
75        validator: &TransactionValidator,
76    ) -> Result<ExecutableTransaction, String> {
77        TestTransaction::new_v2_builder(nonce)
78            .finish_with_root_intent(self, initial_proofs)
79            .into_executable(validator)
80            .map_err(|err| format!("Could not prepare: {err:?}"))
81    }
82}
83
84impl BuildableManifestSupportingChildren for TransactionManifestV2 {}
85
86impl TransactionManifestV2 {
87    pub fn empty() -> Self {
88        Self {
89            instructions: Default::default(),
90            blobs: Default::default(),
91            children: Default::default(),
92            object_names: ManifestObjectNames::Unknown,
93        }
94    }
95
96    pub fn from_intent_core(intent: &IntentCoreV2) -> Self {
97        Self {
98            instructions: intent.instructions.to_vec(),
99            blobs: intent.blobs.clone().into(),
100            children: intent.children.children.clone(),
101            object_names: ManifestObjectNames::Unknown,
102        }
103    }
104
105    pub fn to_intent_core(self, header: IntentHeaderV2, message: MessageV2) -> IntentCoreV2 {
106        IntentCoreV2 {
107            header,
108            blobs: self.blobs.into(),
109            message,
110            instructions: self.instructions.into(),
111            children: ChildSubintentSpecifiersV2 {
112                children: self.children,
113            },
114        }
115    }
116
117    pub fn for_intent(self) -> (InstructionsV2, BlobsV1, ChildSubintentSpecifiersV2) {
118        (
119            self.instructions.into(),
120            self.blobs.into(),
121            ChildSubintentSpecifiersV2 {
122                children: self.children,
123            },
124        )
125    }
126
127    pub fn for_intent_with_names(
128        self,
129    ) -> (
130        InstructionsV2,
131        BlobsV1,
132        ChildSubintentSpecifiersV2,
133        ManifestObjectNames,
134    ) {
135        (
136            self.instructions.into(),
137            self.blobs.into(),
138            ChildSubintentSpecifiersV2 {
139                children: self.children,
140            },
141            self.object_names,
142        )
143    }
144}