transact 0.4.7

Transact is a transaction execution platform designed to be used as a library or component when implementing distributed ledgers, including blockchains.
Documentation
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
 * Copyright 2020 Cargill Incorporated
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * -----------------------------------------------------------------------------
 */

//! Enables loading smart contract archives from scar files.
//!
//! A scar file is a tar bzip2 archive that contains two required files:
//! - A `.wasm` file that contains a WASM smart contract
//! - A `manifest.yaml` file that contains, at a minimum:
//!   - The contract `name`
//!   - The contract `version`
//!   - The contract's `input` addresses
//!   - The contract's `output` addresses
//!
//! # Examples
//!
//! ```no_run
//! use transact::contract::archive::{default_scar_path, SmartContractArchive};
//!
//! let archive = SmartContractArchive::from_scar_file("xo", "0.1", &default_scar_path())
//!     .expect("failed to load scar file");
//! ```

mod error;

use std::env::{split_paths, var_os};
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};

use bzip2::read::BzDecoder;
use glob::glob;
use semver::{Version, VersionReq};
use tar::Archive;

pub use error::Error;

const DEFAULT_SCAR_PATH: &str = "/usr/share/scar";
const SCAR_PATH_ENV_VAR: &str = "SCAR_PATH";

/// Load default location(s) for scar files.
pub fn default_scar_path() -> Vec<PathBuf> {
    match var_os(SCAR_PATH_ENV_VAR) {
        Some(paths) => split_paths(&paths).collect(),
        None => vec![PathBuf::from(DEFAULT_SCAR_PATH)],
    }
}

/// The definition of a smart contract, including the contract's associated metadata and the bytes
/// of the smart contract itself.
pub struct SmartContractArchive {
    /// The raw bytes of the WASM smart contract.
    pub contract: Vec<u8>,
    /// The metadata associated with this smart contract.
    pub metadata: SmartContractMetadata,
}

impl SmartContractArchive {
    /// Attempt to load a `SmartContractArchive` from a scar file in one of the given `paths`.
    ///
    /// The scar file to load will be named `<name>_<version>.scar`, where `<name>` is the name
    /// specified for this method and `<version>` is a valid semver string.
    ///
    /// The `name` argument for this method must not contain any underscores, '_'.
    ///
    /// The `version` argument for this method must be a valid semver requirement string. The
    /// latest contract (based on semver requirement rules) that matches the given name and meets
    /// the version requirements will be loaded.
    ///
    /// For more info on semver: <https://semver.org/>
    ///
    /// # Example
    ///
    /// If there are two files `/usr/share/scar/xo_1.2.3.scar` and `/some/other/dir/xo_1.2.4`, you
    /// can load v1.2.4 with:
    ///
    /// ```no_run
    /// use transact::contract::archive::SmartContractArchive;
    ///
    /// let archive = SmartContractArchive::from_scar_file(
    ///     "xo",
    ///     "1.2",
    ///     &["/usr/share/scar", "/some/other/dir"],
    /// )
    /// .expect("failed to load scar file");
    /// assert_eq!(&archive.metadata.version, "1.2.4");
    /// ```
    ///
    /// To load v1.2.3 explicitly, specify the exact version:
    ///
    /// ```no_run
    /// use transact::contract::archive::SmartContractArchive;
    ///
    /// let archive = SmartContractArchive::from_scar_file(
    ///     "xo",
    ///     "1.2.3",
    ///     &["/usr/share/scar", "/some/other/dir"],
    /// )
    /// .expect("failed to load scar file");
    /// assert_eq!(&archive.metadata.version, "1.2.3");
    /// ```
    pub fn from_scar_file<P: AsRef<Path>>(
        name: &str,
        version: &str,
        paths: &[P],
    ) -> Result<SmartContractArchive, Error> {
        let file_path = find_scar(name, version, paths)?;

        let scar_file = File::open(&file_path).map_err(|err| {
            Error::new_with_source(
                &format!("failed to open file {}", file_path.display()),
                err.into(),
            )
        })?;
        let mut archive = Archive::new(BzDecoder::new(scar_file));
        let archive_entries = archive.entries().map_err(|err| {
            Error::new_with_source(
                &format!("failed to read scar file {}", file_path.display()),
                err.into(),
            )
        })?;

        let mut metadata = None;
        let mut contract = None;

        for entry in archive_entries {
            let mut entry = entry.map_err(|err| {
                Error::new_with_source(
                    &format!(
                        "invalid scar file: failed to read archive entry from {}",
                        file_path.display()
                    ),
                    err.into(),
                )
            })?;
            let path = entry
                .path()
                .map_err(|err| {
                    Error::new_with_source(
                        &format!(
                            "invalid scar file: failed to get path of archive entry from {}",
                            file_path.display()
                        ),
                        err.into(),
                    )
                })?
                .into_owned();
            if path_is_manifest(&path) {
                metadata = Some(serde_yaml::from_reader(entry).map_err(|err| {
                    Error::new_with_source(
                        &format!(
                            "invalid scar file: manifest.yaml invalid in {}",
                            file_path.display()
                        ),
                        err.into(),
                    )
                })?);
            } else if path_is_wasm(&path) {
                let mut contract_bytes = vec![];
                entry.read_to_end(&mut contract_bytes).map_err(|err| {
                    Error::new_with_source(
                        &format!(
                            "invalid scar file: failed to read smart contract in {}",
                            file_path.display()
                        ),
                        err.into(),
                    )
                })?;
                contract = Some(contract_bytes);
            }
        }

        let contract = contract.ok_or_else(|| {
            Error::new(&format!(
                "invalid scar file: smart contract not found in {}",
                file_path.display()
            ))
        })?;
        let metadata: SmartContractMetadata = metadata.ok_or_else(|| {
            Error::new(&format!(
                "invalid scar file: manifest.yaml not found in {}",
                file_path.display()
            ))
        })?;

        validate_metadata(name, &metadata.name)?;

        Ok(SmartContractArchive { contract, metadata })
    }
}

/// The metadata of a smart contract archive.
#[derive(Debug, Deserialize, Serialize)]
pub struct SmartContractMetadata {
    /// The name of the smart contract.
    pub name: String,
    /// The version of the smart contract.
    pub version: String,
    /// The list of input addresses used by this smart contract.
    pub inputs: Vec<String>,
    /// The list of output addresses used by this smart contract.
    pub outputs: Vec<String>,
}

fn find_scar<P: AsRef<Path>>(name: &str, version: &str, paths: &[P]) -> Result<PathBuf, Error> {
    let file_name_pattern = format!("{}_*.scar", name);

    validate_scar_file_name(name)?;

    let version_req = VersionReq::parse(version)?;

    // Start with all scar files that match the name, from all paths
    paths
        .iter()
        .map(|path| {
            let file_path_pattern = path.as_ref().join(&file_name_pattern);
            let pattern_string = file_path_pattern
                .to_str()
                .ok_or_else(|| Error::new("name is not valid UTF-8"))?;
            Ok(glob(pattern_string)?)
        })
        .collect::<Result<Vec<_>, Error>>()?
        .into_iter()
        .flatten()
        // Filter out any files that can't be read
        .filter_map(|path_res| path_res.ok())
        // Filter out any files that don't meet the version requirements
        .filter_map(|path| {
            let file_stem = path.file_stem()?.to_str()?;
            let version_str = (*file_stem.splitn(2, '_').collect::<Vec<_>>().get(1)?).to_string();
            let version = Version::parse(&version_str).ok()?;
            if version_req.matches(&version) {
                Some((path, version))
            } else {
                None
            }
        })
        // Get the file with the highest matching version
        .max_by(|(_, x_version), (_, y_version)| x_version.cmp(y_version))
        .map(|(path, _)| path)
        .ok_or_else(|| {
            let paths = paths
                .iter()
                .map(|path| path.as_ref().display())
                .collect::<Vec<_>>();
            Error::new(&format!(
                "could not find contract '{}' that meets version requirement '{}' in paths '{:?}'",
                name, version_req, paths,
            ))
        })
}

// Validate that the scar file name does not contain underscores, otherwise return an error.
fn validate_scar_file_name(name: &str) -> Result<(), Error> {
    if name.contains('_') {
        return Err(Error::new(&format!(
            "invalid scar file name, must not include '_': {}",
            name
        )));
    }
    Ok(())
}

// Validate that the metadata collected from the manifest contains a contract name which matches
// the name of the scar file. This includes swapping any underscores which appear in the contract
// name with dashes, as underscores are not allowed in scar file names.
fn validate_metadata(file_name: &str, contract_name: &str) -> Result<(), Error> {
    if file_name != contract_name.replace('_', "-") {
        return Err(Error::new(&format!(
            "scar file name `{}` does not match contract name in manifest `{}`",
            file_name, contract_name,
        )));
    }
    Ok(())
}

fn path_is_manifest(path: &std::path::Path) -> bool {
    path.file_name()
        .map(|file_name| file_name == "manifest.yaml")
        .unwrap_or(false)
}

fn path_is_wasm(path: &std::path::Path) -> bool {
    match path.extension() {
        Some(extension) => extension == "wasm",
        None => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::io::Write;
    use std::path::Path;

    use bzip2::write::BzEncoder;
    use bzip2::Compression;
    use serde::Serialize;
    use serial_test::serial;
    use tar::Builder;
    use tempdir::TempDir;

    const MOCK_CONTRACT_BYTES: &[u8] = &[0x00, 0x01, 0x02, 0x03];

    // The tests that modify and read environment variable(s) must be run serially to prevent
    // interference with each other. Each of these tests is annotated with `#[serial(scar_path)]`
    // to enforce this.

    /// Verify that the `default_scar_path()` method returns DEFAULT_SCAR_PATH when
    /// SCAR_PATH_ENV_VAR is unset.
    #[test]
    #[serial(scar_path)]
    fn default_scar_path_env_unset() {
        std::env::remove_var(SCAR_PATH_ENV_VAR);
        assert_eq!(default_scar_path(), vec![PathBuf::from(DEFAULT_SCAR_PATH)]);
    }

    /// Verify that the `default_scar_path()` method returns all paths in SCAR_PATH_ENV_VAR when it
    /// is set.
    #[test]
    #[serial(scar_path)]
    fn default_scar_path_env_set() {
        let paths = vec!["/test/dir", "/other/dir", ".", "~/"];
        let joined_paths = std::env::join_paths(&paths).expect("failed to join paths");
        std::env::set_var(SCAR_PATH_ENV_VAR, joined_paths);

        assert_eq!(
            default_scar_path(),
            paths
                .iter()
                .map(|path| PathBuf::from(path))
                .collect::<Vec<_>>()
        );
    }

    /// Verify that an error is returned when the contract name contains '_'.
    #[test]
    fn find_scar_invalid_name() {
        let dir = new_temp_dir();
        write_mock_scar(&dir, "non_existent", "0.1.0");

        assert!(find_scar("non_existent", "0.1.0", &[&dir]).is_err());
    }

    /// Verify that an error is returned when the contract version is an invalid semver string.
    #[test]
    fn find_scar_invalid_version() {
        let dir = new_temp_dir();
        write_mock_scar(&dir, "nonexistent", "0.1..0");

        assert!(find_scar("nonexistent", "0.1..0", &[&dir]).is_err());
    }

    /// Verify that an error is returned when no contract with the given name exists.
    #[test]
    fn find_scar_no_matching_name() {
        let dir = new_temp_dir();
        write_mock_scar(&dir, "mock", "0.1.0");

        assert!(find_scar("nonexistent", "0.1.0", &[&dir]).is_err());
    }

    /// Verify that an error is returned when no matching contract is found in the given path.
    #[test]
    fn find_scar_not_in_path() {
        let dir = new_temp_dir();
        write_mock_scar(&dir, "mock", "0.1.0");

        assert!(find_scar("mock", "0.1.0", &["/some/other/dir"]).is_err());
    }

    /// Verify that an error is returned when no contract is found with a sufficient patch version
    /// and no pre-release tag.
    #[test]
    fn find_scar_insufficient_release() {
        let dir = new_temp_dir();
        write_mock_scar(&dir, "mock", "0.1.0-dev");

        assert!(find_scar("mock", "0.1.0", &[&dir]).is_err());
    }

    /// Verify that an error is returned when no contract is found with a sufficient patch version.
    #[test]
    fn find_scar_insufficient_patch() {
        let dir = new_temp_dir();
        write_mock_scar(&dir, "mock", "0.1.0");

        assert!(find_scar("mock", "0.1.1", &[&dir]).is_err());
    }

    /// Verify that an error is returned when no contract is found with a sufficient minor version.
    #[test]
    fn find_scar_insufficient_minor() {
        let dir = new_temp_dir();
        write_mock_scar(&dir, "mock", "0.1.0");

        assert!(find_scar("mock", "0.2.0", &[&dir]).is_err());
    }

    /// Verify that an error is returned when no contract is found with a sufficient major version.
    #[test]
    fn find_scar_insufficient_major() {
        let dir = new_temp_dir();
        write_mock_scar(&dir, "mock", "0.1.0");

        assert!(find_scar("mock", "1.0.0", &[&dir]).is_err());
    }

    /// Verify that a scar file is correctly found when any version (`*`) is requested.
    #[test]
    fn find_scar_any_version() {
        let dir = new_temp_dir();
        let scar_path = write_mock_scar(&dir, "mock", "0.1.0");

        assert_eq!(
            find_scar("mock", "*", &[&dir]).expect("failed to find scar"),
            scar_path
        );
    }

    /// Verify that a scar file is correctly found when the exact version is requested.
    #[test]
    fn find_scar_exact_version() {
        let dir = new_temp_dir();
        let scar_path = write_mock_scar(&dir, "mock", "0.1.2-dev");

        assert_eq!(
            find_scar("mock", "0.1.2-dev", &[&dir]).expect("failed to find scar"),
            scar_path
        );
    }

    /// Verify that a scar file is correctly found when it meets the minimum minor version.
    #[test]
    fn find_scar_minimum_minor() {
        let dir = new_temp_dir();
        let scar_path = write_mock_scar(&dir, "mock", "0.1.2");

        assert_eq!(
            find_scar("mock", "0.1", &[&dir]).expect("failed to find scar"),
            scar_path
        );
    }

    /// Verify that a scar file is correctly found when it meets the minimum major version.
    #[test]
    fn find_scar_minimum_major() {
        let dir = new_temp_dir();
        let scar_path = write_mock_scar(&dir, "mock", "1.2.3");

        assert_eq!(
            find_scar("mock", "1", &[&dir]).expect("failed to find scar"),
            scar_path
        );
    }

    /// Verify that the scar file with no pre-release tag is returned rather than the same patch
    /// version with a pre-release tag.
    #[test]
    fn find_scar_highest_matching_patch() {
        let dir = new_temp_dir();
        write_mock_scar(&dir, "mock", "0.1.2-dev");
        let scar_path = write_mock_scar(&dir, "mock", "0.1.2");

        assert_eq!(
            find_scar("mock", "0.1.2", &[&dir]).expect("failed to find scar"),
            scar_path
        );
    }

    /// Verify that the scar file with the highest patch version is returned for the specified
    /// minor version.
    #[test]
    fn find_scar_highest_matching_minor() {
        let dir = new_temp_dir();
        write_mock_scar(&dir, "mock", "0.1.1");
        let scar_path = write_mock_scar(&dir, "mock", "0.1.2");

        assert_eq!(
            find_scar("mock", "0.1", &[&dir]).expect("failed to find scar"),
            scar_path
        );
    }

    /// Verify that the scar file with the highest minor version is returned for the specified
    /// major version.
    #[test]
    fn find_scar_highest_matching_major() {
        let dir = new_temp_dir();
        write_mock_scar(&dir, "mock", "1.1.0");
        let scar_path = write_mock_scar(&dir, "mock", "1.2.0");

        assert_eq!(
            find_scar("mock", "1", &[&dir]).expect("failed to find scar"),
            scar_path
        );
    }

    /// Verify that the scar file with the highest matching version is returned when there are
    /// versions in different paths.
    #[test]
    fn find_scar_highest_matching_across_paths() {
        let thread_id = format!("{:?}", std::thread::current().id());
        let dir1 = TempDir::new(&format!("{}1", thread_id)).expect("failed to create temp dir1");
        let dir2 = TempDir::new(&format!("{}2", thread_id)).expect("failed to create temp dir2");
        write_mock_scar(&dir1, "mock", "1.2.3");
        let scar_path = write_mock_scar(&dir2, "mock", "1.2.4");

        assert_eq!(
            find_scar("mock", "1", &[&dir1, &dir2]).expect("failed to find scar"),
            scar_path
        );
    }

    /// Verify that a valid scar file is successfully loaded.
    #[test]
    fn load_scar_file_successful() {
        let dir = new_temp_dir();
        write_mock_scar(&dir, "mock-scar", "1.0.0");

        let scar = SmartContractArchive::from_scar_file("mock-scar", "1.0.0", &[&dir])
            .expect("failed to load scar");

        assert_eq!(scar.contract, MOCK_CONTRACT_BYTES);
        assert_eq!(scar.metadata.name, mock_smart_contract_metadata().name);
        assert_eq!(
            scar.metadata.version,
            mock_smart_contract_metadata().version
        );
        assert_eq!(scar.metadata.inputs, mock_smart_contract_metadata().inputs);
        assert_eq!(
            scar.metadata.outputs,
            mock_smart_contract_metadata().outputs
        );
    }

    /// Verify that an error is returned when attempting to load a scar file that does not contain
    /// a `manifest.yaml` file.
    #[test]
    fn load_scar_manifest_not_found() {
        let dir = new_temp_dir();

        let contract_file_path = write_contract(&dir);
        write_scar_file::<&Path>(
            dir.as_ref(),
            "mock-scar",
            "1.0.0",
            None,
            Some(contract_file_path.as_path()),
        );

        assert!(SmartContractArchive::from_scar_file("mock-scar", "1.0.0", &[&dir]).is_err());
    }

    /// Verify that an error is returned when attempting to load a scar file whose `manifest.yaml`
    /// is invalidly formatted.
    #[test]
    fn load_scar_manifest_invalid() {
        let dir = new_temp_dir();

        let manifest_file_path = write_manifest(&dir, &[0x00]);
        let contract_file_path = write_contract(&dir);
        write_scar_file::<&Path>(
            dir.as_ref(),
            "mock-scar",
            "1.0.0",
            Some(manifest_file_path.as_path()),
            Some(contract_file_path.as_path()),
        );

        assert!(SmartContractArchive::from_scar_file("mock-scar", "1.0.0", &[&dir]).is_err());
    }

    /// Verify that an error is returned when attempting to load a scar file that does not contain
    /// a .wasm smart contract.
    #[test]
    fn load_scar_contract_not_found() {
        let dir = new_temp_dir();

        let manifest_file_path = write_manifest(&dir, &mock_smart_contract_metadata());
        write_scar_file::<&Path>(
            dir.as_ref(),
            "mock-scar",
            "1.0.0",
            Some(manifest_file_path.as_path()),
            None,
        );

        assert!(SmartContractArchive::from_scar_file("mock-scar", "1.0.0", &[&dir]).is_err());
    }

    /// Verify that an error is returned when attempting to load a scar file that does not contain
    /// a smart contract with a name that matches the file name.
    #[test]
    fn load_scar_manifest_not_matching() {
        let dir = new_temp_dir();
        let manifest_file_path = write_manifest(&dir, &mock_invalid_smart_contract_metadata());

        write_scar_file::<&Path>(
            dir.as_ref(),
            "mock-scar",
            "1.0.0",
            Some(&manifest_file_path),
            None,
        );

        assert!(SmartContractArchive::from_scar_file("mock-scar", "1.0.0", &[&dir]).is_err());
    }

    fn new_temp_dir() -> TempDir {
        let thread_id = format!("{:?}", std::thread::current().id());
        TempDir::new(&thread_id).expect("failed to create temp dir")
    }

    fn write_mock_scar<P: AsRef<Path>>(dir: P, name: &str, version: &str) -> PathBuf {
        let manifest_file_path = write_manifest(&dir, &mock_smart_contract_metadata());
        let contract_file_path = write_contract(&dir);
        write_scar_file::<&Path>(
            dir.as_ref(),
            name,
            version,
            Some(manifest_file_path.as_path()),
            Some(contract_file_path.as_path()),
        )
    }

    fn write_manifest<P: AsRef<Path>, T: Serialize>(dir: P, manifest: &T) -> PathBuf {
        let manifest_file_path = dir.as_ref().join("manifest.yaml");
        let manifest_file =
            File::create(manifest_file_path.as_path()).expect("failed to create manifest file");
        serde_yaml::to_writer(manifest_file, manifest).expect("failed to write manifest file");
        manifest_file_path
    }

    fn write_contract<P: AsRef<Path>>(dir: P) -> PathBuf {
        let contract_file_path = dir.as_ref().join("mock.wasm");
        let mut contract_file =
            File::create(contract_file_path.as_path()).expect("failed to create contract file");
        contract_file
            .write_all(MOCK_CONTRACT_BYTES)
            .expect("failed to write contract file");
        contract_file_path
    }

    fn write_scar_file<P: AsRef<Path>>(
        dir: P,
        name: &str,
        version: &str,
        manifest_file_path: Option<P>,
        contract_file_path: Option<P>,
    ) -> PathBuf {
        let scar_file_path = dir.as_ref().join(format!("{}_{}.scar", name, version));
        let scar_file = File::create(&scar_file_path).expect("failed to create scar file");
        let mut scar_file_builder = Builder::new(BzEncoder::new(scar_file, Compression::fast()));

        if let Some(manifest_file_path) = manifest_file_path {
            scar_file_builder
                .append_path_with_name(manifest_file_path, "manifest.yaml")
                .expect("failed to add manifest to scar file");
        }

        if let Some(contract_file_path) = contract_file_path {
            scar_file_builder
                .append_path_with_name(contract_file_path, "mock.wasm")
                .expect("failed to add contract to scar file");
        }

        scar_file_builder
            .finish()
            .expect("failed to write scar file");

        scar_file_path
    }

    fn mock_smart_contract_metadata() -> SmartContractMetadata {
        SmartContractMetadata {
            name: "mock_scar".into(),
            version: "1.0".into(),
            inputs: vec!["abcdef".into()],
            outputs: vec!["012345".into()],
        }
    }

    fn mock_invalid_smart_contract_metadata() -> SmartContractMetadata {
        SmartContractMetadata {
            name: "invalid_scar".into(),
            version: "1.0".into(),
            inputs: vec!["abcdef".into()],
            outputs: vec!["012345".into()],
        }
    }
}