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

fix(libs/pidstore): Detect corruption on startup and reset pidstore #3132

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions libs/pidstore/pidstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewPeerIDStore(ctx context.Context, ds datastore.Datastore) (*PeerIDStore,
pidstore := &PeerIDStore{
ds: namespace.Wrap(ds, storePrefix),
}

// check if pidstore is already initialized, and if not,
// initialize the pidstore
exists, err := pidstore.ds.Has(ctx, peersKey)
Expand All @@ -37,6 +38,14 @@ func NewPeerIDStore(ctx context.Context, ds datastore.Datastore) (*PeerIDStore,
if !exists {
return pidstore, pidstore.Put(ctx, []peer.ID{})
}

// if pidstore exists, ensure its contents are uncorrupted
_, err = pidstore.Load(ctx)
if err != nil {
log.Warn("pidstore: corrupted pidstore detected, resetting...", "err", err)
return pidstore, pidstore.reset(ctx)
}

return pidstore, nil
}

Expand Down Expand Up @@ -75,3 +84,14 @@ func (p *PeerIDStore) Put(ctx context.Context, peers []peer.ID) error {
log.Infow("Persisted peers successfully", "amount", len(peers))
return nil
}

// reset resets the pidstore in case of corruption.
func (p *PeerIDStore) reset(ctx context.Context) error {
log.Warn("pidstore: resetting the pidstore...")
err := p.ds.Delete(ctx, peersKey)
if err != nil {
return fmt.Errorf("pidstore: error resetting datastore: %w", err)
}

return p.Put(ctx, []peer.ID{})
}
22 changes: 22 additions & 0 deletions libs/pidstore/pidstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
"github.com/ipfs/go-datastore/sync"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
Expand All @@ -29,6 +30,27 @@ func TestPutLoad(t *testing.T) {
})
}

// TestCorruptedPidstore tests whether a pidstore can detect
// corruption and reset itself on construction.
func TestCorruptedPidstore(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer t.Cleanup(cancel)

ds := sync.MutexWrap(datastore.NewMapDatastore())

// intentionally corrupt the store
wrappedDS := namespace.Wrap(ds, storePrefix)
err := wrappedDS.Put(ctx, peersKey, []byte("corrupted"))
require.NoError(t, err)

pidstore, err := NewPeerIDStore(ctx, ds)
require.NoError(t, err)

got, err := pidstore.Load(ctx)
require.NoError(t, err)
assert.Equal(t, []peer.ID{}, got)
}

func testPutLoad(ctx context.Context, ds datastore.Datastore, t *testing.T) {
peerstore, err := NewPeerIDStore(ctx, ds)
require.NoError(t, err)
Expand Down
Loading