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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
//! Accounts module.
use std::{
    cmp::Ordering,
    collections::{BTreeMap, BTreeSet},
    convert::TryInto,
};

use num_traits::Zero;
use once_cell::sync::Lazy;
use thiserror::Error;

use crate::{
    context::Context,
    core::common::quantity::Quantity,
    handler, migration, module,
    module::{Module as _, Parameters as _},
    modules,
    modules::core::{Error as CoreError, API as _},
    runtime::Runtime,
    sdk_derive,
    sender::SenderMeta,
    state::CurrentState,
    storage,
    storage::Prefix,
    types::{
        address::{Address, SignatureAddressSpec},
        token,
        transaction::{AuthInfo, Transaction},
    },
};

pub mod fee;
#[cfg(test)]
pub(crate) mod test;
pub mod types;

/// Unique module name.
const MODULE_NAME: &str = "accounts";

/// Maximum delta that the transaction nonce can be in the future from the current nonce to still
/// be accepted during transaction checks.
const MAX_CHECK_NONCE_FUTURE_DELTA: u64 = 0; // Increase once supported in Oasis Core.

/// Errors emitted by the accounts module.
#[derive(Error, Debug, oasis_runtime_sdk_macros::Error)]
pub enum Error {
    #[error("invalid argument")]
    #[sdk_error(code = 1)]
    InvalidArgument,

    #[error("insufficient balance")]
    #[sdk_error(code = 2)]
    InsufficientBalance,

    #[error("forbidden by policy")]
    #[sdk_error(code = 3)]
    Forbidden,

    #[error("not found")]
    #[sdk_error(code = 4)]
    NotFound,

    #[error("core: {0}")]
    #[sdk_error(transparent)]
    Core(#[from] modules::core::Error),
}

/// Events emitted by the accounts module.
#[derive(Debug, cbor::Encode, oasis_runtime_sdk_macros::Event)]
#[cbor(untagged)]
pub enum Event {
    #[sdk_event(code = 1)]
    Transfer {
        from: Address,
        to: Address,
        amount: token::BaseUnits,
    },

    #[sdk_event(code = 2)]
    Burn {
        owner: Address,
        amount: token::BaseUnits,
    },

    #[sdk_event(code = 3)]
    Mint {
        owner: Address,
        amount: token::BaseUnits,
    },
}

/// Gas costs.
#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
pub struct GasCosts {
    pub tx_transfer: u64,
}

/// Parameters for the accounts module.
#[derive(Clone, Default, Debug, cbor::Encode, cbor::Decode)]
pub struct Parameters {
    pub transfers_disabled: bool,
    pub gas_costs: GasCosts,

    #[cbor(optional)]
    pub debug_disable_nonce_check: bool,

    #[cbor(optional)]
    pub denomination_infos: BTreeMap<token::Denomination, types::DenominationInfo>,
}

/// Errors emitted during rewards parameter validation.
#[derive(Error, Debug)]
pub enum ParameterValidationError {
    #[error("debug option used: {0}")]
    DebugOptionUsed(String),
}

impl module::Parameters for Parameters {
    type Error = ParameterValidationError;

    #[cfg(not(feature = "unsafe-allow-debug"))]
    fn validate_basic(&self) -> Result<(), Self::Error> {
        if self.debug_disable_nonce_check {
            return Err(ParameterValidationError::DebugOptionUsed(
                "debug_disable_nonce_check".to_string(),
            ));
        }

        Ok(())
    }
}

/// Genesis state for the accounts module.
#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
pub struct Genesis {
    pub parameters: Parameters,
    pub accounts: BTreeMap<Address, types::Account>,
    pub balances: BTreeMap<Address, BTreeMap<token::Denomination, u128>>,
    pub total_supplies: BTreeMap<token::Denomination, u128>,
}

/// Interface that can be called from other modules.
pub trait API {
    /// Transfer an amount from one account to the other.
    fn transfer(from: Address, to: Address, amount: &token::BaseUnits) -> Result<(), Error>;

    /// Transfer an amount from one account to the other without emitting an event.
    fn transfer_silent(from: Address, to: Address, amount: &token::BaseUnits) -> Result<(), Error>;

    /// Mint new tokens, increasing the total supply.
    fn mint(to: Address, amount: &token::BaseUnits) -> Result<(), Error>;

    /// Burn existing tokens, decreasing the total supply.
    fn burn(from: Address, amount: &token::BaseUnits) -> Result<(), Error>;

    /// Sets an account's nonce.
    fn set_nonce(address: Address, nonce: u64);

    /// Fetch an account's current nonce.
    fn get_nonce(address: Address) -> Result<u64, Error>;

    /// Increments an account's nonce.
    fn inc_nonce(address: Address);

    /// Sets an account's balance of the given denomination.
    ///
    /// # Warning
    ///
    /// This method is dangerous as it can result in invariant violations.
    fn set_balance(address: Address, amount: &token::BaseUnits);

    /// Fetch an account's balance of the given denomination.
    fn get_balance(address: Address, denomination: token::Denomination) -> Result<u128, Error>;

    /// Ensures that the given account has at least the specified balance.
    fn ensure_balance(address: Address, amount: &token::BaseUnits) -> Result<(), Error> {
        let balance = Self::get_balance(address, amount.denomination().clone())?;
        if balance < amount.amount() {
            Err(Error::InsufficientBalance)
        } else {
            Ok(())
        }
    }

    /// Fetch an account's current balances.
    fn get_balances(address: Address) -> Result<types::AccountBalances, Error>;

    /// Fetch addresses.
    fn get_addresses(denomination: token::Denomination) -> Result<Vec<Address>, Error>;

    /// Fetch total supplies.
    fn get_total_supplies() -> Result<BTreeMap<token::Denomination, u128>, Error>;

    /// Sets the total supply for the given denomination.
    ///
    /// # Warning
    ///
    /// This method is dangerous as it can result in invariant violations.
    fn set_total_supply(amount: &token::BaseUnits);

    /// Fetch information about a denomination.
    fn get_denomination_info(
        denomination: &token::Denomination,
    ) -> Result<types::DenominationInfo, Error>;

    /// Moves the amount into the per-transaction fee accumulator.
    fn charge_tx_fee(from: Address, amount: &token::BaseUnits) -> Result<(), modules::core::Error>;

    /// Indicates that the unused portion of the transaction fee should be refunded after the
    /// transaction completes (even in case it fails).
    fn set_refund_unused_tx_fee(refund: bool);

    /// Take the flag indicating that the unused portion of the transaction fee should be refunded
    /// after the transaction completes is set.
    ///
    /// After calling this method the flag is reset to `false`.
    fn take_refund_unused_tx_fee() -> bool;

    /// Check transaction signer account nonces.
    /// Return payer address.
    fn check_signer_nonces<C: Context>(
        ctx: &C,
        tx_auth_info: &AuthInfo,
    ) -> Result<Address, modules::core::Error>;

    /// Update transaction signer account nonces.
    fn update_signer_nonces<C: Context>(
        ctx: &C,
        tx_auth_info: &AuthInfo,
    ) -> Result<(), modules::core::Error>;
}

/// State schema constants.
pub mod state {
    /// Map of account addresses to account metadata.
    pub const ACCOUNTS: &[u8] = &[0x01];
    /// Map of account addresses to map of denominations to balances.
    pub const BALANCES: &[u8] = &[0x02];
    /// Map of total supplies (per denomination).
    pub const TOTAL_SUPPLY: &[u8] = &[0x03];
}

pub struct Module;

/// Module's address that has the common pool.
///
/// oasis1qz78phkdan64g040cvqvqpwkplfqf6tj6uwcsh30
pub static ADDRESS_COMMON_POOL: Lazy<Address> =
    Lazy::new(|| Address::from_module(MODULE_NAME, "common-pool"));
/// Module's address that has the fee accumulator.
///
/// oasis1qp3r8hgsnphajmfzfuaa8fhjag7e0yt35cjxq0u4
pub static ADDRESS_FEE_ACCUMULATOR: Lazy<Address> =
    Lazy::new(|| Address::from_module(MODULE_NAME, "fee-accumulator"));

/// This is needed to properly iterate over the BALANCES map.
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord)]
struct AddressWithDenomination(Address, token::Denomination);

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

    #[error("malformed denomination")]
    MalformedDenomination,
}

impl std::convert::TryFrom<&[u8]> for AddressWithDenomination {
    type Error = AWDError;

    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        let address =
            Address::try_from(&bytes[..Address::SIZE]).map_err(|_| AWDError::MalformedAddress)?;
        let denomination = token::Denomination::try_from(&bytes[Address::SIZE..])
            .map_err(|_| AWDError::MalformedDenomination)?;
        Ok(AddressWithDenomination(address, denomination))
    }
}

impl Module {
    /// Add given amount of tokens to the specified account's balance.
    fn add_amount(addr: Address, amount: &token::BaseUnits) -> Result<(), Error> {
        if amount.amount() == 0 {
            return Ok(());
        }

        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let balances = storage::PrefixStore::new(store, &state::BALANCES);
            let mut account = storage::TypedStore::new(storage::PrefixStore::new(balances, &addr));
            let mut value: u128 = account.get(amount.denomination()).unwrap_or_default();

            value = value
                .checked_add(amount.amount())
                .ok_or(Error::InvalidArgument)?;
            account.insert(amount.denomination(), value);
            Ok(())
        })
    }

    /// Subtract given amount of tokens from the specified account's balance.
    fn sub_amount(addr: Address, amount: &token::BaseUnits) -> Result<(), Error> {
        if amount.amount() == 0 {
            return Ok(());
        }

        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let balances = storage::PrefixStore::new(store, &state::BALANCES);
            let mut account = storage::TypedStore::new(storage::PrefixStore::new(balances, &addr));
            let mut value: u128 = account.get(amount.denomination()).unwrap_or_default();

            value = value
                .checked_sub(amount.amount())
                .ok_or(Error::InsufficientBalance)?;
            account.insert(amount.denomination(), value);
            Ok(())
        })
    }

    /// Increment the total supply for the given amount.
    fn inc_total_supply(amount: &token::BaseUnits) -> Result<(), Error> {
        if amount.amount() == 0 {
            return Ok(());
        }

        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let mut total_supplies =
                storage::TypedStore::new(storage::PrefixStore::new(store, &state::TOTAL_SUPPLY));
            let mut total_supply: u128 = total_supplies
                .get(amount.denomination())
                .unwrap_or_default();

            total_supply = total_supply
                .checked_add(amount.amount())
                .ok_or(Error::InvalidArgument)?;
            total_supplies.insert(amount.denomination(), total_supply);
            Ok(())
        })
    }

    /// Decrement the total supply for the given amount.
    fn dec_total_supply(amount: &token::BaseUnits) -> Result<(), Error> {
        if amount.amount() == 0 {
            return Ok(());
        }

        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let mut total_supplies =
                storage::TypedStore::new(storage::PrefixStore::new(store, &state::TOTAL_SUPPLY));
            let mut total_supply: u128 = total_supplies
                .get(amount.denomination())
                .unwrap_or_default();

            total_supply = total_supply
                .checked_sub(amount.amount())
                .ok_or(Error::InsufficientBalance)?;
            total_supplies.insert(amount.denomination(), total_supply);
            Ok(())
        })
    }

    /// Get all balances.
    fn get_all_balances() -> Result<BTreeMap<Address, BTreeMap<token::Denomination, u128>>, Error> {
        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let balances =
                storage::TypedStore::new(storage::PrefixStore::new(store, &state::BALANCES));

            // Unfortunately, we can't just return balances.iter().collect() here,
            // because the stored format doesn't match -- we need this workaround
            // instead.

            let balmap: BTreeMap<AddressWithDenomination, u128> = balances.iter().collect();

            let mut b: BTreeMap<Address, BTreeMap<token::Denomination, u128>> = BTreeMap::new();

            for (addrden, amt) in &balmap {
                let addr = &addrden.0;
                let den = &addrden.1;

                // Fetch existing account's balances or insert blank ones.
                let addr_bals = b.entry(*addr).or_default();

                // Add to given denomination's balance or insert it if new.
                addr_bals
                    .entry(den.clone())
                    .and_modify(|a| *a += amt)
                    .or_insert_with(|| *amt);
            }

            Ok(b)
        })
    }
}

/// Context key for the per-transaction unused fee refund decision.
const CONTEXT_KEY_TX_FEE_REFUND_UNUSED: &str = "accounts.TxRefundUnusedFee";
/// Context key for the per block fee manager.
const CONTEXT_KEY_FEE_MANAGER: &str = "accounts.FeeManager";

impl API for Module {
    fn transfer(from: Address, to: Address, amount: &token::BaseUnits) -> Result<(), Error> {
        if CurrentState::with_env(|env| env.is_check_only()) || amount.amount() == 0 {
            return Ok(());
        }

        Self::transfer_silent(from, to, amount)?;

        // Emit a transfer event.
        CurrentState::with(|state| {
            state.emit_event(Event::Transfer {
                from,
                to,
                amount: amount.clone(),
            })
        });

        Ok(())
    }

    fn transfer_silent(from: Address, to: Address, amount: &token::BaseUnits) -> Result<(), Error> {
        // Subtract from source account.
        Self::sub_amount(from, amount)?;
        // Add to destination account.
        Self::add_amount(to, amount)?;

        Ok(())
    }

    fn mint(to: Address, amount: &token::BaseUnits) -> Result<(), Error> {
        if CurrentState::with_env(|env| env.is_check_only()) || amount.amount() == 0 {
            return Ok(());
        }

        // Add to destination account.
        Self::add_amount(to, amount)?;

        // Increase total supply.
        Self::inc_total_supply(amount)?;

        // Emit a mint event.
        CurrentState::with(|state| {
            state.emit_event(Event::Mint {
                owner: to,
                amount: amount.clone(),
            });
        });

        Ok(())
    }

    fn burn(from: Address, amount: &token::BaseUnits) -> Result<(), Error> {
        if CurrentState::with_env(|env| env.is_check_only()) || amount.amount() == 0 {
            return Ok(());
        }

        // Remove from target account.
        Self::sub_amount(from, amount)?;

        // Decrease total supply.
        Self::dec_total_supply(amount)
            .expect("target account had enough balance so total supply should not underflow");

        // Emit a burn event.
        CurrentState::with(|state| {
            state.emit_event(Event::Burn {
                owner: from,
                amount: amount.clone(),
            });
        });

        Ok(())
    }

    fn set_nonce(address: Address, nonce: u64) {
        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let mut accounts =
                storage::TypedStore::new(storage::PrefixStore::new(store, &state::ACCOUNTS));
            let mut account: types::Account = accounts.get(address).unwrap_or_default();
            account.nonce = nonce;
            accounts.insert(address, account);
        })
    }

    fn get_nonce(address: Address) -> Result<u64, Error> {
        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let accounts =
                storage::TypedStore::new(storage::PrefixStore::new(store, &state::ACCOUNTS));
            let account: types::Account = accounts.get(address).unwrap_or_default();
            Ok(account.nonce)
        })
    }

    fn inc_nonce(address: Address) {
        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let mut accounts =
                storage::TypedStore::new(storage::PrefixStore::new(store, &state::ACCOUNTS));
            let mut account: types::Account = accounts.get(address).unwrap_or_default();
            account.nonce = account.nonce.saturating_add(1);
            accounts.insert(address, account);
        })
    }

    fn set_balance(address: Address, amount: &token::BaseUnits) {
        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let balances = storage::PrefixStore::new(store, &state::BALANCES);
            let mut account =
                storage::TypedStore::new(storage::PrefixStore::new(balances, &address));
            account.insert(amount.denomination(), amount.amount());
        });
    }

    fn get_balance(address: Address, denomination: token::Denomination) -> Result<u128, Error> {
        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let balances = storage::PrefixStore::new(store, &state::BALANCES);
            let account = storage::TypedStore::new(storage::PrefixStore::new(balances, &address));

            Ok(account.get(denomination).unwrap_or_default())
        })
    }

    fn get_balances(address: Address) -> Result<types::AccountBalances, Error> {
        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let balances = storage::PrefixStore::new(store, &state::BALANCES);
            let account = storage::TypedStore::new(storage::PrefixStore::new(balances, &address));

            Ok(types::AccountBalances {
                balances: account.iter().collect(),
            })
        })
    }

    fn get_addresses(denomination: token::Denomination) -> Result<Vec<Address>, Error> {
        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let balances: BTreeMap<AddressWithDenomination, Quantity> =
                storage::TypedStore::new(storage::PrefixStore::new(store, &state::BALANCES))
                    .iter()
                    .collect();

            Ok(balances
                .into_keys()
                .filter(|bal| bal.1 == denomination)
                .map(|bal| bal.0)
                .collect())
        })
    }

    fn get_total_supplies() -> Result<BTreeMap<token::Denomination, u128>, Error> {
        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let ts =
                storage::TypedStore::new(storage::PrefixStore::new(store, &state::TOTAL_SUPPLY));

            Ok(ts.iter().collect())
        })
    }

    fn set_total_supply(amount: &token::BaseUnits) {
        CurrentState::with_store(|store| {
            let store = storage::PrefixStore::new(store, &MODULE_NAME);
            let mut total_supplies =
                storage::TypedStore::new(storage::PrefixStore::new(store, &state::TOTAL_SUPPLY));
            total_supplies.insert(amount.denomination(), amount.amount());
        });
    }

    fn get_denomination_info(
        denomination: &token::Denomination,
    ) -> Result<types::DenominationInfo, Error> {
        Self::params()
            .denomination_infos
            .get(denomination)
            .cloned()
            .ok_or(Error::NotFound)
    }

    fn charge_tx_fee(from: Address, amount: &token::BaseUnits) -> Result<(), modules::core::Error> {
        if CurrentState::with_env(|env| env.is_simulation()) {
            return Ok(());
        }

        Self::sub_amount(from, amount).map_err(|_| modules::core::Error::InsufficientFeeBalance)?;

        CurrentState::with(|state| {
            state
                .block_value::<fee::FeeManager>(CONTEXT_KEY_FEE_MANAGER)
                .or_default()
                .record_fee(from, amount);
        });

        Ok(())
    }

    fn set_refund_unused_tx_fee(refund: bool) {
        CurrentState::with(|state| {
            if state.env().is_simulation() {
                return;
            }

            state
                .block_value(CONTEXT_KEY_TX_FEE_REFUND_UNUSED)
                .set(refund);
        });
    }

    fn take_refund_unused_tx_fee() -> bool {
        CurrentState::with(|state| {
            if state.env().is_simulation() {
                return false;
            }

            state
                .block_value(CONTEXT_KEY_TX_FEE_REFUND_UNUSED)
                .take()
                .unwrap_or(false)
        })
    }

    fn check_signer_nonces<C: Context>(
        _ctx: &C,
        auth_info: &AuthInfo,
    ) -> Result<Address, modules::core::Error> {
        let is_pre_schedule = CurrentState::with_env(|env| env.is_pre_schedule());
        let is_check_only = CurrentState::with_env(|env| env.is_check_only());

        // TODO: Optimize the check/update pair so that the accounts are
        // fetched only once.
        let params = Self::params();
        let sender = CurrentState::with_store(|store| {
            // Fetch information about each signer.
            let mut store = storage::PrefixStore::new(store, &MODULE_NAME);
            let accounts =
                storage::TypedStore::new(storage::PrefixStore::new(&mut store, &state::ACCOUNTS));
            let mut sender = None;
            for si in auth_info.signer_info.iter() {
                let address = si.address_spec.address();
                let account: types::Account = accounts.get(address).unwrap_or_default();

                // First signer pays for the fees and is considered the sender.
                if sender.is_none() {
                    sender = Some(SenderMeta {
                        address,
                        tx_nonce: si.nonce,
                        state_nonce: account.nonce,
                    });
                }

                // When nonce checking is disabled, skip the rest of the checks.
                if params.debug_disable_nonce_check {
                    continue;
                }

                // Check signer nonce against the corresponding account nonce.
                match si.nonce.cmp(&account.nonce) {
                    Ordering::Less => {
                        // In the past and will never become valid, reject.
                        return Err(modules::core::Error::InvalidNonce);
                    }
                    Ordering::Equal => {} // Ok.
                    Ordering::Greater => {
                        // If too much in the future, reject.
                        if si.nonce - account.nonce > MAX_CHECK_NONCE_FUTURE_DELTA {
                            return Err(modules::core::Error::InvalidNonce);
                        }

                        // If in the future and this is before scheduling, reject with separate error
                        // that will make the scheduler skip the transaction.
                        if is_pre_schedule {
                            return Err(modules::core::Error::FutureNonce);
                        }

                        // If in the future and this is during execution, reject.
                        if !is_check_only {
                            return Err(modules::core::Error::InvalidNonce);
                        }

                        // If in the future and this is during checks, accept.
                    }
                }
            }

            Ok(sender)
        })?;

        // Configure the sender.
        let sender = sender.expect("at least one signer is always present");
        let sender_address = sender.address;
        if is_check_only {
            <C::Runtime as Runtime>::Core::set_sender_meta(sender);
        }

        Ok(sender_address)
    }

    fn update_signer_nonces<C: Context>(
        _ctx: &C,
        auth_info: &AuthInfo,
    ) -> Result<(), modules::core::Error> {
        CurrentState::with_store(|store| {
            // Fetch information about each signer.
            let mut store = storage::PrefixStore::new(store, &MODULE_NAME);
            let mut accounts =
                storage::TypedStore::new(storage::PrefixStore::new(&mut store, &state::ACCOUNTS));
            for si in auth_info.signer_info.iter() {
                let address = si.address_spec.address();
                let mut account: types::Account = accounts.get(address).unwrap_or_default();

                // Update nonce.
                account.nonce = account
                    .nonce
                    .checked_add(1)
                    .ok_or(modules::core::Error::InvalidNonce)?; // Should never overflow.
                accounts.insert(address, account);
            }
            Ok(())
        })
    }
}

#[sdk_derive(Module)]
impl Module {
    const NAME: &'static str = MODULE_NAME;
    type Error = Error;
    type Event = Event;
    type Parameters = Parameters;
    type Genesis = Genesis;

    #[migration(init)]
    pub fn init(genesis: Genesis) {
        CurrentState::with_store(|store| {
            // Create accounts.
            let mut store = storage::PrefixStore::new(store, &MODULE_NAME);
            let mut accounts =
                storage::TypedStore::new(storage::PrefixStore::new(&mut store, &state::ACCOUNTS));
            for (address, account) in genesis.accounts {
                accounts.insert(address, account);
            }

            // Create balances.
            let mut balances = storage::PrefixStore::new(&mut store, &state::BALANCES);
            let mut computed_total_supply: BTreeMap<token::Denomination, u128> = BTreeMap::new();
            for (address, denominations) in genesis.balances.iter() {
                let mut account =
                    storage::TypedStore::new(storage::PrefixStore::new(&mut balances, &address));
                for (denomination, value) in denominations {
                    account.insert(denomination, value);

                    // Update computed total supply.
                    computed_total_supply
                        .entry(denomination.clone())
                        .and_modify(|v| *v += value)
                        .or_insert_with(|| *value);
                }
            }

            // Validate and set total supply.
            let mut total_supplies = storage::TypedStore::new(storage::PrefixStore::new(
                &mut store,
                &state::TOTAL_SUPPLY,
            ));
            for (denomination, total_supply) in genesis.total_supplies.iter() {
                let computed = computed_total_supply
                    .remove(denomination)
                    .expect("unexpected total supply");
                assert!(
                    &computed == total_supply,
                    "unexpected total supply (expected: {total_supply} got: {computed})",
                );

                total_supplies.insert(denomination, total_supply);
            }
            if let Some((denomination, total_supply)) = computed_total_supply.iter().next() {
                panic!("missing expected total supply: {total_supply} {denomination}",);
            }
        });

        // Validate genesis parameters.
        genesis
            .parameters
            .validate_basic()
            .expect("invalid genesis parameters");

        // Set genesis parameters.
        Self::set_params(genesis.parameters);
    }

    #[handler(prefetch = "accounts.Transfer")]
    fn prefetch_transfer(
        add_prefix: &mut dyn FnMut(Prefix),
        body: cbor::Value,
        auth_info: &AuthInfo,
    ) -> Result<(), crate::error::RuntimeError> {
        let args: types::Transfer = cbor::from_value(body).map_err(|_| Error::InvalidArgument)?;
        let from = auth_info.signer_info[0].address_spec.address();

        // Prefetch accounts 'to'.
        add_prefix(Prefix::from(
            [MODULE_NAME.as_bytes(), state::ACCOUNTS, args.to.as_ref()].concat(),
        ));
        add_prefix(Prefix::from(
            [MODULE_NAME.as_bytes(), state::BALANCES, args.to.as_ref()].concat(),
        ));
        // Prefetch accounts 'from'.
        add_prefix(Prefix::from(
            [MODULE_NAME.as_bytes(), state::ACCOUNTS, from.as_ref()].concat(),
        ));
        add_prefix(Prefix::from(
            [MODULE_NAME.as_bytes(), state::BALANCES, from.as_ref()].concat(),
        ));

        Ok(())
    }

    #[handler(call = "accounts.Transfer")]
    fn tx_transfer<C: Context>(_ctx: &C, body: types::Transfer) -> Result<(), Error> {
        let params = Self::params();

        // Reject transfers when they are disabled.
        if params.transfers_disabled {
            return Err(Error::Forbidden);
        }

        <C::Runtime as Runtime>::Core::use_tx_gas(params.gas_costs.tx_transfer)?;

        let tx_caller_address = CurrentState::with_env(|env| env.tx_caller_address());
        Self::transfer(tx_caller_address, body.to, &body.amount)?;

        Ok(())
    }

    #[handler(query = "accounts.Nonce")]
    fn query_nonce<C: Context>(_ctx: &C, args: types::NonceQuery) -> Result<u64, Error> {
        Self::get_nonce(args.address)
    }

    #[handler(query = "accounts.Addresses", expensive)]
    fn query_addresses<C: Context>(
        _ctx: &C,
        args: types::AddressesQuery,
    ) -> Result<Vec<Address>, Error> {
        Self::get_addresses(args.denomination)
    }

    #[handler(query = "accounts.Balances")]
    fn query_balances<C: Context>(
        _ctx: &C,
        args: types::BalancesQuery,
    ) -> Result<types::AccountBalances, Error> {
        Self::get_balances(args.address)
    }

    #[handler(query = "accounts.DenominationInfo")]
    fn query_denomination_info<C: Context>(
        _ctx: &C,
        args: types::DenominationInfoQuery,
    ) -> Result<types::DenominationInfo, Error> {
        Self::get_denomination_info(&args.denomination)
    }
}

impl module::TransactionHandler for Module {
    fn authenticate_tx<C: Context>(ctx: &C, tx: &Transaction) -> Result<(), modules::core::Error> {
        // Check whether the transaction is currently valid.
        let round = ctx.runtime_header().round;
        if let Some(not_before) = tx.auth_info.not_before {
            if round < not_before {
                // Too early.
                return Err(modules::core::Error::ExpiredTransaction);
            }
        }
        if let Some(not_after) = tx.auth_info.not_after {
            if round > not_after {
                // Too late.
                return Err(modules::core::Error::ExpiredTransaction);
            }
        }

        // Check nonces.
        let payer = Self::check_signer_nonces(ctx, &tx.auth_info)?;

        // Charge the specified amount of fees.
        if !tx.auth_info.fee.amount.amount().is_zero() {
            if CurrentState::with_env(|env| env.is_check_only()) {
                // Do not update balances during transaction checks. In case of checks, only do it
                // after all the other checks have already passed as otherwise retrying the
                // transaction will not be possible.
                Self::ensure_balance(payer, &tx.auth_info.fee.amount)
                    .map_err(|_| modules::core::Error::InsufficientFeeBalance)?;
            } else {
                // Actually perform the move.
                Self::charge_tx_fee(payer, &tx.auth_info.fee.amount)?;
            }

            let gas_price = tx.auth_info.fee.gas_price();
            // Set transaction priority.
            <C::Runtime as Runtime>::Core::set_priority(gas_price.try_into().unwrap_or(u64::MAX));
        }

        // Do not update nonces early during transaction checks. In case of checks, only do it after
        // all the other checks have already passed as otherwise retrying the transaction will not
        // be possible.
        if !CurrentState::with_env(|env| env.is_check_only()) {
            Self::update_signer_nonces(ctx, &tx.auth_info)?;
        }

        Ok(())
    }

    fn after_handle_call<C: Context>(
        _ctx: &C,
        result: module::CallResult,
    ) -> Result<module::CallResult, modules::core::Error> {
        // Check whether unused part of the fee should be refunded.
        let refund_fee = if Self::take_refund_unused_tx_fee() {
            let remaining_gas = <C::Runtime as Runtime>::Core::remaining_tx_gas();
            let gas_price = CurrentState::with_env(|env| env.tx_auth_info().fee.gas_price());

            gas_price.saturating_mul(remaining_gas.into())
        } else {
            0
        };

        CurrentState::with(|state| {
            let mgr = state
                .block_value::<fee::FeeManager>(CONTEXT_KEY_FEE_MANAGER)
                .or_default();

            // Update the per-tx fee accumulator. State must be updated in `after_dispatch_tx` as
            // otherwise any state updates may be reverted in case call result is a failure.
            mgr.record_refund(refund_fee);

            // Emit event for paid fee.
            let tx_fee = mgr.tx_fee().cloned().unwrap_or_default();
            if tx_fee.amount() > 0 {
                state.emit_unconditional_event(Event::Transfer {
                    from: tx_fee.payer(),
                    to: *ADDRESS_FEE_ACCUMULATOR,
                    amount: token::BaseUnits::new(tx_fee.amount(), tx_fee.denomination()),
                });
            }
        });

        Ok(result)
    }

    fn after_dispatch_tx<C: Context>(
        ctx: &C,
        tx_auth_info: &AuthInfo,
        result: &module::CallResult,
    ) {
        // Move transaction fees into the per-block fee accumulator.
        let fee_updates = CurrentState::with(|state| {
            let mgr = state
                .block_value::<fee::FeeManager>(CONTEXT_KEY_FEE_MANAGER)
                .or_default();
            mgr.commit_tx()
        });
        // Refund any fees. This needs to happen after tx dispatch to ensure state is updated.
        Self::add_amount(fee_updates.payer, &fee_updates.refund).unwrap();

        if !CurrentState::with_env(|env| env.is_check_only()) {
            // Do nothing further outside transaction checks.
            return;
        }
        if !matches!(result, module::CallResult::Ok(_)) {
            // Do nothing in case the call failed to allow retries.
            return;
        }

        // Update payer balance.
        let payer = Self::check_signer_nonces(ctx, tx_auth_info).unwrap(); // Already checked.
        let amount = &tx_auth_info.fee.amount;
        Self::sub_amount(payer, amount).unwrap(); // Already checked.

        // Update nonces.
        Self::update_signer_nonces(ctx, tx_auth_info).unwrap();
    }
}

impl module::BlockHandler for Module {
    fn end_block<C: Context>(ctx: &C) {
        // Determine the fees that are available for disbursement from the last block.
        let mut previous_fees = Self::get_balances(*ADDRESS_FEE_ACCUMULATOR)
            .expect("get_balances must succeed")
            .balances;

        // Drain previous fees from the fee accumulator.
        for (denom, remainder) in &previous_fees {
            Self::sub_amount(
                *ADDRESS_FEE_ACCUMULATOR,
                &token::BaseUnits::new(*remainder, denom.clone()),
            )
            .expect("sub_amount must succeed");
        }

        // Disburse transaction fees to entities controlling all the good nodes in the committee.
        let addrs: Vec<Address> = ctx
            .runtime_round_results()
            .good_compute_entities
            .iter()
            .map(|pk| Address::from_sigspec(&SignatureAddressSpec::Ed25519(pk.into())))
            .collect();

        if !addrs.is_empty() {
            let amounts: Vec<_> = previous_fees
                .iter()
                .filter_map(|(denom, fee)| {
                    let fee = fee
                        .checked_div(addrs.len() as u128)
                        .expect("addrs is non-empty");

                    // Filter out zero-fee entries to avoid needless operations.
                    if fee.is_zero() {
                        None
                    } else {
                        Some(token::BaseUnits::new(fee, denom.clone()))
                    }
                })
                .collect();

            for address in addrs {
                for amount in &amounts {
                    let remaining = previous_fees
                        .get_mut(amount.denomination())
                        .expect("designated denomination should be there");
                    *remaining = remaining
                        .checked_sub(amount.amount())
                        .expect("there should be enough to disburse");

                    Self::add_amount(address, amount)
                        .expect("add_amount must succeed for fee disbursement");

                    // Emit transfer event for fee disbursement.
                    CurrentState::with(|state| {
                        state.emit_event(Event::Transfer {
                            from: *ADDRESS_FEE_ACCUMULATOR,
                            to: address,
                            amount: amount.clone(),
                        });
                    });
                }
            }
        }

        // Transfer remainder to a common pool account.
        for (denom, remainder) in previous_fees.into_iter() {
            if remainder.is_zero() {
                continue;
            }

            let amount = token::BaseUnits::new(remainder, denom);
            Self::add_amount(*ADDRESS_COMMON_POOL, &amount)
                .expect("add_amount must succeed for transfer to common pool");

            // Emit transfer event for fee disbursement.
            CurrentState::with(|state| {
                state.emit_event(Event::Transfer {
                    from: *ADDRESS_FEE_ACCUMULATOR,
                    to: *ADDRESS_COMMON_POOL,
                    amount,
                })
            });
        }

        // Fees for the active block should be transferred to the fee accumulator address.
        let block_fees = CurrentState::with(|state| {
            let mgr = state
                .block_value::<fee::FeeManager>(CONTEXT_KEY_FEE_MANAGER)
                .take()
                .unwrap_or_default();
            mgr.commit_block().into_iter()
        });

        for (denom, amount) in block_fees {
            Self::add_amount(
                *ADDRESS_FEE_ACCUMULATOR,
                &token::BaseUnits::new(amount, denom),
            )
            .expect("add_amount must succeed for transfer to fee accumulator")
        }
    }
}

impl module::InvariantHandler for Module {
    /// Check invariants.
    fn check_invariants<C: Context>(_ctx: &C) -> Result<(), CoreError> {
        // All account balances should sum up to the total supply for their
        // corresponding denominations.

        #[allow(clippy::or_fun_call)]
        let balances = Self::get_all_balances().or(Err(CoreError::InvariantViolation(
            "unable to get balances of all accounts".to_string(),
        )))?;
        #[allow(clippy::or_fun_call)]
        let total_supplies = Self::get_total_supplies().or(Err(CoreError::InvariantViolation(
            "unable to get total supplies".to_string(),
        )))?;

        // First, compute total supplies based on account balances.
        let mut computed_ts: BTreeMap<token::Denomination, u128> = BTreeMap::new();

        for bals in balances.values() {
            for (den, amt) in bals {
                computed_ts
                    .entry(den.clone())
                    .and_modify(|a| *a += amt)
                    .or_insert_with(|| *amt);
            }
        }

        // Now check if the computed and given total supplies match.
        for (den, ts) in &total_supplies {
            // Return error if total supplies have a denomination that we
            // didn't encounter when computing total supplies based on account
            // balances.
            #[allow(clippy::or_fun_call)]
            let computed = computed_ts
                .remove(den)
                .ok_or(CoreError::InvariantViolation(
                    "unexpected denomination".to_string(),
                ))?;

            if &computed != ts {
                // Computed and actual total supplies don't match.
                return Err(CoreError::InvariantViolation(format!(
                    "computed and actual total supplies don't match (computed={computed}, actual={ts})",
                )));
            }
        }

        // There should be no remaining denominations in the computed supplies,
        // because that would mean that accounts have denominations that don't
        // appear in the total supplies table, which would obviously be wrong.
        if computed_ts.is_empty() {
            Ok(())
        } else {
            Err(CoreError::InvariantViolation(
                "encountered denomination that isn't present in total supplies table".to_string(),
            ))
        }
    }
}