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
#![recursion_limit = "128"]

#[macro_use]
extern crate tower_web;

use bytes::Bytes;
use futures::Future;
use hyper::StatusCode;
use interledger_packet::Address;
use interledger_service::Account;
use lazy_static::lazy_static;
use std::str::FromStr;
use url::Url;

mod api;
mod client;
#[cfg(test)]
mod fixtures;
mod message_service;
#[cfg(test)]
mod test_helpers;
use num_bigint::BigUint;
use std::ops::{Div, Mul};

pub use api::SettlementApi;
pub use client::SettlementClient;
pub use message_service::SettlementMessageService;

lazy_static! {
    pub static ref SE_ILP_ADDRESS: Address = Address::from_str("peer.settle").unwrap();
}

#[derive(Extract, Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub struct Quantity {
    pub amount: String,
    pub scale: u8,
}

impl Quantity {
    pub fn new(amount: impl ToString, scale: u8) -> Self {
        Quantity {
            amount: amount.to_string(),
            scale,
        }
    }
}

// TODO: Since we still haven't finalized all the settlement details, we might
// end up deciding to add some more values, e.g. some settlement engine uid or similar.
// All instances of this struct should be replaced with Url instances once/if we
// agree that there is no more info required to refer to an engine.
pub struct SettlementEngineDetails {
    /// Base URL of the settlement engine
    pub url: Url,
}

pub trait SettlementAccount: Account {
    fn settlement_engine_details(&self) -> Option<SettlementEngineDetails> {
        None
    }
}

pub trait SettlementStore {
    type Account: SettlementAccount;

    fn update_balance_for_incoming_settlement(
        &self,
        account_id: <Self::Account as Account>::AccountId,
        amount: u64,
        idempotency_key: Option<String>,
    ) -> Box<dyn Future<Item = (), Error = ()> + Send>;

    fn refund_settlement(
        &self,
        account_id: <Self::Account as Account>::AccountId,
        settle_amount: u64,
    ) -> Box<dyn Future<Item = (), Error = ()> + Send>;
}

pub type IdempotentData = (StatusCode, Bytes, [u8; 32]);

pub trait IdempotentStore {
    /// Returns the API response that was saved when the idempotency key was used
    /// Also returns a hash of the input data which resulted in the response
    fn load_idempotent_data(
        &self,
        idempotency_key: String,
    ) -> Box<dyn Future<Item = Option<IdempotentData>, Error = ()> + Send>;

    /// Saves the data that was passed along with the api request for later
    /// The store MUST also save a hash of the input, so that it errors out on requests
    fn save_idempotent_data(
        &self,
        idempotency_key: String,
        input_hash: [u8; 32],
        status_code: StatusCode,
        data: Bytes,
    ) -> Box<dyn Future<Item = (), Error = ()> + Send>;
}

#[derive(Debug)]
pub struct ConvertDetails {
    pub from: u8,
    pub to: u8,
}

/// Traits for u64 and f64 asset code conversions for amounts and rates
pub trait Convert {
    type Item: Sized;

    // Returns the scaled result, or an error if there was an overflow
    fn normalize_scale(&self, details: ConvertDetails) -> Result<Self::Item, ()>;
}

impl Convert for u64 {
    type Item = u64;

    fn normalize_scale(&self, details: ConvertDetails) -> Result<Self::Item, ()> {
        let scale_diff = (details.from as i8 - details.to as i8).abs() as u8;
        let scale = 10u64.pow(scale_diff.into());
        let (res, overflow) = if details.to >= details.from {
            self.overflowing_mul(scale)
        } else {
            self.overflowing_div(scale)
        };
        if overflow {
            Err(())
        } else {
            Ok(res)
        }
    }
}

impl Convert for f64 {
    type Item = f64;
    // Not overflow safe. Would require using a package for Big floating point
    // numbers such as BigDecimal
    fn normalize_scale(&self, details: ConvertDetails) -> Result<Self::Item, ()> {
        let scale_diff = (details.from as i8 - details.to as i8).abs() as u8;
        let scale = 10f64.powi(scale_diff.into());
        let res = if details.to >= details.from {
            self * scale
        } else {
            self / scale
        };
        if res == std::f64::INFINITY {
            Err(())
        } else {
            Ok(res)
        }
    }
}

impl Convert for BigUint {
    type Item = BigUint;

    fn normalize_scale(&self, details: ConvertDetails) -> Result<Self::Item, ()> {
        let scale_diff = (details.from as i8 - details.to as i8).abs() as u8;
        let scale = 10u64.pow(scale_diff.into());
        if details.to >= details.from {
            Ok(self.mul(scale))
        } else {
            Ok(self.div(scale))
        }
    }
}

#[cfg(test)]
mod tests {
    /// Tests for the asset conversion
    use super::*;
    use super::{Convert, ConvertDetails};
    use num_traits::cast::FromPrimitive;

    #[test]
    fn biguint_test() {
        let hundred_gwei = BigUint::from_str("100000000000").unwrap();
        assert_eq!(
            hundred_gwei
                .normalize_scale(ConvertDetails { from: 18, to: 9 })
                .unwrap()
                .to_string(),
            BigUint::from_u64(100u64).unwrap().to_string(),
        );
    }

    #[test]
    fn u64_test() {
        // overflows
        let huge_number = std::u64::MAX / 10;
        assert_eq!(
            huge_number
                .normalize_scale(ConvertDetails { from: 1, to: 18 })
                .unwrap_err(),
            (),
        );
        // 1 unit with scale 1, is 1 unit with scale 1
        assert_eq!(
            1u64.normalize_scale(ConvertDetails { from: 1, to: 1 })
                .unwrap(),
            1
        );
        // there's uncredited_settlement_amount for all number slots which do not increase in
        // increments of 10^abs(to_scale-from_scale)
        assert_eq!(
            1u64.normalize_scale(ConvertDetails { from: 2, to: 1 })
                .unwrap(),
            0
        );
        // 1 unit with scale 1, is 10 units with scale 2
        assert_eq!(
            1u64.normalize_scale(ConvertDetails { from: 1, to: 2 })
                .unwrap(),
            10
        );
        // 1 gwei (scale 9) is 1e9 wei (scale 18)
        assert_eq!(
            1u64.normalize_scale(ConvertDetails { from: 9, to: 18 })
                .unwrap(),
            1_000_000_000
        );
        // 1_000_000_000 wei is 1gwei
        assert_eq!(
            1_000_000_000u64
                .normalize_scale(ConvertDetails { from: 18, to: 9 })
                .unwrap(),
            1,
        );
        // 10 units with base 2 is 100 units with base 3
        assert_eq!(
            10u64
                .normalize_scale(ConvertDetails { from: 2, to: 3 })
                .unwrap(),
            100
        );
        // 299 units with base 3 is 29 units with base 2 (0.9 uncredited_settlement_amount)
        assert_eq!(
            299u64
                .normalize_scale(ConvertDetails { from: 3, to: 2 })
                .unwrap(),
            29
        );
        assert_eq!(
            999u64
                .normalize_scale(ConvertDetails { from: 9, to: 6 })
                .unwrap(),
            0
        );
        assert_eq!(
            1000u64
                .normalize_scale(ConvertDetails { from: 9, to: 6 })
                .unwrap(),
            1
        );
        assert_eq!(
            1999u64
                .normalize_scale(ConvertDetails { from: 9, to: 6 })
                .unwrap(),
            1
        );
    }

    #[allow(clippy::float_cmp)]
    #[test]
    fn f64_test() {
        // overflow
        assert_eq!(
            std::f64::MAX
                .normalize_scale(ConvertDetails {
                    from: 1,
                    to: std::u8::MAX,
                })
                .unwrap_err(),
            ()
        );

        // 1 unit with base 1, is 1 unit with base 1
        assert_eq!(
            1f64.normalize_scale(ConvertDetails { from: 1, to: 1 })
                .unwrap(),
            1.0
        );
        // 1 unit with base 10, is 10 units with base 1
        assert_eq!(
            1f64.normalize_scale(ConvertDetails { from: 1, to: 2 })
                .unwrap(),
            10.0
        );
        // 1 sat is 1e9 wei (multiplied by rate)
        assert_eq!(
            1f64.normalize_scale(ConvertDetails { from: 9, to: 18 })
                .unwrap(),
            1_000_000_000.0
        );

        // 1.0 unit with base 2 is 0.1 unit with base 1
        assert_eq!(
            1f64.normalize_scale(ConvertDetails { from: 2, to: 1 })
                .unwrap(),
            0.1
        );
        assert_eq!(
            10.5f64
                .normalize_scale(ConvertDetails { from: 2, to: 1 })
                .unwrap(),
            1.05
        );
        // 100 units with base 3 is 10 units with base 2
        assert_eq!(
            100f64
                .normalize_scale(ConvertDetails { from: 3, to: 2 })
                .unwrap(),
            10.0
        );
        // 299 units with base 3 is 29.9 with base 2
        assert_eq!(
            299f64
                .normalize_scale(ConvertDetails { from: 3, to: 2 })
                .unwrap(),
            29.9
        );

        assert_eq!(
            999f64
                .normalize_scale(ConvertDetails { from: 9, to: 6 })
                .unwrap(),
            0.999
        );
        assert_eq!(
            1000f64
                .normalize_scale(ConvertDetails { from: 9, to: 6 })
                .unwrap(),
            1.0
        );
        assert_eq!(
            1999f64
                .normalize_scale(ConvertDetails { from: 9, to: 6 })
                .unwrap(),
            1.999
        );
    }
}