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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! Consensus staking structures.
//!
//! # Note
//!
//! This **MUST** be kept in sync with go/staking/api.
//!
use std::collections::BTreeMap;

use crate::{
    common::{crypto::hash::Hash, quantity::Quantity},
    consensus::{address::Address, beacon::EpochTime},
};

/// A stake transfer.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct Transfer {
    pub to: Address,
    pub amount: Quantity,
}

/// A withdrawal from an account.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct Withdraw {
    pub from: Address,
    pub amount: Quantity,
}

/// A stake escrow.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct Escrow {
    pub account: Address,
    pub amount: Quantity,
}

/// A reclaim escrow.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct ReclaimEscrow {
    pub account: Address,
    pub shares: Quantity,
}

/// Kind of staking threshold.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, cbor::Encode, cbor::Decode)]
#[repr(i32)]
pub enum ThresholdKind {
    /// Entity staking threshold.
    KindEntity = 0,
    /// Validator node staking threshold.
    KindNodeValidator = 1,
    /// Compute node staking threshold.
    KindNodeCompute = 2,
    /// Keymanager node staking threshold.
    KindNodeKeyManager = 4,
    /// Compute runtime staking threshold.
    KindRuntimeCompute = 5,
    /// Keymanager runtime staking threshold.
    KindRuntimeKeyManager = 6,
}

/// Entry in the staking ledger.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct Account {
    #[cbor(optional)]
    pub general: GeneralAccount,

    #[cbor(optional)]
    pub escrow: EscrowAccount,
}

/// General purpose account.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct GeneralAccount {
    #[cbor(optional)]
    pub balance: Quantity,

    #[cbor(optional)]
    pub nonce: u64,

    #[cbor(optional)]
    pub allowances: BTreeMap<Address, Quantity>,
}

/// Escrow account.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct EscrowAccount {
    #[cbor(optional)]
    pub active: SharePool,

    #[cbor(optional)]
    pub debonding: SharePool,

    #[cbor(optional)]
    pub commission_schedule: CommissionSchedule,

    #[cbor(optional)]
    pub stake_accumulator: StakeAccumulator,
}

/// Combined balance of serval entries, the relative sizes of which are tracked through shares.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct SharePool {
    #[cbor(optional)]
    pub balance: Quantity,

    #[cbor(optional)]
    pub total_shares: Quantity,
}

/// Defines a list of commission rates and commission rate bounds with their starting times.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct CommissionSchedule {
    #[cbor(optional)]
    pub rates: Vec<CommissionRateStep>,

    #[cbor(optional)]
    pub bounds: Vec<CommissionRateBoundStep>,
}

/// Commission rate and its starting time.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct CommissionRateStep {
    pub start: EpochTime,
    pub rate: Quantity,
}

/// Commission rate bound and its starting time.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct CommissionRateBoundStep {
    #[cbor(optional)]
    pub start: EpochTime,

    #[cbor(optional)]
    pub rate_min: Quantity,

    #[cbor(optional)]
    pub rate_max: Quantity,
}

/// Per escrow account stake accumulator.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct StakeAccumulator {
    #[cbor(optional)]
    pub claims: BTreeMap<StakeClaim, Vec<StakeThreshold>>,
}

/// Unique stake claim identifier.
pub type StakeClaim = String;

/// Stake threshold used in the stake accumulator.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct StakeThreshold {
    #[cbor(optional)]
    pub global: Option<ThresholdKind>,

    #[cbor(optional, rename = "const")]
    pub constant: Option<Quantity>,
}

/// Delegation descriptor.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct Delegation {
    pub shares: Quantity,
}

/// Debonding delegation descriptor.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct DebondingDelegation {
    pub shares: Quantity,

    #[cbor(rename = "debond_end")]
    pub debond_end_time: EpochTime,
}

/// Reason for slashing an entity.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, cbor::Encode, cbor::Decode)]
#[repr(u8)]
pub enum SlashReason {
    /// Slashing due to submission of incorrect results in runtime executor commitments.
    RuntimeIncorrectResults = 0x80,
    /// Slashing due to signing two different executor commits or proposed batches for the same
    /// round.
    RuntimeEquivocation = 0x81,
    /// Slashing due to not doing the required work.
    RuntimeLiveness = 0x82,
}

/// Per-reason slashing configuration.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct Slash {
    pub amount: Quantity,
    pub freeze_interval: EpochTime,
}

/// Transfer result.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct TransferResult {
    pub from: Address,
    pub to: Address,
    pub amount: Quantity,
}

/// Add escrow result.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct AddEscrowResult {
    pub owner: Address,
    pub escrow: Address,
    pub amount: Quantity,
    pub new_shares: Quantity,
}

/// Reclaim escrow result.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct ReclaimEscrowResult {
    pub owner: Address,
    pub escrow: Address,
    pub amount: Quantity,
    pub remaining_shares: Quantity,
    pub debonding_shares: Quantity,
    pub debond_end_time: EpochTime,
}

/// Withdraw result.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct WithdrawResult {
    pub owner: Address,
    pub beneficiary: Address,
    pub allowance: Quantity,
    pub amount_change: Quantity,
}

/// A staking-related event.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct Event {
    #[cbor(optional)]
    pub height: i64,
    #[cbor(optional)]
    pub tx_hash: Hash,

    // TODO: Consider refactoring this to be an enum.
    #[cbor(optional)]
    pub transfer: Option<TransferEvent>,
    #[cbor(optional)]
    pub burn: Option<BurnEvent>,
    #[cbor(optional)]
    pub escrow: Option<EscrowEvent>,
    #[cbor(optional)]
    pub allowance_change: Option<AllowanceChangeEvent>,
}

/// Event emitted when stake is transferred, either by a call to Transfer or Withdraw.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct TransferEvent {
    pub from: Address,
    pub to: Address,
    pub amount: Quantity,
}

/// Event emitted when stake is destroyed via a call to Burn.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct BurnEvent {
    pub owner: Address,
    pub amount: Quantity,
}

/// Escrow-related events.
#[derive(Clone, Debug, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub enum EscrowEvent {
    /// Event emitted when stake is transferred into an escrow account.
    #[cbor(rename = "add")]
    Add {
        owner: Address,
        escrow: Address,
        amount: Quantity,
        new_shares: Quantity,
    },

    /// Event emitted when stake is taken from an escrow account (i.e. stake is slashed).
    #[cbor(rename = "take")]
    Take {
        owner: Address,
        // The sum of amounts slashed from active and debonding escrow balances.
        amount: Quantity,
        // The amount slashed from the debonding escrow balance.
        debonding_amount: Quantity,
    },

    /// Event emitted when the debonding process has started and the given number of active shares
    /// have been moved into the debonding pool and started debonding.
    ///
    /// Note that the given amount is valid at the time of debonding start and may not correspond to
    /// the final debonded amount in case any escrowed stake is subject to slashing.
    #[cbor(rename = "debonding_start")]
    DebondingStart {
        owner: Address,
        escrow: Address,
        amount: Quantity,
        active_shares: Quantity,
        debonding_shares: Quantity,
        debond_end_time: EpochTime,
    },

    /// Event emitted when stake is reclaimed from an escrow account back into owner's general
    /// account.
    #[cbor(rename = "reclaim")]
    Reclaim {
        owner: Address,
        escrow: Address,
        amount: Quantity,
        shares: Quantity,
    },
}

/// Event emitted when allowance is changed for a beneficiary.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct AllowanceChangeEvent {
    pub owner: Address,
    pub beneficiary: Address,
    pub allowance: Quantity,
    #[cbor(optional)]
    pub negative: bool,
    pub amount_change: Quantity,
}

#[cfg(test)]
mod tests {
    use base64::prelude::*;

    use super::*;
    use crate::{
        common::crypto::signature::PublicKey,
        consensus::address::{COMMON_POOL_ADDRESS, GOVERNANCE_DEPOSITS_ADDRESS},
    };

    #[test]
    fn test_consistent_accounts() {
        let tcs = vec![
        ("oA==", Account::default()),
        (
            "oWdnZW5lcmFsomVub25jZRghZ2JhbGFuY2VBCg==",
            Account {
                general: GeneralAccount {
                    balance: Quantity::from(10u32),
                    nonce: 33,
                    ..Default::default()
                },
                ..Default::default()
            },
        ),
        (
            "oWdnZW5lcmFsoWphbGxvd2FuY2VzolUAdU/0RxQ6XsX0cbMPhna5TVaxV1BBIVUA98Te1iET4sKC6oZyI6VE7VXWum5BZA==",
            {
                Account {
                    general: GeneralAccount {
                        allowances: [
                            (COMMON_POOL_ADDRESS.clone(), Quantity::from(100u32)),
                            (GOVERNANCE_DEPOSITS_ADDRESS.clone(), Quantity::from(33u32))
                        ].iter().cloned().collect(),
                        ..Default::default()
                        },
                    ..Default::default()
                }
                },
        ),
        (
            "oWZlc2Nyb3ejZmFjdGl2ZaJnYmFsYW5jZUIETGx0b3RhbF9zaGFyZXNBC3FzdGFrZV9hY2N1bXVsYXRvcqFmY2xhaW1zoWZlbnRpdHmCoWVjb25zdEFNoWZnbG9iYWwCc2NvbW1pc3Npb25fc2NoZWR1bGWhZmJvdW5kc4GjZXN0YXJ0GCFocmF0ZV9tYXhCA+hocmF0ZV9taW5BCg==",
            Account {
                escrow: EscrowAccount {
                    active: SharePool{
                        balance: Quantity::from(1100u32),
                        total_shares: Quantity::from(11u32),
                    },
                    debonding: SharePool::default(),
                    commission_schedule: CommissionSchedule {
                        bounds: vec![CommissionRateBoundStep{
                            start: 33,
                            rate_min: Quantity::from(10u32),
                            rate_max: Quantity::from(1000u32),
                        }],
                        ..Default::default()
                    },
                    stake_accumulator: StakeAccumulator {
                        claims: [
                            (
                                "entity".to_string(),
                                vec![
                                    StakeThreshold{
                                        constant: Some(Quantity::from(77u32)),
                                        ..Default::default()
                                    },
                                    StakeThreshold{
                                        global: Some(ThresholdKind::KindNodeCompute),
                                        ..Default::default()
                                    },
                                ]
                            )
                        ].iter().cloned().collect()
                    }
                },
                ..Default::default()
            },
        )
    ];
        for (encoded_base64, rr) in tcs {
            let dec: Account = cbor::from_slice(&BASE64_STANDARD.decode(encoded_base64).unwrap())
                .expect("account should deserialize correctly");
            assert_eq!(dec, rr, "decoded account should match the expected value");
        }
    }

    #[test]
    fn test_consistent_delegations() {
        let tcs = vec![
            ("oWZzaGFyZXNA", Delegation::default()),
            (
                "oWZzaGFyZXNBZA==",
                Delegation {
                    shares: Quantity::from(100u32),
                },
            ),
        ];
        for (encoded_base64, rr) in tcs {
            let dec: Delegation =
                cbor::from_slice(&BASE64_STANDARD.decode(encoded_base64).unwrap())
                    .expect("delegation should deserialize correctly");
            assert_eq!(dec, rr, "decoded account should match the expected value");
        }
    }

    #[test]
    fn test_consistent_debonding_delegations() {
        let tcs = vec![
            (
                "omZzaGFyZXNAamRlYm9uZF9lbmQA",
                DebondingDelegation::default(),
            ),
            (
                "omZzaGFyZXNBZGpkZWJvbmRfZW5kFw==",
                DebondingDelegation {
                    shares: Quantity::from(100u32),
                    debond_end_time: 23,
                },
            ),
        ];
        for (encoded_base64, rr) in tcs {
            let dec: DebondingDelegation =
                cbor::from_slice(&BASE64_STANDARD.decode(encoded_base64).unwrap())
                    .expect("debonding delegation should deserialize correctly");
            assert_eq!(dec, rr, "decoded account should match the expected value");
        }
    }

    #[test]
    fn test_consistent_transfer_results() {
        let addr1 = Address::from_pk(&PublicKey::from(
            "aaafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        ));
        let addr2 = Address::from_pk(&PublicKey::from(
            "bbbfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        ));

        let tcs = vec![
            ("o2J0b1UAAAAAAAAAAAAAAAAAAAAAAAAAAABkZnJvbVUAAAAAAAAAAAAAAAAAAAAAAAAAAABmYW1vdW50QA==", TransferResult::default()),
            (
                "o2J0b1UAuRI5eJXmRwxR+r7MndyD9wrthqFkZnJvbVUAIHIUNIk/YWwJgUjiz5+Z4+KCbhNmYW1vdW50QWQ=",
                TransferResult {
                    amount: Quantity::from(100u32),
                    from: addr1,
                    to: addr2,
                },
            ),
        ];
        for (encoded_base64, rr) in tcs {
            let dec: TransferResult =
                cbor::from_slice(&BASE64_STANDARD.decode(encoded_base64).unwrap())
                    .expect("transfer result should deserialize correctly");
            assert_eq!(dec, rr, "decoded result should match the expected value");
        }
    }

    #[test]
    fn test_consistent_withdraw_results() {
        let addr1 = Address::from_pk(&PublicKey::from(
            "aaafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        ));
        let addr2 = Address::from_pk(&PublicKey::from(
            "bbbfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        ));

        let tcs = vec![
            ("pGVvd25lclUAAAAAAAAAAAAAAAAAAAAAAAAAAABpYWxsb3dhbmNlQGtiZW5lZmljaWFyeVUAAAAAAAAAAAAAAAAAAAAAAAAAAABtYW1vdW50X2NoYW5nZUA=", WithdrawResult::default()),
            (
                "pGVvd25lclUAIHIUNIk/YWwJgUjiz5+Z4+KCbhNpYWxsb3dhbmNlQQprYmVuZWZpY2lhcnlVALkSOXiV5kcMUfq+zJ3cg/cK7YahbWFtb3VudF9jaGFuZ2VBBQ==",
                WithdrawResult {
                    owner: addr1,
                    beneficiary: addr2,
                    allowance: Quantity::from(10u32),
                    amount_change: Quantity::from(5u32),
                },
            ),
        ];
        for (encoded_base64, rr) in tcs {
            let dec: WithdrawResult =
                cbor::from_slice(&BASE64_STANDARD.decode(encoded_base64).unwrap())
                    .expect("withdraw result should deserialize correctly");
            assert_eq!(dec, rr, "decoded result should match the expected value");
        }
    }

    #[test]
    fn test_consistent_add_escrow_results() {
        let addr1 = Address::from_pk(&PublicKey::from(
            "aaafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        ));
        let addr2 = Address::from_pk(&PublicKey::from(
            "bbbfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        ));

        let tcs = vec![
            ("pGVvd25lclUAAAAAAAAAAAAAAAAAAAAAAAAAAABmYW1vdW50QGZlc2Nyb3dVAAAAAAAAAAAAAAAAAAAAAAAAAAAAam5ld19zaGFyZXNA", AddEscrowResult::default()),
            (
                "pGVvd25lclUAIHIUNIk/YWwJgUjiz5+Z4+KCbhNmYW1vdW50QWRmZXNjcm93VQC5Ejl4leZHDFH6vsyd3IP3Cu2GoWpuZXdfc2hhcmVzQQU=",
                AddEscrowResult {
                    owner: addr1,
                    escrow: addr2,
                    amount: Quantity::from(100u32),
                    new_shares: Quantity::from(5u32),
                },
            ),
        ];
        for (encoded_base64, rr) in tcs {
            let dec: AddEscrowResult =
                cbor::from_slice(&BASE64_STANDARD.decode(encoded_base64).unwrap())
                    .expect("add escrow result should deserialize correctly");
            assert_eq!(dec, rr, "decoded result should match the expected value");
        }
    }

    #[test]
    fn test_consistent_reclaim_escrow_results() {
        let addr1 = Address::from_pk(&PublicKey::from(
            "aaafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        ));
        let addr2 = Address::from_pk(&PublicKey::from(
            "bbbfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        ));

        let tcs = vec![
            ("pmVvd25lclUAAAAAAAAAAAAAAAAAAAAAAAAAAABmYW1vdW50QGZlc2Nyb3dVAAAAAAAAAAAAAAAAAAAAAAAAAAAAb2RlYm9uZF9lbmRfdGltZQBwZGVib25kaW5nX3NoYXJlc0BwcmVtYWluaW5nX3NoYXJlc0A=", ReclaimEscrowResult::default()),
            (
                "pmVvd25lclUAIHIUNIk/YWwJgUjiz5+Z4+KCbhNmYW1vdW50QWRmZXNjcm93VQC5Ejl4leZHDFH6vsyd3IP3Cu2GoW9kZWJvbmRfZW5kX3RpbWUYKnBkZWJvbmRpbmdfc2hhcmVzQRlwcmVtYWluaW5nX3NoYXJlc0Ey",
                ReclaimEscrowResult {
                    owner: addr1,
                    escrow: addr2,
                    amount: Quantity::from(100u32),
                    remaining_shares: Quantity::from(50u32),
                    debonding_shares: Quantity::from(25u32),
                    debond_end_time: 42,
                },
            ),
        ];
        for (encoded_base64, rr) in tcs {
            let dec: ReclaimEscrowResult =
                cbor::from_slice(&BASE64_STANDARD.decode(encoded_base64).unwrap())
                    .expect("reclaim escrow result should deserialize correctly");
            assert_eq!(dec, rr, "decoded result should match the expected value");
        }
    }

    #[test]
    fn test_consistent_events() {
        let addr1 = Address::from_pk(&PublicKey::from(
            "aaafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        ));
        let addr2 = Address::from_pk(&PublicKey::from(
            "bbbfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        ));
        let tx_hash = Hash::empty_hash();

        let tcs = vec![
            (
                "oWd0eF9oYXNoWCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==",
                Event::default(),
            ),
            (
                "omZoZWlnaHQYKmd0eF9oYXNoWCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==",
                Event {
                    height: 42,
                    ..Default::default()
                },
            ),
            (
                "omZoZWlnaHQYKmd0eF9oYXNoWCDGcrjR71btKKuHw2IsURQGm90617j5c3SY0MAezvCWeg==",
                Event {
                    height: 42,
                    tx_hash,
                    ..Default::default()
                },
            ),

            // Transfer.
            (
                "o2ZoZWlnaHQYKmd0eF9oYXNoWCDGcrjR71btKKuHw2IsURQGm90617j5c3SY0MAezvCWemh0cmFuc2ZlcqNidG9VALkSOXiV5kcMUfq+zJ3cg/cK7YahZGZyb21VACByFDSJP2FsCYFI4s+fmePigm4TZmFtb3VudEFk",
                Event {
                    height: 42,
                    tx_hash,
                    transfer: Some(TransferEvent {
                        from: addr1.clone(),
                        to: addr2.clone(),
                        amount: 100u32.into(),
                    }),
                    ..Default::default()
                },
            ),

            // Burn.
            (
                "o2RidXJuomVvd25lclUAIHIUNIk/YWwJgUjiz5+Z4+KCbhNmYW1vdW50QWRmaGVpZ2h0GCpndHhfaGFzaFggxnK40e9W7Sirh8NiLFEUBpvdOte4+XN0mNDAHs7wlno=",
                Event {
                    height: 42,
                    tx_hash,
                    burn: Some(BurnEvent {
                        owner: addr1.clone(),
                        amount: 100u32.into(),
                    }),
                    ..Default::default()
                },
            ),

            // Escrow.
            (
                "o2Zlc2Nyb3ehY2FkZKRlb3duZXJVACByFDSJP2FsCYFI4s+fmePigm4TZmFtb3VudEFkZmVzY3Jvd1UAuRI5eJXmRwxR+r7MndyD9wrthqFqbmV3X3NoYXJlc0EyZmhlaWdodBgqZ3R4X2hhc2hYIMZyuNHvVu0oq4fDYixRFAab3TrXuPlzdJjQwB7O8JZ6",
                Event {
                    height: 42,
                    tx_hash,
                    escrow: Some(EscrowEvent::Add {
                        owner: addr1.clone(),
                        escrow: addr2.clone(),
                        amount: 100u32.into(),
                        new_shares: 50u32.into(),
                    }),
                    ..Default::default()
                },
            ),
            (
                "o2Zlc2Nyb3ehZHRha2WjZW93bmVyVQAgchQ0iT9hbAmBSOLPn5nj4oJuE2ZhbW91bnRBZHBkZWJvbmRpbmdfYW1vdW50QRRmaGVpZ2h0GCpndHhfaGFzaFggxnK40e9W7Sirh8NiLFEUBpvdOte4+XN0mNDAHs7wlno=",
                Event {
                    height: 42,
                    tx_hash,
                    escrow: Some(EscrowEvent::Take {
                        owner: addr1.clone(),
                        amount: 100u32.into(),
                        debonding_amount: 20u32.into()
                    }),
                    ..Default::default()
                },
            ),
            (
                "o2Zlc2Nyb3ehb2RlYm9uZGluZ19zdGFydKZlb3duZXJVACByFDSJP2FsCYFI4s+fmePigm4TZmFtb3VudEFkZmVzY3Jvd1UAuRI5eJXmRwxR+r7MndyD9wrthqFtYWN0aXZlX3NoYXJlc0Eyb2RlYm9uZF9lbmRfdGltZRgqcGRlYm9uZGluZ19zaGFyZXNBGWZoZWlnaHQYKmd0eF9oYXNoWCDGcrjR71btKKuHw2IsURQGm90617j5c3SY0MAezvCWeg==",
                Event {
                    height: 42,
                    tx_hash,
                    escrow: Some(EscrowEvent::DebondingStart {
                        owner: addr1.clone(),
                        escrow: addr2.clone(),
                        amount: 100u32.into(),
                        active_shares: 50u32.into(),
                        debonding_shares: 25u32.into(),
                        debond_end_time: 42,
                    }),
                    ..Default::default()
                },
            ),
            (
                "o2Zlc2Nyb3ehZ3JlY2xhaW2kZW93bmVyVQAgchQ0iT9hbAmBSOLPn5nj4oJuE2ZhbW91bnRBZGZlc2Nyb3dVALkSOXiV5kcMUfq+zJ3cg/cK7YahZnNoYXJlc0EZZmhlaWdodBgqZ3R4X2hhc2hYIMZyuNHvVu0oq4fDYixRFAab3TrXuPlzdJjQwB7O8JZ6",
                Event {
                    height: 42,
                    tx_hash,
                    escrow: Some(EscrowEvent::Reclaim {
                        owner: addr1.clone(),
                        escrow: addr2.clone(),
                        amount: 100u32.into(),
                        shares: 25u32.into(),
                    }),
                    ..Default::default()
                },
            ),

            // Allowance change.
            (
                "o2ZoZWlnaHQYKmd0eF9oYXNoWCDGcrjR71btKKuHw2IsURQGm90617j5c3SY0MAezvCWenBhbGxvd2FuY2VfY2hhbmdlpGVvd25lclUAIHIUNIk/YWwJgUjiz5+Z4+KCbhNpYWxsb3dhbmNlQWRrYmVuZWZpY2lhcnlVALkSOXiV5kcMUfq+zJ3cg/cK7YahbWFtb3VudF9jaGFuZ2VBMg==",
                Event {
                    height: 42,
                    tx_hash,
                    allowance_change: Some(AllowanceChangeEvent {
                        owner: addr1.clone(),
                        beneficiary: addr2.clone(),
                        allowance: 100u32.into(),
                        negative: false,
                        amount_change: 50u32.into(),
                    }),
                    ..Default::default()
                },
            ),
            (
                "o2ZoZWlnaHQYKmd0eF9oYXNoWCDGcrjR71btKKuHw2IsURQGm90617j5c3SY0MAezvCWenBhbGxvd2FuY2VfY2hhbmdlpWVvd25lclUAIHIUNIk/YWwJgUjiz5+Z4+KCbhNobmVnYXRpdmX1aWFsbG93YW5jZUFka2JlbmVmaWNpYXJ5VQC5Ejl4leZHDFH6vsyd3IP3Cu2GoW1hbW91bnRfY2hhbmdlQTI=",
                Event {
                    height: 42,
                    tx_hash,
                    allowance_change: Some(AllowanceChangeEvent {
                        owner: addr1.clone(),
                        beneficiary: addr2.clone(),
                        allowance: 100u32.into(),
                        negative: true,
                        amount_change: 50u32.into(),
                    }),
                    ..Default::default()
                },
            ),
        ];
        for (encoded_base64, ev) in tcs {
            let dec: Event = cbor::from_slice(&BASE64_STANDARD.decode(encoded_base64).unwrap())
                .expect("event should deserialize correctly");
            assert_eq!(dec, ev, "decoded event should match the expected value");
        }
    }
}