Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion p2p/host/peerstore/pstoreds/cyclic_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func newCyclicBatch(ds ds.Batching, threshold int) (ds.Batch, error) {
if err != nil {
return nil, err
}
return &cyclicBatch{Batch: batch, ds: ds}, nil
return &cyclicBatch{Batch: batch, ds: ds, threshold: threshold}, nil
}

func (cb *cyclicBatch) cycle() (err error) {
Expand Down
32 changes: 32 additions & 0 deletions p2p/host/peerstore/pstoreds/cyclic_batch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package pstoreds

import (
"context"
"testing"

ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/require"
)

// TestCyclicBatchThreshold verifies that cyclicBatch respects the threshold parameter.
func TestCyclicBatchThreshold(t *testing.T) {
store := dssync.MutexWrap(ds.NewMapDatastore())

cb, err := newCyclicBatch(store, 3) // threshold = 3
require.NoError(t, err)

ctx := context.Background()

err = cb.Put(ctx, ds.NewKey("key1"), []byte("value1"))
require.NoError(t, err)
err = cb.Put(ctx, ds.NewKey("key2"), []byte("value2"))
require.NoError(t, err)
// Manually commit the batch
err = cb.Commit(ctx)
require.NoError(t, err)

val, err := store.Get(ctx, ds.NewKey("key1"))
require.NoError(t, err)
require.Equal(t, []byte("value1"), val)
}
14 changes: 11 additions & 3 deletions p2p/host/peerstore/pstoreds/peerstore.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package pstoreds

import (
Expand Down Expand Up @@ -127,9 +127,17 @@

ids := make(peer.IDSlice, 0, len(idset))
for id := range idset {
pid, _ := base32.RawStdEncoding.DecodeString(id)
id, _ := peer.IDFromBytes(pid)
ids = append(ids, id)
pid, err := base32.RawStdEncoding.DecodeString(id)
if err != nil {
log.Debugf("failed to decode peer id %s from base32: %v; skipping", id, err)
continue
}
peerID, err := peer.IDFromBytes(pid)
if err != nil {
log.Debugf("failed to create peer id from bytes for %s: %v; skipping", id, err)
continue
}
ids = append(ids, peerID)
}
return ids, nil
}
Expand Down
49 changes: 49 additions & 0 deletions p2p/host/peerstore/pstoreds/peerstore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package pstoreds

import (
"context"
"strings"
"testing"

ds "github.com/ipfs/go-datastore"
query "github.com/ipfs/go-datastore/query"
dssync "github.com/ipfs/go-datastore/sync"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-base32"
"github.com/stretchr/testify/require"
)

// TestUniquePeerIds verifies that uniquePeerIds correctly handles valid and invalid peer IDs.
func TestUniquePeerIds(t *testing.T) {
store := dssync.MutexWrap(ds.NewMapDatastore())

ctx := context.Background()

// Insert a garbage peer ID (random invalid bytes encoded to base32)
badKey := base32.RawStdEncoding.EncodeToString([]byte("garbagebytes"))
err := store.Put(ctx, ds.NewKey("/peers/"+badKey), []byte{})
require.NoError(t, err)

// Insert a valid peer ID
validPeerID, err := peer.Decode("12D3KooWPshfDXh6bsy1ru6CkMBuqoK2LRpXsPTmGVJ4AzD26tVn")
require.NoError(t, err)

validBase32 := base32.RawStdEncoding.EncodeToString([]byte(validPeerID))
err = store.Put(ctx, ds.NewKey("/peers/"+validBase32), []byte{})
require.NoError(t, err)

ids, err := uniquePeerIds(store, ds.NewKey("/peers"), func(result query.Result) string {
return result.Key[strings.LastIndex(result.Key, "/")+1:]
})
require.NoError(t, err)

// Instead of checking length 1, check that our validPeerID is found
found := false
for _, id := range ids {
if id == validPeerID {
found = true
break
}
}
require.True(t, found, "valid peer ID should be present in the results")
}
Loading