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
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use std::cmp::{max, min};
use std::collections::BTreeMap;

use cid::Cid;
use fil_actors_shared::v13::DealWeight;
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::tuple::*;
use fvm_shared4::address::Address;
use fvm_shared4::bigint::BigInt;
use fvm_shared4::clock::{ChainEpoch, EPOCH_UNDEFINED};
use fvm_shared4::deal::DealID;
use fvm_shared4::econ::TokenAmount;
use fvm_shared4::error::ExitCode;
use fvm_shared4::sector::SectorNumber;
use fvm_shared4::{ActorID, HAMT_BIT_WIDTH};
use num_traits::Zero;
use std::collections::BTreeSet;

use fil_actors_shared::actor_error_v14;
use fil_actors_shared::v14::{
    ActorContext, ActorError, Array, AsActorError, Config, Map2, Set, SetMultimap,
    SetMultimapConfig, DEFAULT_HAMT_CONFIG,
};

use crate::v14::balance_table::BalanceTable;
use crate::v14::ext::verifreg::AllocationID;

use super::policy::*;
use super::types::*;
use super::{DealProposal, DealState, EX_DEAL_EXPIRED};

pub enum Reason {
    ClientCollateral,
    ClientStorageFee,
    ProviderCollateral,
}

/// Market actor state
#[derive(Clone, Default, Serialize_tuple, Deserialize_tuple, Debug)]
pub struct State {
    /// Proposals are deals that have been proposed and not yet cleaned up after expiry or termination.
    /// Array<DealID, DealProposal>
    pub proposals: Cid,

    // States contains state for deals that have been activated and not yet cleaned up after expiry or termination.
    // After expiration, the state exists until the proposal is cleaned up too.
    // Invariant: keys(States) ⊆ keys(Proposals).
    /// Array<DealID, DealState>
    pub states: Cid,

    /// PendingProposals tracks dealProposals that have not yet reached their deal start date.
    /// We track them here to ensure that miners can't publish the same deal proposal twice
    /// Set<CID>
    pub pending_proposals: Cid,

    /// Total amount held in escrow, indexed by actor address (including both locked and unlocked amounts).
    pub escrow_table: Cid,

    /// Amount locked, indexed by actor address.
    /// Note: the amounts in this table do not affect the overall amount in escrow:
    /// only the _portion_ of the total escrow amount that is locked.
    pub locked_table: Cid,

    /// Deal id state sequential incrementer
    pub next_id: DealID,

    /// Metadata cached for efficient iteration over deals.
    /// SetMultimap<Address>
    pub deal_ops_by_epoch: Cid,
    pub last_cron: ChainEpoch,

    /// Total Client Collateral that is locked -> unlocked when deal is terminated
    pub total_client_locked_collateral: TokenAmount,
    /// Total Provider Collateral that is locked -> unlocked when deal is terminated
    pub total_provider_locked_collateral: TokenAmount,
    /// Total storage fee that is locked in escrow -> unlocked when payments are made
    pub total_client_storage_fee: TokenAmount,

    /// Verified registry allocation IDs for deals that are not yet activated.
    // HAMT[DealID]AllocationID
    pub pending_deal_allocation_ids: Cid,

    /// Maps providers to their sector IDs to deal IDs.
    /// This supports finding affected deals when a sector is terminated early
    /// or has data replaced.
    /// Grouping by provider limits the cost of operations in the expected use case
    /// of multiple sectors all belonging to the same provider.
    /// HAMT[ActorID]HAMT[SectorNumber][]DealID
    pub provider_sectors: Cid,
}

pub type PendingProposalsSet<BS> = Set<BS, Cid>;
pub const PENDING_PROPOSALS_CONFIG: Config = DEFAULT_HAMT_CONFIG;

pub type DealOpsByEpoch<BS> = SetMultimap<BS, ChainEpoch, DealID>;
pub const DEAL_OPS_BY_EPOCH_CONFIG: SetMultimapConfig = SetMultimapConfig {
    outer: DEFAULT_HAMT_CONFIG,
    inner: DEFAULT_HAMT_CONFIG,
};

pub type PendingDealAllocationsMap<BS> = Map2<BS, DealID, AllocationID>;
pub const PENDING_ALLOCATIONS_CONFIG: Config = Config {
    bit_width: HAMT_BIT_WIDTH,
    ..DEFAULT_HAMT_CONFIG
};

pub type ProviderSectorsMap<BS> = Map2<BS, ActorID, Cid>;
pub const PROVIDER_SECTORS_CONFIG: Config = Config {
    bit_width: HAMT_BIT_WIDTH,
    ..DEFAULT_HAMT_CONFIG
};

pub type SectorDealsMap<BS> = Map2<BS, SectorNumber, Vec<DealID>>;
pub const SECTOR_DEALS_CONFIG: Config = Config {
    bit_width: HAMT_BIT_WIDTH,
    ..DEFAULT_HAMT_CONFIG
};

fn get_proposals<BS: Blockstore>(
    proposal_array: &DealArray<BS>,
    deal_ids: &[DealID],
    next_id: DealID,
) -> Result<Vec<(DealID, DealProposal)>, ActorError> {
    let mut proposals = Vec::new();
    let mut seen_deal_ids = BTreeSet::new();
    for deal_id in deal_ids {
        if !seen_deal_ids.insert(deal_id) {
            return Err(actor_error_v14!(
                illegal_argument,
                "duplicate deal ID {} in sector",
                deal_id
            ));
        }
        let proposal = get_proposal(proposal_array, *deal_id, next_id)?;
        proposals.push((*deal_id, proposal));
    }
    Ok(proposals)
}

fn validate_deal_can_activate(
    proposal: &DealProposal,
    miner_addr: &Address,
    sector_expiration: ChainEpoch,
    curr_epoch: ChainEpoch,
) -> Result<(), ActorError> {
    if &proposal.provider != miner_addr {
        return Err(ActorError::forbidden(format!(
            "proposal has provider {}, must be {}",
            proposal.provider, miner_addr
        )));
    };

    if curr_epoch > proposal.start_epoch {
        return Err(ActorError::unchecked(
            // Use the same code as if the proposal had already been cleaned up from state.
            EX_DEAL_EXPIRED,
            format!(
                "proposal start epoch {} has already elapsed at {}",
                proposal.start_epoch, curr_epoch
            ),
        ));
    };

    if proposal.end_epoch > sector_expiration {
        return Err(ActorError::illegal_argument(format!(
            "proposal expiration {} exceeds sector expiration {}",
            proposal.end_epoch, sector_expiration
        )));
    };

    Ok(())
}

// Returns (deal_weight, verified_deal_weight)
fn get_deal_weights(deal: DealProposal) -> (DealWeight, DealWeight) {
    if deal.verified_deal {
        return (
            DealWeight::zero(),
            DealWeight::from(deal.piece_size.0 * deal.duration() as u64),
        );
    }
    (
        DealWeight::from(deal.piece_size.0 * deal.duration() as u64),
        DealWeight::zero(),
    )
}

impl State {
    pub fn new<BS: Blockstore>(store: &BS) -> Result<Self, ActorError> {
        let empty_proposals_array =
            Array::<(), BS>::new_with_bit_width(store, PROPOSALS_AMT_BITWIDTH)
                .flush()
                .context_code(
                    ExitCode::USR_ILLEGAL_STATE,
                    "failed to create empty proposals array",
                )?;

        let empty_states_array = Array::<(), BS>::new_with_bit_width(store, STATES_AMT_BITWIDTH)
            .flush()
            .context_code(
                ExitCode::USR_ILLEGAL_STATE,
                "failed to create empty states array",
            )?;

        let empty_pending_proposals =
            PendingProposalsSet::empty(store, PENDING_PROPOSALS_CONFIG, "pending proposals")
                .flush()?;
        let empty_balance_table = BalanceTable::new(store, "balance table").root()?;
        let empty_deal_ops =
            DealOpsByEpoch::empty(store, DEAL_OPS_BY_EPOCH_CONFIG, "deal ops").flush()?;

        let empty_pending_deal_allocation_map = PendingDealAllocationsMap::empty(
            store,
            PENDING_ALLOCATIONS_CONFIG,
            "pending deal allocations",
        )
        .flush()?;

        let empty_sector_deals_hamt =
            ProviderSectorsMap::empty(store, PROVIDER_SECTORS_CONFIG, "sector deals").flush()?;

        Ok(Self {
            proposals: empty_proposals_array,
            states: empty_states_array,
            pending_proposals: empty_pending_proposals,
            escrow_table: empty_balance_table,
            locked_table: empty_balance_table,
            next_id: 0,
            deal_ops_by_epoch: empty_deal_ops,
            last_cron: EPOCH_UNDEFINED,

            total_client_locked_collateral: TokenAmount::default(),
            total_provider_locked_collateral: TokenAmount::default(),
            total_client_storage_fee: TokenAmount::default(),
            pending_deal_allocation_ids: empty_pending_deal_allocation_map,
            provider_sectors: empty_sector_deals_hamt,
        })
    }

    pub fn get_total_locked(&self) -> TokenAmount {
        &self.total_client_locked_collateral
            + &self.total_provider_locked_collateral
            + &self.total_client_storage_fee
    }

    pub fn load_deal_states<'bs, BS>(
        &self,
        store: &'bs BS,
    ) -> Result<DealMetaArray<'bs, BS>, ActorError>
    where
        BS: Blockstore,
    {
        DealMetaArray::load(&self.states, store).context_code(
            ExitCode::USR_ILLEGAL_STATE,
            "failed to load deal state array",
        )
    }

    fn save_deal_states<BS>(&mut self, states: &mut DealMetaArray<BS>) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        self.states = states
            .flush()
            .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush deal states")?;
        Ok(())
    }

    pub fn find_deal_state<BS>(
        &self,
        store: &BS,
        deal_id: DealID,
    ) -> Result<Option<DealState>, ActorError>
    where
        BS: Blockstore,
    {
        let states = self.load_deal_states(store)?;
        find_deal_state(&states, deal_id)
    }

    pub fn put_deal_states<BS>(
        &mut self,
        store: &BS,
        new_deal_states: &[(DealID, DealState)],
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        let mut states = self.load_deal_states(store)?;
        new_deal_states
            .iter()
            .try_for_each(|(id, deal_state)| -> Result<(), ActorError> {
                states
                    .set(*id, *deal_state)
                    .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to set deal state")?;
                Ok(())
            })?;
        self.save_deal_states(&mut states)
    }

    pub fn remove_deal_state<BS>(
        &mut self,
        store: &BS,
        deal_id: DealID,
    ) -> Result<Option<DealState>, ActorError>
    where
        BS: Blockstore,
    {
        let mut states = self.load_deal_states(store)?;
        let removed = states
            .delete(deal_id)
            .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to delete deal state")?;
        self.save_deal_states(&mut states)?;
        Ok(removed)
    }

    pub fn load_proposals<'bs, BS>(&self, store: &'bs BS) -> Result<DealArray<'bs, BS>, ActorError>
    where
        BS: Blockstore,
    {
        DealArray::load(&self.proposals, store).context_code(
            ExitCode::USR_ILLEGAL_STATE,
            "failed to load deal proposal array",
        )
    }

    pub fn get_proposal<BS: Blockstore>(
        &self,
        store: &BS,
        id: DealID,
    ) -> Result<DealProposal, ActorError> {
        get_proposal(&self.load_proposals(store)?, id, self.next_id)
    }

    pub fn find_proposal<BS>(
        &self,
        store: &BS,
        deal_id: DealID,
    ) -> Result<Option<DealProposal>, ActorError>
    where
        BS: Blockstore,
    {
        find_proposal(&self.load_proposals(store)?, deal_id)
    }

    pub fn remove_proposal<BS>(
        &mut self,
        store: &BS,
        deal_id: DealID,
    ) -> Result<Option<DealProposal>, ActorError>
    where
        BS: Blockstore,
    {
        let mut deal_proposals = DealArray::load(&self.proposals, store).context_code(
            ExitCode::USR_ILLEGAL_STATE,
            "failed to load deal proposal array",
        )?;

        let proposal = deal_proposals
            .delete(deal_id)
            .with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
                format!("no such deal proposal {}", deal_id)
            })?;

        self.proposals = deal_proposals.flush().context_code(
            ExitCode::USR_ILLEGAL_STATE,
            "failed to flush deal proposals",
        )?;

        Ok(proposal)
    }

    pub fn put_deal_proposals<BS>(
        &mut self,
        store: &BS,
        new_deal_proposals: &[(DealID, DealProposal)],
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        let mut deal_proposals = DealArray::load(&self.proposals, store).context_code(
            ExitCode::USR_ILLEGAL_STATE,
            "failed to load deal proposal array",
        )?;

        new_deal_proposals
            .iter()
            .try_for_each(|(id, proposal)| -> Result<(), ActorError> {
                deal_proposals
                    .set(*id, proposal.clone())
                    .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to set deal proposal")?;
                Ok(())
            })?;

        self.proposals = deal_proposals.flush().context_code(
            ExitCode::USR_ILLEGAL_STATE,
            "failed to flush deal proposals",
        )?;

        Ok(())
    }

    pub fn load_pending_deal_allocation_ids<BS>(
        &mut self,
        store: BS,
    ) -> Result<PendingDealAllocationsMap<BS>, ActorError>
    where
        BS: Blockstore,
    {
        PendingDealAllocationsMap::load(
            store,
            &self.pending_deal_allocation_ids,
            PENDING_ALLOCATIONS_CONFIG,
            "pending deal allocations",
        )
    }

    pub fn save_pending_deal_allocation_ids<BS>(
        &mut self,
        pending_deal_allocation_ids: &mut PendingDealAllocationsMap<BS>,
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        self.pending_deal_allocation_ids = pending_deal_allocation_ids.flush()?;
        Ok(())
    }

    pub fn put_pending_deal_allocation_ids<BS>(
        &mut self,
        store: &BS,
        new_pending_deal_allocation_ids: &[(DealID, AllocationID)],
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        let mut pending_deal_allocation_ids = self.load_pending_deal_allocation_ids(store)?;
        new_pending_deal_allocation_ids.iter().try_for_each(
            |(deal_id, allocation_id)| -> Result<(), ActorError> {
                pending_deal_allocation_ids.set(deal_id, *allocation_id)?;
                Ok(())
            },
        )?;
        self.save_pending_deal_allocation_ids(&mut pending_deal_allocation_ids)?;
        Ok(())
    }

    pub fn get_pending_deal_allocation_ids<BS>(
        &mut self,
        store: &BS,
        deal_id_keys: &[DealID],
    ) -> Result<Vec<AllocationID>, ActorError>
    where
        BS: Blockstore,
    {
        let pending_deal_allocation_ids = self.load_pending_deal_allocation_ids(store)?;

        let mut allocation_ids: Vec<AllocationID> = vec![];
        deal_id_keys
            .iter()
            .try_for_each(|deal_id| -> Result<(), ActorError> {
                let allocation_id = pending_deal_allocation_ids.get(&deal_id.clone())?;
                allocation_ids.push(
                    *allocation_id
                        .ok_or(ActorError::not_found("no such deal proposal".to_string()))?,
                );
                Ok(())
            })?;

        Ok(allocation_ids)
    }

    pub fn remove_pending_deal_allocation_id<BS>(
        &mut self,
        store: &BS,
        deal_id: DealID,
    ) -> Result<Option<AllocationID>, ActorError>
    where
        BS: Blockstore,
    {
        let mut pending_deal_allocation_ids = self.load_pending_deal_allocation_ids(store)?;
        let maybe_alloc_id = pending_deal_allocation_ids.delete(&deal_id)?;
        self.save_pending_deal_allocation_ids(&mut pending_deal_allocation_ids)?;
        Ok(maybe_alloc_id)
    }

    pub fn load_deal_ops<BS>(
        &self,
        store: BS,
    ) -> Result<SetMultimap<BS, ChainEpoch, DealID>, ActorError>
    where
        BS: Blockstore,
    {
        DealOpsByEpoch::load(
            store,
            &self.deal_ops_by_epoch,
            DEAL_OPS_BY_EPOCH_CONFIG,
            "deal ops",
        )
    }

    pub fn put_deals_by_epoch<BS>(
        &mut self,
        store: &BS,
        new_deals_by_epoch: &[(ChainEpoch, DealID)],
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        let mut deals_by_epoch = self.load_deal_ops(store)?;
        new_deals_by_epoch
            .iter()
            .try_for_each(|(epoch, id)| -> Result<(), ActorError> {
                deals_by_epoch.put(epoch, *id)?;
                Ok(())
            })?;

        self.deal_ops_by_epoch = deals_by_epoch.flush()?;
        Ok(())
    }

    pub fn put_batch_deals_by_epoch<BS>(
        &mut self,
        store: &BS,
        new_deals_by_epoch: &BTreeMap<ChainEpoch, Vec<DealID>>,
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        let mut deals_by_epoch = self.load_deal_ops(store)?;
        new_deals_by_epoch
            .iter()
            .try_for_each(|(epoch, deals)| -> Result<(), ActorError> {
                deals_by_epoch.put_many(epoch, deals)?;
                Ok(())
            })?;

        self.deal_ops_by_epoch = deals_by_epoch.flush()?;
        Ok(())
    }

    pub fn get_deals_for_epoch<BS>(
        &self,
        store: &BS,
        key: ChainEpoch,
    ) -> Result<Vec<DealID>, ActorError>
    where
        BS: Blockstore,
    {
        let mut deal_ids = Vec::new();
        let deals_by_epoch = self.load_deal_ops(store)?;
        deals_by_epoch.for_each_in(&key, |deal_id| {
            deal_ids.push(deal_id);
            Ok(())
        })?;

        Ok(deal_ids)
    }

    pub fn remove_deals_by_epoch<BS>(
        &mut self,
        store: &BS,
        epochs_to_remove: &[ChainEpoch],
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        let mut deals_by_epoch = self.load_deal_ops(store)?;
        epochs_to_remove
            .iter()
            .try_for_each(|epoch| -> Result<(), ActorError> {
                deals_by_epoch.remove_all(epoch)?;
                Ok(())
            })?;

        self.deal_ops_by_epoch = deals_by_epoch.flush()?;
        Ok(())
    }

    pub fn add_balance_to_escrow_table<BS>(
        &mut self,
        store: &BS,
        addr: &Address,
        amount: &TokenAmount,
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        let mut escrow_table = BalanceTable::from_root(store, &self.escrow_table, "escrow table")?;
        escrow_table.add(addr, amount)?;
        self.escrow_table = escrow_table.root()?;
        Ok(())
    }

    pub fn withdraw_balance_from_escrow_table<BS>(
        &mut self,
        store: &BS,
        addr: &Address,
        amount: &TokenAmount,
    ) -> Result<TokenAmount, ActorError>
    where
        BS: Blockstore,
    {
        let mut escrow_table = BalanceTable::from_root(store, &self.escrow_table, "escrow table")?;
        let locked_table = BalanceTable::from_root(store, &self.locked_table, "locked table")?;

        let min_balance = locked_table.get(addr)?;
        let ex = escrow_table.subtract_with_minimum(addr, amount, &min_balance)?;

        self.escrow_table = escrow_table.root()?;
        Ok(ex)
    }

    pub fn load_pending_deals<BS>(&self, store: BS) -> Result<PendingProposalsSet<BS>, ActorError>
    where
        BS: Blockstore,
    {
        PendingProposalsSet::load(
            store,
            &self.pending_proposals,
            PENDING_PROPOSALS_CONFIG,
            "pending proposals",
        )
    }

    fn save_pending_deals<BS>(
        &mut self,
        pending_deals: &mut PendingProposalsSet<BS>,
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        self.pending_proposals = pending_deals.flush()?;
        Ok(())
    }

    pub fn has_pending_deal<BS>(&self, store: &BS, key: &Cid) -> Result<bool, ActorError>
    where
        BS: Blockstore,
    {
        let pending_deals = self.load_pending_deals(store)?;
        pending_deals.has(key)
    }

    pub fn put_pending_deals<BS>(
        &mut self,
        store: &BS,
        new_pending_deals: &[Cid],
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        let mut pending_deals = self.load_pending_deals(store)?;
        new_pending_deals
            .iter()
            .try_for_each(|key: &Cid| -> Result<(), ActorError> {
                pending_deals.put(key)?;
                Ok(())
            })?;

        self.save_pending_deals(&mut pending_deals)
    }

    pub fn remove_pending_deal<BS>(
        &mut self,
        store: &BS,
        pending_deal_key: Cid,
    ) -> Result<Option<()>, ActorError>
    where
        BS: Blockstore,
    {
        let mut pending_deals = self.load_pending_deals(store)?;
        let removed = pending_deals.delete(&pending_deal_key)?;

        self.save_pending_deals(&mut pending_deals)?;
        Ok(removed)
    }

    ////////////////////////////////////////////////////////////////////////////////
    // Provider sector/deal operations
    ////////////////////////////////////////////////////////////////////////////////

    // Stores deal IDs associated with sectors for a provider.
    // Deal IDs are added to any already stored for the provider and sector.
    // Returns the root cid of the sector deals map.
    pub fn put_sector_deal_ids(
        &mut self,
        store: &impl Blockstore,
        provider: ActorID,
        sector_deal_ids: &[(SectorNumber, Vec<DealID>)],
    ) -> Result<(), ActorError> {
        let mut provider_sectors = self.load_provider_sectors(store)?;
        let mut sector_deals = load_provider_sector_deals(store, &provider_sectors, provider)?;

        for (sector_number, deals) in sector_deal_ids {
            let mut new_deals = deals.clone();
            let existing_deal_ids = sector_deals
                .get(sector_number)
                .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to read sector deals")?;
            if let Some(existing_deal_ids) = existing_deal_ids {
                new_deals.extend(existing_deal_ids.iter());
            }
            new_deals.sort();
            new_deals.dedup();
            sector_deals
                .set(sector_number, new_deals)
                .with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
                    format!(
                        "failed to set sector deals for {} {}",
                        provider, sector_number
                    )
                })?;
        }

        save_provider_sector_deals(&mut provider_sectors, provider, &mut sector_deals)?;
        self.save_provider_sectors(&mut provider_sectors)?;
        Ok(())
    }

    // Reads and removes the sector deals mapping for an array of sector numbers,
    pub fn pop_sector_deal_ids(
        &mut self,
        store: &impl Blockstore,
        provider: ActorID,
        sector_numbers: impl Iterator<Item = SectorNumber>,
    ) -> Result<Vec<DealID>, ActorError> {
        let mut provider_sectors = self.load_provider_sectors(store)?;
        let mut sector_deals = load_provider_sector_deals(store, &provider_sectors, provider)?;

        let mut popped_sector_deals = Vec::new();
        let mut flush = false;
        for sector_number in sector_numbers {
            let deals: Option<Vec<DealID>> = sector_deals
                .delete(&sector_number)
                .with_context(|| format!("provider {}", provider))?;
            if let Some(deals) = deals {
                popped_sector_deals.extend(deals.iter());
                flush = true;
            }
        }

        // Flush if any of the requested sectors were found.
        if flush {
            save_provider_sector_deals(&mut provider_sectors, provider, &mut sector_deals)?;
            self.save_provider_sectors(&mut provider_sectors)?;
        }

        Ok(popped_sector_deals)
    }

    // Removes specified deals from the sector deals mapping.
    // Missing deals are ignored.
    pub fn remove_sector_deal_ids(
        &mut self,
        store: &impl Blockstore,
        provider_sector_deal_ids: &BTreeMap<ActorID, BTreeMap<SectorNumber, Vec<DealID>>>,
    ) -> Result<(), ActorError> {
        let mut provider_sectors = self.load_provider_sectors(store)?;
        for (provider, sector_deal_ids) in provider_sector_deal_ids {
            let mut flush = false;
            let mut sector_deals = load_provider_sector_deals(store, &provider_sectors, *provider)?;
            for (sector_number, deals_to_remove) in sector_deal_ids {
                let existing_deal_ids = sector_deals
                    .get(sector_number)
                    .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to read sector deals")?;
                if let Some(existing_deal_ids) = existing_deal_ids {
                    // The filter below is a linear scan of deals_to_remove.
                    // This is expected to be a small list, often a singleton, so is usually
                    // pretty fast.
                    // Loading into a HashSet could be an improvement for large collections of deals
                    // in a single sector being removed at one time.
                    let new_deals: Vec<_> = existing_deal_ids
                        .iter()
                        .filter(|deal_id| !deals_to_remove.contains(*deal_id))
                        .cloned()
                        .collect();
                    flush = true;

                    if new_deals.is_empty() {
                        sector_deals.delete(sector_number).with_context_code(
                            ExitCode::USR_ILLEGAL_STATE,
                            || {
                                format!(
                                    "failed to delete sector deals for {} {}",
                                    provider, sector_number
                                )
                            },
                        )?;
                    } else {
                        sector_deals
                            .set(sector_number, new_deals)
                            .with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
                                format!(
                                    "failed to set sector deals for {} {}",
                                    provider, sector_number
                                )
                            })?;
                    }
                }
            }
            if flush {
                save_provider_sector_deals(&mut provider_sectors, *provider, &mut sector_deals)?;
            }
        }
        self.save_provider_sectors(&mut provider_sectors)?;
        Ok(())
    }

    pub fn load_provider_sectors<BS>(&self, store: BS) -> Result<ProviderSectorsMap<BS>, ActorError>
    where
        BS: Blockstore,
    {
        ProviderSectorsMap::load(
            store,
            &self.provider_sectors,
            PROVIDER_SECTORS_CONFIG,
            "provider sectors",
        )
    }

    fn save_provider_sectors<BS>(
        &mut self,
        provider_sectors: &mut ProviderSectorsMap<BS>,
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        self.provider_sectors = provider_sectors.flush()?;
        Ok(())
    }

    /// Delete proposal and state simultaneously.
    pub fn remove_completed_deal<BS>(
        &mut self,
        store: &BS,
        deal_id: DealID,
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        let state = self.remove_deal_state(store, deal_id)?;
        if state.is_none() {
            return Err(actor_error_v14!(
                illegal_state,
                "failed to delete deal state: does not exist"
            ));
        }
        let proposal = self.remove_proposal(store, deal_id)?;
        if proposal.is_none() {
            return Err(actor_error_v14!(
                illegal_state,
                "failed to delete deal proposal: does not exist"
            ));
        }
        Ok(())
    }

    /// Given a DealProposal, checks that the corresponding deal has activated
    /// If not, checks that the deal is past its activation epoch and performs cleanup
    pub fn get_active_deal_or_process_timeout<BS>(
        &mut self,
        store: &BS,
        curr_epoch: ChainEpoch,
        deal_id: DealID,
        deal_proposal: &DealProposal,
        dcid: &Cid,
    ) -> Result<LoadDealState, ActorError>
    where
        BS: Blockstore,
    {
        let deal_state = self.find_deal_state(store, deal_id)?;

        match deal_state {
            Some(deal_state) => Ok(LoadDealState::Loaded(deal_state)),
            None => {
                // deal_id called too early
                if curr_epoch < deal_proposal.start_epoch {
                    return Ok(LoadDealState::TooEarly);
                }

                // if not activated, the proposal has timed out
                let slashed = self.process_deal_init_timed_out(store, deal_proposal)?;

                // delete the proposal (but not state, which doesn't exist)
                let deleted = self.remove_proposal(store, deal_id)?;
                if deleted.is_none() {
                    return Err(actor_error_v14!(
                        illegal_state,
                        format!(
                            "failed to delete deal {} proposal {}: does not exist",
                            deal_id, dcid
                        )
                    ));
                }

                // delete pending deal cid
                self.remove_pending_deal(store, *dcid)?.ok_or_else(|| {
                    actor_error_v14!(
                        illegal_state,
                        format!(
                            "failed to delete pending deal {}: cid {} does not exist",
                            deal_id, dcid
                        )
                    )
                })?;

                // delete pending deal allocation id (if present)
                self.remove_pending_deal_allocation_id(store, deal_id)?;

                Ok(LoadDealState::ProposalExpired(slashed))
            }
        }
    }

    ////////////////////////////////////////////////////////////////////////////////
    // Deal state operations
    ////////////////////////////////////////////////////////////////////////////////

    // TODO: change return value when marked-for-termination sectors are cleared from state
    // https://github.com/filecoin-project/builtin-actors/issues/1388
    // drop slash_amount, bool return value indicates a completed deal
    pub fn process_deal_update<BS>(
        &mut self,
        store: &BS,
        state: &DealState,
        deal: &DealProposal,
        deal_cid: &Cid,
        epoch: ChainEpoch,
    ) -> Result<
        (
            /* slash_amount */ TokenAmount,
            /* payment_amount */ TokenAmount,
            /* is_deal_completed */ bool,
            /* remove */ bool,
        ),
        ActorError,
    >
    where
        BS: Blockstore,
    {
        let ever_updated = state.last_updated_epoch != EPOCH_UNDEFINED;

        // seeing a slashed deal here will eventually be an unreachable state
        // during the transition to synchronous deal termination there may be marked-for-termination
        // deals that have not been processed in cron yet
        // https://github.com/filecoin-project/builtin-actors/issues/1388
        // TODO: remove this and calculations below that assume deals can be slashed
        let ever_slashed = state.slash_epoch != EPOCH_UNDEFINED;

        if !ever_updated {
            // pending deal might have been removed by manual settlement or cron so we don't care if it's missing
            self.remove_pending_deal(store, *deal_cid)?;
        }

        // if the deal was ever updated, make sure it didn't happen in the future
        if ever_updated && state.last_updated_epoch > epoch {
            return Err(actor_error_v14!(
                illegal_state,
                "deal updated at future epoch {}",
                state.last_updated_epoch
            ));
        }

        // this is a safe no-op but can happen if a storage provider calls settle_deal_payments too early
        if deal.start_epoch > epoch {
            return Ok((TokenAmount::zero(), TokenAmount::zero(), false, false));
        }

        let payment_end_epoch = if ever_slashed {
            if epoch < state.slash_epoch {
                return Err(actor_error_v14!(
                    illegal_state,
                    "current epoch less than deal slash epoch {}",
                    state.slash_epoch
                ));
            }
            if state.slash_epoch > deal.end_epoch {
                return Err(actor_error_v14!(
                    illegal_state,
                    "deal slash epoch {} after deal end {}",
                    state.slash_epoch,
                    deal.end_epoch
                ));
            }
            state.slash_epoch
        } else {
            std::cmp::min(deal.end_epoch, epoch)
        };

        let payment_start_epoch = if ever_updated && state.last_updated_epoch > deal.start_epoch {
            state.last_updated_epoch
        } else {
            deal.start_epoch
        };

        let num_epochs_elapsed = payment_end_epoch - payment_start_epoch;

        let elapsed_payment = &deal.storage_price_per_epoch * num_epochs_elapsed;
        if elapsed_payment.is_positive() {
            self.transfer_balance(store, &deal.client, &deal.provider, &elapsed_payment)?;
        }

        // TODO: remove handling of terminated deals *after* transition to synchronous deal termination
        // at that point, this function can be modified to return a bool only, indicating whether the deal is completed
        // https://github.com/filecoin-project/builtin-actors/issues/1388
        if ever_slashed {
            // unlock client collateral and locked storage fee
            let payment_remaining = deal_get_payment_remaining(deal, state.slash_epoch)?;

            // Unlock remaining storage fee
            self.unlock_balance(
                store,
                &deal.client,
                &payment_remaining,
                Reason::ClientStorageFee,
            )
            .context("unlocking client storage fee")?;

            // Unlock client collateral
            self.unlock_balance(
                store,
                &deal.client,
                &deal.client_collateral,
                Reason::ClientCollateral,
            )
            .context("unlocking client collateral")?;

            // slash provider collateral
            let slashed = deal.provider_collateral.clone();
            self.slash_balance(store, &deal.provider, &slashed, Reason::ProviderCollateral)
                .context("slashing balance")?;

            return Ok((slashed, payment_remaining + elapsed_payment, false, true));
        }

        if epoch >= deal.end_epoch {
            self.process_deal_expired(store, deal, state)?;
            return Ok((TokenAmount::zero(), elapsed_payment, true, true));
        }

        Ok((TokenAmount::zero(), elapsed_payment, false, false))
    }

    pub fn process_slashed_deal<BS>(
        &mut self,
        store: &BS,
        proposal: &DealProposal,
        state: &DealState,
    ) -> Result<TokenAmount, ActorError>
    where
        BS: Blockstore,
    {
        // make payments for epochs until termination
        let payment_start_epoch = max(proposal.start_epoch, state.last_updated_epoch);
        let payment_end_epoch = min(proposal.end_epoch, state.slash_epoch);
        let num_epochs_elapsed = max(0, payment_end_epoch - payment_start_epoch);
        let total_payment = &proposal.storage_price_per_epoch * num_epochs_elapsed;
        if total_payment.is_positive() {
            self.transfer_balance(store, &proposal.client, &proposal.provider, &total_payment)?;
        }

        // unlock client collateral and locked storage fee
        let payment_remaining = deal_get_payment_remaining(proposal, state.slash_epoch)?;

        // Unlock remaining storage fee
        self.unlock_balance(
            store,
            &proposal.client,
            &payment_remaining,
            Reason::ClientStorageFee,
        )
        .context("unlocking client storage fee")?;

        // Unlock client collateral
        self.unlock_balance(
            store,
            &proposal.client,
            &proposal.client_collateral,
            Reason::ClientCollateral,
        )
        .context("unlocking client collateral")?;

        // slash provider collateral
        let slashed = proposal.provider_collateral.clone();
        self.slash_balance(
            store,
            &proposal.provider,
            &slashed,
            Reason::ProviderCollateral,
        )
        .context("slashing balance")?;

        Ok(slashed)
    }

    /// Deal start deadline elapsed without appearing in a proven sector.
    /// Slash a portion of provider's collateral, and unlock remaining collaterals
    /// for both provider and client.
    pub fn process_deal_init_timed_out<BS>(
        &mut self,
        store: &BS,
        deal: &DealProposal,
    ) -> Result<TokenAmount, ActorError>
    where
        BS: Blockstore,
    {
        self.unlock_balance(
            store,
            &deal.client,
            &deal.total_storage_fee(),
            Reason::ClientStorageFee,
        )
        .context("unlocking client storage fee")?;

        self.unlock_balance(
            store,
            &deal.client,
            &deal.client_collateral,
            Reason::ClientCollateral,
        )
        .context("unlocking client collateral")?;

        let amount_slashed =
            collateral_penalty_for_deal_activation_missed(deal.provider_collateral.clone());
        let amount_remaining = deal.provider_balance_requirement() - &amount_slashed;

        self.slash_balance(
            store,
            &deal.provider,
            &amount_slashed,
            Reason::ProviderCollateral,
        )
        .context("slashing balance")?;

        self.unlock_balance(
            store,
            &deal.provider,
            &amount_remaining,
            Reason::ProviderCollateral,
        )
        .context("unlocking deal provider balance")?;

        Ok(amount_slashed)
    }

    /// Normal expiration. Unlock collaterals for both miner and client.
    fn process_deal_expired<BS>(
        &mut self,
        store: &BS,
        deal: &DealProposal,
        state: &DealState,
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        if state.sector_start_epoch == EPOCH_UNDEFINED {
            return Err(actor_error_v14!(
                illegal_state,
                "start sector epoch undefined"
            ));
        }

        self.unlock_balance(
            store,
            &deal.provider,
            &deal.provider_collateral,
            Reason::ProviderCollateral,
        )
        .context("unlocking deal provider balance")?;

        self.unlock_balance(
            store,
            &deal.client,
            &deal.client_collateral,
            Reason::ClientCollateral,
        )
        .context("unlocking deal client balance")?;

        Ok(())
    }

    pub fn generate_storage_deal_id(&mut self) -> DealID {
        let ret = self.next_id;
        self.next_id += 1;
        ret
    }

    pub fn escrow_table<'a, BS: Blockstore>(
        &self,
        store: &'a BS,
    ) -> Result<BalanceTable<&'a BS>, ActorError> {
        BalanceTable::from_root(store, &self.escrow_table, "escrow table")
    }

    pub fn locked_table<'a, BS: Blockstore>(
        &self,
        store: &'a BS,
    ) -> Result<BalanceTable<&'a BS>, ActorError> {
        BalanceTable::from_root(store, &self.locked_table, "locked table")
    }

    // Return true when the funds in escrow for the input address can cover an additional lockup of amountToLock
    pub fn balance_covered<BS>(
        &self,
        store: &BS,
        addr: Address,
        amount_to_lock: &TokenAmount,
    ) -> Result<bool, ActorError>
    where
        BS: Blockstore,
    {
        let escrow_table = BalanceTable::from_root(store, &self.escrow_table, "escrow table")?;
        let locked_table = BalanceTable::from_root(store, &self.locked_table, "locked table")?;

        let escrow_balance = escrow_table.get(&addr)?;
        let prev_locked = locked_table.get(&addr)?;
        Ok((prev_locked + amount_to_lock) <= escrow_balance)
    }

    fn maybe_lock_balance<BS>(
        &mut self,
        store: &BS,
        addr: &Address,
        amount: &TokenAmount,
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        if amount.is_negative() {
            return Err(actor_error_v14!(
                illegal_state,
                "cannot lock negative amount {}",
                amount
            ));
        }

        let escrow_table = BalanceTable::from_root(store, &self.escrow_table, "escrow table")?;
        let mut locked_table = BalanceTable::from_root(store, &self.locked_table, "locked table")?;

        let prev_locked = locked_table.get(addr)?;
        let escrow_balance = escrow_table.get(addr)?;
        if &prev_locked + amount > escrow_balance {
            return Err(actor_error_v14!(insufficient_funds;
                    "not enough balance to lock for addr{}: \
                    escrow balance {} < prev locked {} + amount {}",
                    addr, escrow_balance, prev_locked, amount));
        }

        locked_table.add(addr, amount)?;
        self.locked_table = locked_table.root()?;
        Ok(())
    }

    pub fn lock_client_and_provider_balances<BS>(
        &mut self,
        store: &BS,
        proposal: &DealProposal,
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        self.maybe_lock_balance(
            store,
            &proposal.client,
            &proposal.client_balance_requirement(),
        )
        .context("locking client funds")?;
        self.maybe_lock_balance(store, &proposal.provider, &proposal.provider_collateral)
            .context("locking provider funds")?;

        self.total_client_locked_collateral += &proposal.client_collateral;
        self.total_client_storage_fee += proposal.total_storage_fee();
        self.total_provider_locked_collateral += &proposal.provider_collateral;
        Ok(())
    }

    fn unlock_balance<BS>(
        &mut self,
        store: &BS,
        addr: &Address,
        amount: &TokenAmount,
        lock_reason: Reason,
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        if amount.is_negative() {
            return Err(actor_error_v14!(
                illegal_state,
                "unlock negative amount: {}",
                amount
            ));
        }

        let mut locked_table = BalanceTable::from_root(store, &self.locked_table, "locked table")?;
        locked_table
            .must_subtract(addr, amount)
            .context("unlocking balance")?;

        match lock_reason {
            Reason::ClientCollateral => {
                self.total_client_locked_collateral -= amount;
            }
            Reason::ClientStorageFee => {
                self.total_client_storage_fee -= amount;
            }
            Reason::ProviderCollateral => {
                self.total_provider_locked_collateral -= amount;
            }
        };

        self.locked_table = locked_table.root()?;
        Ok(())
    }

    /// move funds from locked in client to available in provider
    fn transfer_balance<BS>(
        &mut self,
        store: &BS,
        from_addr: &Address,
        to_addr: &Address,
        amount: &TokenAmount,
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        if amount.is_negative() {
            return Err(actor_error_v14!(
                illegal_state,
                "transfer negative amount: {}",
                amount
            ));
        }

        let mut escrow_table = BalanceTable::from_root(store, &self.escrow_table, "escrow table")?;

        // Subtract from locked and escrow tables
        escrow_table.must_subtract(from_addr, amount)?;
        self.unlock_balance(store, from_addr, amount, Reason::ClientStorageFee)
            .context("unlocking client balance")?;

        // Add subtracted amount to the recipient
        escrow_table.add(to_addr, amount)?;
        self.escrow_table = escrow_table.root()?;
        Ok(())
    }

    fn slash_balance<BS>(
        &mut self,
        store: &BS,
        addr: &Address,
        amount: &TokenAmount,
        lock_reason: Reason,
    ) -> Result<(), ActorError>
    where
        BS: Blockstore,
    {
        if amount.is_negative() {
            return Err(actor_error_v14!(
                illegal_state,
                "negative amount to slash: {}",
                amount
            ));
        }

        let mut escrow_table = BalanceTable::from_root(store, &self.escrow_table, "escrow table")?;

        // Subtract from locked and escrow tables
        escrow_table.must_subtract(addr, amount)?;
        self.escrow_table = escrow_table.root()?;
        self.unlock_balance(store, addr, amount, lock_reason)
    }

    /// Verify that a given set of storage deals is valid for a sector currently being PreCommitted
    pub fn verify_deals_for_activation<BS>(
        &self,
        store: &BS,
        addr: &Address,
        deal_ids: Vec<DealID>,
        curr_epoch: ChainEpoch,
        sector_exp: i64,
    ) -> Result<(DealWeight, DealWeight), ActorError>
    where
        BS: Blockstore,
    {
        let proposal_array = self.load_proposals(store)?;
        let mut total_w = BigInt::zero();
        let mut total_vw = BigInt::zero();
        let sector_proposals = get_proposals(&proposal_array, &deal_ids, self.next_id)?;
        for (deal_id, proposal) in sector_proposals.into_iter() {
            validate_deal_can_activate(&proposal, addr, sector_exp, curr_epoch)
                .with_context(|| format!("cannot activate deal {}", deal_id))?;
            let (w, vw) = get_deal_weights(proposal);
            total_w += w;
            total_vw += vw;
        }

        Ok((total_w, total_vw))
    }
}

pub enum LoadDealState {
    TooEarly,
    ProposalExpired(/* slashed_amount */ TokenAmount),
    Loaded(DealState),
}

pub fn deal_get_payment_remaining(
    deal: &DealProposal,
    mut slash_epoch: ChainEpoch,
) -> Result<TokenAmount, ActorError> {
    if slash_epoch > deal.end_epoch {
        return Err(actor_error_v14!(
            illegal_state,
            "deal slash epoch {} after end epoch {}",
            slash_epoch,
            deal.end_epoch
        ));
    }

    // Payments are always for start -> end epoch irrespective of when the deal is slashed.
    slash_epoch = std::cmp::max(slash_epoch, deal.start_epoch);

    let duration_remaining = deal.end_epoch - slash_epoch;
    if duration_remaining < 0 {
        return Err(actor_error_v14!(
            illegal_state,
            "deal remaining duration negative: {}",
            duration_remaining
        ));
    }

    Ok(&deal.storage_price_per_epoch * duration_remaining as u64)
}

pub fn get_proposal<BS: Blockstore>(
    proposals: &DealArray<BS>,
    id: DealID,
    next_id: DealID,
) -> Result<DealProposal, ActorError> {
    let found = find_proposal(proposals, id)?.ok_or_else(|| {
        if id < next_id {
            // If the deal ID has been used, it must have been cleaned up.
            ActorError::unchecked(EX_DEAL_EXPIRED, format!("deal {} expired", id))
        } else {
            // Never been published.
            ActorError::not_found(format!("no such deal {}", id))
        }
    })?;
    Ok(found)
}

pub fn find_proposal<BS>(
    proposals: &DealArray<BS>,
    deal_id: DealID,
) -> Result<Option<DealProposal>, ActorError>
where
    BS: Blockstore,
{
    let proposal = proposals
        .get(deal_id)
        .with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
            format!("failed to load deal proposal {}", deal_id)
        })?;
    Ok(proposal.cloned())
}

pub fn find_deal_state<BS>(
    states: &DealMetaArray<BS>,
    deal_id: DealID,
) -> Result<Option<DealState>, ActorError>
where
    BS: Blockstore,
{
    let state = states
        .get(deal_id)
        .with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
            format!("failed to load deal state {}", deal_id)
        })?;
    Ok(state.cloned())
}

pub fn load_provider_sector_deals<BS>(
    store: BS,
    provider_sectors: &ProviderSectorsMap<BS>,
    provider: ActorID,
) -> Result<SectorDealsMap<BS>, ActorError>
where
    BS: Blockstore,
{
    let sectors_root = (*provider_sectors).get(&provider)?;
    let sector_deals = if let Some(sectors_root) = sectors_root {
        SectorDealsMap::load(store, sectors_root, SECTOR_DEALS_CONFIG, "sector deals")
            .with_context(|| format!("provider {}", provider))?
    } else {
        SectorDealsMap::empty(store, SECTOR_DEALS_CONFIG, "empty")
    };
    Ok(sector_deals)
}

fn save_provider_sector_deals<BS>(
    provider_sectors: &mut ProviderSectorsMap<BS>,
    provider: ActorID,
    sector_deals: &mut SectorDealsMap<BS>,
) -> Result<(), ActorError>
where
    BS: Blockstore,
{
    if sector_deals.is_empty() {
        provider_sectors
            .delete(&provider)
            .with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
                format!("failed to delete sector deals for {}", provider)
            })?;
    } else {
        let sectors_root = sector_deals.flush()?;
        provider_sectors.set(&provider, sectors_root)?;
    }
    Ok(())
}