-
Notifications
You must be signed in to change notification settings - Fork 971
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(nodebuilder/pruner): Move all migration logic to nodebuilder…
… pruner module
- Loading branch information
Showing
6 changed files
with
209 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package pruner | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ipfs/go-datastore" | ||
|
||
fullavail "github.com/celestiaorg/celestia-node/share/availability/full" | ||
) | ||
|
||
var ( | ||
storePrefix = datastore.NewKey("full_avail") | ||
previousModeKey = datastore.NewKey("previous_run") | ||
pruned = []byte("pruned") | ||
archival = []byte("archival") | ||
) | ||
|
||
// convertFromArchivalToPruned ensures that a node has not been run with pruning enabled before | ||
// cannot revert to archival mode. It returns true only if the node is converting to | ||
// pruned mode for the first time. | ||
func convertFromArchivalToPruned(ctx context.Context, cfg *Config, ds datastore.Datastore) (bool, error) { | ||
prevMode, err := ds.Get(ctx, previousModeKey) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if bytes.Equal(prevMode, pruned) && !cfg.EnableService { | ||
return false, fullavail.ErrDisallowRevertToArchival | ||
} | ||
|
||
if bytes.Equal(prevMode, archival) && cfg.EnableService { | ||
// allow conversion from archival to pruned | ||
err = ds.Put(ctx, previousModeKey, pruned) | ||
if err != nil { | ||
return false, fmt.Errorf("share/availability/full: failed to updated pruning mode in "+ | ||
"datastore: %w", err) | ||
} | ||
return true, nil | ||
} | ||
|
||
// no changes in pruning mode | ||
return false, nil | ||
} | ||
|
||
// detectFirstRun is a temporary function that serves to assist migration to the refactored pruner | ||
// implementation (v0.21.0). It checks if the node has been run with pruning enabled before by checking | ||
// if the pruner service ran before, and disallows running as an archival node in the case it has. | ||
// | ||
// TODO @renaynay: remove this function after a few releases. | ||
func detectFirstRun(ctx context.Context, cfg *Config, ds datastore.Datastore, lastPrunedHeight uint64) error { | ||
exists, err := ds.Has(ctx, previousModeKey) | ||
if err != nil { | ||
return fmt.Errorf("share/availability/full: failed to check previous pruned run in "+ | ||
"datastore: %w", err) | ||
} | ||
if exists { | ||
return nil | ||
} | ||
|
||
if !cfg.EnableService { | ||
if lastPrunedHeight > 1 { | ||
return fullavail.ErrDisallowRevertToArchival | ||
} | ||
|
||
return ds.Put(ctx, previousModeKey, archival) | ||
} | ||
|
||
return ds.Put(ctx, previousModeKey, pruned) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package pruner | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ipfs/go-datastore" | ||
"github.com/ipfs/go-datastore/namespace" | ||
ds_sync "github.com/ipfs/go-datastore/sync" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
fullavail "github.com/celestiaorg/celestia-node/share/availability/full" | ||
) | ||
|
||
// TestDisallowRevertArchival tests that a node that has been previously run | ||
// with full pruning cannot convert back into an "archival" node | ||
func TestDisallowRevertArchival(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
t.Cleanup(cancel) | ||
|
||
// create a pruned node instance (non-archival) for the first time | ||
cfg := &Config{EnableService: true} | ||
ds := ds_sync.MutexWrap(datastore.NewMapDatastore()) | ||
nsWrapped := namespace.Wrap(ds, storePrefix) | ||
err := nsWrapped.Put(ctx, previousModeKey, pruned) | ||
require.NoError(t, err) | ||
|
||
convert, err := convertFromArchivalToPruned(ctx, cfg, nsWrapped) | ||
assert.NoError(t, err) | ||
assert.False(t, convert) | ||
// ensure availability impl recorded the pruned run | ||
prevMode, err := nsWrapped.Get(ctx, previousModeKey) | ||
require.NoError(t, err) | ||
assert.Equal(t, pruned, prevMode) | ||
|
||
// now change to archival mode | ||
cfg.EnableService = false | ||
|
||
// ensure failure | ||
convert, err = convertFromArchivalToPruned(ctx, cfg, nsWrapped) | ||
assert.Error(t, err) | ||
assert.ErrorIs(t, err, fullavail.ErrDisallowRevertToArchival) | ||
assert.False(t, convert) | ||
|
||
// ensure the node can still run in pruned mode | ||
cfg.EnableService = true | ||
convert, err = convertFromArchivalToPruned(ctx, cfg, nsWrapped) | ||
assert.NoError(t, err) | ||
assert.False(t, convert) | ||
} | ||
|
||
// TestAllowConversionFromArchivalToPruned tests that a node that has been previously run | ||
// in archival mode can convert to a pruned node | ||
func TestAllowConversionFromArchivalToPruned(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
t.Cleanup(cancel) | ||
|
||
ds := ds_sync.MutexWrap(datastore.NewMapDatastore()) | ||
nsWrapped := namespace.Wrap(ds, storePrefix) | ||
err := nsWrapped.Put(ctx, previousModeKey, archival) | ||
require.NoError(t, err) | ||
|
||
cfg := &Config{EnableService: false} | ||
|
||
convert, err := convertFromArchivalToPruned(ctx, cfg, nsWrapped) | ||
assert.NoError(t, err) | ||
assert.False(t, convert) | ||
|
||
cfg.EnableService = true | ||
|
||
convert, err = convertFromArchivalToPruned(ctx, cfg, nsWrapped) | ||
assert.NoError(t, err) | ||
assert.True(t, convert) | ||
|
||
prevMode, err := nsWrapped.Get(ctx, previousModeKey) | ||
require.NoError(t, err) | ||
assert.Equal(t, pruned, prevMode) | ||
} | ||
|
||
func TestDetectFirstRun(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
t.Run("FirstRunArchival", func(t *testing.T) { | ||
ds := ds_sync.MutexWrap(datastore.NewMapDatastore()) | ||
nsWrapped := namespace.Wrap(ds, storePrefix) | ||
|
||
cfg := &Config{EnableService: false} | ||
|
||
err := detectFirstRun(ctx, cfg, nsWrapped, 1) | ||
assert.NoError(t, err) | ||
|
||
prevMode, err := nsWrapped.Get(ctx, previousModeKey) | ||
require.NoError(t, err) | ||
assert.Equal(t, archival, prevMode) | ||
}) | ||
|
||
t.Run("FirstRunPruned", func(t *testing.T) { | ||
ds := ds_sync.MutexWrap(datastore.NewMapDatastore()) | ||
nsWrapped := namespace.Wrap(ds, storePrefix) | ||
|
||
cfg := &Config{EnableService: true} | ||
|
||
err := detectFirstRun(ctx, cfg, nsWrapped, 1) | ||
assert.NoError(t, err) | ||
|
||
prevMode, err := nsWrapped.Get(ctx, previousModeKey) | ||
require.NoError(t, err) | ||
assert.Equal(t, pruned, prevMode) | ||
}) | ||
|
||
t.Run("RevertToArchivalNotAllowed", func(t *testing.T) { | ||
ds := ds_sync.MutexWrap(datastore.NewMapDatastore()) | ||
nsWrapped := namespace.Wrap(ds, storePrefix) | ||
|
||
cfg := &Config{EnableService: false} | ||
|
||
err := detectFirstRun(ctx, cfg, nsWrapped, 500) | ||
assert.Error(t, err) | ||
assert.ErrorIs(t, err, fullavail.ErrDisallowRevertToArchival) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.