This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
commit_post_test.go
987 lines (847 loc) · 40.9 KB
/
commit_post_test.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
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
package test
import (
"context"
"fmt"
"strings"
"testing"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/dline"
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/text/language"
"golang.org/x/text/message"
"github.com/filecoin-project/specs-actors/v8/actors/builtin"
"github.com/filecoin-project/specs-actors/v8/actors/builtin/miner"
"github.com/filecoin-project/specs-actors/v8/actors/builtin/power"
"github.com/filecoin-project/specs-actors/v8/actors/runtime/proof"
"github.com/filecoin-project/specs-actors/v8/actors/states"
"github.com/filecoin-project/specs-actors/v8/support/ipld"
"github.com/filecoin-project/specs-actors/v8/support/vm"
)
func TestCommitPoStFlow(t *testing.T) {
ctx := context.Background()
v := vm.NewVMWithSingletons(ctx, t, ipld.NewBlockStoreInMemory())
sealProof := abi.RegisteredSealProof_StackedDrg32GiBV1_1
wPoStProof, err := sealProof.RegisteredWindowPoStProof()
require.NoError(t, err)
sectorSize, err := sealProof.SectorSize()
require.NoError(t, err)
addrs := vm.CreateAccounts(ctx, t, v, 1, big.Mul(big.NewInt(10_000), builtin.TokenPrecision), 93837778)
owner, worker := addrs[0], addrs[0]
minerAddrs := createMiner(t, v, owner, worker, wPoStProof, big.Mul(big.NewInt(10_000), vm.FIL))
// advance vm so we can have seal randomness epoch in the past
v, err = v.WithEpoch(200)
require.NoError(t, err)
sectorNumber := abi.SectorNumber(100)
precommits := preCommitSectors(t, v, 1, 1, worker, minerAddrs.IDAddress, sealProof, sectorNumber, true, -1)
balances := vm.GetMinerBalances(t, v, minerAddrs.IDAddress)
assert.True(t, balances.PreCommitDeposit.GreaterThan(big.Zero()))
proveTime := v.GetEpoch() + miner.MaxProveCommitDuration[sealProof]
v, _ = vm.AdvanceByDeadlineTillEpoch(t, v, minerAddrs.IDAddress, proveTime)
//
// overdue precommit
//
t.Run("missed prove commit results in precommit expiry and cleanup", func(t *testing.T) {
// advance time to precommit clean up epoch
cleanUpTime := proveTime + miner.ExpiredPreCommitCleanUpDelay
v, dlInfo := vm.AdvanceByDeadlineTillEpoch(t, v, minerAddrs.IDAddress, cleanUpTime)
// advanced one more deadline so precommit clean up is reached
tv, err := v.WithEpoch(dlInfo.Close)
require.NoError(t, err)
// run cron which should clean up precommit
vm.ApplyOk(t, tv, builtin.SystemActorAddr, builtin.CronActorAddr, big.Zero(), builtin.MethodsCron.EpochTick, nil)
vm.ExpectInvocation{
To: builtin.CronActorAddr,
Method: builtin.MethodsCron.EpochTick,
SubInvocations: []vm.ExpectInvocation{
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.CronTick, SubInvocations: []vm.ExpectInvocation{
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.ThisEpochReward},
{To: minerAddrs.IDAddress, Method: builtin.MethodsMiner.OnDeferredCronEvent, SubInvocations: []vm.ExpectInvocation{
// The call to burnt funds indicates the overdue precommit has been penalized
{To: builtin.BurntFundsActorAddr, Method: builtin.MethodSend, Value: vm.ExpectAttoFil(precommits[0].PreCommitDeposit)},
// No re-enrollment of cron because burning of PCD discontinues miner cron scheduling
}},
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.UpdateNetworkKPI},
}},
{To: builtin.StorageMarketActorAddr, Method: builtin.MethodsMarket.CronTick},
},
}.Matches(t, tv.LastInvocation())
// precommit deposit has been reset
balances := vm.GetMinerBalances(t, tv, minerAddrs.IDAddress)
assert.Equal(t, big.Zero(), balances.InitialPledge)
assert.Equal(t, big.Zero(), balances.PreCommitDeposit)
// no power is added
networkStats := vm.GetNetworkStats(t, tv)
assert.Equal(t, big.Zero(), networkStats.TotalBytesCommitted)
assert.Equal(t, big.Zero(), networkStats.TotalPledgeCollateral)
assert.Equal(t, big.Zero(), networkStats.TotalRawBytePower)
assert.Equal(t, big.Zero(), networkStats.TotalQualityAdjPower)
})
//
// prove and verify
//
// Prove commit sector after max seal duration
v, err = v.WithEpoch(proveTime)
require.NoError(t, err)
proveCommitParams := miner.ProveCommitSectorParams{
SectorNumber: sectorNumber,
}
vm.ApplyOk(t, v, addrs[0], minerAddrs.RobustAddress, big.Zero(), builtin.MethodsMiner.ProveCommitSector, &proveCommitParams)
vm.ExpectInvocation{
To: minerAddrs.IDAddress,
Method: builtin.MethodsMiner.ProveCommitSector,
Params: vm.ExpectObject(&proveCommitParams),
SubInvocations: []vm.ExpectInvocation{
{To: builtin.StorageMarketActorAddr, Method: builtin.MethodsMarket.ComputeDataCommitment},
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.SubmitPoRepForBulkVerify},
},
}.Matches(t, v.LastInvocation())
// In the same epoch, trigger cron to validate prove commit
vm.ApplyOk(t, v, builtin.SystemActorAddr, builtin.CronActorAddr, big.Zero(), builtin.MethodsCron.EpochTick, nil)
vm.ExpectInvocation{
To: builtin.CronActorAddr,
Method: builtin.MethodsCron.EpochTick,
SubInvocations: []vm.ExpectInvocation{
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.CronTick, SubInvocations: []vm.ExpectInvocation{
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.ThisEpochReward},
// expect confirm sector proofs valid because we prove committed,
// but not an on deferred cron event because this is not a deadline boundary
{To: minerAddrs.IDAddress, Method: builtin.MethodsMiner.ConfirmSectorProofsValid, SubInvocations: []vm.ExpectInvocation{
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.UpdatePledgeTotal},
}},
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.UpdateNetworkKPI},
}},
{To: builtin.StorageMarketActorAddr, Method: builtin.MethodsMarket.CronTick},
},
}.Matches(t, v.Invocations()[1])
// precommit deposit is released, ipr is added
balances = vm.GetMinerBalances(t, v, minerAddrs.IDAddress)
assert.True(t, balances.InitialPledge.GreaterThan(big.Zero()))
assert.Equal(t, big.Zero(), balances.PreCommitDeposit)
// power is unproven so network stats are unchanged
networkStats := vm.GetNetworkStats(t, v)
assert.Equal(t, big.Zero(), networkStats.TotalBytesCommitted)
assert.True(t, networkStats.TotalPledgeCollateral.GreaterThan(big.Zero()))
//
// Submit PoSt
//
// advance to proving period
dlInfo, pIdx, v := vm.AdvanceTillProvingDeadline(t, v, minerAddrs.IDAddress, sectorNumber)
var minerState miner.State
err = v.GetState(minerAddrs.IDAddress, &minerState)
require.NoError(t, err)
sector, found, err := minerState.GetSector(v.Store(), sectorNumber)
require.NoError(t, err)
require.True(t, found)
t.Run("submit PoSt succeeds", func(t *testing.T) {
tv, err := v.WithEpoch(v.GetEpoch())
require.NoError(t, err)
partitions := []miner.PoStPartition{{
Index: pIdx,
Skipped: bitfield.New(),
}}
sectorPower := miner.PowerForSector(sectorSize, sector)
submitWindowPoSt(t, tv, worker, minerAddrs.IDAddress, dlInfo, partitions, sectorPower)
// miner still has initial pledge
balances = vm.GetMinerBalances(t, tv, minerAddrs.IDAddress)
assert.True(t, balances.InitialPledge.GreaterThan(big.Zero()))
// committed bytes are added (miner would have gained power if minimum requirement were met)
networkStats := vm.GetNetworkStats(t, tv)
assert.Equal(t, big.NewInt(int64(sectorSize)), networkStats.TotalBytesCommitted)
assert.True(t, networkStats.TotalPledgeCollateral.GreaterThan(big.Zero()))
// Trigger cron to keep reward accounting correct
vm.ApplyOk(t, tv, builtin.SystemActorAddr, builtin.CronActorAddr, big.Zero(), builtin.MethodsCron.EpochTick, nil)
stateTree, err := tv.GetStateTree()
require.NoError(t, err)
totalBalance, err := tv.GetTotalActorBalance()
require.NoError(t, err)
acc, err := states.CheckStateInvariants(stateTree, totalBalance, tv.GetEpoch())
require.NoError(t, err)
assert.True(t, acc.IsEmpty(), strings.Join(acc.Messages(), "\n"))
})
t.Run("skip sector", func(t *testing.T) {
tv, err := v.WithEpoch(v.GetEpoch())
require.NoError(t, err)
// Submit PoSt
submitParams := miner.SubmitWindowedPoStParams{
Deadline: dlInfo.Index,
Partitions: []miner.PoStPartition{{
Index: pIdx,
Skipped: bitfield.NewFromSet([]uint64{uint64(sectorNumber)}),
}},
Proofs: []proof.PoStProof{{
PoStProof: abi.RegisteredPoStProof_StackedDrgWindow32GiBV1,
}},
ChainCommitEpoch: dlInfo.Challenge,
ChainCommitRand: []byte(vm.RandString),
}
// PoSt is rejected for skipping all sectors.
result := vm.RequireApplyMessage(t, tv, addrs[0], minerAddrs.RobustAddress, big.Zero(), builtin.MethodsMiner.SubmitWindowedPoSt, &submitParams, t.Name())
assert.Equal(t, exitcode.ErrIllegalArgument, result.Code)
vm.ExpectInvocation{
To: minerAddrs.IDAddress,
Method: builtin.MethodsMiner.SubmitWindowedPoSt,
Params: vm.ExpectObject(&submitParams),
Exitcode: exitcode.ErrIllegalArgument,
}.Matches(t, tv.LastInvocation())
// miner still has initial pledge
balances = vm.GetMinerBalances(t, v, minerAddrs.IDAddress)
assert.True(t, balances.InitialPledge.GreaterThan(big.Zero()))
// network power is unchanged
networkStats := vm.GetNetworkStats(t, tv)
assert.Equal(t, big.Zero(), networkStats.TotalBytesCommitted)
assert.True(t, networkStats.TotalPledgeCollateral.GreaterThan(big.Zero()))
})
t.Run("missed first PoSt deadline", func(t *testing.T) {
// move to proving period end
tv, err := v.WithEpoch(dlInfo.Last())
require.NoError(t, err)
// Run cron to detect missing PoSt
vm.ApplyOk(t, tv, builtin.SystemActorAddr, builtin.CronActorAddr, big.Zero(), builtin.MethodsCron.EpochTick, nil)
vm.ExpectInvocation{
To: builtin.CronActorAddr,
Method: builtin.MethodsCron.EpochTick,
SubInvocations: []vm.ExpectInvocation{
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.CronTick, SubInvocations: []vm.ExpectInvocation{
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.ThisEpochReward},
{To: minerAddrs.IDAddress, Method: builtin.MethodsMiner.OnDeferredCronEvent, SubInvocations: []vm.ExpectInvocation{
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.EnrollCronEvent},
}},
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.UpdateNetworkKPI},
}},
{To: builtin.StorageMarketActorAddr, Method: builtin.MethodsMarket.CronTick},
},
}.Matches(t, tv.LastInvocation())
// network power is unchanged
networkStats := vm.GetNetworkStats(t, tv)
assert.Equal(t, big.Zero(), networkStats.TotalBytesCommitted)
assert.True(t, networkStats.TotalPledgeCollateral.GreaterThan(big.Zero()))
})
}
func TestMeasurePreCommitGas(t *testing.T) {
// Number of sectors to pre-commit in each test.
// We don't expect miners to have very large collections of outstanding pre-commits, but should
// model at least the number for a maximally-aggregated prove-commit.
// Smaller numbers show a much greater improvement to batching.
sectorCount := 1000
ctx := context.Background()
blkStore := ipld.NewBlockStoreInMemory()
metrics := ipld.NewMetricsBlockStore(blkStore)
v := vm.NewVMWithSingletons(ctx, t, metrics)
v.SetStatsSource(metrics)
sealProof := abi.RegisteredSealProof_StackedDrg32GiBV1_1
wPoStProof, err := sealProof.RegisteredWindowPoStProof()
require.NoError(t, err)
addrs := vm.CreateAccounts(ctx, t, v, 1, big.Mul(big.NewInt(100_000), builtin.TokenPrecision), 93837778)
owner, worker := addrs[0], addrs[0]
minerAddrs := createMiner(t, v, owner, worker, wPoStProof, big.Mul(big.NewInt(10_000), vm.FIL))
// Advance vm so we can have seal randomness epoch in the past
v, err = v.WithEpoch(abi.ChainEpoch(200))
require.NoError(t, err)
// Note that the miner's pre-committed sector HAMT will increase in size and depth over the course of this
// test, making later operations slightly more expensive.
// The expected state of this structure is to have not many sectors in it.
statsKey := vm.MethodKey{Code: builtin.StorageMinerActorCodeID, Method: builtin.MethodsMiner.PreCommitSectorBatch}
p := message.NewPrinter(language.English)
_, _ = p.Printf("Sector count\t%d\n", sectorCount)
_, _ = p.Printf("Batch size\tIPLD cost\tCalls cost\tTotal per sector\tTotal per batch\n")
for batchSize := 1; batchSize <= miner.PreCommitSectorBatchMaxSize; batchSize *= 2 {
// Clone the VM so each batch get the same starting state.
tv, err := v.WithEpoch(v.GetEpoch())
require.NoError(t, err)
firstSectorNo := abi.SectorNumber(sectorCount * batchSize)
preCommitSectors(t, tv, sectorCount, batchSize, addrs[0], minerAddrs.IDAddress, sealProof, firstSectorNo, true, -1)
stats := tv.GetCallStats()
precommitStats := stats[statsKey]
ipldCost := ipldGas(precommitStats)
callCost := callGas(precommitStats)
perSector := (ipldCost + callCost) / uint64(sectorCount)
perBatch := perSector * uint64(batchSize)
_, _ = p.Printf("%d\t%d\t%d\t%d\t%d\n", batchSize, ipldCost, callCost, perSector, perBatch)
}
fmt.Println()
}
func TestMeasurePoRepGas(t *testing.T) {
sectorCount := 819
fmt.Printf("Batch Size = %d\n", sectorCount)
printPoRepMsgGas(sectorCount)
ctx := context.Background()
blkStore := ipld.NewBlockStoreInMemory()
metrics := ipld.NewMetricsBlockStore(blkStore)
v := vm.NewVMWithSingletons(ctx, t, metrics)
v.SetStatsSource(metrics)
sealProof := abi.RegisteredSealProof_StackedDrg32GiBV1_1
wPoStProof, err := sealProof.RegisteredWindowPoStProof()
require.NoError(t, err)
addrs := vm.CreateAccounts(ctx, t, v, 1, big.Mul(big.NewInt(10_000), builtin.TokenPrecision), 93837778)
owner, worker := addrs[0], addrs[0]
minerAddrs := createMiner(t, v, owner, worker, wPoStProof, big.Mul(big.NewInt(10_000), vm.FIL))
// advance vm so we can have seal randomness epoch in the past
v, err = v.WithEpoch(abi.ChainEpoch(200))
require.NoError(t, err)
//
// precommit sectors
//
firstSectorNo := abi.SectorNumber(100)
precommits := preCommitSectors(t, v, sectorCount, miner.PreCommitSectorBatchMaxSize, addrs[0], minerAddrs.IDAddress, sealProof, firstSectorNo, true, -1)
balances := vm.GetMinerBalances(t, v, minerAddrs.IDAddress)
assert.True(t, balances.PreCommitDeposit.GreaterThan(big.Zero()))
// advance time to max seal duration
proveTime := v.GetEpoch() + miner.MaxProveCommitDuration[sealProof]
v, _ = vm.AdvanceByDeadlineTillEpoch(t, v, minerAddrs.IDAddress, proveTime)
//
// prove and verify
//
v, err = v.WithEpoch(proveTime)
require.NoError(t, err)
sectorsProven := 0
crons := 0
// Prove sectors in batches of 200 to avoid going over the max 200 commits per miner per power cron invocation
for sectorsProven < sectorCount {
sectorsToProveThisCron := min(sectorCount-sectorsProven, power.MaxMinerProveCommitsPerEpoch)
for i := 0; i < sectorsToProveThisCron; i++ {
// Prove commit sector at a valid epoch
proveCommitParams := miner.ProveCommitSectorParams{
SectorNumber: precommits[i+sectorsProven].Info.SectorNumber,
}
vm.ApplyOk(t, v, addrs[0], minerAddrs.RobustAddress, big.Zero(), builtin.MethodsMiner.ProveCommitSector, &proveCommitParams)
vm.ExpectInvocation{
To: minerAddrs.IDAddress,
Method: builtin.MethodsMiner.ProveCommitSector,
Params: vm.ExpectObject(&proveCommitParams),
SubInvocations: []vm.ExpectInvocation{
{To: builtin.StorageMarketActorAddr, Method: builtin.MethodsMarket.ComputeDataCommitment},
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.SubmitPoRepForBulkVerify},
},
}.Matches(t, v.Invocations()[sectorsProven+crons+i])
}
sectorsProven += sectorsToProveThisCron
proveCommitKey := vm.MethodKey{Code: builtin.StorageMinerActorCodeID, Method: builtin.MethodsMiner.ProveCommitSector}
stats := v.GetCallStats()
fmt.Printf("\n--------------------- Batch %d ---------------------\n", crons)
printCallStats(proveCommitKey, stats[proveCommitKey], "")
// In the same epoch, trigger cron to validate prove commits
vm.ApplyOk(t, v, builtin.SystemActorAddr, builtin.CronActorAddr, big.Zero(), builtin.MethodsCron.EpochTick, nil)
crons += 1
cronKey := vm.MethodKey{Code: builtin.CronActorCodeID, Method: builtin.MethodsCron.EpochTick}
stats = v.GetCallStats()
printCallStats(cronKey, stats[cronKey], "")
vm.ExpectInvocation{
To: builtin.CronActorAddr,
Method: builtin.MethodsCron.EpochTick,
SubInvocations: []vm.ExpectInvocation{
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.CronTick, SubInvocations: []vm.ExpectInvocation{
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.ThisEpochReward},
// expect confirm sector proofs valid because we prove committed,
// but not an on deferred cron event because this is not a deadline boundary
{To: minerAddrs.IDAddress, Method: builtin.MethodsMiner.ConfirmSectorProofsValid, SubInvocations: []vm.ExpectInvocation{
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.UpdatePledgeTotal},
}},
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.UpdateNetworkKPI},
}},
{To: builtin.StorageMarketActorAddr, Method: builtin.MethodsMarket.CronTick},
},
}.Matches(t, v.Invocations()[sectorsProven+crons-1])
}
// precommit deposit is released, ipr is added
balances = vm.GetMinerBalances(t, v, minerAddrs.IDAddress)
assert.True(t, balances.InitialPledge.GreaterThan(big.Zero()))
assert.Equal(t, big.Zero(), balances.PreCommitDeposit)
// power is unproven so network stats are unchanged
networkStats := vm.GetNetworkStats(t, v)
assert.Equal(t, big.Zero(), networkStats.TotalBytesCommitted)
assert.True(t, networkStats.TotalPledgeCollateral.GreaterThan(big.Zero()))
}
// Tests batch pre-commit and aggregate prove-commit in various interleavings and batch sizes.
func TestBatchOnboarding(t *testing.T) {
ctx := context.Background()
blkStore := ipld.NewBlockStoreInMemory()
metrics := ipld.NewMetricsBlockStore(blkStore)
v := vm.NewVMWithSingletons(ctx, t, metrics)
v.SetStatsSource(metrics)
sealProof := abi.RegisteredSealProof_StackedDrg32GiBV1_1
wPoStProof, err := sealProof.RegisteredWindowPoStProof()
require.NoError(t, err)
sectorSize, err := sealProof.SectorSize()
require.NoError(t, err)
addrs := vm.CreateAccounts(ctx, t, v, 1, big.Mul(big.NewInt(10_000), builtin.TokenPrecision), 93837778)
owner, worker := addrs[0], addrs[0]
minerAddrs := createMiner(t, v, owner, worker, wPoStProof, big.Mul(big.NewInt(10_000), vm.FIL))
v, err = v.WithEpoch(abi.ChainEpoch(200))
require.NoError(t, err)
// A series of pre-commit and prove-commit actions intended to cover paths including:
// - different pre-commit batch sizes
// - different prove-commit aggregate sizes
// - multiple pre-commit batches before proof
// - proving only some of the pre-commits
// - proving part of multiple pre-commit batches
// - proving all pre-commits across multiple batches
// - interleaving of pre- and prove-commit
//
// Sectors are still proven in the order of pre-commitment.
var precommits []*miner.SectorPreCommitOnChainInfo
nextSectorNo := abi.SectorNumber(0)
preCommittedCount, provenCount := 0, 0
for _, spec := range []struct {
epochDelay abi.ChainEpoch // epochs to advance since the prior action
preCommitSectorCount int // sectors to batch pre-commit
preCommitBatchSize int // batch size (multiple batches if committing more)
proveCommitSectorCount int // sectors to aggregate prove-commit
proveCommitAggregateSize int // aggregate size (multiple aggregates if proving more)
}{
{ // Pre: 10, Proven: 0
epochDelay: 0,
preCommitSectorCount: 10,
preCommitBatchSize: miner.PreCommitSectorBatchMaxSize,
},
{ // Pre: 30, Proven: 0
epochDelay: 1,
preCommitSectorCount: 20,
preCommitBatchSize: 12,
},
{ // Pre: 30, Proven: 8
epochDelay: miner.PreCommitChallengeDelay + 1,
proveCommitSectorCount: 8,
proveCommitAggregateSize: miner.MaxAggregatedSectors,
},
{ // Pre: 30, Proven: 16 (spanning pre-commit batches)
epochDelay: 1,
proveCommitSectorCount: 8,
proveCommitAggregateSize: 4,
},
{ // Pre: 40, Proven: 16
epochDelay: 1,
preCommitSectorCount: 10,
preCommitBatchSize: 4,
},
{ // Pre: 40, Proven: 40
epochDelay: miner.PreCommitChallengeDelay + 1,
proveCommitSectorCount: 24,
proveCommitAggregateSize: 10,
},
} {
v, err = v.WithEpoch(v.GetEpoch() + spec.epochDelay)
require.NoError(t, err)
if spec.preCommitSectorCount > 0 {
newPrecommits := preCommitSectors(t, v, spec.preCommitSectorCount, spec.preCommitBatchSize, worker, minerAddrs.IDAddress, sealProof, nextSectorNo, nextSectorNo == 0, -1)
precommits = append(precommits, newPrecommits...)
nextSectorNo += abi.SectorNumber(spec.preCommitSectorCount)
preCommittedCount += spec.preCommitSectorCount
}
if spec.proveCommitSectorCount > 0 {
toProve := precommits[:spec.proveCommitSectorCount]
precommits = precommits[spec.proveCommitSectorCount:]
proveCommitSectors(t, v, worker, minerAddrs.IDAddress, toProve, spec.proveCommitAggregateSize)
provenCount += spec.proveCommitSectorCount
}
}
//
// Window PoSt all proven sectors.
//
// The sectors are all in the same partition. Advance to it's proving window.
dlInfo, pIdx, v := vm.AdvanceTillProvingDeadline(t, v, minerAddrs.IDAddress, abi.SectorNumber(0))
var minerState miner.State
err = v.GetState(minerAddrs.IDAddress, &minerState)
require.NoError(t, err)
sector, found, err := minerState.GetSector(v.Store(), abi.SectorNumber(0))
require.NoError(t, err)
require.True(t, found)
partitions := []miner.PoStPartition{{
Index: pIdx,
Skipped: bitfield.New(),
}}
newPower := miner.PowerForSector(sectorSize, sector).Mul(big.NewInt(int64(provenCount)))
submitWindowPoSt(t, v, worker, minerAddrs.IDAddress, dlInfo, partitions, newPower)
// Miner has initial pledge
balances := vm.GetMinerBalances(t, v, minerAddrs.IDAddress)
assert.True(t, balances.InitialPledge.GreaterThan(big.Zero()))
// Committed bytes are added (miner would have gained power if minimum requirement were met)
networkStats := vm.GetNetworkStats(t, v)
assert.Equal(t, big.NewInt(int64(sectorSize)*int64(provenCount)), networkStats.TotalBytesCommitted)
assert.True(t, networkStats.TotalPledgeCollateral.GreaterThan(big.Zero()))
// Trigger cron to keep reward accounting correct
vm.ApplyOk(t, v, builtin.SystemActorAddr, builtin.CronActorAddr, big.Zero(), builtin.MethodsCron.EpochTick, nil)
stateTree, err := v.GetStateTree()
require.NoError(t, err)
totalBalance, err := v.GetTotalActorBalance()
require.NoError(t, err)
acc, err := states.CheckStateInvariants(stateTree, totalBalance, v.GetEpoch())
require.NoError(t, err)
assert.True(t, acc.IsEmpty(), strings.Join(acc.Messages(), "\n"))
}
func TestAggregateOnePreCommitExpires(t *testing.T) {
ctx := context.Background()
blkStore := ipld.NewBlockStoreInMemory()
v := vm.NewVMWithSingletons(ctx, t, blkStore)
sealProof := abi.RegisteredSealProof_StackedDrg32GiBV1_1
wPoStProof, err := sealProof.RegisteredWindowPoStProof()
require.NoError(t, err)
addrs := vm.CreateAccounts(ctx, t, v, 1, big.Mul(big.NewInt(10_000), builtin.TokenPrecision), 93837778)
owner, worker := addrs[0], addrs[0]
minerAddrs := createMiner(t, v, owner, worker, wPoStProof, big.Mul(big.NewInt(10_000), vm.FIL))
// advance vm so we can have seal randomness epoch in the past
v, err = v.WithEpoch(abi.ChainEpoch(200))
require.NoError(t, err)
//
// precommit secotrs
//
firstSectorNo := abi.SectorNumber(100)
// early precommit
earlyPreCommitTime := v.GetEpoch()
earlyPrecommits := preCommitSectors(t, v, 1, miner.PreCommitSectorBatchMaxSize, addrs[0], minerAddrs.IDAddress, sealProof, firstSectorNo, true, -1)
earlyPreCommitInvalid := earlyPreCommitTime + miner.MaxProveCommitDuration[sealProof] + abi.ChainEpoch(1)
v, _ = vm.AdvanceByDeadlineTillEpoch(t, v, minerAddrs.IDAddress, earlyPreCommitInvalid)
// later precommits
laterPrecommits := preCommitSectors(t, v, 3, miner.PreCommitSectorBatchMaxSize, addrs[0], minerAddrs.IDAddress, sealProof, firstSectorNo+1, false, -1)
allPrecommits := append(earlyPrecommits, laterPrecommits...)
sectorNosBf := precommitSectorNumbers(allPrecommits)
// Advance minimum epochs past later precommits for later commits to be valid
proveTime := v.GetEpoch() + miner.PreCommitChallengeDelay + abi.ChainEpoch(1)
v, dlInfo := vm.AdvanceByDeadlineTillEpoch(t, v, minerAddrs.IDAddress, proveTime)
v, _ = vm.AdvanceByDeadlineTillEpoch(t, v, minerAddrs.IDAddress, dlInfo.Close)
// Assert that precommit should not yet be cleaned up. This makes fixing this test easier if parameters change.
require.True(t, proveTime < earlyPreCommitTime+miner.MaxProveCommitDuration[sealProof]+miner.ExpiredPreCommitCleanUpDelay)
// Assert that we have a valid aggregate batch size
aggSectorsCount, err := sectorNosBf.Count()
require.NoError(t, err)
require.True(t, aggSectorsCount >= miner.MinAggregatedSectors && aggSectorsCount < miner.MaxAggregatedSectors)
proveCommitAggregateParams := miner.ProveCommitAggregateParams{
SectorNumbers: sectorNosBf,
}
// Aggregate passes, proving the 2 unexpired commitments
vm.ApplyOk(t, v, addrs[0], minerAddrs.RobustAddress, big.Zero(), builtin.MethodsMiner.ProveCommitAggregate, &proveCommitAggregateParams)
vm.ExpectInvocation{
To: minerAddrs.IDAddress,
Method: builtin.MethodsMiner.ProveCommitAggregate,
Params: vm.ExpectObject(&proveCommitAggregateParams),
SubInvocations: []vm.ExpectInvocation{
{To: builtin.StorageMarketActorAddr, Method: builtin.MethodsMarket.ComputeDataCommitment},
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.ThisEpochReward},
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.CurrentTotalPower},
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.UpdatePledgeTotal},
{To: builtin.BurntFundsActorAddr, Method: builtin.MethodSend},
},
}.Matches(t, v.LastInvocation())
balances := vm.GetMinerBalances(t, v, minerAddrs.IDAddress)
assert.True(t, balances.InitialPledge.GreaterThan(big.Zero()))
assert.True(t, balances.PreCommitDeposit.GreaterThan(big.Zero()))
}
func TestAggregateSizeLimits(t *testing.T) {
overSizedBatch := 820
ctx := context.Background()
blkStore := ipld.NewBlockStoreInMemory()
v := vm.NewVMWithSingletons(ctx, t, blkStore)
addrs := vm.CreateAccounts(ctx, t, v, 1, big.Mul(big.NewInt(10_000), big.NewInt(1e18)), 93837778)
// create miner
sealProof := abi.RegisteredSealProof_StackedDrg32GiBV1_1
wPoStProof, err := sealProof.RegisteredWindowPoStProof()
require.NoError(t, err)
owner, worker := addrs[0], addrs[0]
minerAddrs := createMiner(t, v, owner, worker, wPoStProof, big.Mul(big.NewInt(10_000), vm.FIL))
// advance vm so we can have seal randomness epoch in the past
v, err = v.WithEpoch(abi.ChainEpoch(200))
require.NoError(t, err)
//
// precommit sectors
//
firstSectorNo := abi.SectorNumber(100)
precommits := preCommitSectors(t, v, overSizedBatch, miner.PreCommitSectorBatchMaxSize, addrs[0], minerAddrs.IDAddress, sealProof, firstSectorNo, true, -1)
balances := vm.GetMinerBalances(t, v, minerAddrs.IDAddress)
assert.True(t, balances.PreCommitDeposit.GreaterThan(big.Zero()))
// advance time to max seal duration
proveTime := v.GetEpoch() + miner.MaxProveCommitDuration[sealProof]
v, _ = vm.AdvanceByDeadlineTillEpoch(t, v, minerAddrs.IDAddress, proveTime)
//
// attempt proving with invalid args
//
// Fail with too many sectors
v, err = v.WithEpoch(proveTime)
require.NoError(t, err)
sectorNosBf := precommitSectorNumbers(precommits)
proveCommitAggregateTooManyParams := miner.ProveCommitAggregateParams{
SectorNumbers: sectorNosBf,
}
res := vm.RequireApplyMessage(t, v, addrs[0], minerAddrs.RobustAddress, big.Zero(), builtin.MethodsMiner.ProveCommitAggregate, &proveCommitAggregateTooManyParams, t.Name())
assert.Equal(t, exitcode.ErrIllegalArgument, res.Code) // fail with too many aggregates
// Fail with too few sectors
tooFewSectorNosBf := precommitSectorNumbers(precommits[:miner.MinAggregatedSectors-1])
proveCommitAggregateTooFewParams := miner.ProveCommitAggregateParams{
SectorNumbers: tooFewSectorNosBf,
}
res = vm.RequireApplyMessage(t, v, addrs[0], minerAddrs.RobustAddress, big.Zero(), builtin.MethodsMiner.ProveCommitAggregate, &proveCommitAggregateTooFewParams, t.Name())
assert.Equal(t, exitcode.ErrIllegalArgument, res.Code)
// Fail with proof too big
justRightSectorNosBf := precommitSectorNumbers(precommits[:miner.MaxAggregatedSectors])
proveCommitAggregateTooBigProofParams := miner.ProveCommitAggregateParams{
SectorNumbers: justRightSectorNosBf,
AggregateProof: make([]byte, miner.MaxAggregateProofSize+1),
}
res = vm.RequireApplyMessage(t, v, addrs[0], minerAddrs.RobustAddress, big.Zero(), builtin.MethodsMiner.ProveCommitAggregate, &proveCommitAggregateTooBigProofParams, t.Name())
assert.Equal(t, exitcode.ErrIllegalArgument, res.Code)
}
func TestAggregateBadSender(t *testing.T) {
ctx := context.Background()
blkStore := ipld.NewBlockStoreInMemory()
v := vm.NewVMWithSingletons(ctx, t, blkStore)
sealProof := abi.RegisteredSealProof_StackedDrg32GiBV1_1
wPoStProof, err := sealProof.RegisteredWindowPoStProof()
require.NoError(t, err)
addrs := vm.CreateAccounts(ctx, t, v, 2, big.Mul(big.NewInt(10_000), builtin.TokenPrecision), 93837778)
owner, worker := addrs[0], addrs[0]
minerAddrs := createMiner(t, v, owner, worker, wPoStProof, big.Mul(big.NewInt(10_000), vm.FIL))
// advance vm so we can have seal randomness epoch in the past
v, err = v.WithEpoch(abi.ChainEpoch(200))
require.NoError(t, err)
//
// precommit good secotrs
//
firstSectorNo := abi.SectorNumber(100)
// early precommit
preCommitTime := v.GetEpoch()
precommits := preCommitSectors(t, v, 4, miner.PreCommitSectorBatchMaxSize, addrs[0], minerAddrs.IDAddress, sealProof, firstSectorNo, true, -1)
//
// attempt proving with invalid args
//
// advance time to max seal duration
proveTime := preCommitTime + miner.MaxProveCommitDuration[sealProof]
v, _ = vm.AdvanceByDeadlineTillEpoch(t, v, minerAddrs.IDAddress, proveTime)
v, err = v.WithEpoch(proveTime)
require.NoError(t, err)
sectorNosBf := precommitSectorNumbers(precommits)
proveCommitAggregateParams := miner.ProveCommitAggregateParams{
SectorNumbers: sectorNosBf,
}
res := vm.RequireApplyMessage(t, v, addrs[1], minerAddrs.RobustAddress, big.Zero(), builtin.MethodsMiner.ProveCommitAggregate, &proveCommitAggregateParams, t.Name())
assert.Equal(t, exitcode.SysErrForbidden, res.Code)
}
func TestAggregateBadSectorNumber(t *testing.T) {
ctx := context.Background()
blkStore := ipld.NewBlockStoreInMemory()
v := vm.NewVMWithSingletons(ctx, t, blkStore)
sealProof := abi.RegisteredSealProof_StackedDrg32GiBV1_1
wPoStProof, err := sealProof.RegisteredWindowPoStProof()
require.NoError(t, err)
addrs := vm.CreateAccounts(ctx, t, v, 1, big.Mul(big.NewInt(10_000), builtin.TokenPrecision), 93837778)
owner, worker := addrs[0], addrs[0]
minerAddrs := createMiner(t, v, owner, worker, wPoStProof, big.Mul(big.NewInt(10_000), vm.FIL))
// advance vm so we can have seal randomness epoch in the past
v, err = v.WithEpoch(abi.ChainEpoch(200))
require.NoError(t, err)
//
// precommit good secotrs
//
firstSectorNo := abi.SectorNumber(100)
// early precommit
preCommitTime := v.GetEpoch()
precommits := preCommitSectors(t, v, 4, miner.PreCommitSectorBatchMaxSize, addrs[0], minerAddrs.IDAddress, sealProof, firstSectorNo, true, -1)
//
// attempt proving with invalid args
//
// advance time to max seal duration
proveTime := preCommitTime + miner.MaxProveCommitDuration[sealProof]
v, _ = vm.AdvanceByDeadlineTillEpoch(t, v, minerAddrs.IDAddress, proveTime)
v, err = v.WithEpoch(proveTime)
// construct invalid bitfield with a non-committed sector number > abi.MaxSectorNumber
require.NoError(t, err)
sectorNosBf := precommitSectorNumbers(precommits)
sectorNosBf.Set(abi.MaxSectorNumber + 1)
proveCommitAggregateTooManyParams := miner.ProveCommitAggregateParams{
SectorNumbers: sectorNosBf,
}
res := vm.RequireApplyMessage(t, v, addrs[0], minerAddrs.RobustAddress, big.Zero(), builtin.MethodsMiner.ProveCommitAggregate, &proveCommitAggregateTooManyParams, t.Name())
assert.Equal(t, exitcode.ErrIllegalArgument, res.Code)
}
func TestMeasureAggregatePorepGas(t *testing.T) {
sectorCount := 819
fmt.Printf("batch size = %d\n", sectorCount)
ctx := context.Background()
blkStore := ipld.NewBlockStoreInMemory()
metrics := ipld.NewMetricsBlockStore(blkStore)
v := vm.NewVMWithSingletons(ctx, t, metrics)
v.SetStatsSource(metrics)
sealProof := abi.RegisteredSealProof_StackedDrg32GiBV1_1
wPoStProof, err := sealProof.RegisteredWindowPoStProof()
require.NoError(t, err)
addrs := vm.CreateAccounts(ctx, t, v, 1, big.Mul(big.NewInt(10_000), builtin.TokenPrecision), 93837778)
owner, worker := addrs[0], addrs[0]
minerAddrs := createMiner(t, v, owner, worker, wPoStProof, big.Mul(big.NewInt(10_000), vm.FIL))
// advance vm so we can have seal randomness epoch in the past
v, err = v.WithEpoch(abi.ChainEpoch(200))
require.NoError(t, err)
//
// precommit sectors
//
firstSectorNo := abi.SectorNumber(100)
precommits := preCommitSectors(t, v, sectorCount, miner.PreCommitSectorBatchMaxSize, addrs[0], minerAddrs.IDAddress, sealProof, firstSectorNo, true, -1)
balances := vm.GetMinerBalances(t, v, minerAddrs.IDAddress)
assert.True(t, balances.PreCommitDeposit.GreaterThan(big.Zero()))
// advance time to max seal duration
proveTime := v.GetEpoch() + miner.MaxProveCommitDuration[sealProof]
v, _ = vm.AdvanceByDeadlineTillEpoch(t, v, minerAddrs.IDAddress, proveTime)
//
// prove and verify
//
v, err = v.WithEpoch(proveTime)
require.NoError(t, err)
sectorNosBf := precommitSectorNumbers(precommits)
proveCommitAggregateParams := miner.ProveCommitAggregateParams{
SectorNumbers: sectorNosBf,
}
vm.ApplyOk(t, v, addrs[0], minerAddrs.RobustAddress, big.Zero(), builtin.MethodsMiner.ProveCommitAggregate, &proveCommitAggregateParams)
vm.ExpectInvocation{
To: minerAddrs.IDAddress,
Method: builtin.MethodsMiner.ProveCommitAggregate,
Params: vm.ExpectObject(&proveCommitAggregateParams),
SubInvocations: []vm.ExpectInvocation{
{To: builtin.StorageMarketActorAddr, Method: builtin.MethodsMarket.ComputeDataCommitment},
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.ThisEpochReward},
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.CurrentTotalPower},
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.UpdatePledgeTotal},
{To: builtin.BurntFundsActorAddr, Method: builtin.MethodSend},
},
}.Matches(t, v.LastInvocation())
proveCommitAggrKey := vm.MethodKey{Code: builtin.StorageMinerActorCodeID, Method: builtin.MethodsMiner.ProveCommitAggregate}
stats := v.GetCallStats()
printCallStats(proveCommitAggrKey, stats[proveCommitAggrKey], "")
// In the same epoch, trigger cron to
vm.ApplyOk(t, v, builtin.SystemActorAddr, builtin.CronActorAddr, big.Zero(), builtin.MethodsCron.EpochTick, nil)
cronKey := vm.MethodKey{Code: builtin.CronActorCodeID, Method: builtin.MethodsCron.EpochTick}
stats = v.GetCallStats()
printCallStats(cronKey, stats[cronKey], "")
vm.ExpectInvocation{
To: builtin.CronActorAddr,
Method: builtin.MethodsCron.EpochTick,
SubInvocations: []vm.ExpectInvocation{
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.CronTick, SubInvocations: []vm.ExpectInvocation{
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.ThisEpochReward},
// expect no confirm sector proofs valid because we prove committed with aggregation.
// expect no on deferred cron event because this is not a deadline boundary
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.UpdateNetworkKPI},
}},
{To: builtin.StorageMarketActorAddr, Method: builtin.MethodsMarket.CronTick},
},
}.Matches(t, v.Invocations()[1])
// precommit deposit is released, ipr is added
balances = vm.GetMinerBalances(t, v, minerAddrs.IDAddress)
assert.True(t, balances.InitialPledge.GreaterThan(big.Zero()))
assert.Equal(t, big.Zero(), balances.PreCommitDeposit)
// power is unproven so network stats are unchanged
networkStats := vm.GetNetworkStats(t, v)
assert.Equal(t, big.Zero(), networkStats.TotalBytesCommitted)
assert.True(t, networkStats.TotalPledgeCollateral.GreaterThan(big.Zero()))
}
// Proves pre-committed sectors as batches of aggSize.
func proveCommitSectors(t *testing.T, v *vm.VM, worker, actor address.Address, precommits []*miner.SectorPreCommitOnChainInfo, aggSize int) {
for len(precommits) > 0 {
batchSize := min(aggSize, len(precommits))
toProve := precommits[:batchSize]
precommits = precommits[batchSize:]
sectorNosBf := precommitSectorNumbers(toProve)
proveCommitAggregateParams := miner.ProveCommitAggregateParams{
SectorNumbers: sectorNosBf,
}
vm.ApplyOk(t, v, worker, actor, big.Zero(), builtin.MethodsMiner.ProveCommitAggregate, &proveCommitAggregateParams)
vm.ExpectInvocation{
To: actor,
Method: builtin.MethodsMiner.ProveCommitAggregate,
Params: vm.ExpectObject(&proveCommitAggregateParams),
SubInvocations: []vm.ExpectInvocation{
{To: builtin.StorageMarketActorAddr, Method: builtin.MethodsMarket.ComputeDataCommitment},
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.ThisEpochReward},
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.CurrentTotalPower},
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.UpdatePledgeTotal},
{To: builtin.BurntFundsActorAddr, Method: builtin.MethodSend},
},
}.Matches(t, v.LastInvocation())
}
}
// Submits a Window PoSt for partitions in a deadline.
func submitWindowPoSt(t *testing.T, v *vm.VM, worker, actor address.Address, dlInfo *dline.Info, partitions []miner.PoStPartition,
newPower miner.PowerPair) {
submitParams := miner.SubmitWindowedPoStParams{
Deadline: dlInfo.Index,
Partitions: partitions,
Proofs: []proof.PoStProof{{
PoStProof: abi.RegisteredPoStProof_StackedDrgWindow32GiBV1,
}},
ChainCommitEpoch: dlInfo.Challenge,
ChainCommitRand: []byte(vm.RandString),
}
vm.ApplyOk(t, v, worker, actor, big.Zero(), builtin.MethodsMiner.SubmitWindowedPoSt, &submitParams)
updatePowerParams := &power.UpdateClaimedPowerParams{
RawByteDelta: newPower.Raw,
QualityAdjustedDelta: newPower.QA,
}
vm.ExpectInvocation{
To: actor,
Method: builtin.MethodsMiner.SubmitWindowedPoSt,
Params: vm.ExpectObject(&submitParams),
SubInvocations: []vm.ExpectInvocation{
{
To: builtin.StoragePowerActorAddr,
Method: builtin.MethodsPower.UpdateClaimedPower,
Params: vm.ExpectObject(updatePowerParams),
},
},
}.Matches(t, v.LastInvocation())
}
// Returns a bitfield of the sector numbers from a collection pre-committed sectors.
func precommitSectorNumbers(precommits []*miner.SectorPreCommitOnChainInfo) bitfield.BitField {
intSectorNumbers := make([]uint64, len(precommits))
for i := range precommits {
intSectorNumbers[i] = uint64(precommits[i].Info.SectorNumber)
}
return bitfield.NewFromSet(intSectorNumbers)
}
func printCallStats(method vm.MethodKey, stats *vm.CallStats, indent string) { // nolint:unused
p := message.NewPrinter(language.English) // For readable large numbers
_, _ = p.Printf("%s%v:%d: calls: %d gets: %d puts: %d read: %d written: %d avg gets: %.2f, avg puts: %.2f\n",
indent, builtin.ActorNameByCode(method.Code), method.Method, stats.Calls, stats.Reads, stats.Writes,
stats.ReadBytes, stats.WriteBytes, float32(stats.Reads)/float32(stats.Calls),
float32(stats.Writes)/float32(stats.Calls))
_, _ = p.Printf("%s%v:%d: ipld gas: %d call gas: %d\n", indent, builtin.ActorNameByCode(method.Code), method.Method,
ipldGas(stats), callGas(stats))
if stats.SubStats == nil {
return
}
for m, s := range stats.SubStats {
printCallStats(m, s, indent+" ")
}
}
func ipldGas(stats *vm.CallStats) uint64 {
gasGetObj := uint64(75242)
gasPutObj := uint64(84070)
gasPutPerByte := uint64(1)
gasStorageMultiplier := uint64(1300)
return stats.Reads*gasGetObj + stats.Writes*gasPutObj + stats.WriteBytes*gasPutPerByte*gasStorageMultiplier
}
func callGas(stats *vm.CallStats) uint64 {
gasPerCall := uint64(29233)
return stats.Calls * gasPerCall
}
// Using gas params from filecoin v12 and assumptions about parameters to ProveCommitAggregate print an estimate
// of the gas charged for the on chain ProveCommitAggregate message.
func printPoRepMsgGas(batchSize int) {
// Ignore message fields and sector number bytes for both.
// Ignoring non-parma message fields under estimates both by the same amount
// Ignoring sector numbers/bitfields underestimates current porep compared to aggregate
// which is the right direction for finding a starting bound (we can optimize later)
onChainMessageComputeBase := 38863
onChainMessageStorageBase := 36
onChainMessageStoragePerByte := 1
storageGasMultiplier := 1300
msgBytes := 1920
msgGas := onChainMessageComputeBase + (onChainMessageStorageBase+onChainMessageStoragePerByte*msgBytes)*storageGasMultiplier
allMsgsGas := batchSize * msgGas
fmt.Printf("%d batchsize: all proof param byte gas: %d\n", batchSize, allMsgsGas)
}
func min(a, b int) int {
if a <= b {
return a
}
return b
}