Skip to content

Commit 16efdf6

Browse files
committed
feat: itest: minor UnmanagedMiner refactor for reusability and simplicity
* move some utility methods into UnmanagedMiner so they can be used from other itests * make UnmanagedMiner aware of mock proofs so we can remove a lot of branching
1 parent 5dffc05 commit 16efdf6

File tree

3 files changed

+106
-201
lines changed

3 files changed

+106
-201
lines changed

itests/kit/ensemble.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ func (n *Ensemble) UnmanagedMiner(full *TestFullNode, opts ...NodeOpt) (*TestUnm
327327
actorAddr, err := address.NewIDAddress(genesis2.MinerStart + n.minerCount())
328328
require.NoError(n.t, err)
329329

330-
minerNode := NewTestUnmanagedMiner(n.t, full, actorAddr, opts...)
330+
minerNode := NewTestUnmanagedMiner(n.t, full, actorAddr, n.options.mockProofs, opts...)
331331
n.AddInactiveUnmanagedMiner(minerNode)
332332
return minerNode, n
333333
}

itests/kit/node_unmanaged.go

Lines changed: 97 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ import (
3434
// TestUnmanagedMiner is a miner that's not managed by the storage/infrastructure, all tasks must be manually executed, managed and scheduled by the test or test kit.
3535
// Note: `TestUnmanagedMiner` is not thread safe and assumes linear access of it's methods
3636
type TestUnmanagedMiner struct {
37-
t *testing.T
38-
options nodeOpts
37+
t *testing.T
38+
options nodeOpts
39+
mockProofs bool
3940

4041
cacheDir string
4142
unsealedSectorDir string
@@ -65,7 +66,7 @@ type WindowPostResp struct {
6566
Error error
6667
}
6768

68-
func NewTestUnmanagedMiner(t *testing.T, full *TestFullNode, actorAddr address.Address, opts ...NodeOpt) *TestUnmanagedMiner {
69+
func NewTestUnmanagedMiner(t *testing.T, full *TestFullNode, actorAddr address.Address, mockProofs bool, opts ...NodeOpt) *TestUnmanagedMiner {
6970
require.NotNil(t, full, "full node required when instantiating miner")
7071

7172
options := DefaultNodeOpts
@@ -94,6 +95,7 @@ func NewTestUnmanagedMiner(t *testing.T, full *TestFullNode, actorAddr address.A
9495
tm := TestUnmanagedMiner{
9596
t: t,
9697
options: options,
98+
mockProofs: mockProofs,
9799
cacheDir: cacheDir,
98100
unsealedSectorDir: unsealedSectorDir,
99101
sealedSectorDir: sealedSectorDir,
@@ -222,7 +224,7 @@ func (tm *TestUnmanagedMiner) makeAndSaveCCSector(_ context.Context, sectorNumbe
222224
tm.cacheDirPaths[sectorNumber] = cacheDirPath
223225
}
224226

225-
func (tm *TestUnmanagedMiner) OnboardSectorWithPiecesAndRealProofs(ctx context.Context, proofType abi.RegisteredSealProof) (abi.SectorNumber, chan WindowPostResp,
227+
func (tm *TestUnmanagedMiner) OnboardSectorWithPieces(ctx context.Context, proofType abi.RegisteredSealProof) (abi.SectorNumber, chan WindowPostResp,
226228
context.CancelFunc) {
227229
req := require.New(tm.t)
228230
sectorNumber := tm.currentSectorNum
@@ -232,10 +234,23 @@ func (tm *TestUnmanagedMiner) OnboardSectorWithPiecesAndRealProofs(ctx context.C
232234
preCommitSealRand := tm.waitPreCommitSealRandomness(ctx, sectorNumber)
233235

234236
// Step 2: Build a sector with non 0 Pieces that we want to onboard
235-
pieces := tm.mkAndSavePiecesToOnboard(ctx, sectorNumber, proofType)
237+
var pieces []abi.PieceInfo
238+
if !tm.mockProofs {
239+
pieces = tm.mkAndSavePiecesToOnboard(ctx, sectorNumber, proofType)
240+
} else {
241+
pieces = []abi.PieceInfo{{
242+
Size: abi.PaddedPieceSize(tm.options.sectorSize),
243+
PieceCID: cid.MustParse("baga6ea4seaqjtovkwk4myyzj56eztkh5pzsk5upksan6f5outesy62bsvl4dsha"),
244+
}}
245+
}
236246

237247
// Step 3: Generate a Pre-Commit for the CC sector -> this persists the proof on the `TestUnmanagedMiner` Miner State
238-
tm.generatePreCommit(ctx, sectorNumber, preCommitSealRand, proofType, pieces)
248+
if !tm.mockProofs {
249+
tm.generatePreCommit(ctx, sectorNumber, preCommitSealRand, proofType, pieces)
250+
} else {
251+
tm.sealedCids[sectorNumber] = cid.MustParse("bagboea4b5abcatlxechwbp7kjpjguna6r6q7ejrhe6mdp3lf34pmswn27pkkiekz")
252+
tm.unsealedCids[sectorNumber] = cid.MustParse("baga6ea4seaqjtovkwk4myyzj56eztkh5pzsk5upksan6f5outesy62bsvl4dsha")
253+
}
239254

240255
// Step 4 : Submit the Pre-Commit to the network
241256
unsealedCid := tm.unsealedCids[sectorNumber]
@@ -255,75 +270,11 @@ func (tm *TestUnmanagedMiner) OnboardSectorWithPiecesAndRealProofs(ctx context.C
255270
// Step 5: Generate a ProveCommit for the CC sector
256271
waitSeedRandomness := tm.proveCommitWaitSeed(ctx, sectorNumber)
257272

258-
proveCommit := tm.generateProveCommit(ctx, sectorNumber, proofType, waitSeedRandomness, pieces)
259-
260-
// Step 6: Submit the ProveCommit to the network
261-
tm.t.Log("Submitting ProveCommitSector ...")
262-
263-
var manifest []miner14.PieceActivationManifest
264-
for _, piece := range pieces {
265-
manifest = append(manifest, miner14.PieceActivationManifest{
266-
CID: piece.PieceCID,
267-
Size: piece.Size,
268-
})
273+
proveCommit := []byte{0xde, 0xad, 0xbe, 0xef} // mock prove commit
274+
if !tm.mockProofs {
275+
proveCommit = tm.generateProveCommit(ctx, sectorNumber, proofType, waitSeedRandomness, pieces)
269276
}
270277

271-
r, err = tm.submitMessage(ctx, &miner14.ProveCommitSectors3Params{
272-
SectorActivations: []miner14.SectorActivationManifest{{SectorNumber: sectorNumber, Pieces: manifest}},
273-
SectorProofs: [][]byte{proveCommit},
274-
RequireActivationSuccess: true,
275-
}, 1, builtin.MethodsMiner.ProveCommitSectors3)
276-
req.NoError(err)
277-
req.True(r.Receipt.ExitCode.IsSuccess())
278-
279-
tm.proofType[sectorNumber] = proofType
280-
281-
respCh := make(chan WindowPostResp, 1)
282-
283-
wdCtx, cancelF := context.WithCancel(ctx)
284-
go tm.wdPostLoop(wdCtx, sectorNumber, respCh, false, tm.sealedCids[sectorNumber], tm.sealedSectorPaths[sectorNumber], tm.cacheDirPaths[sectorNumber])
285-
286-
return sectorNumber, respCh, cancelF
287-
}
288-
289-
func (tm *TestUnmanagedMiner) OnboardSectorWithPiecesAndMockProofs(ctx context.Context, proofType abi.RegisteredSealProof) (abi.SectorNumber, chan WindowPostResp,
290-
context.CancelFunc) {
291-
req := require.New(tm.t)
292-
sectorNumber := tm.currentSectorNum
293-
tm.currentSectorNum++
294-
295-
// Step 1: Wait for the pre-commitseal randomness to be available (we can only draw seal randomness from tipsets that have already achieved finality)
296-
preCommitSealRand := tm.waitPreCommitSealRandomness(ctx, sectorNumber)
297-
298-
// Step 2: Build a sector with non 0 Pieces that we want to onboard
299-
pieces := []abi.PieceInfo{{
300-
Size: abi.PaddedPieceSize(tm.options.sectorSize),
301-
PieceCID: cid.MustParse("baga6ea4seaqjtovkwk4myyzj56eztkh5pzsk5upksan6f5outesy62bsvl4dsha"),
302-
}}
303-
304-
// Step 3: Generate a Pre-Commit for the CC sector -> this persists the proof on the `TestUnmanagedMiner` Miner State
305-
tm.sealedCids[sectorNumber] = cid.MustParse("bagboea4b5abcatlxechwbp7kjpjguna6r6q7ejrhe6mdp3lf34pmswn27pkkiekz")
306-
tm.unsealedCids[sectorNumber] = cid.MustParse("baga6ea4seaqjtovkwk4myyzj56eztkh5pzsk5upksan6f5outesy62bsvl4dsha")
307-
308-
// Step 4 : Submit the Pre-Commit to the network
309-
unsealedCid := tm.unsealedCids[sectorNumber]
310-
r, err := tm.submitMessage(ctx, &miner14.PreCommitSectorBatchParams2{
311-
Sectors: []miner14.SectorPreCommitInfo{{
312-
Expiration: 2880 * 300,
313-
SectorNumber: sectorNumber,
314-
SealProof: TestSpt,
315-
SealedCID: tm.sealedCids[sectorNumber],
316-
SealRandEpoch: preCommitSealRand,
317-
UnsealedCid: &unsealedCid,
318-
}},
319-
}, 1, builtin.MethodsMiner.PreCommitSectorBatch2)
320-
req.NoError(err)
321-
req.True(r.Receipt.ExitCode.IsSuccess())
322-
323-
// Step 5: Generate a ProveCommit for the CC sector
324-
_ = tm.proveCommitWaitSeed(ctx, sectorNumber)
325-
sectorProof := []byte{0xde, 0xad, 0xbe, 0xef}
326-
327278
// Step 6: Submit the ProveCommit to the network
328279
tm.t.Log("Submitting ProveCommitSector ...")
329280

@@ -337,7 +288,7 @@ func (tm *TestUnmanagedMiner) OnboardSectorWithPiecesAndMockProofs(ctx context.C
337288

338289
r, err = tm.submitMessage(ctx, &miner14.ProveCommitSectors3Params{
339290
SectorActivations: []miner14.SectorActivationManifest{{SectorNumber: sectorNumber, Pieces: manifest}},
340-
SectorProofs: [][]byte{sectorProof},
291+
SectorProofs: [][]byte{proveCommit},
341292
RequireActivationSuccess: true,
342293
}, 1, builtin.MethodsMiner.ProveCommitSectors3)
343294
req.NoError(err)
@@ -348,7 +299,7 @@ func (tm *TestUnmanagedMiner) OnboardSectorWithPiecesAndMockProofs(ctx context.C
348299
respCh := make(chan WindowPostResp, 1)
349300

350301
wdCtx, cancelF := context.WithCancel(ctx)
351-
go tm.wdPostLoop(wdCtx, sectorNumber, respCh, true, tm.sealedCids[sectorNumber], tm.sealedSectorPaths[sectorNumber], tm.cacheDirPaths[sectorNumber])
302+
go tm.wdPostLoop(wdCtx, sectorNumber, respCh, tm.sealedCids[sectorNumber], tm.sealedSectorPaths[sectorNumber], tm.cacheDirPaths[sectorNumber])
352303

353304
return sectorNumber, respCh, cancelF
354305
}
@@ -393,7 +344,11 @@ func (tm *TestUnmanagedMiner) mkStagedFileWithPieces(pt abi.RegisteredSealProof)
393344
return publicPieces, unsealedSectorFile.Name()
394345
}
395346

396-
func (tm *TestUnmanagedMiner) SnapDealWithRealProofs(ctx context.Context, proofType abi.RegisteredSealProof, sectorNumber abi.SectorNumber) {
347+
func (tm *TestUnmanagedMiner) SnapDeal(ctx context.Context, proofType abi.RegisteredSealProof, sectorNumber abi.SectorNumber) {
348+
if tm.mockProofs {
349+
tm.t.Fatal("snap deal with mock proofs currently not supported")
350+
}
351+
397352
// generate sector key
398353
pieces, unsealedPath := tm.mkStagedFileWithPieces(proofType)
399354
updateProofType := abi.SealProofInfos[proofType].UpdateProof
@@ -489,56 +444,7 @@ func (tm *TestUnmanagedMiner) waitForMutableDeadline(ctx context.Context, sector
489444
}
490445
}
491446

492-
func (tm *TestUnmanagedMiner) OnboardCCSectorWithMockProofs(ctx context.Context, proofType abi.RegisteredSealProof) (abi.SectorNumber, chan WindowPostResp,
493-
context.CancelFunc) {
494-
req := require.New(tm.t)
495-
sectorNumber := tm.currentSectorNum
496-
tm.currentSectorNum++
497-
498-
// Step 1: Wait for the pre-commitseal randomness to be available (we can only draw seal randomness from tipsets that have already achieved finality)
499-
preCommitSealRand := tm.waitPreCommitSealRandomness(ctx, sectorNumber)
500-
501-
tm.sealedCids[sectorNumber] = cid.MustParse("bagboea4b5abcatlxechwbp7kjpjguna6r6q7ejrhe6mdp3lf34pmswn27pkkiekz")
502-
503-
// Step 4 : Submit the Pre-Commit to the network
504-
r, err := tm.submitMessage(ctx, &miner14.PreCommitSectorBatchParams2{
505-
Sectors: []miner14.SectorPreCommitInfo{{
506-
Expiration: 2880 * 300,
507-
SectorNumber: sectorNumber,
508-
SealProof: TestSpt,
509-
SealedCID: tm.sealedCids[sectorNumber],
510-
SealRandEpoch: preCommitSealRand,
511-
}},
512-
}, 1, builtin.MethodsMiner.PreCommitSectorBatch2)
513-
req.NoError(err)
514-
req.True(r.Receipt.ExitCode.IsSuccess())
515-
516-
// Step 5: Generate a ProveCommit for the CC sector
517-
_ = tm.proveCommitWaitSeed(ctx, sectorNumber)
518-
sectorProof := []byte{0xde, 0xad, 0xbe, 0xef}
519-
520-
// Step 6: Submit the ProveCommit to the network
521-
tm.t.Log("Submitting ProveCommitSector ...")
522-
523-
r, err = tm.submitMessage(ctx, &miner14.ProveCommitSectors3Params{
524-
SectorActivations: []miner14.SectorActivationManifest{{SectorNumber: sectorNumber}},
525-
SectorProofs: [][]byte{sectorProof},
526-
RequireActivationSuccess: true,
527-
}, 0, builtin.MethodsMiner.ProveCommitSectors3)
528-
req.NoError(err)
529-
req.True(r.Receipt.ExitCode.IsSuccess())
530-
531-
tm.proofType[sectorNumber] = proofType
532-
533-
respCh := make(chan WindowPostResp, 1)
534-
535-
wdCtx, cancelF := context.WithCancel(ctx)
536-
go tm.wdPostLoop(wdCtx, sectorNumber, respCh, true, tm.sealedCids[sectorNumber], tm.sealedSectorPaths[sectorNumber], tm.cacheDirPaths[sectorNumber])
537-
538-
return sectorNumber, respCh, cancelF
539-
}
540-
541-
func (tm *TestUnmanagedMiner) OnboardCCSectorWithRealProofs(ctx context.Context, proofType abi.RegisteredSealProof) (abi.SectorNumber, chan WindowPostResp,
447+
func (tm *TestUnmanagedMiner) OnboardCCSector(ctx context.Context, proofType abi.RegisteredSealProof) (abi.SectorNumber, chan WindowPostResp,
542448
context.CancelFunc) {
543449
req := require.New(tm.t)
544450
sectorNumber := tm.currentSectorNum
@@ -549,11 +455,16 @@ func (tm *TestUnmanagedMiner) OnboardCCSectorWithRealProofs(ctx context.Context,
549455
// Step 1: Wait for the pre-commitseal randomness to be available (we can only draw seal randomness from tipsets that have already achieved finality)
550456
preCommitSealRand := tm.waitPreCommitSealRandomness(ctx, sectorNumber)
551457

552-
// Step 2: Write empty bytes that we want to seal i.e. create our CC sector
553-
tm.makeAndSaveCCSector(ctx, sectorNumber)
458+
if !tm.mockProofs {
459+
// Step 2: Write empty bytes that we want to seal i.e. create our CC sector
460+
tm.makeAndSaveCCSector(ctx, sectorNumber)
554461

555-
// Step 3: Generate a Pre-Commit for the CC sector -> this persists the proof on the `TestUnmanagedMiner` Miner State
556-
tm.generatePreCommit(ctx, sectorNumber, preCommitSealRand, proofType, []abi.PieceInfo{})
462+
// Step 3: Generate a Pre-Commit for the CC sector -> this persists the proof on the `TestUnmanagedMiner` Miner State
463+
tm.generatePreCommit(ctx, sectorNumber, preCommitSealRand, proofType, []abi.PieceInfo{})
464+
} else {
465+
// skip the above steps and use a mock sealed CID
466+
tm.sealedCids[sectorNumber] = cid.MustParse("bagboea4b5abcatlxechwbp7kjpjguna6r6q7ejrhe6mdp3lf34pmswn27pkkiekz")
467+
}
557468

558469
// Step 4 : Submit the Pre-Commit to the network
559470
r, err := tm.submitMessage(ctx, &miner14.PreCommitSectorBatchParams2{
@@ -571,7 +482,10 @@ func (tm *TestUnmanagedMiner) OnboardCCSectorWithRealProofs(ctx context.Context,
571482
// Step 5: Generate a ProveCommit for the CC sector
572483
waitSeedRandomness := tm.proveCommitWaitSeed(ctx, sectorNumber)
573484

574-
proveCommit := tm.generateProveCommit(ctx, sectorNumber, proofType, waitSeedRandomness, []abi.PieceInfo{})
485+
proveCommit := []byte{0xde, 0xad, 0xbe, 0xef} // mock prove commit
486+
if !tm.mockProofs {
487+
proveCommit = tm.generateProveCommit(ctx, sectorNumber, proofType, waitSeedRandomness, []abi.PieceInfo{})
488+
}
575489

576490
// Step 6: Submit the ProveCommit to the network
577491
tm.t.Log("Submitting ProveCommitSector ...")
@@ -589,12 +503,12 @@ func (tm *TestUnmanagedMiner) OnboardCCSectorWithRealProofs(ctx context.Context,
589503
respCh := make(chan WindowPostResp, 1)
590504

591505
wdCtx, cancelF := context.WithCancel(ctx)
592-
go tm.wdPostLoop(wdCtx, sectorNumber, respCh, false, tm.sealedCids[sectorNumber], tm.sealedSectorPaths[sectorNumber], tm.cacheDirPaths[sectorNumber])
506+
go tm.wdPostLoop(wdCtx, sectorNumber, respCh, tm.sealedCids[sectorNumber], tm.sealedSectorPaths[sectorNumber], tm.cacheDirPaths[sectorNumber])
593507

594508
return sectorNumber, respCh, cancelF
595509
}
596510

597-
func (tm *TestUnmanagedMiner) wdPostLoop(ctx context.Context, sectorNumber abi.SectorNumber, respCh chan WindowPostResp, withMockProofs bool, sealedCid cid.Cid, sealedPath, cacheDir string) {
511+
func (tm *TestUnmanagedMiner) wdPostLoop(ctx context.Context, sectorNumber abi.SectorNumber, respCh chan WindowPostResp, sealedCid cid.Cid, sealedPath, cacheDir string) {
598512
go func() {
599513
var firstPost bool
600514

@@ -635,7 +549,7 @@ func (tm *TestUnmanagedMiner) wdPostLoop(ctx context.Context, sectorNumber abi.S
635549
}
636550
}
637551

638-
err = tm.submitWindowPost(ctx, sectorNumber, withMockProofs, sealedCid, sealedPath, cacheDir)
552+
err = tm.submitWindowPost(ctx, sectorNumber, sealedCid, sealedPath, cacheDir)
639553
writeRespF(err) // send an error, or first post, or nothing if no error and this isn't the first post
640554
postCount++
641555
tm.t.Logf("Sector %d: WindowPoSt #%d submitted", sectorNumber, postCount)
@@ -675,7 +589,7 @@ func (tm *TestUnmanagedMiner) SubmitPostDispute(ctx context.Context, sectorNumbe
675589
return err
676590
}
677591

678-
func (tm *TestUnmanagedMiner) submitWindowPost(ctx context.Context, sectorNumber abi.SectorNumber, withMockProofs bool, sealedCid cid.Cid, sealedPath, cacheDir string) error {
592+
func (tm *TestUnmanagedMiner) submitWindowPost(ctx context.Context, sectorNumber abi.SectorNumber, sealedCid cid.Cid, sealedPath, cacheDir string) error {
679593
tm.t.Logf("Miner(%s): WindowPoST(%d): Running WindowPoSt ...\n", tm.ActorAddr, sectorNumber)
680594

681595
head, err := tm.FullNode.ChainHead(ctx)
@@ -698,7 +612,7 @@ func (tm *TestUnmanagedMiner) submitWindowPost(ctx context.Context, sectorNumber
698612
}
699613

700614
var proofBytes []byte
701-
if withMockProofs {
615+
if tm.mockProofs {
702616
proofBytes = []byte{0xde, 0xad, 0xbe, 0xef}
703617
} else {
704618
proofBytes, err = tm.generateWindowPost(ctx, sectorNumber, sealedCid, sealedPath, cacheDir)
@@ -1070,3 +984,50 @@ func requireTempFile(t *testing.T, fileContentsReader io.Reader, size uint64) *o
1070984

1071985
return tempFile
1072986
}
987+
988+
func (tm *TestUnmanagedMiner) WaitTillActivatedAndAssertPower(
989+
ctx context.Context,
990+
respCh chan WindowPostResp,
991+
sector abi.SectorNumber,
992+
) {
993+
994+
// wait till sector is activated
995+
select {
996+
case resp := <-respCh:
997+
require.NoError(tm.t, resp.Error)
998+
require.True(tm.t, resp.Posted)
999+
case <-ctx.Done():
1000+
tm.t.Fatal("timed out waiting for sector activation")
1001+
}
1002+
1003+
// Fetch on-chain sector properties
1004+
head, err := tm.FullNode.ChainHead(ctx)
1005+
require.NoError(tm.t, err)
1006+
1007+
soi, err := tm.FullNode.StateSectorGetInfo(ctx, tm.ActorAddr, sector, head.Key())
1008+
require.NoError(tm.t, err)
1009+
tm.t.Logf("Miner %s SectorOnChainInfo %d: %+v", tm.ActorAddr.String(), sector, soi)
1010+
1011+
_ = tm.FullNode.WaitTillChain(ctx, HeightAtLeast(head.Height()+5))
1012+
1013+
tm.t.Log("Checking power after PoSt ...")
1014+
1015+
// Miner B should now have power
1016+
tm.AssertPower(ctx, uint64(tm.options.sectorSize), uint64(tm.options.sectorSize))
1017+
1018+
if tm.mockProofs {
1019+
// WindowPost Dispute should succeed as we are using mock proofs
1020+
err := tm.SubmitPostDispute(ctx, sector)
1021+
require.NoError(tm.t, err)
1022+
} else {
1023+
// WindowPost Dispute should fail
1024+
tm.AssertDisputeFails(ctx, sector)
1025+
}
1026+
}
1027+
1028+
func (tm *TestUnmanagedMiner) AssertDisputeFails(ctx context.Context, sector abi.SectorNumber) {
1029+
err := tm.SubmitPostDispute(ctx, sector)
1030+
require.Error(tm.t, err)
1031+
require.Contains(tm.t, err.Error(), "failed to dispute valid post")
1032+
require.Contains(tm.t, err.Error(), "(RetCode=16)")
1033+
}

0 commit comments

Comments
 (0)