Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Clean up all files upon stop smeshing #4732

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions activation/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"encoding/hex"
"errors"
"fmt"
"io/fs"
"os"
"sync"
"time"
Expand Down Expand Up @@ -251,6 +252,19 @@
b.log.With().Error("failed to delete post files", log.Err(err))
return err
}
if err := discardBuilderState(b.nipostBuilder.DataDir()); err != nil && !errors.Is(err, fs.ErrNotExist) {
b.log.With().Error("failed to delete builder state", log.Err(err))
return err
}

Check warning on line 258 in activation/activation.go

View check run for this annotation

Codecov / codecov/patch

activation/activation.go#L256-L258

Added lines #L256 - L258 were not covered by tests
if err := discardNipostChallenge(b.nipostBuilder.DataDir()); err != nil && !errors.Is(err, fs.ErrNotExist) {
b.log.With().Error("failed to delete nipost challenge", log.Err(err))
return err
}

Check warning on line 262 in activation/activation.go

View check run for this annotation

Codecov / codecov/patch

activation/activation.go#L260-L262

Added lines #L260 - L262 were not covered by tests
if err := discardPost(b.nipostBuilder.DataDir()); err != nil && !errors.Is(err, fs.ErrNotExist) {
b.log.With().Error("failed to delete post", log.Err(err))
return err
}

Check warning on line 266 in activation/activation.go

View check run for this annotation

Codecov / codecov/patch

activation/activation.go#L264-L266

Added lines #L264 - L266 were not covered by tests

return nil
default:
return fmt.Errorf("failed to stop post data creation session: %w", err)
Expand Down
39 changes: 39 additions & 0 deletions activation/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,45 @@ func TestBuilder_RestartSmeshing(t *testing.T) {
})
}

func TestBuilder_StopSmeshing_Delete(t *testing.T) {
tab := newTestBuilder(t)

tab.mpost.EXPECT().PrepareInitializer(gomock.Any(), gomock.Any()).AnyTimes()
tab.mpost.EXPECT().StartSession(gomock.Any()).DoAndReturn(func(ctx context.Context) error {
// wait for stop to be called
<-ctx.Done()
return ctx.Err()
}).AnyTimes()

// Create state files
require.NoError(t, saveBuilderState(tab.nipostBuilder.DataDir(), &types.NIPostBuilderState{}))
require.NoError(t, savePost(tab.nipostBuilder.DataDir(), &types.Post{}))
require.NoError(t, SaveNipostChallenge(tab.nipostBuilder.DataDir(), &types.NIPostChallenge{}))
files, err := os.ReadDir(tab.nipostBuilder.DataDir())
require.NoError(t, err)
require.Len(t, files, 3) // 3 state files created

require.NoError(t, tab.StartSmeshing(types.Address{}, PostSetupOpts{}))
require.NoError(t, tab.StopSmeshing(false))
files, err = os.ReadDir(tab.nipostBuilder.DataDir())
require.NoError(t, err)
require.Len(t, files, 3) // state files still present

require.NoError(t, tab.StartSmeshing(types.Address{}, PostSetupOpts{}))
tab.mpost.EXPECT().Reset().Return(nil)
require.NoError(t, tab.StopSmeshing(true))
files, err = os.ReadDir(tab.nipostBuilder.DataDir())
require.NoError(t, err)
require.Len(t, files, 0) // state files deleted

require.NoError(t, tab.StartSmeshing(types.Address{}, PostSetupOpts{}))
tab.mpost.EXPECT().Reset().Return(nil)
require.NoError(t, tab.StopSmeshing(true)) // no-op
files, err = os.ReadDir(tab.nipostBuilder.DataDir())
require.NoError(t, err)
require.Len(t, files, 0) // state files still deleted
}

func TestBuilder_StoppingSmeshingBefore_Initialized(t *testing.T) {
tab := newTestBuilder(t)
tab.mpost.EXPECT().PrepareInitializer(gomock.Any(), gomock.Any()).AnyTimes()
Expand Down
16 changes: 16 additions & 0 deletions activation/nipost_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ func loadBuilderState(dir string) (*types.NIPostBuilderState, error) {
return &state, nil
}

func discardBuilderState(dir string) error {
filename := filepath.Join(dir, builderFilename)
if err := os.Remove(filename); err != nil {
return fmt.Errorf("discarding nipost builder state: %w", err)
}
return nil
}

func savePost(dir string, post *types.Post) error {
if err := save(filepath.Join(dir, postFilename), post); err != nil {
return fmt.Errorf("saving post: %w", err)
Expand All @@ -167,3 +175,11 @@ func loadPost(dir string) (*types.Post, error) {
}
return &post, nil
}

func discardPost(dir string) error {
filename := filepath.Join(dir, postFilename)
if err := os.Remove(filename); err != nil {
return fmt.Errorf("discarding post: %w", err)
}
return nil
}