2019-02-06 12:37:17 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package kademlia_test
|
|
|
|
|
|
|
|
import (
|
2019-03-22 17:09:37 +00:00
|
|
|
"context"
|
2019-02-06 12:37:17 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
2019-03-22 17:09:37 +00:00
|
|
|
"time"
|
2019-02-06 12:37:17 +00:00
|
|
|
|
2019-03-22 17:09:37 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-06 12:37:17 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2019-04-22 10:07:50 +01:00
|
|
|
|
2019-03-22 17:09:37 +00:00
|
|
|
"storj.io/storj/pkg/peertls/tlsopts"
|
2019-02-06 12:37:17 +00:00
|
|
|
|
2019-03-22 17:09:37 +00:00
|
|
|
"storj.io/storj/internal/memory"
|
2019-02-06 12:37:17 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/internal/testplanet"
|
|
|
|
"storj.io/storj/pkg/kademlia"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/storj"
|
2019-03-22 17:09:37 +00:00
|
|
|
"storj.io/storj/pkg/transport"
|
2019-02-06 12:37:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDialer(t *testing.T) {
|
2019-04-10 13:10:08 +01:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2019-02-06 12:37:17 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
planet, err := testplanet.New(t, 1, 4, 3)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
2019-02-06 12:37:17 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
planet.Start(ctx)
|
2019-02-06 12:37:17 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
expectedKademliaEntries := len(planet.Satellites) + len(planet.StorageNodes)
|
2019-02-06 12:37:17 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
// TODO: also use satellites
|
|
|
|
peers := planet.StorageNodes
|
2019-02-06 12:37:17 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
{ // PingNode: storage node pings all other storage nodes
|
|
|
|
self := planet.StorageNodes[0]
|
2019-03-20 08:30:42 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
dialer := kademlia.NewDialer(zaptest.NewLogger(t), self.Transport)
|
|
|
|
defer ctx.Check(dialer.Close)
|
2019-03-20 08:30:42 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
var group errgroup.Group
|
|
|
|
defer ctx.Check(group.Wait)
|
2019-03-20 08:30:42 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
for _, peer := range peers {
|
|
|
|
peer := peer
|
2019-03-20 08:30:42 +00:00
|
|
|
group.Go(func() error {
|
2019-04-22 10:07:50 +01:00
|
|
|
pinged, err := dialer.PingNode(ctx, peer.Local().Node)
|
2019-04-10 13:10:08 +01:00
|
|
|
var pingErr error
|
|
|
|
if !pinged {
|
|
|
|
pingErr = fmt.Errorf("ping to %s should have succeeded", peer.ID())
|
2019-03-20 08:30:42 +00:00
|
|
|
}
|
2019-04-10 13:10:08 +01:00
|
|
|
return errs.Combine(pingErr, err)
|
2019-03-20 08:30:42 +00:00
|
|
|
})
|
2019-02-06 12:37:17 +00:00
|
|
|
}
|
2019-04-10 13:10:08 +01:00
|
|
|
}
|
2019-02-06 12:37:17 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
{ // FetchPeerIdentity: storage node fetches identity of the satellite
|
|
|
|
self := planet.StorageNodes[0]
|
2019-02-06 12:37:17 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
dialer := kademlia.NewDialer(zaptest.NewLogger(t), self.Transport)
|
|
|
|
defer ctx.Check(dialer.Close)
|
2019-02-06 12:37:17 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
var group errgroup.Group
|
|
|
|
defer ctx.Check(group.Wait)
|
2019-02-06 12:37:17 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
group.Go(func() error {
|
2019-04-22 10:07:50 +01:00
|
|
|
ident, err := dialer.FetchPeerIdentity(ctx, planet.Satellites[0].Local().Node)
|
2019-04-10 13:10:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to fetch peer identity")
|
|
|
|
}
|
|
|
|
if ident.ID != planet.Satellites[0].Local().Id {
|
|
|
|
return fmt.Errorf("fetched wrong identity")
|
2019-02-06 12:37:17 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
ident, err = dialer.FetchPeerIdentityUnverified(ctx, planet.Satellites[0].Addr())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to fetch peer identity from address")
|
|
|
|
}
|
|
|
|
if ident.ID != planet.Satellites[0].Local().Id {
|
|
|
|
return fmt.Errorf("fetched wrong identity from address")
|
2019-02-06 12:37:17 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
{ // Lookup: storage node query every node for everyone elese
|
|
|
|
self := planet.StorageNodes[1]
|
|
|
|
dialer := kademlia.NewDialer(zaptest.NewLogger(t), self.Transport)
|
|
|
|
defer ctx.Check(dialer.Close)
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
var group errgroup.Group
|
|
|
|
defer ctx.Check(group.Wait)
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
for _, peer := range peers {
|
|
|
|
peer := peer
|
|
|
|
group.Go(func() error {
|
|
|
|
for _, target := range peers {
|
|
|
|
errTag := fmt.Errorf("lookup peer:%s target:%s", peer.ID(), target.ID())
|
|
|
|
peer.Local().Type.DPanicOnInvalid("test client peer")
|
|
|
|
target.Local().Type.DPanicOnInvalid("test client target")
|
|
|
|
|
2019-04-22 10:07:50 +01:00
|
|
|
results, err := dialer.Lookup(ctx, self.Local().Node, peer.Local().Node, target.Local().Node)
|
2019-04-10 13:10:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return errs.Combine(errTag, err)
|
|
|
|
}
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
if containsResult(results, target.ID()) {
|
|
|
|
continue
|
|
|
|
}
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
// with small network we expect to return everything
|
|
|
|
if len(results) != expectedKademliaEntries {
|
|
|
|
return errs.Combine(errTag, fmt.Errorf("expected %d got %d: %s", expectedKademliaEntries, len(results), pb.NodesToIDs(results)))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
{ // Lookup: storage node queries every node for missing storj.NodeID{} and storj.NodeID{255}
|
|
|
|
self := planet.StorageNodes[2]
|
|
|
|
dialer := kademlia.NewDialer(zaptest.NewLogger(t), self.Transport)
|
|
|
|
defer ctx.Check(dialer.Close)
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
targets := []storj.NodeID{
|
|
|
|
{}, // empty target
|
|
|
|
{255}, // non-empty
|
|
|
|
}
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
var group errgroup.Group
|
|
|
|
defer ctx.Check(group.Wait)
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
for _, target := range targets {
|
|
|
|
target := target
|
2019-03-22 17:09:37 +00:00
|
|
|
for _, peer := range peers {
|
|
|
|
peer := peer
|
|
|
|
group.Go(func() error {
|
2019-04-10 13:10:08 +01:00
|
|
|
errTag := fmt.Errorf("invalid lookup peer:%s target:%s", peer.ID(), target)
|
|
|
|
peer.Local().Type.DPanicOnInvalid("peer info")
|
2019-04-22 10:07:50 +01:00
|
|
|
results, err := dialer.Lookup(ctx, self.Local().Node, peer.Local().Node, pb.Node{Id: target})
|
2019-04-10 13:10:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return errs.Combine(errTag, err)
|
|
|
|
}
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
// with small network we expect to return everything
|
|
|
|
if len(results) != expectedKademliaEntries {
|
|
|
|
return errs.Combine(errTag, fmt.Errorf("expected %d got %d: %s", expectedKademliaEntries, len(results), pb.NodesToIDs(results)))
|
|
|
|
}
|
2019-03-22 17:09:37 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 13:10:08 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
func TestSlowDialerHasTimeout(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
planet, err := testplanet.New(t, 1, 4, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
planet.Start(ctx)
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
// TODO: also use satellites
|
|
|
|
peers := planet.StorageNodes
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
{ // PingNode
|
|
|
|
self := planet.StorageNodes[0]
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
tlsOpts, err := tlsopts.NewOptions(self.Identity, tlsopts.Config{})
|
|
|
|
require.NoError(t, err)
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
self.Transport = transport.NewClientWithTimeout(tlsOpts, 20*time.Millisecond)
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
network := &transport.SimulatedNetwork{
|
|
|
|
DialLatency: 200 * time.Second,
|
|
|
|
BytesPerSecond: 1 * memory.KB,
|
|
|
|
}
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
slowClient := network.NewClient(self.Transport)
|
|
|
|
require.NotNil(t, slowClient)
|
|
|
|
|
|
|
|
dialer := kademlia.NewDialer(zaptest.NewLogger(t), slowClient)
|
|
|
|
defer ctx.Check(dialer.Close)
|
|
|
|
|
|
|
|
var group errgroup.Group
|
|
|
|
defer ctx.Check(group.Wait)
|
|
|
|
|
|
|
|
for _, peer := range peers {
|
|
|
|
peer := peer
|
|
|
|
group.Go(func() error {
|
2019-04-22 10:07:50 +01:00
|
|
|
_, err := dialer.PingNode(ctx, peer.Local().Node)
|
2019-03-22 17:09:37 +00:00
|
|
|
require.Error(t, err, context.DeadlineExceeded)
|
|
|
|
require.True(t, transport.Error.Has(err))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2019-04-10 13:10:08 +01:00
|
|
|
}
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
{ // FetchPeerIdentity
|
|
|
|
self := planet.StorageNodes[1]
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
tlsOpts, err := tlsopts.NewOptions(self.Identity, tlsopts.Config{})
|
|
|
|
require.NoError(t, err)
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
self.Transport = transport.NewClientWithTimeout(tlsOpts, 20*time.Millisecond)
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
network := &transport.SimulatedNetwork{
|
|
|
|
DialLatency: 200 * time.Second,
|
|
|
|
BytesPerSecond: 1 * memory.KB,
|
|
|
|
}
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
slowClient := network.NewClient(self.Transport)
|
|
|
|
require.NotNil(t, slowClient)
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
dialer := kademlia.NewDialer(zaptest.NewLogger(t), slowClient)
|
|
|
|
defer ctx.Check(dialer.Close)
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
var group errgroup.Group
|
|
|
|
defer ctx.Check(group.Wait)
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
group.Go(func() error {
|
2019-04-22 10:07:50 +01:00
|
|
|
_, err := dialer.FetchPeerIdentity(ctx, planet.Satellites[0].Local().Node)
|
2019-04-10 13:10:08 +01:00
|
|
|
require.Error(t, err, context.DeadlineExceeded)
|
|
|
|
require.True(t, transport.Error.Has(err))
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
_, err = dialer.FetchPeerIdentityUnverified(ctx, planet.Satellites[0].Addr())
|
|
|
|
require.Error(t, err, context.DeadlineExceeded)
|
|
|
|
require.True(t, transport.Error.Has(err))
|
2019-03-22 17:09:37 +00:00
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Lookup
|
|
|
|
self := planet.StorageNodes[2]
|
|
|
|
|
|
|
|
tlsOpts, err := tlsopts.NewOptions(self.Identity, tlsopts.Config{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
self.Transport = transport.NewClientWithTimeout(tlsOpts, 20*time.Millisecond)
|
|
|
|
|
|
|
|
network := &transport.SimulatedNetwork{
|
|
|
|
DialLatency: 200 * time.Second,
|
|
|
|
BytesPerSecond: 1 * memory.KB,
|
2019-03-22 17:09:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 13:10:08 +01:00
|
|
|
slowClient := network.NewClient(self.Transport)
|
|
|
|
require.NotNil(t, slowClient)
|
|
|
|
|
|
|
|
dialer := kademlia.NewDialer(zaptest.NewLogger(t), slowClient)
|
|
|
|
defer ctx.Check(dialer.Close)
|
|
|
|
|
|
|
|
var group errgroup.Group
|
|
|
|
defer ctx.Check(group.Wait)
|
|
|
|
|
|
|
|
for _, peer := range peers {
|
|
|
|
peer := peer
|
|
|
|
group.Go(func() error {
|
|
|
|
for _, target := range peers {
|
|
|
|
errTag := fmt.Errorf("lookup peer:%s target:%s", peer.ID(), target.ID())
|
|
|
|
peer.Local().Type.DPanicOnInvalid("test client peer")
|
|
|
|
target.Local().Type.DPanicOnInvalid("test client target")
|
|
|
|
|
2019-04-22 10:07:50 +01:00
|
|
|
_, err := dialer.Lookup(ctx, self.Local().Node, peer.Local().Node, target.Local().Node)
|
2019-04-10 13:10:08 +01:00
|
|
|
require.Error(t, err, context.DeadlineExceeded, errTag)
|
|
|
|
require.True(t, transport.Error.Has(err), errTag)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-03-22 17:09:37 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 12:37:17 +00:00
|
|
|
func containsResult(nodes []*pb.Node, target storj.NodeID) bool {
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.Id == target {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|