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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the Aleo SDK library.

// The Aleo SDK library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Aleo SDK library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.

use super::*;

impl<N: Network> ProgramManager<N> {
    /// Create an offline execution of a program to share with a third party.
    ///
    /// DISCLAIMER: Offline executions will not interact with the Aleo network and cannot use all
    /// of the features of the Leo programming language or Aleo instructions. Any code written
    /// inside finalize blocks will not be executed, mappings cannot be initialized, updated or read,
    /// and a chain of records cannot be created.
    ///
    /// Offline executions however can be used to verify that program outputs follow from program
    /// inputs and that the program was executed correctly. If this is the aim and no chain
    /// interaction is desired, this function can be used.
    #[allow(clippy::too_many_arguments)]
    pub fn execute_program_offline<A: Aleo<Network = N>>(
        &self,
        private_key: &PrivateKey<N>,
        program: &Program<N>,
        function: impl TryInto<Identifier<N>>,
        imports: &[Program<N>],
        inputs: impl ExactSizeIterator<Item = impl TryInto<Value<N>>>,
        include_outputs: bool,
        url: &str,
    ) -> Result<OfflineExecution<N>> {
        // Initialize an RNG and query object for the transaction
        let rng = &mut rand::thread_rng();
        let query = Query::<N, BlockMemory<N>>::from(url);

        // Check that the function exists in the program
        let function_name = function.try_into().map_err(|_| anyhow!("Invalid function name"))?;
        let program_id = program.id();
        println!("Checking function {function_name:?} exists in {program_id:?}");
        ensure!(
            program.contains_function(&function_name),
            "Program {program_id:?} does not contain function {function_name:?}, aborting execution"
        );

        // Create an ephemeral SnarkVM to store the programs
        let store = ConsensusStore::<N, ConsensusMemory<N>>::open(None)?;
        let vm = VM::<N, ConsensusMemory<N>>::from(store)?;
        let credits_id = ProgramID::<N>::from_str("credits.aleo")?;
        imports.iter().try_for_each(|program| {
            if &credits_id != program.id() {
                vm.process().write().add_program(program)?
            }
            Ok::<(), Error>(())
        })?;
        let _ = vm.process().write().add_program(program);

        // Compute the authorization.
        let authorization = vm.authorize(private_key, program_id, function_name, inputs, rng)?;

        // Compute the trace
        let locator = Locator::new(*program_id, function_name);
        let (response, mut trace) = vm.process().write().execute::<A>(authorization)?;
        trace.prepare(query)?;
        let execution = trace.prove_execution::<A, _>(&locator.to_string(), &mut rand::thread_rng())?;

        // Get the public outputs
        let mut public_outputs = vec![];
        response.outputs().iter().zip(response.output_ids().iter()).for_each(|(output, output_id)| {
            if let OutputID::Public(_) = output_id {
                public_outputs.push(output.clone());
            }
        });

        // If all outputs are requested, include them
        let response = if include_outputs { Some(response) } else { None };

        // Return the execution
        Ok(OfflineExecution::new(execution, response, trace, Some(public_outputs)))
    }

    /// Execute a program function on the Aleo Network.
    ///
    /// To run this function successfully, the program must already be deployed on the Aleo Network
    pub fn execute_program(
        &mut self,
        program_id: impl TryInto<ProgramID<N>>,
        function: impl TryInto<Identifier<N>>,
        inputs: impl ExactSizeIterator<Item = impl TryInto<Value<N>>>,
        priority_fee: u64,
        fee_record: Option<Record<N, Plaintext<N>>>,
        password: Option<&str>,
    ) -> Result<String> {
        // Ensure a network client is set, otherwise online execution is not possible
        ensure!(
            self.api_client.is_some(),
            "❌ Network client not set. A network client must be set before execution in order to send an execution transaction to the Aleo network"
        );

        // Check program and function have valid names
        let program_id = program_id.try_into().map_err(|_| anyhow!("Invalid program ID"))?;
        let function_id = function.try_into().map_err(|_| anyhow!("Invalid function name"))?;
        let function_name = function_id.to_string();

        // Get the program from chain, error if it doesn't exist
        let program = self
            .api_client()?
            .get_program(program_id)
            .map_err(|_| anyhow!("Program {program_id:?} does not exist on the Aleo Network. Try deploying the program first before executing."))?;

        // Create the execution transaction
        let private_key = self.get_private_key(password)?;
        let node_url = self.api_client.as_ref().unwrap().base_url().to_string();
        let transaction = Self::create_execute_transaction(
            &private_key,
            priority_fee,
            inputs,
            fee_record,
            &program,
            function_id,
            node_url,
            self.api_client()?,
            &self.vm,
        )?;

        // Broadcast the execution transaction to the network
        println!("Attempting to broadcast execution transaction for {program_id:?}");
        let execution = self.broadcast_transaction(transaction);

        // Tell the user about the result of the execution before returning it
        if execution.is_ok() {
            println!("✅ Execution of function {function_name:?} from program {program_id:?}' broadcast successfully");
        } else {
            println!("❌ Execution of function {function_name:?} from program {program_id:?} failed to broadcast");
        }

        execution
    }

    /// Create an execute transaction without initializing a program manager instance
    #[allow(clippy::too_many_arguments)]
    pub fn create_execute_transaction(
        private_key: &PrivateKey<N>,
        priority_fee: u64,
        inputs: impl ExactSizeIterator<Item = impl TryInto<Value<N>>>,
        fee_record: Option<Record<N, Plaintext<N>>>,
        program: &Program<N>,
        function: impl TryInto<Identifier<N>>,
        node_url: String,
        api_client: &AleoAPIClient<N>,
        vm: &Option<VM<N, ConsensusMemory<N>>>,
    ) -> Result<Transaction<N>> {
        // Initialize an RNG and query object for the transaction
        let rng = &mut rand::thread_rng();
        let query = Query::from(node_url);

        // Check that the function exists in the program
        let function_name = function.try_into().map_err(|_| anyhow!("Invalid function name"))?;
        let program_id = program.id();
        println!("Checking function {function_name:?} exists in {program_id:?}");
        ensure!(
            program.contains_function(&function_name),
            "Program {program_id:?} does not contain function {function_name:?}, aborting execution"
        );

        // Initialize the VM
        if let Some(vm) = vm {
            let credits_id = ProgramID::<N>::from_str("credits.aleo")?;
            api_client.get_program_imports_from_source(program)?.iter().try_for_each(|(_, import)| {
                if import.id() != &credits_id && !vm.process().read().contains_program(import.id()) {
                    vm.process().write().add_program(import)?
                }
                Ok::<_, Error>(())
            })?;

            // If the initialization is for an execution, add the program. Otherwise, don't add it as
            // it will be added during the deployment process
            if !vm.process().read().contains_program(program.id()) {
                vm.process().write().add_program(program)?;
            }
            vm.execute(private_key, (program_id, function_name), inputs, fee_record, priority_fee, Some(query), rng)
        } else {
            let vm = Self::initialize_vm(api_client, program, true)?;
            vm.execute(private_key, (program_id, function_name), inputs, fee_record, priority_fee, Some(query), rng)
        }
    }

    /// Estimate the cost of executing a program with the given inputs in microcredits. The response
    /// will be in the form of (total_cost, (storage_cost, finalize_cost))
    ///
    /// Disclaimer: Fee estimation is experimental and may not represent a correct estimate on any current or future network
    pub fn estimate_execution_fee<A: Aleo<Network = N>>(
        &self,
        program: &Program<N>,
        function: impl TryInto<Identifier<N>>,
        inputs: impl ExactSizeIterator<Item = impl TryInto<Value<N>>>,
    ) -> Result<(u64, (u64, u64))> {
        let url = self.api_client.as_ref().map_or_else(
            || bail!("A network client must be configured to estimate a program execution fee"),
            |api_client| Ok(api_client.base_url()),
        )?;

        // Check that the function exists in the program
        let function_name = function.try_into().map_err(|_| anyhow!("Invalid function name"))?;
        let program_id = program.id();
        println!("Checking function {function_name:?} exists in {program_id:?}");
        ensure!(
            program.contains_function(&function_name),
            "Program {program_id:?} does not contain function {function_name:?}, aborting execution"
        );

        // Create an ephemeral SnarkVM to store the programs
        // Initialize an RNG and query object for the transaction
        let rng = &mut rand::thread_rng();
        let query = Query::<N, BlockMemory<N>>::from(url);
        let vm = Self::initialize_vm(self.api_client()?, program, true)?;

        // Create an ephemeral private key for the sample execution
        let private_key = PrivateKey::<N>::new(rng)?;

        // Compute the authorization.
        let authorization = vm.authorize(&private_key, program_id, function_name, inputs, rng)?;

        let locator = Locator::new(*program_id, function_name);
        let (_, mut trace) = vm.process().write().execute::<A>(authorization)?;
        trace.prepare(query)?;
        let execution = trace.prove_execution::<A, _>(&locator.to_string(), &mut rand::thread_rng())?;
        execution_cost(&vm, &execution)
    }

    /// Estimate the finalize fee component for executing a function. This fee is additional to the
    /// size of the execution of the program in bytes. If the function does not have a finalize
    /// step, then the finalize fee is 0.
    ///
    /// Disclaimer: Fee estimation is experimental and may not represent a correct estimate on any current or future network
    pub fn estimate_finalize_fee(&self, program: &Program<N>, function: impl TryInto<Identifier<N>>) -> Result<u64> {
        let function_name = function.try_into().map_err(|_| anyhow!("Invalid function name"))?;
        match program.get_function(&function_name)?.finalize_logic() {
            Some(finalize) => cost_in_microcredits(finalize),
            None => Ok(0u64),
        }
    }
}

#[cfg(test)]
#[cfg(not(feature = "wasm"))]
mod tests {
    use super::*;
    use crate::{random_program, random_program_id, AleoAPIClient, RECORD_5_MICROCREDITS};
    use snarkvm::circuit::AleoV0;
    use snarkvm_console::network::Testnet3;

    #[test]
    fn test_fee_estimation() {
        let private_key = PrivateKey::<Testnet3>::from_str(RECIPIENT_PRIVATE_KEY).unwrap();
        let api_client = AleoAPIClient::<Testnet3>::testnet3();
        let program_manager =
            ProgramManager::<Testnet3>::new(Some(private_key), None, Some(api_client.clone()), None, false).unwrap();

        let finalize_program = program_manager.api_client.as_ref().unwrap().get_program("credits.aleo").unwrap();
        let hello_hello = program_manager.api_client.as_ref().unwrap().get_program("hello_hello.aleo").unwrap();
        // Ensure a finalize scope program execution fee is estimated correctly
        let (total, (storage, finalize)) = program_manager
            .estimate_execution_fee::<AleoV0>(
                &finalize_program,
                "transfer_public",
                vec!["aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px", "5u64"].into_iter(),
            )
            .unwrap();
        let finalize_only = program_manager.estimate_finalize_fee(&finalize_program, "transfer_public").unwrap();
        assert!(finalize_only > 0);
        assert!(finalize > storage);
        assert_eq!(finalize, finalize_only);
        assert_eq!(total, finalize_only + storage);
        assert_eq!(storage, total - finalize_only);

        // Ensure a non-finalize scope program execution fee is estimated correctly
        let (total, (storage, finalize)) = program_manager
            .estimate_execution_fee::<AleoV0>(&hello_hello, "hello", vec!["5u32", "5u32"].into_iter())
            .unwrap();
        let finalize_only = program_manager.estimate_finalize_fee(&hello_hello, "hello").unwrap();
        assert!(storage > 0);
        assert_eq!(finalize_only, 0);
        assert_eq!(finalize, finalize_only);
        assert_eq!(total, finalize_only + storage);
        assert_eq!(storage, total - finalize_only);

        // Ensure a deployment fee is estimated correctly
        let random = random_program();
        let (total, (storage, namespace)) = program_manager.estimate_deployment_fee::<AleoV0>(&random).unwrap();
        let namespace_only = ProgramManager::estimate_namespace_fee(random.id()).unwrap();
        assert_eq!(namespace, 1000000);
        assert_eq!(namespace, namespace_only);
        assert_eq!(total, namespace_only + storage);
        assert_eq!(storage, total - namespace_only);

        // Ensure a program with imports is estimated correctly
        let nested_import_program = api_client.get_program("imported_add_mul.aleo").unwrap();
        let finalize_only = program_manager.estimate_finalize_fee(&nested_import_program, "add_and_double").unwrap();
        let (total, (storage, finalize)) = program_manager
            .estimate_execution_fee::<AleoV0>(
                &nested_import_program,
                "add_and_double",
                vec!["5u32", "5u32"].into_iter(),
            )
            .unwrap();
        assert!(storage > 0);
        assert_eq!(finalize_only, 0);
        assert_eq!(finalize, finalize_only);
        assert_eq!(total, finalize_only + storage);
        assert_eq!(storage, total - finalize_only);

        let (total, (storage, namespace)) =
            program_manager.estimate_deployment_fee::<AleoV0>(&nested_import_program).unwrap();
        let namespace_only = ProgramManager::estimate_namespace_fee(nested_import_program.id()).unwrap();
        assert_eq!(namespace, 1000000);
        assert_eq!(namespace, namespace_only);
        assert_eq!(total, namespace_only + storage);
        assert_eq!(storage, total - namespace_only);
    }

    #[test]
    #[ignore]
    fn test_execution() {
        let private_key = PrivateKey::<Testnet3>::from_str(RECIPIENT_PRIVATE_KEY).unwrap();
        let encrypted_private_key =
            crate::Encryptor::encrypt_private_key_with_secret(&private_key, "password").unwrap();
        let api_client = AleoAPIClient::<Testnet3>::local_testnet3("3033");
        let record_finder = RecordFinder::new(api_client.clone());
        let mut program_manager =
            ProgramManager::<Testnet3>::new(Some(private_key), None, Some(api_client.clone()), None, false).unwrap();

        let fee = 2_500_000;
        let finalize_fee = 8_000_000;

        // Test execution of an on chain program is successful
        for i in 0..5 {
            let fee_record = record_finder.find_one_record(&private_key, fee, None).unwrap();
            // Test execution of a on chain program is successful
            let execution = program_manager.execute_program(
                "credits_import_test.aleo",
                "test",
                ["1312u32", "62131112u32"].into_iter(),
                fee,
                Some(fee_record),
                None,
            );
            println!("{:?}", execution);

            if execution.is_ok() {
                break;
            } else if i == 4 {
                panic!("{}", format!("Execution failed after 5 attempts with error: {:?}", execution));
            }
        }

        // Test programs can be executed with an encrypted private key
        let mut program_manager =
            ProgramManager::<Testnet3>::new(None, Some(encrypted_private_key), Some(api_client), None, false).unwrap();

        for i in 0..5 {
            let fee_record = record_finder.find_one_record(&private_key, fee, None).unwrap();
            // Test execution of an on chain program is successful using an encrypted private key
            let execution = program_manager.execute_program(
                "credits_import_test.aleo",
                "test",
                ["1337u32", "42u32"].into_iter(),
                fee,
                Some(fee_record),
                Some("password"),
            );
            if execution.is_ok() {
                break;
            } else if i == 4 {
                panic!("{}", format!("Execution failed after 5 attempts with error: {:?}", execution));
            }
        }

        // Test execution with a finalize scope can be done
        for i in 0..5 {
            let fee_record = record_finder.find_one_record(&private_key, finalize_fee, None).unwrap();
            // Test execution of an on chain program is successful using an encrypted private key
            let execution = program_manager.execute_program(
                "finalize_test.aleo",
                "increase_counter",
                ["0u32", "42u32"].into_iter(),
                finalize_fee,
                Some(fee_record),
                Some("password"),
            );
            if execution.is_ok() {
                break;
            } else if i == 4 {
                panic!("{}", format!("Execution failed after 5 attempts with error: {:?}", execution));
            }
        }

        // Test execution of a program with imports other than credits.aleo is successful
        for i in 0..5 {
            let fee_record = record_finder.find_one_record(&private_key, finalize_fee, None).unwrap();
            // Test execution of an on chain program is successful using an encrypted private key
            let execution = program_manager.execute_program(
                "double_test.aleo",
                "double_it",
                ["42u32"].into_iter(),
                finalize_fee,
                Some(fee_record),
                Some("password"),
            );
            if execution.is_ok() {
                break;
            } else if i == 4 {
                panic!("{}", format!("Execution failed after 5 attempts with error: {:?}", execution));
            }
        }
    }

    #[test]
    fn test_execution_failure_modes() {
        let rng = &mut rand::thread_rng();
        let recipient_private_key = PrivateKey::<Testnet3>::new(rng).unwrap();
        let api_client = AleoAPIClient::<Testnet3>::testnet3();
        let record_5_microcredits = Record::<Testnet3, Plaintext<Testnet3>>::from_str(RECORD_5_MICROCREDITS).unwrap();
        let record_2000000001_microcredits =
            Record::<Testnet3, Plaintext<Testnet3>>::from_str(RECORD_2000000001_MICROCREDITS).unwrap();

        // Ensure that program manager creation fails if no key is provided
        let mut program_manager =
            ProgramManager::<Testnet3>::new(Some(recipient_private_key), None, Some(api_client), None, false).unwrap();

        // Assert that execution fails if record's available microcredits are below the fee
        let execution = program_manager.execute_program(
            "hello.aleo",
            "hello",
            ["5u32", "6u32"].into_iter(),
            500000,
            Some(record_5_microcredits),
            None,
        );

        assert!(execution.is_err());

        // Assert that execution fails if a fee is specified but no records are
        let execution = program_manager.execute_program(
            "hello.aleo",
            "hello",
            ["5u32", "6u32"].into_iter(),
            200,
            Some(record_2000000001_microcredits.clone()),
            None,
        );

        assert!(execution.is_err());

        // Assert that execution fails if the program is not found
        let randomized_program_id = random_program_id(16);
        let execution = program_manager.execute_program(
            &randomized_program_id,
            "hello",
            ["5u32", "6u32"].into_iter(),
            500000,
            Some(record_2000000001_microcredits.clone()),
            None,
        );

        assert!(execution.is_err());

        // Assert that execution fails if the function is not found
        let execution = program_manager.execute_program(
            "hello.aleo",
            "random_function",
            ["5u32", "6u32"].into_iter(),
            500000,
            Some(record_2000000001_microcredits.clone()),
            None,
        );

        assert!(execution.is_err());

        // Assert that execution fails if the function is not found
        let execution = program_manager.execute_program(
            "hello.aleo",
            "random_function",
            ["5u32", "6u32"].into_iter(),
            500000,
            Some(record_2000000001_microcredits),
            None,
        );

        assert!(execution.is_err());
    }
}