-
Notifications
You must be signed in to change notification settings - Fork 959
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
152 additions
and
115 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package full | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ipfs/go-datastore" | ||
"github.com/ipfs/go-datastore/namespace" | ||
) | ||
|
||
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, ds datastore.Datastore, isArchival bool) (bool, error) { | ||
ds = namespace.Wrap(ds, storePrefix) | ||
|
||
prevMode, err := ds.Get(ctx, previousModeKey) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if bytes.Equal(prevMode, pruned) && isArchival { | ||
return false, ErrDisallowRevertToArchival | ||
} | ||
|
||
if bytes.Equal(prevMode, archival) && !isArchival { | ||
// 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 | ||
} |
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,72 @@ | ||
package full | ||
|
||
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" | ||
) | ||
|
||
// 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) | ||
|
||
ds := ds_sync.MutexWrap(datastore.NewMapDatastore()) | ||
|
||
// create a pruned node instance (non-archival) for the first time | ||
nsWrapped := namespace.Wrap(ds, storePrefix) | ||
err := nsWrapped.Put(ctx, previousModeKey, pruned) | ||
require.NoError(t, err) | ||
|
||
convert, err := ConvertFromArchivalToPruned(ctx, ds, false) | ||
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, ensure failure | ||
convert, err = ConvertFromArchivalToPruned(ctx, ds, true) | ||
assert.Error(t, err) | ||
assert.ErrorIs(t, err, ErrDisallowRevertToArchival) | ||
assert.False(t, convert) | ||
|
||
// ensure the node can still run in pruned mode | ||
convert, err = ConvertFromArchivalToPruned(ctx, ds, false) | ||
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()) | ||
|
||
// make an archival node | ||
nsWrapped := namespace.Wrap(ds, storePrefix) | ||
err := nsWrapped.Put(ctx, previousModeKey, archival) | ||
require.NoError(t, err) | ||
|
||
convert, err := ConvertFromArchivalToPruned(ctx, ds, true) | ||
assert.NoError(t, err) | ||
assert.False(t, convert) | ||
|
||
// turn into a pruned node | ||
convert, err = ConvertFromArchivalToPruned(ctx, ds, false) | ||
assert.NoError(t, err) | ||
assert.True(t, convert) | ||
|
||
prevMode, err := nsWrapped.Get(ctx, previousModeKey) | ||
require.NoError(t, err) | ||
assert.Equal(t, pruned, prevMode) | ||
} |