-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathstate.rs
997 lines (837 loc) · 33.3 KB
/
state.rs
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
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
use crate::balance_table::BalanceTable;
use crate::ext::verifreg::AllocationID;
use cid::Cid;
use fil_actors_runtime::{
actor_error, make_empty_map, make_map_with_root_and_bitwidth, ActorResult, ActorError, Array,
AsActorError, Set, SetMultimap,
};
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::tuple::*;
use fvm_ipld_hamt::BytesKey;
use fvm_shared::address::Address;
use fvm_shared::clock::{ChainEpoch, EPOCH_UNDEFINED};
use fvm_shared::deal::DealID;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ExitCode;
use fvm_shared::HAMT_BIT_WIDTH;
use num_traits::Zero;
use std::collections::BTreeMap;
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
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.
pub pending_deal_allocation_ids: Cid, // HAMT[DealID]AllocationID
}
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_map =
make_empty_map::<_, ()>(store, HAMT_BIT_WIDTH).flush().context_code(
ExitCode::USR_ILLEGAL_STATE,
"Failed to create empty pending proposals map state",
)?;
let empty_balance_table = BalanceTable::new(store).root().context_code(
ExitCode::USR_ILLEGAL_STATE,
"Failed to create empty balance table map",
)?;
let empty_deal_ops_hamt = SetMultimap::new(store)
.root()
.context_code(ExitCode::USR_ILLEGAL_STATE, "Failed to create empty multiset")?;
let empty_pending_deal_allocation_map =
make_empty_map::<_, AllocationID>(store, HAMT_BIT_WIDTH).flush().context_code(
ExitCode::USR_ILLEGAL_STATE,
"Failed to create empty pending deal allocation map",
)?;
Ok(Self {
proposals: empty_proposals_array,
states: empty_states_array,
pending_proposals: empty_pending_proposals_map,
escrow_table: empty_balance_table,
locked_table: empty_balance_table,
next_id: 0,
deal_ops_by_epoch: empty_deal_ops_hamt,
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,
})
}
pub fn get_total_locked(&self) -> TokenAmount {
&self.total_client_locked_collateral
+ &self.total_provider_locked_collateral
+ &self.total_client_storage_fee
}
pub fn find_deal_state<BS>(
&self,
store: &BS,
deal_id: DealID,
) -> Result<Option<DealState>, ActorError>
where
BS: Blockstore,
{
let states = DealMetaArray::load(&self.states, store)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load deal state array")?;
let found = states.get(deal_id).with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
format!("no such deal state for {}", deal_id)
})?;
Ok(found.cloned())
}
pub fn put_deal_states<BS>(
&mut self,
store: &BS,
new_deal_states: &[(DealID, DealState)],
) -> Result<(), ActorError>
where
BS: Blockstore,
{
let mut states = DealMetaArray::load(&self.states, store)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load deal proposal array")?;
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.states = states
.flush()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush deal states")?;
Ok(())
}
pub fn remove_deal_state<BS>(
&mut self,
store: &BS,
deal_id: DealID,
) -> Result<Option<DealState>, ActorError>
where
BS: Blockstore,
{
let mut states = DealMetaArray::load(&self.states, store)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load deal proposal array")?;
let rval_deal_state = states
.delete(deal_id)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to delete deal state")?;
self.states = states
.flush()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush deal states")?;
Ok(rval_deal_state)
}
pub fn get_proposal_array<'a, BS>(&'a self, store: &'a BS) -> Result<DealArray<BS>, ActorError>
where
BS: Blockstore,
{
let deal_proposals = DealArray::load(&self.proposals, store)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load deal proposal array")?;
Ok(deal_proposals)
}
pub fn get_proposal<BS: Blockstore>(
&self,
store: &BS,
id: DealID,
) -> Result<DealProposal, ActorError> {
let found = self.find_proposal(store, id)?.ok_or_else(|| {
if id < self.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 {
ActorError::not_found(format!("no such deal {}", id))
}
})?;
Ok(found)
}
pub fn find_proposal<BS>(
&self,
store: &BS,
deal_id: DealID,
) -> Result<Option<DealProposal>, ActorError>
where
BS: Blockstore,
{
let deal_proposals = self.get_proposal_array(store)?;
let proposal =
deal_proposals.get(deal_id).with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
format!("failed to load deal proposal {}", deal_id)
})?;
Ok(proposal.cloned())
}
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 put_pending_deal_allocation_ids<BS>(
&mut self,
store: &BS,
new_pending_deal_allocation_ids: &[(BytesKey, AllocationID)],
) -> Result<(), ActorError>
where
BS: Blockstore,
{
let mut pending_deal_allocation_ids = make_map_with_root_and_bitwidth(
&self.pending_deal_allocation_ids,
store,
HAMT_BIT_WIDTH,
)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending deal allocation id's")?;
new_pending_deal_allocation_ids.iter().try_for_each(
|(deal_id, allocation_id)| -> Result<(), ActorError> {
pending_deal_allocation_ids.set(deal_id.clone(), *allocation_id).context_code(
ExitCode::USR_ILLEGAL_STATE,
"failed to set pending deal allocation id",
)?;
Ok(())
},
)?;
self.pending_deal_allocation_ids = pending_deal_allocation_ids.flush().context_code(
ExitCode::USR_ILLEGAL_STATE,
"failed to flush pending deal allocation id",
)?;
Ok(())
}
pub fn get_pending_deal_allocation_ids<BS>(
&mut self,
store: &BS,
deal_id_keys: &[BytesKey],
) -> Result<Vec<AllocationID>, ActorError>
where
BS: Blockstore,
{
let pending_deal_allocation_ids = make_map_with_root_and_bitwidth(
&self.pending_deal_allocation_ids,
store,
HAMT_BIT_WIDTH,
)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending deal allocation id's")?;
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()).context_code(
ExitCode::USR_ILLEGAL_STATE,
"failed to get pending deal allocation id",
)?;
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_key: &BytesKey,
) -> Result<Option<(BytesKey, AllocationID)>, ActorError>
where
BS: Blockstore,
{
let mut pending_deal_allocation_ids = make_map_with_root_and_bitwidth(
&self.pending_deal_allocation_ids,
store,
HAMT_BIT_WIDTH,
)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending deal allocation id's")?;
let rval_allocation_id = pending_deal_allocation_ids
.delete(deal_id_key)
.with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
format!("no such deal proposal {:#?}", deal_id_key)
})?;
self.pending_deal_allocation_ids = pending_deal_allocation_ids
.flush()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush deal proposals")?;
Ok(rval_allocation_id)
}
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 = SetMultimap::from_root(store, &self.deal_ops_by_epoch)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load deals by epoch")?;
new_deals_by_epoch.iter().try_for_each(|(epoch, id)| -> Result<(), ActorError> {
deals_by_epoch
.put(*epoch, *id)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to set deal")?;
Ok(())
})?;
self.deal_ops_by_epoch = deals_by_epoch
.root()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush deals by epoch")?;
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 = SetMultimap::from_root(store, &self.deal_ops_by_epoch)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load deals by epoch")?;
new_deals_by_epoch.iter().try_for_each(|(epoch, deals)| -> Result<(), ActorError> {
deals_by_epoch
.put_many(*epoch, deals)
.with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
format!("failed to reinsert deal IDs for epoch {}", epoch)
})?;
Ok(())
})?;
self.deal_ops_by_epoch = deals_by_epoch
.root()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush deals by epoch")?;
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 = SetMultimap::from_root(store, &self.deal_ops_by_epoch)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load deals by epoch")?;
deals_by_epoch
.for_each(key, |deal_id| {
deal_ids.push(deal_id);
Ok(())
})
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to set deal state")?;
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 = SetMultimap::from_root(store, &self.deal_ops_by_epoch)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load deals by epoch")?;
epochs_to_remove.iter().try_for_each(|epoch| -> Result<(), ActorError> {
deals_by_epoch
.remove_all(*epoch)
.with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
format!("failed to delete deal ops for epoch {}", epoch)
})?;
Ok(())
})?;
self.deal_ops_by_epoch = deals_by_epoch
.root()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush deals by epoch")?;
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)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load escrow table")?;
escrow_table
.add(addr, amount)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to add escrow table")?;
self.escrow_table = escrow_table
.root()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush escrow table")?;
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)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load escrow table")?;
let locked_table = BalanceTable::from_root(store, &self.locked_table)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load locked table")?;
let min_balance = locked_table
.get(addr)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to get locked balance")?;
let ex = escrow_table
.subtract_with_minimum(addr, amount, &min_balance)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to subtract from escrow table")?;
self.escrow_table = escrow_table
.root()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush escrow table")?;
Ok(ex)
}
pub fn has_pending_deal<BS>(&self, store: &BS, key: Cid) -> Result<bool, ActorError>
where
BS: Blockstore,
{
let pending_deals = Set::from_root(store, &self.pending_proposals)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to get pending deals")?;
let rval = pending_deals
.has(&key.to_bytes())
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to get pending deals")?;
Ok(rval)
}
pub fn put_pending_deals<BS>(
&mut self,
store: &BS,
new_pending_deals: &[Cid],
) -> Result<(), ActorError>
where
BS: Blockstore,
{
let mut pending_deals = Set::from_root(store, &self.pending_proposals)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending deals")?;
new_pending_deals.iter().try_for_each(|key: &Cid| -> Result<(), ActorError> {
pending_deals
.put(key.to_bytes().into())
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to set deal")?;
Ok(())
})?;
self.pending_proposals = pending_deals
.root()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush pending deals")?;
Ok(())
}
pub fn remove_pending_deal<BS>(
&mut self,
store: &BS,
pending_deal_key: Cid,
) -> Result<Option<()>, ActorError>
where
BS: Blockstore,
{
let mut pending_deals = Set::from_root(store, &self.pending_proposals)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending deals")?;
let rval_pending_deal = pending_deals
.delete(&pending_deal_key.to_bytes())
.with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
format!("failed to delete pending proposal {}", pending_deal_key)
})?;
self.pending_proposals = pending_deals
.root()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush pending deals")?;
Ok(rval_pending_deal)
}
////////////////////////////////////////////////////////////////////////////////
// Deal state operations
////////////////////////////////////////////////////////////////////////////////
pub fn process_deal_update<BS>(
&mut self,
store: &BS,
state: &DealState,
deal: &DealProposal,
epoch: ChainEpoch,
) -> Result<(TokenAmount, bool), ActorError>
where
BS: Blockstore,
{
let ever_updated = state.last_updated_epoch != EPOCH_UNDEFINED;
let ever_slashed = state.slash_epoch != EPOCH_UNDEFINED;
// 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!(
illegal_state,
"deal updated at future epoch {}",
state.last_updated_epoch
));
}
// This would be the case that the first callback somehow triggers before it is scheduled to
// This is expected not to be able to happen
if deal.start_epoch > epoch {
return Ok((TokenAmount::zero(), false));
}
let payment_end_epoch = if ever_slashed {
if epoch < state.slash_epoch {
return Err(actor_error!(
illegal_state,
"current epoch less than deal slash epoch {}",
state.slash_epoch
));
}
if state.slash_epoch > deal.end_epoch {
return Err(actor_error!(
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 total_payment = &deal.storage_price_per_epoch * num_epochs_elapsed;
if total_payment.is_positive() {
self.transfer_balance(store, &deal.client, &deal.provider, &total_payment)?;
}
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("failed to unlock remaining client storage fee")?;
// Unlock client collateral
self.unlock_balance(
store,
&deal.client,
&deal.client_collateral,
Reason::ClientCollateral,
)
.context("failed to unlock 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, true));
}
if epoch >= deal.end_epoch {
self.process_deal_expired(store, deal, state)?;
return Ok((TokenAmount::zero(), true));
}
Ok((TokenAmount::zero(), false))
}
/// 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("failure unlocking client storage fee")?;
self.unlock_balance(store, &deal.client, &deal.client_collateral, Reason::ClientCollateral)
.context("failure 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("failed to slash balance")?;
self.unlock_balance(store, &deal.provider, &amount_remaining, Reason::ProviderCollateral)
.context("failed to unlock 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!(illegal_state, "start sector epoch undefined"));
}
self.unlock_balance(
store,
&deal.provider,
&deal.provider_collateral,
Reason::ProviderCollateral,
)
.context("failed unlocking deal provider balance")?;
self.unlock_balance(store, &deal.client, &deal.client_collateral, Reason::ClientCollateral)
.context("failed unlocking deal client balance")?;
Ok(())
}
pub fn generate_storage_deal_id(&mut self) -> DealID {
let ret = self.next_id;
self.next_id += 1;
ret
}
// 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)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load escrow table")?;
let locked_table = BalanceTable::from_root(store, &self.locked_table)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load locked table")?;
let escrow_balance = escrow_table
.get(&addr)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to get escrow balance")?;
let prev_locked = locked_table
.get(&addr)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to get locked balance")?;
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!(illegal_state, "cannot lock negative amount {}", amount));
}
let escrow_table = BalanceTable::from_root(store, &self.escrow_table)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load escrow table")?;
let mut locked_table = BalanceTable::from_root(store, &self.locked_table)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load locked table")?;
let prev_locked = locked_table
.get(addr)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to get locked balance")?;
let escrow_balance = escrow_table
.get(addr)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to get escrow balance")?;
if &prev_locked + amount > escrow_balance {
return Err(actor_error!(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)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to add locked balance")?;
self.locked_table = locked_table
.root()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush locked table")?;
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("failed to lock client funds")?;
self.maybe_lock_balance(store, &proposal.provider, &proposal.provider_collateral)
.context("failed to lock 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!(illegal_state, "unlock negative amount: {}", amount));
}
let mut locked_table = BalanceTable::from_root(store, &self.locked_table)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load locked table")?;
locked_table
.must_subtract(addr, amount)
.context_code(ExitCode::USR_ILLEGAL_STATE, "subtract from locked table failed")?;
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()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush locked table")?;
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!(illegal_state, "transfer negative amount: {}", amount));
}
let mut escrow_table = BalanceTable::from_root(store, &self.escrow_table)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load escrow table")?;
// Subtract from locked and escrow tables
escrow_table
.must_subtract(from_addr, amount)
.context_code(ExitCode::USR_ILLEGAL_STATE, "subtract from escrow")?;
self.unlock_balance(store, from_addr, amount, Reason::ClientStorageFee)
.context("subtract from locked")?;
// Add subtracted amount to the recipient
escrow_table
.add(to_addr, amount)
.context_code(ExitCode::USR_ILLEGAL_STATE, "add to escrow")?;
self.escrow_table = escrow_table
.root()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush escrow table")?;
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!(illegal_state, "negative amount to slash: {}", amount));
}
let mut escrow_table = BalanceTable::from_root(store, &self.escrow_table)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load escrow table")?;
// Subtract from locked and escrow tables
escrow_table
.must_subtract(addr, amount)
.context_code(ExitCode::USR_ILLEGAL_STATE, "subtract from escrow failed")?;
self.escrow_table = escrow_table
.root()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush escrow table")?;
self.unlock_balance(store, addr, amount, lock_reason)
}
}
fn deal_get_payment_remaining(
deal: &DealProposal,
mut slash_epoch: ChainEpoch,
) -> Result<TokenAmount, ActorError> {
if slash_epoch > deal.end_epoch {
return Err(actor_error!(
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!(
illegal_state,
"deal remaining duration negative: {}",
duration_remaining
));
}
Ok(&deal.storage_price_per_epoch * duration_remaining as u64)
}