-
Notifications
You must be signed in to change notification settings - Fork 462
/
view.go
869 lines (722 loc) · 24.7 KB
/
view.go
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
package state
import (
"context"
"fmt"
"strconv"
"github.com/filecoin-project/venus/venus-shared/types"
"github.com/pkg/errors"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/dline"
"github.com/filecoin-project/go-state-types/network"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
addr "github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
vmstate "github.com/filecoin-project/venus/pkg/state/tree"
"github.com/filecoin-project/venus/pkg/util/ffiwrapper"
"github.com/filecoin-project/venus/venus-shared/actors/adt"
"github.com/filecoin-project/venus/venus-shared/actors/builtin"
"github.com/filecoin-project/venus/venus-shared/actors/builtin/account"
"github.com/filecoin-project/venus/venus-shared/actors/builtin/datacap"
notinit "github.com/filecoin-project/venus/venus-shared/actors/builtin/init"
"github.com/filecoin-project/venus/venus-shared/actors/builtin/market"
lminer "github.com/filecoin-project/venus/venus-shared/actors/builtin/miner"
paychActor "github.com/filecoin-project/venus/venus-shared/actors/builtin/paych"
"github.com/filecoin-project/venus/venus-shared/actors/builtin/power"
"github.com/filecoin-project/venus/venus-shared/actors/builtin/reward"
"github.com/filecoin-project/venus/venus-shared/actors/builtin/verifreg"
)
// Viewer builds state views from state root CIDs.
// remove tipset argument in many function of viewer
type Viewer struct {
ipldStore cbor.IpldStore
}
// NewViewer creates a new state
func NewViewer(store cbor.IpldStore) *Viewer {
return &Viewer{store}
}
// StateView returns a new state view.
func (c *Viewer) StateView(root cid.Cid) *View {
return NewView(c.ipldStore, root)
}
// View is a read-only interface to a snapshot of application-level actor state.
// This object interprets the actor state, abstracting the concrete on-chain structures so as to
// hide the complications of protocol versions.
// Exported methods on this type avoid exposing concrete state structures (which may be subject to versioning)
// where possible.
type View struct {
ipldStore cbor.IpldStore
root cid.Cid
}
// NewView creates a new state view
func NewView(store cbor.IpldStore, root cid.Cid) *View {
return &View{
ipldStore: store,
root: root,
}
}
// InitNetworkName Returns the network name from the init actor state.
func (v *View) InitNetworkName(ctx context.Context) (string, error) {
initState, err := v.LoadInitState(ctx)
if err != nil {
return "", err
}
return initState.NetworkName()
}
// InitResolveAddress Returns ID address if public key address is given.
func (v *View) InitResolveAddress(ctx context.Context, a addr.Address) (addr.Address, error) {
if a.Protocol() == addr.ID {
return a, nil
}
initState, err := v.LoadInitState(ctx)
if err != nil {
return addr.Undef, err
}
rAddr, found, err := initState.ResolveAddress(a)
if err != nil {
return addr.Undef, err
}
if !found {
return addr.Undef, fmt.Errorf("not found resolve address %v", a)
}
return rAddr, nil
}
// GetMinerWorkerRaw returns the public key type of address (`BLS`/`SECP256K1`) of an account actor identified by `addr`.
func (v *View) GetMinerWorkerRaw(ctx context.Context, maddr addr.Address) (addr.Address, error) {
minerState, err := v.LoadMinerState(ctx, maddr)
if err != nil {
return addr.Undef, err
}
minerInfo, err := minerState.Info()
if err != nil {
return addr.Undef, err
}
return v.ResolveToDeterministicAddress(ctx, minerInfo.Worker)
}
// MinerInfo returns info about the indicated miner
func (v *View) MinerInfo(ctx context.Context, maddr addr.Address, nv network.Version) (*lminer.MinerInfo, error) {
minerState, err := v.LoadMinerState(ctx, maddr)
if err != nil {
return nil, err
}
info, err := minerState.Info()
if err != nil {
return nil, err
}
return &info, nil
}
// Loads sector info from miner state.
func (v *View) MinerSectorInfo(ctx context.Context, maddr addr.Address, sectorNum abi.SectorNumber) (*types.SectorOnChainInfo, error) {
minerState, err := v.LoadMinerState(ctx, maddr)
if err != nil {
return nil, err
}
info, err := minerState.GetSector(sectorNum)
if err != nil {
return nil, err
}
return info, nil
}
// GetSectorsForWinningPoSt return sector of winning post challenge result
func (v *View) GetSectorsForWinningPoSt(ctx context.Context, nv network.Version, pv ffiwrapper.Verifier, maddr addr.Address, rand abi.PoStRandomness) ([]builtin.ExtendedSectorInfo, error) {
mas, err := v.LoadMinerState(ctx, maddr)
if err != nil {
return nil, fmt.Errorf("failed to load miner actor state: %s", err)
}
var provingSectors bitfield.BitField
if nv < network.Version7 {
allSectors, err := lminer.AllPartSectors(mas, lminer.Partition.AllSectors)
if err != nil {
return nil, fmt.Errorf("get all sectors: %v", err)
}
faultySectors, err := lminer.AllPartSectors(mas, lminer.Partition.FaultySectors)
if err != nil {
return nil, fmt.Errorf("get faulty sectors: %v", err)
}
provingSectors, err = bitfield.SubtractBitField(allSectors, faultySectors)
if err != nil {
return nil, fmt.Errorf("calc proving sectors: %v", err)
}
} else {
provingSectors, err = lminer.AllPartSectors(mas, lminer.Partition.ActiveSectors)
if err != nil {
return nil, fmt.Errorf("get active sectors sectors: %v", err)
}
}
numProvSect, err := provingSectors.Count()
if err != nil {
return nil, fmt.Errorf("failed to count bits: %s", err)
}
// TODO(review): is this right? feels fishy to me
if numProvSect == 0 {
return nil, nil
}
info, err := mas.Info()
if err != nil {
return nil, fmt.Errorf("getting miner info: %s", err)
}
mid, err := addr.IDFromAddress(maddr)
if err != nil {
return nil, fmt.Errorf("getting miner ID: %s", err)
}
proofType, err := lminer.WinningPoStProofTypeFromWindowPoStProofType(nv, info.WindowPoStProofType)
if err != nil {
return nil, fmt.Errorf("determining winning post proof type: %v", err)
}
ids, err := pv.GenerateWinningPoStSectorChallenge(ctx, proofType, abi.ActorID(mid), rand, numProvSect)
if err != nil {
return nil, fmt.Errorf("generating winning post challenges: %s", err)
}
iter, err := provingSectors.BitIterator()
if err != nil {
return nil, fmt.Errorf("iterating over proving sectors: %s", err)
}
// Select winning sectors by _index_ in the all-sectors bitfield.
selectedSectors := bitfield.New()
prev := uint64(0)
for _, n := range ids {
sno, err := iter.Nth(n - prev)
if err != nil {
return nil, fmt.Errorf("iterating over proving sectors: %s", err)
}
selectedSectors.Set(sno)
prev = n
}
sectors, err := mas.LoadSectors(&selectedSectors)
if err != nil {
return nil, fmt.Errorf("loading proving sectors: %s", err)
}
out := make([]builtin.ExtendedSectorInfo, len(sectors))
for i, sinfo := range sectors {
out[i] = builtin.ExtendedSectorInfo{
SealProof: sinfo.SealProof,
SectorNumber: sinfo.SectorNumber,
SealedCID: sinfo.SealedCID,
SectorKey: sinfo.SectorKeyCID,
}
}
return out, nil
}
// StateSectorPreCommitInfo returns the PreCommit info for the specified miner's sector
func (v *View) SectorPreCommitInfo(ctx context.Context, maddr addr.Address, sid abi.SectorNumber) (*types.SectorPreCommitOnChainInfo, error) {
mas, err := v.LoadMinerState(ctx, maddr)
if err != nil {
return nil, fmt.Errorf("(get sset) failed to load miner actor: %v", err)
}
return mas.GetPrecommittedSector(sid)
}
// StateSectorPartition finds deadline/partition with the specified sector
func (v *View) StateSectorPartition(ctx context.Context, maddr addr.Address, sectorNumber abi.SectorNumber) (*lminer.SectorLocation, error) {
mas, err := v.LoadMinerState(ctx, maddr)
if err != nil {
return nil, fmt.Errorf("(get sset) failed to load miner actor: %v", err)
}
return mas.FindSector(sectorNumber)
}
// MinerDeadlineInfo returns information relevant to the current proving deadline
func (v *View) MinerDeadlineInfo(ctx context.Context, maddr addr.Address, epoch abi.ChainEpoch) (index uint64, open, close, challenge abi.ChainEpoch, _ error) {
minerState, err := v.LoadMinerState(ctx, maddr)
if err != nil {
return 0, 0, 0, 0, err
}
deadlineInfo, err := minerState.DeadlineInfo(epoch)
if err != nil {
return 0, 0, 0, 0, err
}
return deadlineInfo.Index, deadlineInfo.Open, deadlineInfo.Close, deadlineInfo.Challenge, nil
}
// MinerExists Returns true iff the miner exists.
func (v *View) MinerExists(ctx context.Context, maddr addr.Address) (bool, error) {
_, err := v.LoadMinerState(ctx, maddr)
if err == nil {
return true, nil
}
if err == types.ErrActorNotFound {
return false, nil
}
return false, err
}
// MinerGetPrecommittedSector Looks up info for a miners precommitted sector.
// NOTE: exposes on-chain structures directly for storage FSM API.
func (v *View) MinerGetPrecommittedSector(ctx context.Context, maddr addr.Address, sectorNum abi.SectorNumber) (*types.SectorPreCommitOnChainInfo, bool, error) {
minerState, err := v.LoadMinerState(ctx, maddr)
if err != nil {
return nil, false, err
}
info, err := minerState.GetPrecommittedSector(sectorNum)
if err != nil {
return nil, false, err
}
return info, true, nil
}
// MarketEscrowBalance looks up a token amount in the escrow table for the given address
func (v *View) MarketEscrowBalance(ctx context.Context, addr addr.Address) (found bool, amount abi.TokenAmount, err error) {
marketState, err := v.LoadMarketState(ctx)
if err != nil {
return false, abi.NewTokenAmount(0), err
}
state, err := marketState.EscrowTable()
if err != nil {
return false, abi.NewTokenAmount(0), err
}
amount, err = state.Get(addr)
if err != nil {
return false, abi.NewTokenAmount(0), err
}
return true, amount, nil
}
// NOTE: exposes on-chain structures directly for storage FSM interface.
func (v *View) MarketDealProposal(ctx context.Context, dealID abi.DealID) (market.DealProposal, error) {
marketState, err := v.LoadMarketState(ctx)
if err != nil {
return market.DealProposal{}, err
}
proposals, err := marketState.Proposals()
if err != nil {
return market.DealProposal{}, err
}
// map deals to pieceInfo
proposal, bFound, err := proposals.Get(dealID)
if err != nil {
return market.DealProposal{}, err
}
if !bFound {
return market.DealProposal{}, fmt.Errorf("deal %d not found", dealID)
}
return *proposal, nil
}
// NOTE: exposes on-chain structures directly for storage FSM and market module interfaces.
func (v *View) MarketDealState(ctx context.Context, dealID abi.DealID) (market.DealState, bool, error) {
marketState, err := v.LoadMarketState(ctx)
if err != nil {
return nil, false, err
}
deals, err := marketState.States()
if err != nil {
return nil, false, err
}
return deals.Get(dealID)
}
// NOTE: exposes on-chain structures directly for market interface.
// The callback receives a pointer to a transient object; take a copy or drop the reference outside the callback.
func (v *View) MarketDealStatesForEach(ctx context.Context, f func(id abi.DealID, state *market.DealState) error) error {
marketState, err := v.LoadMarketState(ctx)
if err != nil {
return err
}
deals, err := marketState.States()
if err != nil {
return err
}
ff := func(id abi.DealID, ds market.DealState) error {
return f(id, &ds)
}
return deals.ForEach(ff)
}
// StateVerifiedClientStatus returns the data cap for the given address.
// Returns nil if there is no entry in the data cap table for the
// address.
func (v *View) StateVerifiedClientStatus(ctx context.Context, addr addr.Address) (abi.StoragePower, error) {
act, err := v.loadActor(ctx, verifreg.Address)
if err != nil {
return abi.NewStoragePower(0), err
}
state, err := verifreg.Load(adt.WrapStore(ctx, v.ipldStore), act)
if err != nil {
return abi.NewStoragePower(0), err
}
found, storagePower, err := state.VerifiedClientDataCap(addr)
if err != nil {
return abi.NewStoragePower(0), err
}
if !found {
return abi.NewStoragePower(0), errors.New("address not found")
}
return storagePower, nil
}
// StateMarketStorageDeal returns information about the indicated deal
func (v *View) StateMarketStorageDeal(ctx context.Context, dealID abi.DealID) (*types.MarketDeal, error) {
state, err := v.LoadMarketState(ctx)
if err != nil {
return nil, err
}
dealProposals, err := state.Proposals()
if err != nil {
return nil, err
}
dealProposal, found, err := dealProposals.Get(dealID)
if err != nil {
return nil, err
}
if !found {
return nil, errors.New("deal proposal not found")
}
dealStates, err := state.States()
if err != nil {
return nil, err
}
dealState, found, err := dealStates.Get(dealID)
if err != nil {
return nil, err
}
if !found {
return nil, errors.New("deal state not found")
}
return &types.MarketDeal{
Proposal: *dealProposal,
State: types.MakeDealState(dealState),
}, nil
}
// Returns the storage power actor's values for network total power.
func (v *View) PowerNetworkTotal(ctx context.Context) (*NetworkPower, error) {
st, err := v.LoadPowerActor(ctx)
if err != nil {
return nil, err
}
tp, err := st.TotalPower()
if err != nil {
return nil, err
}
minPowerMinerCount, minerCount, err := st.MinerCounts()
if err != nil {
return nil, err
}
return &NetworkPower{
RawBytePower: tp.RawBytePower,
QualityAdjustedPower: tp.QualityAdjPower,
MinerCount: int64(minerCount),
MinPowerMinerCount: int64(minPowerMinerCount),
}, nil
}
// Returns the power of a miner's committed sectors.
func (v *View) MinerClaimedPower(ctx context.Context, miner addr.Address) (raw, qa abi.StoragePower, err error) {
st, err := v.LoadPowerActor(ctx)
if err != nil {
return big.Zero(), big.Zero(), err
}
p, found, err := st.MinerPower(miner)
if err != nil {
return big.Zero(), big.Zero(), err
}
if !found {
return big.Zero(), big.Zero(), errors.New("miner not found")
}
return p.RawBytePower, p.QualityAdjPower, nil
}
// MinerNominalPowerMeetsConsensusMinimum return whether miner meet consensus minmum power
func (v *View) MinerNominalPowerMeetsConsensusMinimum(ctx context.Context, addr addr.Address) (bool, error) {
st, err := v.LoadPowerActor(ctx)
if err != nil {
return false, err
}
return st.MinerNominalPowerMeetsConsensusMinimum(addr)
}
// PaychActorParties returns the From and To addresses for the given payment channel
func (v *View) PaychActorParties(ctx context.Context, paychAddr addr.Address) (from, to addr.Address, err error) {
a, err := v.loadActor(ctx, paychAddr)
if err != nil {
return addr.Undef, addr.Undef, err
}
state, err := paychActor.Load(adt.WrapStore(ctx, v.ipldStore), a)
if err != nil {
return addr.Undef, addr.Undef, err
}
from, err = state.From()
if err != nil {
return addr.Undef, addr.Undef, err
}
to, err = state.To()
if err != nil {
return addr.Undef, addr.Undef, err
}
return from, to, nil
}
// StateMinerProvingDeadline calculates the deadline at some epoch for a proving period
// and returns the deadline-related calculations.
func (v *View) StateMinerProvingDeadline(ctx context.Context, addr addr.Address, ts *types.TipSet) (*dline.Info, error) {
mas, err := v.LoadMinerState(ctx, addr)
if err != nil {
return nil, errors.WithMessage(err, "failed to get proving dealline")
}
height := ts.Height()
di, err := mas.DeadlineInfo(height)
if err != nil {
return nil, fmt.Errorf("failed to get deadline info: %v", err)
}
return di.NextNotElapsed(), nil
}
// StateSectorExpiration returns epoch at which given sector will expire
func (v *View) StateSectorExpiration(ctx context.Context, maddr addr.Address, sectorNumber abi.SectorNumber, key types.TipSetKey) (*lminer.SectorExpiration, error) {
mas, err := v.LoadMinerState(ctx, maddr)
if err != nil {
return nil, err
}
return mas.GetSectorExpiration(sectorNumber)
}
// StateMinerAvailableBalance returns the portion of a miner's balance that can be withdrawn or spent
func (v *View) StateMinerAvailableBalance(ctx context.Context, maddr addr.Address, ts *types.TipSet) (big.Int, error) {
resolvedAddr, err := v.InitResolveAddress(ctx, maddr)
if err != nil {
return big.Int{}, err
}
actor, err := v.loadActor(ctx, resolvedAddr)
if err != nil {
return big.Int{}, err
}
mas, err := lminer.Load(adt.WrapStore(context.TODO(), v.ipldStore), actor)
if err != nil {
return big.Int{}, fmt.Errorf("failed to load miner actor state: %v", err)
}
height := ts.Height()
vested, err := mas.VestedFunds(height)
if err != nil {
return big.Int{}, err
}
abal, err := mas.AvailableBalance(actor.Balance)
if err != nil {
return big.Int{}, err
}
return big.Add(abal, vested), nil
}
// StateListMiners returns the addresses of every miner that has claimed power in the Power Actor
func (v *View) StateListMiners(ctx context.Context, tsk types.TipSetKey) ([]addr.Address, error) {
powState, err := v.LoadPowerActor(ctx)
if err != nil {
return nil, fmt.Errorf("failed to load power actor state: %v", err)
}
return powState.ListAllMiners()
}
// StateMinerPower returns the power of the indicated miner
func (v *View) StateMinerPower(ctx context.Context, maddr addr.Address, tsk types.TipSetKey) (power.Claim, power.Claim, bool, error) {
pas, err := v.LoadPowerActor(ctx)
if err != nil {
return power.Claim{}, power.Claim{}, false, fmt.Errorf("(get sset) failed to load power actor state: %v", err)
}
tpow, err := pas.TotalPower()
if err != nil {
return power.Claim{}, power.Claim{}, false, err
}
var mpow power.Claim
var minpow bool
if maddr != addr.Undef {
var found bool
mpow, found, err = pas.MinerPower(maddr)
if err != nil || !found {
// TODO: return an error when not found?
return power.Claim{}, tpow, false, err
}
minpow, err = pas.MinerNominalPowerMeetsConsensusMinimum(maddr)
if err != nil {
return power.Claim{}, power.Claim{}, false, err
}
}
return mpow, tpow, minpow, nil
}
// StateMarketDeals returns information about every deal in the Storage Market
func (v *View) StateMarketDeals(ctx context.Context, tsk types.TipSetKey) (map[string]*types.MarketDeal, error) {
out := map[string]*types.MarketDeal{}
state, err := v.LoadMarketState(ctx)
if err != nil {
return nil, err
}
da, err := state.Proposals()
if err != nil {
return nil, err
}
sa, err := state.States()
if err != nil {
return nil, err
}
if err := da.ForEach(func(dealID abi.DealID, d market.DealProposal) error {
s, found, err := sa.Get(dealID)
if err != nil {
return fmt.Errorf("failed to get state for deal in proposals array: %v", err)
} else if !found {
s = market.EmptyDealState()
}
out[strconv.FormatInt(int64(dealID), 10)] = &types.MarketDeal{
Proposal: d,
State: types.MakeDealState(s),
}
return nil
}); err != nil {
return nil, err
}
return out, nil
}
// StateMinerActiveSectors returns info about sectors that a given miner is actively proving.
func (v *View) StateMinerActiveSectors(ctx context.Context, maddr addr.Address, tsk types.TipSetKey) ([]*types.SectorOnChainInfo, error) {
mas, err := v.LoadMinerState(ctx, maddr)
if err != nil {
return nil, fmt.Errorf("failed to load miner actor state: %v", err)
}
activeSectors, err := lminer.AllPartSectors(mas, lminer.Partition.ActiveSectors)
if err != nil {
return nil, fmt.Errorf("merge partition active sets: %v", err)
}
return mas.LoadSectors(&activeSectors)
}
// GetFilLocked return all locked fil amount
func (v *View) GetFilLocked(ctx context.Context, st vmstate.Tree) (abi.TokenAmount, error) {
filMarketLocked, err := getFilMarketLocked(ctx, v.ipldStore, st)
if err != nil {
return big.Zero(), fmt.Errorf("failed to get filMarketLocked: %v", err)
}
powerState, err := v.LoadPowerActor(ctx)
if err != nil {
return big.Zero(), fmt.Errorf("failed to get filPowerLocked: %v", err)
}
filPowerLocked, err := powerState.TotalLocked()
if err != nil {
return big.Zero(), fmt.Errorf("failed to get filPowerLocked: %v", err)
}
return big.Add(filMarketLocked, filPowerLocked), nil
}
// LoadActor load actor from tree
func (v *View) LoadActor(ctx context.Context, address addr.Address) (*types.Actor, error) {
return v.loadActor(ctx, address)
}
// ResolveToDeterministicAddress is similar to `vm.ResolveToDeterministicAddress` but does not allow `Actor` type of addresses.
// Uses the `TipSet` `ts` to generate the VM state.
// todo: use pkg/vm/vm.go.ResolveToDeterministicAddress, avoid repetitive implementations
func (v *View) ResolveToDeterministicAddress(ctx context.Context, address addr.Address) (addr.Address, error) {
if address.Protocol() == addr.BLS || address.Protocol() == addr.SECP256K1 || address.Protocol() == addr.Delegated {
return address, nil
}
tree, err := vmstate.LoadState(ctx, v.ipldStore, v.root)
if err != nil {
return addr.Undef, err
}
act, found, err := tree.GetActor(ctx, address)
if err != nil {
return addr.Undef, err
}
if !found {
return addr.Undef, errors.Wrapf(types.ErrActorNotFound, "address is :%s", address)
}
if tree.Version() >= vmstate.StateTreeVersion5 {
if act.Address != nil {
// If there _is_ an f4 address, return it as "key" address
return *act.Address, nil
}
}
aast, err := account.Load(adt.WrapStore(context.TODO(), v.ipldStore), act)
if err != nil {
return addr.Undef, fmt.Errorf("failed to get account actor state for %s: %v", address, err)
}
return aast.PubkeyAddress()
}
func (v *View) LoadInitState(ctx context.Context) (notinit.State, error) {
actr, err := v.loadActor(ctx, notinit.Address)
if err != nil {
return nil, err
}
return notinit.Load(adt.WrapStore(ctx, v.ipldStore), actr)
}
// LoadPaychState get pay channel state for actor
func (v *View) LoadPaychState(ctx context.Context, actor *types.Actor) (paychActor.State, error) {
return paychActor.Load(adt.WrapStore(context.TODO(), v.ipldStore), actor)
}
// LoadMinerState return miner state
func (v *View) LoadMinerState(ctx context.Context, maddr addr.Address) (lminer.State, error) {
resolvedAddr, err := v.InitResolveAddress(ctx, maddr)
if err != nil {
return nil, err
}
actr, err := v.loadActor(ctx, resolvedAddr)
if err != nil {
return nil, err
}
return lminer.Load(adt.WrapStore(context.TODO(), v.ipldStore), actr)
}
func (v *View) LoadPowerActor(ctx context.Context) (power.State, error) {
actr, err := v.loadActor(ctx, power.Address)
if err != nil {
return nil, err
}
return power.Load(adt.WrapStore(ctx, v.ipldStore), actr)
}
func (v *View) LoadVerifregActor(ctx context.Context) (verifreg.State, error) {
actr, err := v.loadActor(ctx, verifreg.Address)
if err != nil {
return nil, err
}
return verifreg.Load(adt.WrapStore(ctx, v.ipldStore), actr)
}
// nolint
func (v *View) LoadRewardState(ctx context.Context) (reward.State, error) {
actr, err := v.loadActor(ctx, reward.Address)
if err != nil {
return nil, err
}
return reward.Load(adt.WrapStore(ctx, v.ipldStore), actr)
}
// nolint
func (v *View) LoadPowerState(ctx context.Context) (power.State, error) {
actr, err := v.loadActor(ctx, power.Address)
if err != nil {
return nil, err
}
return power.Load(adt.WrapStore(ctx, v.ipldStore), actr)
}
func (v *View) LoadMarketState(ctx context.Context) (market.State, error) {
actr, err := v.loadActor(ctx, market.Address)
if err != nil {
return nil, err
}
return market.Load(adt.WrapStore(ctx, v.ipldStore), actr)
}
func (v *View) LoadDatacapState(ctx context.Context) (datacap.State, error) {
actr, err := v.loadActor(ctx, datacap.Address)
if err != nil {
return nil, err
}
return datacap.Load(adt.WrapStore(ctx, v.ipldStore), actr)
}
// nolint
func (v *View) LoadAccountState(ctx context.Context, a addr.Address) (account.State, error) {
resolvedAddr, err := v.InitResolveAddress(ctx, a)
if err != nil {
return nil, err
}
actr, err := v.loadActor(ctx, resolvedAddr)
if err != nil {
return nil, err
}
return account.Load(adt.WrapStore(context.TODO(), v.ipldStore), actr)
}
// loadActor load actor of address in db
func (v *View) loadActor(ctx context.Context, address addr.Address) (*types.Actor, error) {
tree, err := vmstate.LoadState(ctx, v.ipldStore, v.root)
if err != nil {
return nil, err
}
actor, found, err := tree.GetActor(ctx, address)
if err != nil {
return nil, err
}
if !found {
return nil, errors.Wrapf(types.ErrActorNotFound, "address is :%s", address)
}
return actor, err
}
func getFilMarketLocked(ctx context.Context, ipldStore cbor.IpldStore, st vmstate.Tree) (abi.TokenAmount, error) {
mactor, found, err := st.GetActor(ctx, market.Address)
if !found || err != nil {
return big.Zero(), fmt.Errorf("failed to load market actor: %v", err)
}
mst, err := market.Load(adt.WrapStore(ctx, ipldStore), mactor)
if err != nil {
return big.Zero(), fmt.Errorf("failed to load market state: %v", err)
}
return mst.TotalLocked()
}
// LookupID retrieves the ID address of the given address
func (v *View) LookupID(ctx context.Context, address addr.Address) (addr.Address, error) {
sTree, err := vmstate.LoadState(ctx, v.ipldStore, v.root)
if err != nil {
return addr.Address{}, err
}
return sTree.LookupID(address)
}