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
//! Represent when a transaction is valid.
use crate::error::{Error, Result};
use crate::xdr;
use crate::xdr::{XDRDeserialize, XDRSerialize};
use chrono::{DateTime, Duration, TimeZone, Utc};
use xdr_rs_serialize::de::XDRIn;
use xdr_rs_serialize::ser::XDROut;

/// The time window in which a transaction is considered valid.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TimeBounds {
    lower: Option<DateTime<Utc>>,
    upper: Option<DateTime<Utc>>,
}

impl TimeBounds {
    /// Returns time bounds with the upper bounds set to `duration` in the future.
    pub fn valid_for(duration: Duration) -> TimeBounds {
        let lower = Utc::now();
        let upper = lower + duration;
        TimeBounds {
            lower: None,
            upper: Some(upper),
        }
    }

    /// Returns time bounds such that the transaction is always valid.
    pub fn always_valid() -> TimeBounds {
        TimeBounds {
            lower: None,
            upper: None,
        }
    }

    /// Makes a new time bounds with the lower bound changed.
    pub fn with_lower(&self, lower: DateTime<Utc>) -> Result<TimeBounds> {
        ensure_valid_timestamp(&lower)?;
        match self.upper {
            Some(upper) if upper < lower => Err(Error::InvalidTimeBounds),
            Some(upper) => Ok(TimeBounds {
                lower: Some(lower),
                upper: Some(upper),
            }),
            None => Ok(TimeBounds {
                lower: Some(lower),
                upper: None,
            }),
        }
    }

    /// Makes a new time bounds with the upper bound changed.
    pub fn with_upper(&self, upper: DateTime<Utc>) -> Result<TimeBounds> {
        ensure_valid_timestamp(&upper)?;
        match self.lower {
            Some(lower) if upper < lower => Err(Error::InvalidTimeBounds),
            Some(lower) => Ok(TimeBounds {
                lower: Some(lower),
                upper: Some(upper),
            }),
            None => Ok(TimeBounds {
                lower: None,
                upper: Some(upper),
            }),
        }
    }

    /// Retrieves the time bounds lower bound.
    pub fn lower(&self) -> &Option<DateTime<Utc>> {
        &self.lower
    }

    /// Retrieves a mutable reference to the time bounds lower bound.
    pub fn lower_mut(&mut self) -> &mut Option<DateTime<Utc>> {
        &mut self.lower
    }

    /// Retrieves the time bounds lower bound.
    pub fn upper(&self) -> &Option<DateTime<Utc>> {
        &self.upper
    }

    /// Retrieves a mutable reference to the time bounds lower bound.
    pub fn upper_mut(&mut self) -> &mut Option<DateTime<Utc>> {
        &mut self.upper
    }

    /// Returns the xdr object.
    pub fn to_xdr(&self) -> Result<xdr::TimeBounds> {
        let min_time = match self.lower {
            None => xdr::Uint64::new(0),
            Some(t) => xdr::Uint64::new(t.timestamp() as u64),
        };
        let min_time = xdr::TimePoint::new(min_time);
        let max_time = match self.upper {
            None => xdr::Uint64::new(0),
            Some(t) => xdr::Uint64::new(t.timestamp() as u64),
        };
        let max_time = xdr::TimePoint::new(max_time);
        Ok(xdr::TimeBounds { min_time, max_time })
    }

    /// Creates from the xdr object.
    pub fn from_xdr(x: &xdr::TimeBounds) -> Result<TimeBounds> {
        let min_time_epoch = x.min_time.value.value as i64;
        let max_time_epoch = x.max_time.value.value as i64;

        let mut res = TimeBounds::always_valid();

        if min_time_epoch != 0 {
            res = res.with_lower(Utc.timestamp(min_time_epoch, 0))?;
        }
        if max_time_epoch != 0 {
            res = res.with_upper(Utc.timestamp(max_time_epoch, 0))?;
        }

        Ok(res)
    }
}

impl XDRSerialize for TimeBounds {
    fn write_xdr(&self, mut out: &mut Vec<u8>) -> Result<u64> {
        let xdr = self.to_xdr()?;
        xdr.write_xdr(&mut out).map_err(Error::XdrError)
    }
}

impl XDRDeserialize for TimeBounds {
    fn from_xdr_bytes(buffer: &[u8]) -> Result<(Self, u64)> {
        let (xdr_timebounds, bytes_read) =
            xdr::TimeBounds::read_xdr(&buffer).map_err(Error::XdrError)?;
        let res = TimeBounds::from_xdr(&xdr_timebounds)?;
        Ok((res, bytes_read))
    }
}

fn ensure_valid_timestamp(dt: &DateTime<Utc>) -> Result<()> {
    let ts = dt.timestamp();
    if ts >= 0 {
        Ok(())
    } else {
        Err(Error::InvalidTimeBounds)
    }
}

#[cfg(test)]
mod tests {
    use super::TimeBounds;
    use crate::xdr::{XDRDeserialize, XDRSerialize};
    use chrono::{DateTime, Datelike, Duration, NaiveDateTime, Utc};

    #[test]
    fn test_valid_for() {
        let five_min = Duration::minutes(5);
        let tb = TimeBounds::valid_for(five_min);
        assert_eq!(None, *tb.lower());
        assert_ne!(None, *tb.upper());
    }

    #[test]
    fn test_always_valid() {
        let tb = TimeBounds::always_valid();
        assert_eq!(None, *tb.lower());
        assert_eq!(None, *tb.upper());
    }

    #[test]
    fn test_with_upper_success() {
        let tb = TimeBounds::always_valid().with_upper(Utc::now()).unwrap();
        assert_eq!(None, *tb.lower());
        assert_ne!(None, *tb.upper());
    }

    #[test]
    fn test_with_lower_success() {
        let tb = TimeBounds::always_valid().with_lower(Utc::now()).unwrap();
        assert_ne!(None, *tb.lower());
        assert_eq!(None, *tb.upper());
    }

    #[test]
    fn test_with_both_success() {
        let now = Utc::now();
        let before_now = now - Duration::minutes(1);
        let tb = TimeBounds::always_valid()
            .with_lower(before_now)
            .unwrap()
            .with_upper(now)
            .unwrap();
        assert_ne!(None, *tb.lower());
        assert_ne!(None, *tb.upper());
    }

    #[test]
    fn test_with_upper_before_the_seventies() {
        let res = TimeBounds::always_valid().with_upper(Utc::now().with_year(1960).unwrap());
        assert!(res.is_err());
    }

    #[test]
    fn test_with_lower_before_the_seventies() {
        let res = TimeBounds::always_valid().with_lower(Utc::now().with_year(1960).unwrap());
        assert!(res.is_err());
    }

    #[test]
    fn test_with_upper_before_lower() {
        let now = Utc::now();
        let before_now = now - Duration::minutes(1);
        let res = TimeBounds::always_valid()
            .with_lower(now)
            .unwrap()
            .with_upper(before_now);
        assert!(res.is_err());
    }

    #[test]
    fn test_with_lower_after_upper() {
        let now = Utc::now();
        let before_now = now - Duration::minutes(1);
        let res = TimeBounds::always_valid()
            .with_upper(before_now)
            .unwrap()
            .with_lower(now);
        assert!(res.is_err());
    }

    #[test]
    fn test_serialize_always_valid() {
        let tb = TimeBounds::always_valid();
        let xdr = tb.xdr_base64().unwrap();
        assert_eq!("AAAAAAAAAAAAAAAAAAAAAA==", xdr);
    }

    #[test]
    fn test_serialize_with_bounds() {
        let now = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(1594305941, 0), Utc);
        let before_now = now - Duration::minutes(1);
        let tb = TimeBounds::always_valid()
            .with_lower(before_now)
            .unwrap()
            .with_upper(now)
            .unwrap();
        let xdr = tb.xdr_base64().unwrap();
        assert_eq!("AAAAAF8HLVkAAAAAXwctlQ==", xdr);
    }

    #[test]
    fn test_deserialize_always_valid() {
        let expected = TimeBounds::always_valid();
        let tb = TimeBounds::from_xdr_base64("AAAAAAAAAAAAAAAAAAAAAA==").unwrap();
        assert_eq!(expected, tb);
    }

    #[test]
    fn test_deserialize_with_bounds() {
        let now = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(1594305941, 0), Utc);
        let before_now = now - Duration::minutes(1);
        let expected = TimeBounds::always_valid()
            .with_lower(before_now)
            .unwrap()
            .with_upper(now)
            .unwrap();
        let tb = TimeBounds::from_xdr_base64("AAAAAF8HLVkAAAAAXwctlQ==").unwrap();
        assert_eq!(expected, tb);
    }
}