diff --git a/initialization/initialization.go b/initialization/initialization.go index e72ef226c..5acd705c2 100644 --- a/initialization/initialization.go +++ b/initialization/initialization.go @@ -295,9 +295,6 @@ func (init *Initializer) Initialize(ctx context.Context) error { zap.Int("firstFileIndex", layout.FirstFileIdx), zap.Int("lastFileIndex", layout.LastFileIdx), ) - if err := removeRedundantFiles(init.cfg, init.opts, init.logger); err != nil { - return err - } numLabels := uint64(init.opts.NumUnits) * init.cfg.LabelsPerUnit difficulty := init.powDifficultyFunc(numLabels) @@ -395,36 +392,6 @@ func (init *Initializer) Initialize(ctx context.Context) error { return fmt.Errorf("no nonce found") } -func removeRedundantFiles(cfg config.Config, opts config.InitOpts, logger *zap.Logger) error { - // Go over all postdata_N.bin files in the data directory and remove the ones that are not needed. - // The files with indices from 0 to init.opts.TotalFiles(init.cfg.LabelsPerUnit) - 1 are preserved. - // The rest are redundant and can be removed. - maxFileIndex := opts.TotalFiles(cfg.LabelsPerUnit) - 1 - logger.Debug("attempting to remove redundant files above index", zap.Int("maxFileIndex", maxFileIndex)) - - files, err := os.ReadDir(opts.DataDir) - if err != nil { - return err - } - for _, file := range files { - name := file.Name() - fileIndex, err := shared.ParseFileIndex(name) - if err != nil && name != MetadataFileName { - // TODO(mafa): revert back to warning, see https://github.com/spacemeshos/go-spacemesh/issues/4789 - logger.Debug("found unrecognized file", zap.String("fileName", name)) - continue - } - if fileIndex > maxFileIndex { - logger.Info("removing redundant file", zap.String("fileName", name)) - path := filepath.Join(opts.DataDir, name) - if err := os.Remove(path); err != nil { - return fmt.Errorf("failed to delete file (%v): %w", path, err) - } - } - } - return nil -} - func (init *Initializer) NumLabelsWritten() uint64 { return init.numLabelsWritten.Load() } @@ -670,12 +637,18 @@ func (init *Initializer) verifyMetadata(m *shared.PostMetadata) error { if init.opts.NumUnits > m.NumUnits { return ConfigMismatchError{ Param: "NumUnits", - Expected: fmt.Sprintf(">= %d", init.opts.NumUnits), + Expected: fmt.Sprintf("%d", init.opts.NumUnits), Found: fmt.Sprintf("%d", m.NumUnits), DataDir: init.opts.DataDir, } } + if init.opts.NumUnits < m.NumUnits { + init.logger.Warn("initialization: found more units than expected, did you forget to delete some files?", + zap.Uint32("expected", init.opts.NumUnits), + zap.Uint32("found", m.NumUnits)) + } + return nil } diff --git a/initialization/initialization_test.go b/initialization/initialization_test.go index e306f60cf..5dc2d0e9f 100644 --- a/initialization/initialization_test.go +++ b/initialization/initialization_test.go @@ -531,7 +531,9 @@ func TestInitialize_RedundantFiles(t *testing.T) { r.NoError(err) newLayout, err := deriveFilesLayout(cfg, newOpts) r.NoError(err) - r.Equal(newLayout.NumFiles(), numFiles) + + // we allow extra files + r.LessOrEqual(newLayout.NumFiles(), numFiles) r.Less(newLayout.NumFiles(), layout.NumFiles()) cancel() @@ -664,6 +666,7 @@ func TestValidateMetadata(t *testing.T) { r := require.New(t) cfg, opts := getTestConfig(t) + opts.NumUnits++ init, err := NewInitializer( WithNodeId(nodeId), @@ -735,6 +738,19 @@ func TestValidateMetadata(t *testing.T) { ) r.ErrorAs(err, &errConfigMismatch) r.Equal("NumUnits", errConfigMismatch.Param) + + // Attempt to initialize with a lower `opts.NumUnits`. + newOpts = opts + newOpts.NumUnits-- + _, err = NewInitializer( + WithNodeId(nodeId), + WithCommitmentAtxId(commitmentAtxId), + WithConfig(cfg), + WithInitOpts(newOpts), + WithLogger(zaptest.NewLogger(t, zaptest.Level(zap.DebugLevel))), + ) + // this is allowed + r.NoError(err) } func TestStop(t *testing.T) { @@ -1094,33 +1110,3 @@ func TestInitializeLastFileIsSmaller(t *testing.T) { r.NoError(err) r.Equal(cfg.UnitSize(), uint64(file.Size())) } - -func TestRemoveRedundantFiles(t *testing.T) { - cfg := config.DefaultConfig() - - opts := config.DefaultInitOpts() - opts.DataDir = t.TempDir() - opts.NumUnits = 3 - opts.MaxFileSize = 2 * cfg.UnitSize() - - expectedFilesCount := opts.TotalFiles(cfg.LabelsPerUnit) - // Create 2 redundant files - for i := 0; i < expectedFilesCount+2; i++ { - f, err := os.Create(filepath.Join(opts.DataDir, shared.InitFileName(i))) - require.NoError(t, err) - _, err = f.Write([]byte("test")) - require.NoError(t, err) - require.NoError(t, f.Close()) - } - - removeRedundantFiles(cfg, opts, zap.NewNop()) - - files, err := os.ReadDir(opts.DataDir) - require.NoError(t, err) - require.Len(t, files, expectedFilesCount) - - for i := 0; i < expectedFilesCount; i++ { - _, err := os.Stat(filepath.Join(opts.DataDir, shared.InitFileName(i))) - require.NoError(t, err) - } -}