From 59ff6dd99c3773ca0c7fc9ab97dbf3932628dd78 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 16 Dec 2024 18:49:33 -0800 Subject: [PATCH] rhp4: add test prune --- internal/integration/rhp/v4/rhp4_test.go | 68 ++++++++++++++++++------ internal/testutil/testutil.go | 2 +- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/internal/integration/rhp/v4/rhp4_test.go b/internal/integration/rhp/v4/rhp4_test.go index 83dbc2bf..7ad5f5fe 100644 --- a/internal/integration/rhp/v4/rhp4_test.go +++ b/internal/integration/rhp/v4/rhp4_test.go @@ -6,6 +6,7 @@ import ( "net" "path/filepath" "reflect" + "slices" "strings" "sync" "testing" @@ -1118,7 +1119,7 @@ func TestPrune(t *testing.T) { RenterAddress: w.Address(), Allowance: renterAllowance, Collateral: hostCollateral, - ProofHeight: cm.Tip().Height + 50, + ProofHeight: cm.Tip().Height + proto4.TempSectorDuration + 10, }) if err != nil { t.Fatal(err) @@ -1144,29 +1145,64 @@ func TestPrune(t *testing.T) { tokenSigHash := token.SigHash() token.Signature = renterKey.SignHash(tokenSigHash) - data := frand.Bytes(1024) + tempExpirationHeight := cm.Tip().Height + proto4.TempSectorDuration + roots := make([]types.Hash256, 10) + for i := 0; i < len(roots); i++ { + data := frand.Bytes(1024) - // store the sector - writeResult, err := rhp4.RPCWriteSector(context.Background(), transport, settings.Prices, token, bytes.NewReader(data), uint64(len(data))) - if err != nil { - t.Fatal(err) + // store the sector + writeResult, err := rhp4.RPCWriteSector(context.Background(), transport, settings.Prices, token, bytes.NewReader(data), uint64(len(data))) + if err != nil { + t.Fatal(err) + } + roots[i] = writeResult.Root } - // verify the sector root - var sector [proto4.SectorSize]byte - copy(sector[:], data) - if writeResult.Root != proto4.SectorRoot(§or) { - t.Fatal("root mismatch") + assertSectors := func(t *testing.T, available, deleted []types.Hash256) { + t.Helper() + + buf := bytes.NewBuffer(nil) + for _, root := range available { + buf.Reset() + + _, err := rhp4.RPCReadSector(context.Background(), transport, settings.Prices, token, buf, root, 0, proto4.SectorSize) + if err != nil { + t.Fatal(err) + } else if r2 := proto4.SectorRoot((*[4194304]byte)(buf.Bytes())); root != r2 { + t.Fatalf("expected root %q, got %q", root, r2) + } + } + + for _, root := range deleted { + buf.Reset() + + _, err := rhp4.RPCReadSector(context.Background(), transport, settings.Prices, token, buf, root, 0, proto4.SectorSize) + if err == nil || !strings.Contains(err.Error(), "sector not found") { + t.Fatalf("expected err %q, got %q", proto4.ErrSectorNotFound, err) + } + } } - // read the sector back - buf := bytes.NewBuffer(nil) - _, err = rhp4.RPCReadSector(context.Background(), transport, settings.Prices, token, buf, writeResult.Root, 0, 64) + tempSectors, contractSectors := roots[:len(roots)/2], roots[len(roots)/2:] + + appendResult, err := rhp4.RPCAppendSectors(context.Background(), transport, cm.TipState(), settings.Prices, renterKey, revision, contractSectors) if err != nil { t.Fatal(err) - } else if !bytes.Equal(buf.Bytes(), data[:64]) { - t.Fatal("data mismatch") + } else if !slices.Equal(appendResult.Sectors, contractSectors) { + t.Fatal("expect contract sectors") } + + assertSectors(t, roots, nil) + + // mine until the temp sector expire + testutil.MineAndSync(t, hn, types.VoidAddress, int(tempExpirationHeight-cm.Tip().Height)+1) + time.Sleep(time.Second) // wait for the sectors to be pruned + assertSectors(t, contractSectors, tempSectors) + + // mine until the contract sector expires + testutil.MineAndSync(t, hn, types.VoidAddress, int(revision.Revision.ExpirationHeight-cm.Tip().Height)+1) + time.Sleep(time.Second) + assertSectors(t, nil, roots) } func BenchmarkWrite(b *testing.B) { diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go index ff0e25b7..fac6ddde 100644 --- a/internal/testutil/testutil.go +++ b/internal/testutil/testutil.go @@ -167,7 +167,7 @@ func NewHostNode(t testing.TB, pk types.PrivateKey, network *consensus.Network, } t.Cleanup(func() { wm.Close() }) - vm, err := storage.NewVolumeManager(cn.Store, storage.WithLogger(log.Named("storage"))) + vm, err := storage.NewVolumeManager(cn.Store, storage.WithLogger(log.Named("storage")), storage.WithPruneInterval(500*time.Millisecond)) if err != nil { t.Fatal("failed to create volume manager:", err) }