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
//! [Invocation] configuration, typically expressed as metadata for tasks.
//!
//! [Invocation]: crate::Invocation

use crate::{consts, Error, Unit};
use libipld::{serde::from_ipld, Ipld};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, time::Duration};

const FUEL_KEY: &str = "fuel";
const MEMORY_KEY: &str = "memory";
const TIMEOUT_KEY: &str = "time";

/// Resource configuration for defining fuel quota, timeout, etc.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
#[schemars(
    rename = "resources",
    description = "Resource configuration for fuel quota, memory allowance, and timeout"
)]
pub struct Resources {
    fuel: Option<u64>,
    #[schemars(description = "Memory in bytes")]
    memory: Option<u64>,
    #[schemars(with = "Option<u64>", description = "Timeout in milliseconds")]
    time: Option<Duration>,
}

impl Default for Resources {
    fn default() -> Self {
        Self {
            fuel: Some(u64::MAX),
            memory: Some(consts::WASM_MAX_MEMORY),
            time: Some(Duration::from_millis(100_000)),
        }
    }
}

impl Resources {
    /// Create new [Resources] configuration.
    pub fn new(fuel: u64, memory: u64, time: Duration) -> Self {
        Self {
            fuel: Some(fuel),
            memory: Some(memory),
            time: Some(time),
        }
    }

    /// Get fuel limit.
    pub fn fuel(&self) -> Option<u64> {
        self.fuel
    }

    /// Set fuel limit.
    pub fn set_fuel(&mut self, fuel: u64) {
        self.fuel = Some(fuel)
    }

    /// Get timeout.
    pub fn time(&self) -> Option<Duration> {
        self.time
    }

    /// Set timeout.
    pub fn set_time(&mut self, time: Duration) {
        self.time = Some(time)
    }

    /// Get max memory.
    pub fn memory(&self) -> Option<u64> {
        self.memory
    }

    /// Set max memory.
    pub fn set_memory(&mut self, memory: u64) {
        self.memory = Some(memory)
    }
}

impl From<Resources> for Ipld {
    fn from(resources: Resources) -> Ipld {
        Ipld::Map(BTreeMap::from([
            (
                FUEL_KEY.into(),
                resources.fuel().map(Ipld::from).unwrap_or(Ipld::Null),
            ),
            (
                MEMORY_KEY.into(),
                resources.memory().map(Ipld::from).unwrap_or(Ipld::Null),
            ),
            (
                TIMEOUT_KEY.into(),
                resources
                    .time()
                    .map(|t| Ipld::from(t.as_millis() as i128))
                    .unwrap_or(Ipld::Null),
            ),
        ]))
    }
}

impl<'a> TryFrom<&'a Ipld> for Resources {
    type Error = Error<Unit>;

    fn try_from(ipld: &Ipld) -> Result<Self, Self::Error> {
        Resources::try_from(ipld.to_owned())
    }
}

impl TryFrom<Ipld> for Resources {
    type Error = Error<Unit>;

    fn try_from(ipld: Ipld) -> Result<Self, Self::Error> {
        let map = from_ipld::<BTreeMap<String, Ipld>>(ipld)?;

        let fuel = map.get(FUEL_KEY).and_then(|ipld| match ipld {
            Ipld::Null => None,
            ipld => from_ipld(ipld.to_owned()).ok(),
        });

        let memory = map.get(MEMORY_KEY).and_then(|ipld| match ipld {
            Ipld::Null => None,
            ipld => from_ipld(ipld.to_owned()).ok(),
        });

        let time = map.get(TIMEOUT_KEY).and_then(|ipld| match ipld {
            Ipld::Null => None,
            ipld => {
                let time = from_ipld(ipld.to_owned()).unwrap_or(100_000);
                Some(Duration::from_millis(time))
            }
        });

        Ok(Resources { fuel, memory, time })
    }
}

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

    #[test]
    fn ipld_roundtrip() {
        let config = Resources::default();
        let ipld = Ipld::from(config.clone());

        assert_eq!(
            ipld,
            Ipld::Map(BTreeMap::from([
                (FUEL_KEY.into(), Ipld::Integer(u64::MAX.into())),
                (
                    MEMORY_KEY.into(),
                    Ipld::Integer(consts::WASM_MAX_MEMORY.into())
                ),
                (TIMEOUT_KEY.into(), Ipld::Integer(100_000))
            ]))
        );
        assert_eq!(config, ipld.try_into().unwrap())
    }

    #[test]
    fn ser_de() {
        let config = Resources::default();
        let ser = serde_json::to_string(&config).unwrap();
        let de = serde_json::from_str(&ser).unwrap();

        assert_eq!(config, de);
    }
}