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
//! State schema.
use std::{
    collections::BTreeMap,
    convert::{TryFrom, TryInto},
};

use oasis_core_runtime::consensus::beacon::EpochTime;

use crate::{
    state::CurrentState,
    storage::{self, Store},
    types::address::Address,
};

use super::{types, Error, MODULE_NAME};

/// Map of active delegations.
pub const DELEGATIONS: &[u8] = &[0x01];
/// Map of undelegations.
pub const UNDELEGATIONS: &[u8] = &[0x02];
/// An undelegation queue.
pub const UNDELEGATION_QUEUE: &[u8] = &[0x03];
/// Receipts.
pub const RECEIPTS: &[u8] = &[0x04];

/// Add delegation for a given (from, to) pair.
///
/// The given shares are added to any existing delegation that may exist for the same (from, to)
/// address pair. If no delegation exists a new one is created.
pub fn add_delegation(from: Address, to: Address, shares: u128) -> Result<(), Error> {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let delegations = storage::PrefixStore::new(store, &DELEGATIONS);
        let mut account = storage::TypedStore::new(storage::PrefixStore::new(delegations, &from));
        let mut di: types::DelegationInfo = account.get(to).unwrap_or_default();

        di.shares = di
            .shares
            .checked_add(shares)
            .ok_or(Error::InvalidArgument)?;

        account.insert(to, di);

        Ok(())
    })
}

/// Subtract delegation from a given (from, to) pair.
pub fn sub_delegation(from: Address, to: Address, shares: u128) -> Result<(), Error> {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let delegations = storage::PrefixStore::new(store, &DELEGATIONS);
        let mut account = storage::TypedStore::new(storage::PrefixStore::new(delegations, &from));
        let mut di: types::DelegationInfo = account.get(to).unwrap_or_default();

        di.shares = di
            .shares
            .checked_sub(shares)
            .ok_or(Error::InsufficientBalance)?;

        if di.shares > 0 {
            account.insert(to, di);
        } else {
            account.remove(to);
        }

        Ok(())
    })
}

/// Retrieve delegation metadata for a given (from, to) pair.
///
/// In case no delegation exists for the given (from, to) address pair, an all-zero delegation
/// metadata are returned.
pub fn get_delegation(from: Address, to: Address) -> Result<types::DelegationInfo, Error> {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let delegations = storage::PrefixStore::new(store, &DELEGATIONS);
        let account = storage::TypedStore::new(storage::PrefixStore::new(delegations, &from));
        Ok(account.get(to).unwrap_or_default())
    })
}

/// Retrieve all delegation metadata originating from a given address.
pub fn get_delegations(from: Address) -> Result<Vec<types::ExtendedDelegationInfo>, Error> {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let delegations = storage::PrefixStore::new(store, &DELEGATIONS);
        let account = storage::TypedStore::new(storage::PrefixStore::new(delegations, &from));

        Ok(account
            .iter()
            .map(
                |(to, di): (Address, types::DelegationInfo)| -> types::ExtendedDelegationInfo {
                    types::ExtendedDelegationInfo {
                        to,
                        shares: di.shares,
                    }
                },
            )
            .collect())
    })
}

/// This is needed to properly iterate over the DELEGATIONS map.
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord)]
struct AddressPair(Address, Address);

#[derive(Error, Debug)]
enum APError {
    #[error("malformed address")]
    MalformedAddress,
}

impl TryFrom<&[u8]> for AddressPair {
    type Error = APError;

    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        let a =
            Address::try_from(&bytes[..Address::SIZE]).map_err(|_| APError::MalformedAddress)?;
        let b =
            Address::try_from(&bytes[Address::SIZE..]).map_err(|_| APError::MalformedAddress)?;
        Ok(AddressPair(a, b))
    }
}

/// Return the number of delegated shares for each destination escrow account.
pub fn get_delegations_by_destination() -> Result<BTreeMap<Address, u128>, Error> {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let delegations = storage::TypedStore::new(storage::PrefixStore::new(store, &DELEGATIONS));

        let mut by_destination: BTreeMap<Address, u128> = BTreeMap::new();
        for (ap, di) in delegations.iter::<AddressPair, types::DelegationInfo>() {
            let total = by_destination.entry(ap.1).or_default();
            *total = total.checked_add(di.shares).ok_or(Error::InvalidArgument)?;
        }

        Ok(by_destination)
    })
}

/// Record new undelegation and add to undelegation queue.
///
/// In case an undelegation for the given (from, to, epoch) tuple already exists, the undelegation
/// entry is merged by adding shares. When a non-zero receipt identifier is passed, the identifier
/// is set in case the existing entry has no such identifier yet.
///
/// It returns the receipt identifier of the undelegation done receipt.
pub fn add_undelegation(
    from: Address,
    to: Address,
    epoch: EpochTime,
    shares: u128,
    receipt: u64,
) -> Result<u64, Error> {
    CurrentState::with_store(|mut root_store| {
        let store = storage::PrefixStore::new(&mut root_store, &MODULE_NAME);
        let undelegations = storage::PrefixStore::new(store, &UNDELEGATIONS);
        let account = storage::PrefixStore::new(undelegations, &to);
        let mut entry = storage::TypedStore::new(storage::PrefixStore::new(account, &from));
        let mut di: types::DelegationInfo = entry.get(epoch.to_storage_key()).unwrap_or_default();

        if receipt > 0 && di.receipt == 0 {
            di.receipt = receipt;
        }
        let done_receipt = di.receipt;

        di.shares = di
            .shares
            .checked_add(shares)
            .ok_or(Error::InvalidArgument)?;

        entry.insert(epoch.to_storage_key(), di);

        // Add to undelegation queue (if existing item is there, this will have no effect).
        let store = storage::PrefixStore::new(root_store, &MODULE_NAME);
        let mut queue = storage::PrefixStore::new(store, &UNDELEGATION_QUEUE);
        queue.insert(
            &queue_entry_key(from, to, epoch),
            &[0xF6], /* CBOR NULL */
        );

        Ok(done_receipt)
    })
}

fn queue_entry_key(from: Address, to: Address, epoch: EpochTime) -> Vec<u8> {
    [&epoch.to_storage_key(), to.as_ref(), from.as_ref()].concat()
}

/// Remove an existing undelegation and return it.
///
/// In case the undelegation doesn't exist, returns a default-constructed DelegationInfo.
pub fn take_undelegation(ud: &Undelegation) -> Result<types::DelegationInfo, Error> {
    CurrentState::with_store(|mut root_store| {
        // Get and remove undelegation metadata.
        let store = storage::PrefixStore::new(&mut root_store, &MODULE_NAME);
        let undelegations = storage::PrefixStore::new(store, &UNDELEGATIONS);
        let account = storage::PrefixStore::new(undelegations, &ud.to);
        let mut entry = storage::TypedStore::new(storage::PrefixStore::new(account, &ud.from));
        let di: types::DelegationInfo = entry.get(ud.epoch.to_storage_key()).unwrap_or_default();
        entry.remove(ud.epoch.to_storage_key());

        // Remove queue entry.
        let store = storage::PrefixStore::new(root_store, &MODULE_NAME);
        let mut queue = storage::PrefixStore::new(store, &UNDELEGATION_QUEUE);
        queue.remove(&queue_entry_key(ud.from, ud.to, ud.epoch));

        Ok(di)
    })
}

struct AddressWithEpoch {
    from: Address,
    epoch: EpochTime,
}

impl TryFrom<&[u8]> for AddressWithEpoch {
    type Error = anyhow::Error;

    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        if value.len() != Address::SIZE + 8 {
            anyhow::bail!("incorrect address with epoch key size");
        }

        Ok(Self {
            from: Address::try_from(&value[..Address::SIZE])?,
            epoch: EpochTime::from_be_bytes(value[Address::SIZE..].try_into()?),
        })
    }
}

/// Retrieve all undelegation metadata to a given address.
pub fn get_undelegations(to: Address) -> Result<Vec<types::UndelegationInfo>, Error> {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let undelegations = storage::PrefixStore::new(store, &UNDELEGATIONS);
        let account = storage::TypedStore::new(storage::PrefixStore::new(undelegations, &to));

        Ok(account
            .iter()
            .map(
                |(ae, di): (AddressWithEpoch, types::DelegationInfo)| -> types::UndelegationInfo {
                    types::UndelegationInfo {
                        from: ae.from,
                        epoch: ae.epoch,
                        shares: di.shares,
                    }
                },
            )
            .collect())
    })
}

/// Undelegation metadata.
pub struct Undelegation {
    pub from: Address,
    pub to: Address,
    pub epoch: EpochTime,
}

impl<'a> TryFrom<&'a [u8]> for Undelegation {
    type Error = anyhow::Error;

    fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
        // Decode a storage key of the format (epoch, to, from).
        if value.len() != 2 * Address::SIZE + 8 {
            anyhow::bail!("incorrect undelegation key size");
        }

        Ok(Self {
            epoch: EpochTime::from_be_bytes(value[..8].try_into()?),
            to: Address::from_bytes(&value[8..8 + Address::SIZE])?,
            from: Address::from_bytes(&value[8 + Address::SIZE..])?,
        })
    }
}

/// Retrieve all queued undelegations for epochs earlier than or equal to the passed epoch.
pub fn get_queued_undelegations(epoch: EpochTime) -> Result<Vec<Undelegation>, Error> {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let queue = storage::TypedStore::new(storage::PrefixStore::new(store, &UNDELEGATION_QUEUE));

        Ok(queue
            .iter()
            .map(|(k, _): (Undelegation, ())| k)
            .take_while(|ud| ud.epoch <= epoch)
            .collect())
    })
}

/// Store the given receipt.
pub fn set_receipt(owner: Address, kind: types::ReceiptKind, id: u64, receipt: types::Receipt) {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let receipts = storage::PrefixStore::new(store, &RECEIPTS);
        let of_owner = storage::PrefixStore::new(receipts, &owner);
        let kind = [kind as u8];
        let mut of_kind = storage::TypedStore::new(storage::PrefixStore::new(of_owner, &kind));

        of_kind.insert(id.to_be_bytes(), receipt);
    });
}

/// Remove the given receipt from storage if it exists and return it, otherwise return `None`.
pub fn take_receipt(owner: Address, kind: types::ReceiptKind, id: u64) -> Option<types::Receipt> {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let receipts = storage::PrefixStore::new(store, &RECEIPTS);
        let of_owner = storage::PrefixStore::new(receipts, &owner);
        let kind = [kind as u8];
        let mut of_kind = storage::TypedStore::new(storage::PrefixStore::new(of_owner, &kind));

        let receipt = of_kind.get(id.to_be_bytes());
        of_kind.remove(id.to_be_bytes());

        receipt
    })
}

/// A trait that exists solely to convert `beacon::EpochTime` to bytes for use as a storage key.
trait ToStorageKey {
    fn to_storage_key(&self) -> [u8; 8];
}

impl ToStorageKey for EpochTime {
    fn to_storage_key(&self) -> [u8; 8] {
        self.to_be_bytes()
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::testing::{keys, mock};

    #[test]
    fn test_delegation() {
        let _mock = mock::Mock::default();

        add_delegation(keys::alice::address(), keys::bob::address(), 500).unwrap();
        add_delegation(keys::alice::address(), keys::bob::address(), 500).unwrap();

        let di = get_delegation(keys::bob::address(), keys::alice::address()).unwrap();
        assert_eq!(di.shares, 0);
        let di = get_delegation(keys::alice::address(), keys::bob::address()).unwrap();
        assert_eq!(di.shares, 1000);

        let dis = get_delegations(keys::bob::address()).unwrap();
        assert!(dis.is_empty());
        let dis = get_delegations(keys::alice::address()).unwrap();
        assert_eq!(dis.len(), 1);
        assert_eq!(dis[0].shares, 1000);

        let totals = get_delegations_by_destination().unwrap();
        assert_eq!(totals.len(), 1);
        assert_eq!(totals[&keys::bob::address()], 1000);

        sub_delegation(keys::alice::address(), keys::bob::address(), 100).unwrap();

        let di = get_delegation(keys::alice::address(), keys::bob::address()).unwrap();
        assert_eq!(di.shares, 900);

        let totals = get_delegations_by_destination().unwrap();
        assert_eq!(totals.len(), 1);
        assert_eq!(totals[&keys::bob::address()], 900);

        add_delegation(keys::bob::address(), keys::bob::address(), 200).unwrap();

        let totals = get_delegations_by_destination().unwrap();
        assert_eq!(totals.len(), 1);
        assert_eq!(totals[&keys::bob::address()], 1100);

        add_delegation(keys::bob::address(), keys::alice::address(), 100).unwrap();

        let totals = get_delegations_by_destination().unwrap();
        assert_eq!(totals.len(), 2);
        assert_eq!(totals[&keys::alice::address()], 100);
        assert_eq!(totals[&keys::bob::address()], 1100);
    }

    #[test]
    fn test_undelegation() {
        let _mock = mock::Mock::default();

        add_undelegation(keys::alice::address(), keys::bob::address(), 42, 500, 12).unwrap();
        add_undelegation(keys::alice::address(), keys::bob::address(), 42, 500, 24).unwrap();
        add_undelegation(keys::alice::address(), keys::bob::address(), 84, 200, 36).unwrap();

        let qd = get_queued_undelegations(10).unwrap();
        assert!(qd.is_empty());
        let qd = get_queued_undelegations(42).unwrap();
        assert_eq!(qd.len(), 1);
        assert_eq!(qd[0].from, keys::alice::address());
        assert_eq!(qd[0].to, keys::bob::address());
        assert_eq!(qd[0].epoch, 42);
        let qd = get_queued_undelegations(43).unwrap();
        assert_eq!(qd.len(), 1);
        assert_eq!(qd[0].from, keys::alice::address());
        assert_eq!(qd[0].to, keys::bob::address());
        assert_eq!(qd[0].epoch, 42);

        let udis = get_undelegations(keys::alice::address()).unwrap();
        assert!(udis.is_empty());
        let udis = get_undelegations(keys::bob::address()).unwrap();
        assert_eq!(udis.len(), 2);
        assert_eq!(udis[0].from, keys::alice::address());
        assert_eq!(udis[0].shares, 1000);
        assert_eq!(udis[0].epoch, 42);
        assert_eq!(udis[1].from, keys::alice::address());
        assert_eq!(udis[1].shares, 200);
        assert_eq!(udis[1].epoch, 84);

        let di = take_undelegation(&qd[0]).unwrap();
        assert_eq!(di.shares, 1000);
        assert_eq!(di.receipt, 12, "receipt id should not be overwritten");

        let qd = get_queued_undelegations(42).unwrap();
        assert!(qd.is_empty());

        let udis = get_undelegations(keys::bob::address()).unwrap();
        assert_eq!(udis.len(), 1);
    }

    #[test]
    fn test_receipts() {
        let _mock = mock::Mock::default();

        let receipt = types::Receipt {
            shares: 123,
            ..Default::default()
        };
        set_receipt(
            keys::alice::address(),
            types::ReceiptKind::Delegate,
            42,
            receipt.clone(),
        );

        let dec_receipt = take_receipt(keys::alice::address(), types::ReceiptKind::Delegate, 10);
        assert!(dec_receipt.is_none(), "missing receipt should return None");

        let dec_receipt = take_receipt(
            keys::alice::address(),
            types::ReceiptKind::UndelegateStart,
            42,
        );
        assert!(dec_receipt.is_none(), "missing receipt should return None");

        let dec_receipt = take_receipt(
            keys::alice::address(),
            types::ReceiptKind::UndelegateDone,
            42,
        );
        assert!(dec_receipt.is_none(), "missing receipt should return None");

        let dec_receipt = take_receipt(keys::alice::address(), types::ReceiptKind::Delegate, 42);
        assert_eq!(dec_receipt, Some(receipt), "receipt should be correct");

        let dec_receipt = take_receipt(keys::alice::address(), types::ReceiptKind::Delegate, 42);
        assert!(dec_receipt.is_none(), "receipt should have been removed");
    }
}