-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
miners.go
701 lines (570 loc) · 22.7 KB
/
miners.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
package genesis
import (
"bytes"
"context"
"fmt"
"math/rand"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
cborutil "github.com/filecoin-project/go-cbor-util"
"github.com/filecoin-project/go-state-types/abi"
actorstypes "github.com/filecoin-project/go-state-types/actors"
"github.com/filecoin-project/go-state-types/big"
builtintypes "github.com/filecoin-project/go-state-types/builtin"
power11 "github.com/filecoin-project/go-state-types/builtin/v11/power"
miner14 "github.com/filecoin-project/go-state-types/builtin/v14/miner"
minertypes "github.com/filecoin-project/go-state-types/builtin/v8/miner"
markettypes "github.com/filecoin-project/go-state-types/builtin/v9/market"
miner9 "github.com/filecoin-project/go-state-types/builtin/v9/miner"
smoothing9 "github.com/filecoin-project/go-state-types/builtin/v9/util/smoothing"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/network"
builtin0 "github.com/filecoin-project/specs-actors/actors/builtin"
miner0 "github.com/filecoin-project/specs-actors/actors/builtin/miner"
power0 "github.com/filecoin-project/specs-actors/actors/builtin/power"
reward0 "github.com/filecoin-project/specs-actors/actors/builtin/reward"
power2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/power"
reward2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/reward"
power4 "github.com/filecoin-project/specs-actors/v4/actors/builtin/power"
reward4 "github.com/filecoin-project/specs-actors/v4/actors/builtin/reward"
builtin6 "github.com/filecoin-project/specs-actors/v6/actors/builtin"
runtime7 "github.com/filecoin-project/specs-actors/v7/actors/runtime"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
"github.com/filecoin-project/lotus/chain/actors/builtin/reward"
"github.com/filecoin-project/lotus/chain/actors/builtin/system"
"github.com/filecoin-project/lotus/chain/actors/policy"
"github.com/filecoin-project/lotus/chain/consensus"
lrand "github.com/filecoin-project/lotus/chain/rand"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/chain/wallet/key"
"github.com/filecoin-project/lotus/genesis"
"github.com/filecoin-project/lotus/lib/sigs"
)
func MinerAddress(genesisIndex uint64) address.Address {
maddr, err := address.NewIDAddress(MinerStart + genesisIndex)
if err != nil {
panic(err)
}
return maddr
}
type fakedSigSyscalls struct {
runtime7.Syscalls
}
func (fss *fakedSigSyscalls) VerifySignature(signature crypto.Signature, signer address.Address, plaintext []byte) error {
return nil
}
func mkFakedSigSyscalls(base vm.SyscallBuilder) vm.SyscallBuilder {
return func(ctx context.Context, rt *vm.Runtime) runtime7.Syscalls {
return &fakedSigSyscalls{
base(ctx, rt),
}
}
}
// Note: Much of this is brittle, if the methodNum / param / return changes, it will break things
func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sys vm.SyscallBuilder, sroot cid.Cid, miners []genesis.Miner, nv network.Version, synthetic bool) (cid.Cid, error) {
cst := cbor.NewCborStore(cs.StateBlockstore())
av, err := actorstypes.VersionForNetwork(nv)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get network version: %w", err)
}
csc := func(context.Context, abi.ChainEpoch, *state.StateTree) (abi.TokenAmount, error) {
return big.Zero(), nil
}
newVM := func(base cid.Cid) (vm.Interface, error) {
vmopt := &vm.VMOpts{
StateBase: base,
Epoch: 0,
Rand: &fakeRand{},
Bstore: cs.StateBlockstore(),
Actors: consensus.NewActorRegistry(),
Syscalls: mkFakedSigSyscalls(sys),
CircSupplyCalc: csc,
NetworkVersion: nv,
BaseFee: big.Zero(),
}
return vm.NewVM(ctx, vmopt)
}
genesisVm, err := newVM(sroot)
if err != nil {
return cid.Undef, fmt.Errorf("creating vm: %w", err)
}
if len(miners) == 0 {
return cid.Undef, xerrors.New("no genesis miners")
}
minerInfos := make([]struct {
maddr address.Address
presealExp abi.ChainEpoch
dealIDs []abi.DealID
sectorWeight []abi.StoragePower
}, len(miners))
maxLifetime, err := policy.GetMaxSectorExpirationExtension(nv)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get max extension: %w", err)
}
maxPeriods := maxLifetime / minertypes.WPoStProvingPeriod
rawPow, qaPow := big.NewInt(0), big.NewInt(0)
for i, m := range miners {
// Create miner through power actor
i := i
m := m
variant := miner.SealProofVariant_Standard
if synthetic {
variant = miner.SealProofVariant_Synthetic
}
spt, err := miner.SealProofTypeFromSectorSize(m.SectorSize, nv, variant)
if err != nil {
return cid.Undef, err
}
{
var params []byte
if nv <= network.Version10 {
constructorParams := &power2.CreateMinerParams{
Owner: m.Worker,
Worker: m.Worker,
Peer: []byte(m.PeerId),
SealProofType: spt,
}
params = mustEnc(constructorParams)
} else {
ppt, err := spt.RegisteredWindowPoStProofByNetworkVersion(nv)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to convert spt to wpt: %w", err)
}
constructorParams := &power11.CreateMinerParams{
Owner: m.Worker,
Worker: m.Worker,
Peer: []byte(m.PeerId),
WindowPoStProofType: ppt,
}
params = mustEnc(constructorParams)
}
rval, err := doExecValue(ctx, genesisVm, power.Address, m.Owner, m.PowerBalance, power.Methods.CreateMiner, params)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err)
}
var ma power0.CreateMinerReturn
if err := ma.UnmarshalCBOR(bytes.NewReader(rval)); err != nil {
return cid.Undef, xerrors.Errorf("unmarshaling CreateMinerReturn: %w", err)
}
expma := MinerAddress(uint64(i))
if ma.IDAddress != expma {
return cid.Undef, xerrors.Errorf("miner assigned wrong address: %s != %s", ma.IDAddress, expma)
}
minerInfos[i].maddr = ma.IDAddress
nh, err := genesisVm.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
}
nst, err := state.LoadStateTree(cst, nh)
if err != nil {
return cid.Undef, xerrors.Errorf("loading new state tree: %w", err)
}
mact, err := nst.GetActor(minerInfos[i].maddr)
if err != nil {
return cid.Undef, xerrors.Errorf("getting newly created miner actor: %w", err)
}
mst, err := miner.Load(adt.WrapStore(ctx, cst), mact)
if err != nil {
return cid.Undef, xerrors.Errorf("getting newly created miner state: %w", err)
}
pps, err := mst.GetProvingPeriodStart()
if err != nil {
return cid.Undef, xerrors.Errorf("getting newly created miner proving period start: %w", err)
}
minerInfos[i].presealExp = (maxPeriods-1)*miner0.WPoStProvingPeriod + pps - 1
}
// Add market funds
if m.MarketBalance.GreaterThan(big.Zero()) {
params := mustEnc(&minerInfos[i].maddr)
_, err := doExecValue(ctx, genesisVm, market.Address, m.Worker, m.MarketBalance, market.Methods.AddBalance, params)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create genesis miner (add balance): %w", err)
}
}
// Publish preseal deals, and calculate the QAPower
{
publish := func(params *markettypes.PublishStorageDealsParams) error {
fmt.Printf("publishing %d storage deals on miner %s with worker %s\n", len(params.Deals), params.Deals[0].Proposal.Provider, m.Worker)
ret, err := doExecValue(ctx, genesisVm, market.Address, m.Worker, big.Zero(), builtin0.MethodsMarket.PublishStorageDeals, mustEnc(params))
if err != nil {
return xerrors.Errorf("failed to create genesis miner (publish deals): %w", err)
}
retval, err := market.DecodePublishStorageDealsReturn(ret, nv)
if err != nil {
return xerrors.Errorf("failed to create genesis miner (decoding published deals): %w", err)
}
ids, err := retval.DealIDs()
if err != nil {
return xerrors.Errorf("failed to create genesis miner (getting published dealIDs): %w", err)
}
if len(ids) != len(params.Deals) {
return xerrors.Errorf("failed to create genesis miner (at least one deal was invalid on publication")
}
minerInfos[i].dealIDs = append(minerInfos[i].dealIDs, ids...)
return nil
}
params := &markettypes.PublishStorageDealsParams{}
for _, presealTmp := range m.Sectors {
preseal := presealTmp
preseal.Deal.VerifiedDeal = true
preseal.Deal.EndEpoch = minerInfos[i].presealExp
p := markettypes.ClientDealProposal{
Proposal: preseal.Deal,
ClientSignature: crypto.Signature{Type: crypto.SigTypeBLS},
}
if av >= actorstypes.Version8 {
buf, err := cborutil.Dump(&preseal.Deal)
if err != nil {
return cid.Undef, fmt.Errorf("failed to marshal proposal: %w", err)
}
sig, err := sigs.Sign(key.ActSigType(preseal.DealClientKey.Type), preseal.DealClientKey.PrivateKey, buf)
if err != nil {
return cid.Undef, fmt.Errorf("failed to sign proposal: %w", err)
}
p.ClientSignature = *sig
}
params.Deals = append(params.Deals, p)
if len(params.Deals) == cbg.MaxLength {
if err := publish(params); err != nil {
return cid.Undef, err
}
params = &markettypes.PublishStorageDealsParams{}
}
rawPow = big.Add(rawPow, big.NewInt(int64(m.SectorSize)))
sectorWeight := builtin.QAPowerForWeight(m.SectorSize, minerInfos[i].presealExp, big.Zero(), markettypes.DealWeight(&preseal.Deal))
minerInfos[i].sectorWeight = append(minerInfos[i].sectorWeight, sectorWeight)
qaPow = big.Add(qaPow, sectorWeight)
}
if len(params.Deals) > 0 {
if err := publish(params); err != nil {
return cid.Undef, err
}
}
}
}
{
nh, err := genesisVm.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
}
if err != nil {
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
}
nst, err := state.LoadStateTree(cst, nh)
if err != nil {
return cid.Undef, xerrors.Errorf("loading new state tree: %w", err)
}
pact, err := nst.GetActor(power.Address)
if err != nil {
return cid.Undef, xerrors.Errorf("getting power actor: %w", err)
}
pst, err := power.Load(adt.WrapStore(ctx, cst), pact)
if err != nil {
return cid.Undef, xerrors.Errorf("getting power state: %w", err)
}
if err = pst.SetTotalQualityAdjPower(qaPow); err != nil {
return cid.Undef, xerrors.Errorf("setting TotalQualityAdjPower in power state: %w", err)
}
if err = pst.SetTotalRawBytePower(rawPow); err != nil {
return cid.Undef, xerrors.Errorf("setting TotalRawBytePower in power state: %w", err)
}
if err = pst.SetThisEpochQualityAdjPower(qaPow); err != nil {
return cid.Undef, xerrors.Errorf("setting ThisEpochQualityAdjPower in power state: %w", err)
}
if err = pst.SetThisEpochRawBytePower(rawPow); err != nil {
return cid.Undef, xerrors.Errorf("setting ThisEpochRawBytePower in power state: %w", err)
}
pcid, err := cst.Put(ctx, pst.GetState())
if err != nil {
return cid.Undef, xerrors.Errorf("putting power state: %w", err)
}
pact.Head = pcid
if err = nst.SetActor(power.Address, pact); err != nil {
return cid.Undef, xerrors.Errorf("setting power state: %w", err)
}
rewact, err := SetupRewardActor(ctx, cs.StateBlockstore(), big.Zero(), av)
if err != nil {
return cid.Undef, xerrors.Errorf("setup reward actor: %w", err)
}
if err = nst.SetActor(reward.Address, rewact); err != nil {
return cid.Undef, xerrors.Errorf("set reward actor: %w", err)
}
nh, err = nst.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing state tree: %w", err)
}
genesisVm, err = newVM(nh)
if err != nil {
return cid.Undef, fmt.Errorf("creating new vm: %w", err)
}
}
for i, m := range miners {
// Commit sectors
{
for pi, preseal := range m.Sectors {
var paramEnc []byte
var preCommitMethodNum abi.MethodNum
if nv >= network.Version22 {
paramEnc = mustEnc(&miner.PreCommitSectorBatchParams2{
Sectors: []miner.SectorPreCommitInfo{
{
SealProof: preseal.ProofType,
SectorNumber: preseal.SectorID,
SealedCID: preseal.CommR,
SealRandEpoch: -1,
DealIDs: []abi.DealID{minerInfos[i].dealIDs[pi]},
Expiration: minerInfos[i].presealExp, // TODO: Allow setting externally!
UnsealedCid: &preseal.CommD,
},
},
})
preCommitMethodNum = builtintypes.MethodsMiner.PreCommitSectorBatch2
} else {
paramEnc = mustEnc(&minertypes.SectorPreCommitInfo{
SealProof: preseal.ProofType,
SectorNumber: preseal.SectorID,
SealedCID: preseal.CommR,
SealRandEpoch: -1,
DealIDs: []abi.DealID{minerInfos[i].dealIDs[pi]},
Expiration: minerInfos[i].presealExp, // TODO: Allow setting externally!
})
preCommitMethodNum = builtintypes.MethodsMiner.PreCommitSector
}
sectorWeight := minerInfos[i].sectorWeight[pi]
// we've added fake power for this sector above, remove it now
nh, err := genesisVm.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
}
nst, err := state.LoadStateTree(cst, nh)
if err != nil {
return cid.Undef, xerrors.Errorf("loading new state tree: %w", err)
}
pact, err := nst.GetActor(power.Address)
if err != nil {
return cid.Undef, xerrors.Errorf("getting power actor: %w", err)
}
pst, err := power.Load(adt.WrapStore(ctx, cst), pact)
if err != nil {
return cid.Undef, xerrors.Errorf("getting power state: %w", err)
}
pc, err := pst.TotalPower()
if err != nil {
return cid.Undef, xerrors.Errorf("getting total power: %w", err)
}
if err = pst.SetTotalRawBytePower(types.BigSub(pc.RawBytePower, types.NewInt(uint64(m.SectorSize)))); err != nil {
return cid.Undef, xerrors.Errorf("setting TotalRawBytePower in power state: %w", err)
}
if err = pst.SetTotalQualityAdjPower(types.BigSub(pc.QualityAdjPower, sectorWeight)); err != nil {
return cid.Undef, xerrors.Errorf("setting TotalQualityAdjPower in power state: %w", err)
}
pcid, err := cst.Put(ctx, pst.GetState())
if err != nil {
return cid.Undef, xerrors.Errorf("putting power state: %w", err)
}
pact.Head = pcid
if err = nst.SetActor(power.Address, pact); err != nil {
return cid.Undef, xerrors.Errorf("setting power state: %w", err)
}
nh, err = nst.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing state tree: %w", err)
}
genesisVm, err = newVM(nh)
if err != nil {
return cid.Undef, fmt.Errorf("creating new vm: %w", err)
}
baselinePower, rewardSmoothed, err := currentEpochBlockReward(ctx, genesisVm, minerInfos[i].maddr, av)
if err != nil {
return cid.Undef, xerrors.Errorf("getting current epoch reward: %w", err)
}
tpow, err := currentTotalPower(ctx, genesisVm, minerInfos[i].maddr)
if err != nil {
return cid.Undef, xerrors.Errorf("getting current total power: %w", err)
}
pcd := miner9.PreCommitDepositForPower(smoothing9.FilterEstimate(rewardSmoothed), smoothing9.FilterEstimate(*tpow.QualityAdjPowerSmoothed), miner9.QAPowerMax(m.SectorSize))
pledge := miner9.InitialPledgeForPower(
sectorWeight,
baselinePower,
smoothing9.FilterEstimate(rewardSmoothed),
smoothing9.FilterEstimate(*tpow.QualityAdjPowerSmoothed),
big.Zero(),
)
pledge = big.Add(pcd, pledge)
_, err = doExecValue(ctx, genesisVm, minerInfos[i].maddr, m.Worker, pledge, preCommitMethodNum, paramEnc)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to confirm presealed sectors: %w", err)
}
// Commit one-by-one, otherwise pledge math tends to explode
var paramBytes []byte
if av >= actorstypes.Version14 {
confirmParams := &miner14.InternalSectorSetupForPresealParams{
Sectors: []abi.SectorNumber{preseal.SectorID},
}
paramBytes = mustEnc(confirmParams)
} else if av >= actorstypes.Version6 {
// TODO: fixup
confirmParams := &builtin6.ConfirmSectorProofsParams{
Sectors: []abi.SectorNumber{preseal.SectorID},
}
paramBytes = mustEnc(confirmParams)
} else {
confirmParams := &builtin0.ConfirmSectorProofsParams{
Sectors: []abi.SectorNumber{preseal.SectorID},
}
paramBytes = mustEnc(confirmParams)
}
var csErr error
if nv >= network.Version23 {
_, csErr = doExecValue(ctx, genesisVm, minerInfos[i].maddr, system.Address, big.Zero(), builtintypes.MethodsMiner.InternalSectorSetupForPreseal,
paramBytes)
} else {
_, csErr = doExecValue(ctx, genesisVm, minerInfos[i].maddr, power.Address, big.Zero(), builtintypes.MethodsMiner.InternalSectorSetupForPreseal,
paramBytes)
}
if csErr != nil {
return cid.Undef, xerrors.Errorf("failed to confirm presealed sectors: %w", csErr)
}
if av >= actorstypes.Version2 {
// post v0, we need to explicitly Claim this power since ConfirmSectorProofsValid doesn't do it anymore
claimParams := &power4.UpdateClaimedPowerParams{
RawByteDelta: types.NewInt(uint64(m.SectorSize)),
QualityAdjustedDelta: sectorWeight,
}
_, err = doExecValue(ctx, genesisVm, power.Address, minerInfos[i].maddr, big.Zero(), power.Methods.UpdateClaimedPower, mustEnc(claimParams))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to confirm presealed sectors: %w", err)
}
nh, err := genesisVm.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
}
nst, err := state.LoadStateTree(cst, nh)
if err != nil {
return cid.Undef, xerrors.Errorf("loading new state tree: %w", err)
}
mact, err := nst.GetActor(minerInfos[i].maddr)
if err != nil {
return cid.Undef, xerrors.Errorf("getting miner actor: %w", err)
}
mst, err := miner.Load(adt.WrapStore(ctx, cst), mact)
if err != nil {
return cid.Undef, xerrors.Errorf("getting miner state: %w", err)
}
if err = mst.EraseAllUnproven(); err != nil {
return cid.Undef, xerrors.Errorf("failed to erase unproven sectors: %w", err)
}
mcid, err := cst.Put(ctx, mst.GetState())
if err != nil {
return cid.Undef, xerrors.Errorf("putting miner state: %w", err)
}
mact.Head = mcid
if err = nst.SetActor(minerInfos[i].maddr, mact); err != nil {
return cid.Undef, xerrors.Errorf("setting miner state: %w", err)
}
nh, err = nst.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing state tree: %w", err)
}
genesisVm, err = newVM(nh)
if err != nil {
return cid.Undef, fmt.Errorf("creating new vm: %w", err)
}
}
}
}
}
// Sanity-check total network power
nh, err := genesisVm.Flush(ctx)
if err != nil {
return cid.Undef, fmt.Errorf("flushing vm: %w", err)
}
nst, err := state.LoadStateTree(cst, nh)
if err != nil {
return cid.Undef, xerrors.Errorf("loading new state tree: %w", err)
}
pact, err := nst.GetActor(power.Address)
if err != nil {
return cid.Undef, xerrors.Errorf("getting power actor: %w", err)
}
pst, err := power.Load(adt.WrapStore(ctx, cst), pact)
if err != nil {
return cid.Undef, xerrors.Errorf("getting power state: %w", err)
}
pc, err := pst.TotalPower()
if err != nil {
return cid.Undef, xerrors.Errorf("getting total power: %w", err)
}
if !pc.RawBytePower.Equals(rawPow) {
return cid.Undef, xerrors.Errorf("TotalRawBytePower (%s) doesn't match previously calculated rawPow (%s)", pc.RawBytePower, rawPow)
}
if !pc.QualityAdjPower.Equals(qaPow) {
return cid.Undef, xerrors.Errorf("QualityAdjPower (%s) doesn't match previously calculated qaPow (%s)", pc.QualityAdjPower, qaPow)
}
// TODO: Should we re-ConstructState for the reward actor using rawPow as currRealizedPower here?
c, err := genesisVm.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
}
return c, nil
}
var _ lrand.Rand = new(fakeRand)
// TODO: copied from actors test harness, deduplicate or remove from here
type fakeRand struct{}
func (fr *fakeRand) GetChainRandomness(ctx context.Context, randEpoch abi.ChainEpoch) ([32]byte, error) {
out := make([]byte, 32)
_, _ = rand.New(rand.NewSource(int64(randEpoch * 1000))).Read(out) //nolint
return *(*[32]byte)(out), nil
}
func (fr *fakeRand) GetBeaconRandomness(ctx context.Context, randEpoch abi.ChainEpoch) ([32]byte, error) {
out := make([]byte, 32)
_, _ = rand.New(rand.NewSource(int64(randEpoch))).Read(out) //nolint
return *(*[32]byte)(out), nil
}
func currentTotalPower(ctx context.Context, vm vm.Interface, maddr address.Address) (*power0.CurrentTotalPowerReturn, error) {
pwret, err := doExecValue(ctx, vm, power.Address, maddr, big.Zero(), builtin0.MethodsPower.CurrentTotalPower, nil)
if err != nil {
return nil, err
}
var pwr power0.CurrentTotalPowerReturn
if err := pwr.UnmarshalCBOR(bytes.NewReader(pwret)); err != nil {
return nil, err
}
return &pwr, nil
}
func currentEpochBlockReward(ctx context.Context, vm vm.Interface, maddr address.Address, av actorstypes.Version) (abi.StoragePower, builtin.FilterEstimate, error) {
rwret, err := doExecValue(ctx, vm, reward.Address, maddr, big.Zero(), reward.Methods.ThisEpochReward, nil)
if err != nil {
return big.Zero(), builtin.FilterEstimate{}, err
}
// TODO: This hack should move to reward actor wrapper
switch av {
case actorstypes.Version0:
var epochReward reward0.ThisEpochRewardReturn
if err := epochReward.UnmarshalCBOR(bytes.NewReader(rwret)); err != nil {
return big.Zero(), builtin.FilterEstimate{}, err
}
return epochReward.ThisEpochBaselinePower, builtin.FilterEstimate(*epochReward.ThisEpochRewardSmoothed), nil
case actorstypes.Version2:
var epochReward reward2.ThisEpochRewardReturn
if err := epochReward.UnmarshalCBOR(bytes.NewReader(rwret)); err != nil {
return big.Zero(), builtin.FilterEstimate{}, err
}
return epochReward.ThisEpochBaselinePower, builtin.FilterEstimate(epochReward.ThisEpochRewardSmoothed), nil
}
var epochReward reward4.ThisEpochRewardReturn
if err := epochReward.UnmarshalCBOR(bytes.NewReader(rwret)); err != nil {
return big.Zero(), builtin.FilterEstimate{}, err
}
return epochReward.ThisEpochBaselinePower, builtin.FilterEstimate(epochReward.ThisEpochRewardSmoothed), nil
}