miden_testing/tx_context/
builder.rs1use alloc::collections::BTreeMap;
5use alloc::sync::Arc;
6use alloc::vec::Vec;
7
8use anyhow::Context;
9use miden_processor::{AdviceInputs, Felt, Word};
10use miden_protocol::EMPTY_WORD;
11use miden_protocol::account::auth::{PublicKeyCommitment, Signature};
12use miden_protocol::account::{Account, AccountHeader, AccountId};
13use miden_protocol::assembly::DefaultSourceManager;
14use miden_protocol::assembly::debuginfo::SourceManagerSync;
15use miden_protocol::block::account_tree::AccountWitness;
16use miden_protocol::note::{Note, NoteId, NoteScript};
17use miden_protocol::testing::account_id::ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_UPDATABLE_CODE;
18use miden_protocol::testing::noop_auth_component::NoopAuthComponent;
19use miden_protocol::transaction::{
20 OutputNote,
21 TransactionArgs,
22 TransactionInputs,
23 TransactionScript,
24};
25use miden_standards::testing::account_component::IncrNonceAuthComponent;
26use miden_standards::testing::mock_account::MockAccountExt;
27use miden_tx::TransactionMastStore;
28use miden_tx::auth::BasicAuthenticator;
29
30use super::TransactionContext;
31use crate::{MockChain, MockChainNote};
32
33pub struct TransactionContextBuilder {
70 source_manager: Arc<dyn SourceManagerSync>,
71 account: Account,
72 advice_inputs: AdviceInputs,
73 authenticator: Option<BasicAuthenticator>,
74 expected_output_notes: Vec<Note>,
75 foreign_account_inputs: BTreeMap<AccountId, (Account, AccountWitness)>,
76 input_notes: Vec<Note>,
77 tx_script: Option<TransactionScript>,
78 tx_script_args: Word,
79 note_args: BTreeMap<NoteId, Word>,
80 tx_inputs: Option<TransactionInputs>,
81 auth_args: Word,
82 signatures: Vec<(PublicKeyCommitment, Word, Signature)>,
83 note_scripts: BTreeMap<Word, NoteScript>,
84 is_lazy_loading_enabled: bool,
85 is_debug_mode_enabled: bool,
86}
87
88impl TransactionContextBuilder {
89 pub fn new(account: Account) -> Self {
90 Self {
91 source_manager: Arc::new(DefaultSourceManager::default()),
92 account,
93 input_notes: Vec::new(),
94 expected_output_notes: Vec::new(),
95 tx_script: None,
96 tx_script_args: EMPTY_WORD,
97 authenticator: None,
98 advice_inputs: Default::default(),
99 tx_inputs: None,
100 note_args: BTreeMap::new(),
101 foreign_account_inputs: BTreeMap::new(),
102 auth_args: EMPTY_WORD,
103 signatures: Vec::new(),
104 note_scripts: BTreeMap::new(),
105 is_lazy_loading_enabled: true,
106 is_debug_mode_enabled: true,
107 }
108 }
109
110 pub fn with_existing_mock_account() -> Self {
120 Self::new(Account::mock(
121 ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_UPDATABLE_CODE,
122 IncrNonceAuthComponent,
123 ))
124 }
125
126 pub fn with_noop_auth_account() -> Self {
128 let account =
129 Account::mock(ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_UPDATABLE_CODE, NoopAuthComponent);
130
131 Self::new(account)
132 }
133
134 pub fn with_fungible_faucet(acct_id: u128, initial_balance: Felt) -> Self {
136 let account = Account::mock_fungible_faucet(acct_id, initial_balance);
137 Self::new(account)
138 }
139
140 pub fn with_non_fungible_faucet(acct_id: u128) -> Self {
142 let account = Account::mock_non_fungible_faucet(acct_id);
143 Self::new(account)
144 }
145
146 pub fn extend_advice_inputs(mut self, advice_inputs: AdviceInputs) -> Self {
148 self.advice_inputs.extend(advice_inputs);
149 self
150 }
151
152 pub fn extend_advice_map(
154 mut self,
155 map_entries: impl IntoIterator<Item = (Word, Vec<Felt>)>,
156 ) -> Self {
157 self.advice_inputs.map.extend(map_entries);
158 self
159 }
160
161 pub fn authenticator(mut self, authenticator: Option<BasicAuthenticator>) -> Self {
163 self.authenticator = authenticator;
164 self
165 }
166
167 pub fn foreign_accounts(
169 mut self,
170 inputs: impl IntoIterator<Item = (Account, AccountWitness)>,
171 ) -> Self {
172 self.foreign_account_inputs.extend(
173 inputs.into_iter().map(|(account, witness)| (account.id(), (account, witness))),
174 );
175 self
176 }
177
178 pub fn extend_input_notes(mut self, input_notes: Vec<Note>) -> Self {
180 self.input_notes.extend(input_notes);
181 self
182 }
183
184 pub fn tx_script(mut self, tx_script: TransactionScript) -> Self {
186 self.tx_script = Some(tx_script);
187 self
188 }
189
190 pub fn tx_script_args(mut self, tx_script_args: Word) -> Self {
192 self.tx_script_args = tx_script_args;
193 self
194 }
195
196 pub fn auth_args(mut self, auth_args: Word) -> Self {
198 self.auth_args = auth_args;
199 self
200 }
201
202 pub fn tx_inputs(mut self, tx_inputs: TransactionInputs) -> Self {
204 assert_eq!(
205 AccountHeader::from(&self.account),
206 tx_inputs.account().into(),
207 "account in context and account provided via tx inputs are not the same account"
208 );
209 self.tx_inputs = Some(tx_inputs);
210 self
211 }
212
213 pub fn disable_lazy_loading(mut self) -> Self {
218 self.is_lazy_loading_enabled = false;
219 self
220 }
221
222 pub fn disable_debug_mode(mut self) -> Self {
227 self.is_debug_mode_enabled = false;
228 self
229 }
230
231 pub fn extend_note_args(mut self, note_args: BTreeMap<NoteId, Word>) -> Self {
233 self.note_args.extend(note_args);
234 self
235 }
236
237 pub fn extend_expected_output_notes(mut self, output_notes: Vec<OutputNote>) -> Self {
239 let output_notes = output_notes.into_iter().filter_map(|n| match n {
240 OutputNote::Full(note) => Some(note),
241 OutputNote::Partial(_) => None,
242 OutputNote::Header(_) => None,
243 });
244
245 self.expected_output_notes.extend(output_notes);
246 self
247 }
248
249 pub fn with_source_manager(mut self, source_manager: Arc<dyn SourceManagerSync>) -> Self {
254 self.source_manager = source_manager.clone();
255 self
256 }
257
258 pub fn add_signature(
260 mut self,
261 pub_key: PublicKeyCommitment,
262 message: Word,
263 signature: Signature,
264 ) -> Self {
265 self.signatures.push((pub_key, message, signature));
266 self
267 }
268
269 pub fn add_note_script(mut self, script: NoteScript) -> Self {
271 self.note_scripts.insert(script.root(), script);
272 self
273 }
274
275 pub fn build(self) -> anyhow::Result<TransactionContext> {
280 let mut tx_inputs = match self.tx_inputs {
281 Some(tx_inputs) => tx_inputs,
282 None => {
283 let mut builder = MockChain::builder();
287 for i in self.input_notes {
288 builder.add_output_note(OutputNote::Full(i));
289 }
290 let mut mock_chain = builder.build()?;
291
292 mock_chain.prove_next_block().context("failed to prove first block")?;
293 mock_chain.prove_next_block().context("failed to prove second block")?;
294
295 let input_note_ids: Vec<NoteId> =
296 mock_chain.committed_notes().values().map(MockChainNote::id).collect();
297
298 mock_chain
299 .get_transaction_inputs(&self.account, &input_note_ids, &[])
300 .context("failed to get transaction inputs from mock chain")?
301 },
302 };
303
304 let mut tx_args = TransactionArgs::default().with_note_args(self.note_args);
305
306 tx_args = if let Some(tx_script) = self.tx_script {
307 tx_args.with_tx_script_and_args(tx_script, self.tx_script_args)
308 } else {
309 tx_args
310 };
311 tx_args = tx_args.with_auth_args(self.auth_args);
312 tx_args.extend_advice_inputs(self.advice_inputs.clone());
313 tx_args.extend_output_note_recipients(self.expected_output_notes.clone());
314
315 for (public_key_commitment, message, signature) in self.signatures {
316 tx_args.add_signature(public_key_commitment, message, signature);
317 }
318
319 tx_inputs.set_tx_args(tx_args);
320
321 let mast_store = {
322 let mast_forest_store = TransactionMastStore::new();
323 mast_forest_store.load_account_code(tx_inputs.account().code());
324
325 for (account, _) in self.foreign_account_inputs.values() {
326 mast_forest_store.insert(account.code().mast());
327 }
328
329 mast_forest_store
330 };
331
332 Ok(TransactionContext {
333 account: self.account,
334 expected_output_notes: self.expected_output_notes,
335 foreign_account_inputs: self.foreign_account_inputs,
336 tx_inputs,
337 mast_store,
338 authenticator: self.authenticator,
339 source_manager: self.source_manager,
340 note_scripts: self.note_scripts,
341 is_lazy_loading_enabled: self.is_lazy_loading_enabled,
342 is_debug_mode_enabled: self.is_debug_mode_enabled,
343 })
344 }
345}
346
347impl Default for TransactionContextBuilder {
348 fn default() -> Self {
349 Self::with_existing_mock_account()
350 }
351}