multiversx_sc_meta_lib/contract/sc_config/
contract_variant_settings.rs

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
mod contract_allocator;
mod stack_size;

pub use contract_allocator::{parse_allocator, ContractAllocator};
pub use stack_size::*;

use crate::ei::EIVersion;

use super::ContractVariantProfileSerde;

/// Collection of flags, specified in the multicontract config.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ContractVariantSettings {
    /// External view contracts are just readers of data from another contract.
    pub external_view: bool,

    /// Panic messages add a lot of bloat to the final bytecode,
    /// so they should only be used for debugging purposes.
    pub panic_message: bool,

    /// Post-processing check of the VM hooks is based on this.
    pub check_ei: Option<EIVersion>,

    /// Allocator config, i.e which allocator to choose for the contract.
    pub allocator: ContractAllocator,

    pub stack_size: usize,

    /// Features that are activated on the contract crate, from wasm.
    pub features: Vec<String>,

    /// Allows disabling default features in the contract crate, from wasm.
    pub default_features: Option<bool>,

    /// Forcibly remove the original contrct legacy callback.
    pub kill_legacy_callback: bool,

    pub profile: ContractVariantProfile,
}

impl Default for ContractVariantSettings {
    fn default() -> Self {
        ContractVariantSettings {
            external_view: Default::default(),
            panic_message: Default::default(),
            check_ei: Some(EIVersion::default()),
            allocator: Default::default(),
            stack_size: DEFAULT_STACK_SIZE,
            features: Default::default(),
            default_features: None,
            kill_legacy_callback: false,
            profile: Default::default(),
        }
    }
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ContractVariantProfile {
    pub codegen_units: u8,
    pub opt_level: String,
    pub lto: bool,
    pub debug: bool,
    pub panic: String,
    pub overflow_checks: bool,
}

impl Default for ContractVariantProfile {
    fn default() -> ContractVariantProfile {
        ContractVariantProfile {
            codegen_units: 1u8,
            opt_level: "z".to_owned(),
            lto: true,
            debug: false,
            panic: "abort".to_owned(),
            overflow_checks: false,
        }
    }
}

impl ContractVariantProfile {
    pub fn from_serde(opt_serde_profile: &Option<ContractVariantProfileSerde>) -> Self {
        let mut result = Self::default();
        if let Some(serde_profile) = opt_serde_profile {
            if let Some(codegen_units) = serde_profile.codegen_units {
                result.codegen_units = codegen_units;
            }
            if let Some(opt_level) = &serde_profile.opt_level {
                result.opt_level.clone_from(opt_level);
            }
            if let Some(lto) = serde_profile.lto {
                result.lto = lto;
            }
            if let Some(debug) = serde_profile.debug {
                result.debug = debug;
            }
            if let Some(panic) = &serde_profile.panic {
                result.panic.clone_from(panic);
            }
            if let Some(overflow_checks) = serde_profile.overflow_checks {
                result.overflow_checks = overflow_checks;
            }
        }
        result
    }
}