2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-07-30 20:25:18 +01:00
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package kademlia
|
|
|
|
|
|
|
|
import (
|
2018-11-29 18:39:27 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2018-10-26 17:54:00 +01:00
|
|
|
"sync"
|
2018-07-30 20:25:18 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-11-20 18:29:07 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-07-30 20:25:18 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-26 17:54:00 +01:00
|
|
|
"go.uber.org/zap"
|
2018-09-18 05:39:06 +01:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/internal/teststorj"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-07-30 20:25:18 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-10-26 17:54:00 +01:00
|
|
|
"storj.io/storj/storage/storelogger"
|
2018-10-18 17:20:23 +01:00
|
|
|
"storj.io/storj/storage/teststore"
|
2018-07-30 20:25:18 +01:00
|
|
|
)
|
|
|
|
|
2018-10-26 17:54:00 +01:00
|
|
|
// newTestRoutingTable returns a newly configured instance of a RoutingTable
|
|
|
|
func newTestRoutingTable(localNode pb.Node) (*RoutingTable, error) {
|
|
|
|
rt := &RoutingTable{
|
2018-11-20 16:54:52 +00:00
|
|
|
self: localNode,
|
2018-12-12 15:39:16 +00:00
|
|
|
kadBucketDB: storelogger.New(zap.L().Named("rt.kad"), teststore.New()),
|
|
|
|
nodeBucketDB: storelogger.New(zap.L().Named("rt.node"), teststore.New()),
|
2018-11-20 16:54:52 +00:00
|
|
|
transport: &defaultTransport,
|
|
|
|
|
2018-10-26 17:54:00 +01:00
|
|
|
mutex: &sync.Mutex{},
|
2018-11-29 18:39:27 +00:00
|
|
|
seen: make(map[storj.NodeID]*pb.Node),
|
|
|
|
replacementCache: make(map[bucketID][]*pb.Node),
|
2018-11-20 16:54:52 +00:00
|
|
|
|
|
|
|
bucketSize: 6,
|
|
|
|
rcBucketSize: 2,
|
2018-10-26 17:54:00 +01:00
|
|
|
}
|
|
|
|
ok, err := rt.addNode(&localNode)
|
|
|
|
if !ok || err != nil {
|
|
|
|
return nil, RoutingErr.New("could not add localNode to routing table: %s", err)
|
|
|
|
}
|
|
|
|
return rt, nil
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
func createRoutingTable(t *testing.T, localNodeID storj.NodeID) (*RoutingTable, func()) {
|
|
|
|
if localNodeID == (storj.NodeID{}) {
|
|
|
|
localNodeID = teststorj.NodeIDFromString("AA")
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
localNode := pb.Node{Id: localNodeID}
|
2018-10-18 17:20:23 +01:00
|
|
|
|
2018-10-26 17:54:00 +01:00
|
|
|
rt, err := newTestRoutingTable(localNode)
|
2018-09-11 14:57:12 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-10-26 17:54:00 +01:00
|
|
|
|
2018-09-11 14:57:12 +01:00
|
|
|
return rt, func() {
|
|
|
|
err := rt.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddNode(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
rt, cleanup := createRoutingTable(t, teststorj.NodeIDFromString("OO"))
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2018-11-29 18:39:27 +00:00
|
|
|
// bucket, err := rt.kadBucketDB.Get(storage.Key([]byte{255, 255}))
|
|
|
|
// assert.NoError(t, err)
|
|
|
|
// assert.NotNil(t, bucket)
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
node *pb.Node
|
|
|
|
added bool
|
|
|
|
kadIDs [][]byte
|
|
|
|
nodeIDs [][]string
|
|
|
|
}{
|
|
|
|
{testID: "PO: add node to unfilled kbucket",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("PO"),
|
2018-09-19 19:37:28 +01:00
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"OO", "PO"}},
|
|
|
|
},
|
|
|
|
{testID: "NO: add node to full kbucket and split",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("NO"),
|
2018-09-19 19:37:28 +01:00
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"NO", "OO", "PO"}},
|
|
|
|
},
|
|
|
|
{testID: "MO",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("MO"),
|
2018-09-19 19:37:28 +01:00
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"MO", "NO", "OO", "PO"}},
|
|
|
|
},
|
|
|
|
{testID: "LO",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("LO"),
|
2018-09-19 19:37:28 +01:00
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"LO", "MO", "NO", "OO", "PO"}},
|
|
|
|
},
|
|
|
|
{testID: "QO",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("QO"),
|
2018-09-19 19:37:28 +01:00
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"LO", "MO", "NO", "OO", "PO", "QO"}},
|
|
|
|
},
|
|
|
|
{testID: "SO: split bucket",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("SO"),
|
2018-09-19 19:37:28 +01:00
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
|
|
|
nodeIDs: [][]string{{}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
|
|
|
},
|
|
|
|
{testID: "?O",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("?O"),
|
2018-10-11 21:25:54 +01:00
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
|
|
|
nodeIDs: [][]string{{"?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: ">O",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode(">O"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}}, nodeIDs: [][]string{{">O", "?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "=O",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("=O"),
|
2018-10-11 21:25:54 +01:00
|
|
|
added: true,
|
2018-11-29 18:39:27 +00:00
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
2018-10-11 21:25:54 +01:00
|
|
|
nodeIDs: [][]string{{"=O", ">O", "?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: ";O",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode(";O"),
|
2018-10-11 21:25:54 +01:00
|
|
|
added: true,
|
2018-11-29 18:39:27 +00:00
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
2018-10-11 21:25:54 +01:00
|
|
|
nodeIDs: [][]string{{";O", "=O", ">O", "?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: ":O",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode(":O"),
|
2018-10-11 21:25:54 +01:00
|
|
|
added: true,
|
2018-11-29 18:39:27 +00:00
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
2018-10-11 21:25:54 +01:00
|
|
|
nodeIDs: [][]string{{":O", ";O", "=O", ">O", "?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "9O",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("9O"),
|
2018-10-11 21:25:54 +01:00
|
|
|
added: true,
|
2018-11-29 18:39:27 +00:00
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
2018-10-11 21:25:54 +01:00
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "8O: should drop",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("8O"),
|
2018-10-11 21:25:54 +01:00
|
|
|
added: false,
|
2018-11-29 18:39:27 +00:00
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
2018-10-11 21:25:54 +01:00
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "KO",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("KO"),
|
2018-10-11 21:25:54 +01:00
|
|
|
added: true,
|
2018-11-29 18:39:27 +00:00
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
2018-10-11 21:25:54 +01:00
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"KO", "LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "JO",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("JO"),
|
2018-10-11 21:25:54 +01:00
|
|
|
added: true,
|
2018-11-29 18:39:27 +00:00
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
2018-10-11 21:25:54 +01:00
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"JO", "KO", "LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "]O",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("]O"),
|
2018-10-11 21:25:54 +01:00
|
|
|
added: true,
|
2018-11-29 18:39:27 +00:00
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
2018-10-11 21:25:54 +01:00
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"JO", "KO", "LO", "MO", "NO", "OO"}, {"PO", "QO", "SO", "]O"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "^O",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("^O"),
|
2018-10-11 21:25:54 +01:00
|
|
|
added: true,
|
2018-11-29 18:39:27 +00:00
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
2018-10-11 21:25:54 +01:00
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"JO", "KO", "LO", "MO", "NO", "OO"}, {"PO", "QO", "SO", "]O", "^O"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "_O",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("_O"),
|
2018-10-11 21:25:54 +01:00
|
|
|
added: true,
|
2018-11-29 18:39:27 +00:00
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
2018-10-11 21:25:54 +01:00
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"JO", "KO", "LO", "MO", "NO", "OO"}, {"PO", "QO", "SO", "]O", "^O", "_O"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "@O: split bucket 2",
|
2018-11-29 18:39:27 +00:00
|
|
|
node: teststorj.MockNode("@O"),
|
2018-10-11 21:25:54 +01:00
|
|
|
added: true,
|
2018-11-29 18:39:27 +00:00
|
|
|
kadIDs: [][]byte{{63, 255}, {71, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
2018-10-11 21:25:54 +01:00
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"@O"}, {"JO", "KO", "LO", "MO", "NO", "OO"}, {"PO", "QO", "SO", "]O", "^O", "_O"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
ok, err := rt.addNode(c.node)
|
|
|
|
assert.Equal(t, c.added, ok)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
kadKeys, err := rt.kadBucketDB.List(nil, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
for i, v := range kadKeys {
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.True(t, bytes.Equal(c.kadIDs[i], v[:2]))
|
|
|
|
ids, err := rt.getNodeIDsWithinKBucket(keyToBucketID(v))
|
2018-09-19 19:37:28 +01:00
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
for j, id := range ids {
|
|
|
|
assert.True(t, bytes.Equal(teststorj.NodeIDFromString(c.nodeIDs[i][j]).Bytes(), id.Bytes()))
|
2018-09-19 19:37:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.testID == "8O" {
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID80 := teststorj.NodeIDFromString("8O")
|
|
|
|
n := rt.replacementCache[keyToBucketID(nodeID80.Bytes())]
|
|
|
|
assert.Equal(t, nodeID80.Bytes(), n[0].Id.Bytes())
|
2018-09-19 19:37:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
2018-08-17 20:11:46 +01:00
|
|
|
func TestUpdateNode(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
rt, cleanup := createRoutingTable(t, teststorj.NodeIDFromString("AA"))
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2018-11-29 18:39:27 +00:00
|
|
|
node := teststorj.MockNode("BB")
|
2018-08-17 20:11:46 +01:00
|
|
|
ok, err := rt.addNode(node)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
val, err := rt.nodeBucketDB.Get(node.Id.Bytes())
|
2018-08-17 20:11:46 +01:00
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
unmarshaled, err := unmarshalNodes([]storage.Value{val})
|
2018-08-17 20:11:46 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
x := unmarshaled[0].Address
|
|
|
|
assert.Nil(t, x)
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
node.Address = &pb.NodeAddress{Address: "BB"}
|
2018-08-17 20:11:46 +01:00
|
|
|
err = rt.updateNode(node)
|
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
val, err = rt.nodeBucketDB.Get(node.Id.Bytes())
|
2018-08-17 20:11:46 +01:00
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
unmarshaled, err = unmarshalNodes([]storage.Value{val})
|
2018-08-17 20:11:46 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
y := unmarshaled[0].Address.Address
|
|
|
|
assert.Equal(t, "BB", y)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveNode(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
rt, cleanup := createRoutingTable(t, teststorj.NodeIDFromString("AA"))
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2019-01-02 18:57:11 +00:00
|
|
|
kadBucketID := firstBucketID
|
2018-11-29 18:39:27 +00:00
|
|
|
node := teststorj.MockNode("BB")
|
2018-08-17 20:11:46 +01:00
|
|
|
ok, err := rt.addNode(node)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
val, err := rt.nodeBucketDB.Get(node.Id.Bytes())
|
2018-08-17 20:11:46 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, val)
|
2018-11-29 18:39:27 +00:00
|
|
|
node2 := teststorj.MockNode("CC")
|
2018-08-17 20:11:46 +01:00
|
|
|
rt.addToReplacementCache(kadBucketID, node2)
|
2018-11-29 18:39:27 +00:00
|
|
|
err = rt.removeNode(node.Id)
|
2018-08-17 20:11:46 +01:00
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
val, err = rt.nodeBucketDB.Get(node.Id.Bytes())
|
2018-08-17 20:11:46 +01:00
|
|
|
assert.Nil(t, val)
|
|
|
|
assert.Error(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
val2, err := rt.nodeBucketDB.Get(node2.Id.Bytes())
|
2018-08-17 20:11:46 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, val2)
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.Equal(t, 0, len(rt.replacementCache[kadBucketID]))
|
2018-08-21 19:44:42 +01:00
|
|
|
|
|
|
|
//try to remove node not in rt
|
2018-11-29 18:39:27 +00:00
|
|
|
err = rt.removeNode(teststorj.NodeIDFromString("DD"))
|
2018-08-21 19:44:42 +01:00
|
|
|
assert.NoError(t, err)
|
2018-08-17 20:11:46 +01:00
|
|
|
}
|
|
|
|
|
2018-07-30 20:25:18 +01:00
|
|
|
func TestCreateOrUpdateKBucket(t *testing.T) {
|
2019-01-02 18:57:11 +00:00
|
|
|
id := bucketID{255, 255}
|
2018-11-29 18:39:27 +00:00
|
|
|
rt, cleanup := createRoutingTable(t, storj.NodeID{})
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2019-01-02 18:57:11 +00:00
|
|
|
err := rt.createOrUpdateKBucket(id, time.Now())
|
2018-07-30 20:25:18 +01:00
|
|
|
assert.NoError(t, err)
|
2019-01-02 18:57:11 +00:00
|
|
|
val, e := rt.kadBucketDB.Get(id[:])
|
2018-07-30 20:25:18 +01:00
|
|
|
assert.NotNil(t, val)
|
|
|
|
assert.NoError(t, e)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetKBucketID(t *testing.T) {
|
2019-01-02 18:57:11 +00:00
|
|
|
kadIDA := bucketID{255, 255}
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeIDA := teststorj.NodeIDFromString("AA")
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nodeIDA)
|
|
|
|
defer cleanup()
|
2018-08-09 20:20:39 +01:00
|
|
|
keyA, err := rt.getKBucketID(nodeIDA)
|
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.Equal(t, kadIDA[:2], keyA[:2])
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetermineFurthestIDWithinK(t *testing.T) {
|
2019-01-02 18:57:11 +00:00
|
|
|
rt, cleanup := createRoutingTable(t, storj.NodeID{127, 255})
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
nodeID []byte
|
|
|
|
expectedFurthest []byte
|
|
|
|
}{
|
|
|
|
{testID: "xor 0",
|
|
|
|
nodeID: []byte{127, 255},
|
|
|
|
expectedFurthest: []byte{127, 255},
|
|
|
|
},
|
|
|
|
{testID: "xor 240",
|
|
|
|
nodeID: []byte{143, 255},
|
|
|
|
expectedFurthest: []byte{143, 255},
|
|
|
|
},
|
|
|
|
{testID: "xor 128",
|
|
|
|
nodeID: []byte{255, 255},
|
|
|
|
expectedFurthest: []byte{143, 255},
|
|
|
|
},
|
|
|
|
{testID: "xor 192",
|
|
|
|
nodeID: []byte{191, 255},
|
|
|
|
expectedFurthest: []byte{143, 255},
|
|
|
|
},
|
|
|
|
{testID: "xor 250",
|
|
|
|
nodeID: []byte{133, 255},
|
|
|
|
expectedFurthest: []byte{133, 255},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(teststorj.NodeIDFromBytes(c.nodeID).Bytes(), []byte("")))
|
2018-09-19 19:37:28 +01:00
|
|
|
nodes, err := rt.nodeBucketDB.List(nil, 0)
|
|
|
|
assert.NoError(t, err)
|
2019-01-02 18:57:11 +00:00
|
|
|
furthest := rt.determineFurthestIDWithinK(teststorj.NodeIDsFromBytes(nodes.ByteSlices()...))
|
2018-11-29 18:39:27 +00:00
|
|
|
fmt.Println(furthest.Bytes())
|
|
|
|
assert.Equal(t, c.expectedFurthest, furthest[:2])
|
2018-09-19 19:37:28 +01:00
|
|
|
})
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeIsWithinNearestK(t *testing.T) {
|
2019-01-02 18:57:11 +00:00
|
|
|
rt, cleanup := createRoutingTable(t, storj.NodeID{127, 255})
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2018-08-09 20:20:39 +01:00
|
|
|
rt.bucketSize = 2
|
2019-01-02 18:57:11 +00:00
|
|
|
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
2019-01-02 18:57:11 +00:00
|
|
|
nodeID storj.NodeID
|
2018-09-19 19:37:28 +01:00
|
|
|
closest bool
|
|
|
|
}{
|
|
|
|
{testID: "A",
|
2019-01-02 18:57:11 +00:00
|
|
|
nodeID: storj.NodeID{127, 255},
|
2018-09-19 19:37:28 +01:00
|
|
|
closest: true,
|
|
|
|
},
|
|
|
|
{testID: "B",
|
2019-01-02 18:57:11 +00:00
|
|
|
nodeID: storj.NodeID{143, 255},
|
2018-09-19 19:37:28 +01:00
|
|
|
closest: true,
|
|
|
|
},
|
|
|
|
{testID: "C",
|
2019-01-02 18:57:11 +00:00
|
|
|
nodeID: storj.NodeID{255, 255},
|
2018-09-19 19:37:28 +01:00
|
|
|
closest: true,
|
|
|
|
},
|
|
|
|
{testID: "D",
|
2019-01-02 18:57:11 +00:00
|
|
|
nodeID: storj.NodeID{191, 255},
|
2018-09-19 19:37:28 +01:00
|
|
|
closest: true,
|
|
|
|
},
|
|
|
|
{testID: "E",
|
2019-01-02 18:57:11 +00:00
|
|
|
nodeID: storj.NodeID{133, 255},
|
2018-09-19 19:37:28 +01:00
|
|
|
closest: false,
|
|
|
|
},
|
|
|
|
}
|
2019-01-02 18:57:11 +00:00
|
|
|
|
2018-09-19 19:37:28 +01:00
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
2019-01-02 18:57:11 +00:00
|
|
|
result, err := rt.nodeIsWithinNearestK(c.nodeID)
|
2018-09-19 19:37:28 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, c.closest, result)
|
2019-01-02 18:57:11 +00:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(c.nodeID.Bytes(), []byte("")))
|
2018-09-19 19:37:28 +01:00
|
|
|
})
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestKadBucketContainsLocalNode(t *testing.T) {
|
2019-01-02 18:57:11 +00:00
|
|
|
nodeIDA := storj.NodeID{183, 255} //[10110111, 1111111]
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nodeIDA)
|
|
|
|
defer cleanup()
|
2019-01-02 18:57:11 +00:00
|
|
|
kadIDA := firstBucketID
|
2018-11-29 18:39:27 +00:00
|
|
|
var kadIDB bucketID
|
|
|
|
copy(kadIDB[:], kadIDA[:])
|
|
|
|
kadIDB[0] = 127
|
2018-07-30 20:25:18 +01:00
|
|
|
now := time.Now()
|
2018-08-09 20:20:39 +01:00
|
|
|
err := rt.createOrUpdateKBucket(kadIDB, now)
|
2018-07-30 20:25:18 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
resultTrue, err := rt.kadBucketContainsLocalNode(kadIDA)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
resultFalse, err := rt.kadBucketContainsLocalNode(kadIDB)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, resultTrue)
|
|
|
|
assert.False(t, resultFalse)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKadBucketHasRoom(t *testing.T) {
|
2019-01-02 18:57:11 +00:00
|
|
|
node1 := storj.NodeID{255, 255}
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, node1)
|
|
|
|
defer cleanup()
|
2019-01-02 18:57:11 +00:00
|
|
|
kadIDA := firstBucketID
|
|
|
|
node2 := storj.NodeID{191, 255}
|
|
|
|
node3 := storj.NodeID{127, 255}
|
|
|
|
node4 := storj.NodeID{63, 255}
|
|
|
|
node5 := storj.NodeID{159, 255}
|
|
|
|
node6 := storj.NodeID{0, 127}
|
2018-07-30 20:25:18 +01:00
|
|
|
resultA, err := rt.kadBucketHasRoom(kadIDA)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, resultA)
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node2.Bytes(), []byte("")))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node3.Bytes(), []byte("")))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node4.Bytes(), []byte("")))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node5.Bytes(), []byte("")))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node6.Bytes(), []byte("")))
|
2018-07-30 20:25:18 +01:00
|
|
|
resultB, err := rt.kadBucketHasRoom(kadIDA)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, resultB)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetNodeIDsWithinKBucket(t *testing.T) {
|
2019-01-02 18:57:11 +00:00
|
|
|
nodeIDA := storj.NodeID{183, 255} //[10110111, 1111111]
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nodeIDA)
|
|
|
|
defer cleanup()
|
2019-01-02 18:57:11 +00:00
|
|
|
kadIDA := firstBucketID
|
2018-11-29 18:39:27 +00:00
|
|
|
var kadIDB bucketID
|
|
|
|
copy(kadIDB[:], kadIDA[:])
|
|
|
|
kadIDB[0] = 127
|
2018-07-30 20:25:18 +01:00
|
|
|
now := time.Now()
|
2018-09-11 14:13:25 +01:00
|
|
|
assert.NoError(t, rt.createOrUpdateKBucket(kadIDB, now))
|
2018-07-30 20:25:18 +01:00
|
|
|
|
2019-01-02 18:57:11 +00:00
|
|
|
nodeIDB := storj.NodeID{111, 255} //[01101111, 1111111]
|
|
|
|
nodeIDC := storj.NodeID{47, 255} //[00101111, 1111111]
|
2018-07-30 20:25:18 +01:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeIDB.Bytes(), []byte("")))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeIDC.Bytes(), []byte("")))
|
2018-07-30 20:25:18 +01:00
|
|
|
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
2018-11-29 18:39:27 +00:00
|
|
|
kadID bucketID
|
2018-09-19 19:37:28 +01:00
|
|
|
expected storage.Keys
|
|
|
|
}{
|
|
|
|
{testID: "A",
|
|
|
|
kadID: kadIDA,
|
2018-11-29 18:39:27 +00:00
|
|
|
expected: storage.Keys{nodeIDA.Bytes()},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "B",
|
|
|
|
kadID: kadIDB,
|
2018-11-29 18:39:27 +00:00
|
|
|
expected: storage.Keys{nodeIDC.Bytes(), nodeIDB.Bytes()},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
n, err := rt.getNodeIDsWithinKBucket(c.kadID)
|
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
for i, id := range c.expected {
|
|
|
|
assert.True(t, id.Equal(n[i].Bytes()))
|
|
|
|
}
|
2018-09-19 19:37:28 +01:00
|
|
|
})
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 20:20:39 +01:00
|
|
|
func TestGetNodesFromIDs(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeA := teststorj.MockNode("AA")
|
|
|
|
nodeB := teststorj.MockNode("BB")
|
|
|
|
nodeC := teststorj.MockNode("CC")
|
2018-09-18 05:39:06 +01:00
|
|
|
a, err := proto.Marshal(nodeA)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-09-18 05:39:06 +01:00
|
|
|
b, err := proto.Marshal(nodeB)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-09-18 05:39:06 +01:00
|
|
|
c, err := proto.Marshal(nodeC)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
rt, cleanup := createRoutingTable(t, nodeA.Id)
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2018-08-09 20:20:39 +01:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeA.Id.Bytes(), a))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeB.Id.Bytes(), b))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeC.Id.Bytes(), c))
|
|
|
|
expected := []*pb.Node{nodeA, nodeB, nodeC}
|
2018-08-09 20:20:39 +01:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeKeys, err := rt.nodeBucketDB.List(nil, 0)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
values, err := rt.getNodesFromIDsBytes(teststorj.NodeIDsFromBytes(nodeKeys.ByteSlices()...))
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
for i, n := range expected {
|
|
|
|
assert.True(t, bytes.Equal(n.Id.Bytes(), values[i].Id.Bytes()))
|
|
|
|
}
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalNodes(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeA := teststorj.MockNode("AA")
|
|
|
|
nodeB := teststorj.MockNode("BB")
|
|
|
|
nodeC := teststorj.MockNode("CC")
|
2018-08-09 20:20:39 +01:00
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
a, err := proto.Marshal(nodeA)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-09-18 05:39:06 +01:00
|
|
|
b, err := proto.Marshal(nodeB)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-09-18 05:39:06 +01:00
|
|
|
c, err := proto.Marshal(nodeC)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
rt, cleanup := createRoutingTable(t, nodeA.Id)
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeA.Id.Bytes(), a))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeB.Id.Bytes(), b))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeC.Id.Bytes(), c))
|
|
|
|
nodeKeys, err := rt.nodeBucketDB.List(nil, 0)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
nodes, err := rt.getNodesFromIDsBytes(teststorj.NodeIDsFromBytes(nodeKeys.ByteSlices()...))
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-09-18 05:39:06 +01:00
|
|
|
expected := []*pb.Node{nodeA, nodeB, nodeC}
|
2018-08-09 20:20:39 +01:00
|
|
|
for i, v := range expected {
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.True(t, bytes.Equal(v.Id.Bytes(), nodes[i].Id.Bytes()))
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUnmarshaledNodesFromBucket(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeA := teststorj.MockNode("AA")
|
|
|
|
rt, cleanup := createRoutingTable(t, nodeA.Id)
|
2019-01-02 18:57:11 +00:00
|
|
|
bucketID := firstBucketID
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeB := teststorj.MockNode("BB")
|
|
|
|
nodeC := teststorj.MockNode("CC")
|
2018-09-11 14:13:25 +01:00
|
|
|
var err error
|
|
|
|
_, err = rt.addNode(nodeB)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = rt.addNode(nodeC)
|
|
|
|
assert.NoError(t, err)
|
2018-08-09 20:20:39 +01:00
|
|
|
nodes, err := rt.getUnmarshaledNodesFromBucket(bucketID)
|
2018-09-18 05:39:06 +01:00
|
|
|
expected := []*pb.Node{nodeA, nodeB, nodeC}
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for i, v := range expected {
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.True(t, bytes.Equal(v.Id.Bytes(), nodes[i].Id.Bytes()))
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 20:25:18 +01:00
|
|
|
func TestGetKBucketRange(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
rt, cleanup := createRoutingTable(t, storj.NodeID{})
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2019-01-02 18:57:11 +00:00
|
|
|
idA := storj.NodeID{255, 255}
|
|
|
|
idB := storj.NodeID{127, 255}
|
|
|
|
idC := storj.NodeID{63, 255}
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.NoError(t, rt.kadBucketDB.Put(idA.Bytes(), []byte("")))
|
|
|
|
assert.NoError(t, rt.kadBucketDB.Put(idB.Bytes(), []byte("")))
|
|
|
|
assert.NoError(t, rt.kadBucketDB.Put(idC.Bytes(), []byte("")))
|
|
|
|
zeroBID := bucketID{}
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
2018-11-29 18:39:27 +00:00
|
|
|
id storj.NodeID
|
2018-09-19 19:37:28 +01:00
|
|
|
expected storage.Keys
|
|
|
|
}{
|
|
|
|
{testID: "A",
|
|
|
|
id: idA,
|
2018-11-29 18:39:27 +00:00
|
|
|
expected: storage.Keys{idB.Bytes(), idA.Bytes()},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "B",
|
|
|
|
id: idB,
|
2018-11-29 18:39:27 +00:00
|
|
|
expected: storage.Keys{idC.Bytes(), idB.Bytes()}},
|
2018-09-19 19:37:28 +01:00
|
|
|
{testID: "C",
|
|
|
|
id: idC,
|
2018-11-29 18:39:27 +00:00
|
|
|
expected: storage.Keys{zeroBID[:], idC.Bytes()},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
ep, err := rt.getKBucketRange(keyToBucketID(c.id.Bytes()))
|
2018-09-19 19:37:28 +01:00
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
for i, k := range c.expected {
|
|
|
|
assert.True(t, k.Equal(ep[i][:]))
|
|
|
|
}
|
2018-09-19 19:37:28 +01:00
|
|
|
})
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
func TestBucketIDZeroValue(t *testing.T) {
|
|
|
|
// rt, cleanup := createRoutingTable(t, storj.NodeID{})
|
|
|
|
// defer cleanup()
|
|
|
|
zero := bucketID{} //rt.createZeroAsBucketID()
|
|
|
|
expected := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
|
|
|
assert.True(t, bytes.Equal(zero[:], expected))
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetermineLeafDepth(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
rt, cleanup := createRoutingTable(t, storj.NodeID{})
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2019-01-02 18:57:11 +00:00
|
|
|
idA, idB, idC := storj.NodeID(firstBucketID), storj.NodeID(firstBucketID), storj.NodeID(firstBucketID)
|
|
|
|
idA[0] = 255
|
|
|
|
idB[0] = 127
|
|
|
|
idC[0] = 63
|
2018-07-30 20:25:18 +01:00
|
|
|
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
2018-11-29 18:39:27 +00:00
|
|
|
id storj.NodeID
|
2018-09-19 19:37:28 +01:00
|
|
|
depth int
|
|
|
|
addNode func()
|
|
|
|
}{
|
|
|
|
{testID: "A",
|
|
|
|
id: idA,
|
|
|
|
depth: 0,
|
|
|
|
addNode: func() {
|
2018-11-29 18:39:27 +00:00
|
|
|
e := rt.kadBucketDB.Put(idA.Bytes(), []byte(""))
|
2018-09-19 19:37:28 +01:00
|
|
|
assert.NoError(t, e)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{testID: "B",
|
|
|
|
id: idB,
|
|
|
|
depth: 1,
|
|
|
|
addNode: func() {
|
2018-11-29 18:39:27 +00:00
|
|
|
e := rt.kadBucketDB.Put(idB.Bytes(), []byte(""))
|
2018-09-19 19:37:28 +01:00
|
|
|
assert.NoError(t, e)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{testID: "C",
|
|
|
|
id: idA,
|
|
|
|
depth: 1,
|
|
|
|
addNode: func() {
|
2018-11-29 18:39:27 +00:00
|
|
|
e := rt.kadBucketDB.Put(idC.Bytes(), []byte(""))
|
2018-09-19 19:37:28 +01:00
|
|
|
assert.NoError(t, e)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{testID: "D",
|
|
|
|
id: idB,
|
|
|
|
depth: 2,
|
|
|
|
addNode: func() {},
|
|
|
|
},
|
|
|
|
{testID: "E",
|
|
|
|
id: idC,
|
|
|
|
depth: 2,
|
|
|
|
addNode: func() {},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
c.addNode()
|
2019-01-02 18:57:11 +00:00
|
|
|
d, err := rt.determineLeafDepth(bucketID(c.id))
|
2018-09-19 19:37:28 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, c.depth, d)
|
|
|
|
})
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplitBucket(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
rt, cleanup := createRoutingTable(t, storj.NodeID{})
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
idA []byte
|
|
|
|
idB []byte
|
|
|
|
depth int
|
|
|
|
}{
|
|
|
|
{testID: "A: [11111111, 11111111] -> [10111111, 11111111]",
|
|
|
|
idA: []byte{255, 255},
|
|
|
|
idB: []byte{191, 255},
|
|
|
|
depth: 1,
|
|
|
|
},
|
|
|
|
{testID: "B: [10111111, 11111111] -> [10011111, 11111111]",
|
|
|
|
idA: []byte{191, 255},
|
|
|
|
idB: []byte{159, 255},
|
|
|
|
depth: 2,
|
|
|
|
},
|
|
|
|
{testID: "C: [01111111, 11111111] -> [00111111, 11111111]",
|
|
|
|
idA: []byte{127, 255},
|
|
|
|
idB: []byte{63, 255},
|
|
|
|
depth: 1,
|
|
|
|
},
|
|
|
|
{testID: "D: [00000000, 11111111] -> [00000000, 01111111]",
|
|
|
|
idA: []byte{0, 255},
|
|
|
|
idB: []byte{0, 127},
|
|
|
|
depth: 8,
|
|
|
|
},
|
|
|
|
{testID: "E: [01011111, 11111111] -> [01010111, 11111111]",
|
|
|
|
idA: []byte{95, 255},
|
|
|
|
idB: []byte{87, 255},
|
|
|
|
depth: 4,
|
|
|
|
},
|
|
|
|
{testID: "F: [01011111, 11111111] -> [01001111, 11111111]",
|
|
|
|
idA: []byte{95, 255},
|
|
|
|
idB: []byte{79, 255},
|
|
|
|
depth: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
newID := rt.splitBucket(keyToBucketID(c.idA), c.depth)
|
|
|
|
assert.Equal(t, c.idB, newID[:2])
|
2018-09-19 19:37:28 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|