2018-05-09 15:04:51 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package kademlia
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-08 16:09:37 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-06-22 14:33:57 +01:00
|
|
|
"net"
|
2018-10-08 21:37:52 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-05-09 15:04:51 +01:00
|
|
|
|
2018-10-16 16:22:31 +01:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-06-22 14:33:57 +01:00
|
|
|
"github.com/zeebo/errs"
|
2018-10-10 16:05:05 +01:00
|
|
|
"go.uber.org/zap"
|
2018-10-08 16:09:37 +01:00
|
|
|
"google.golang.org/grpc"
|
2018-06-22 14:33:57 +01:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/dht"
|
2018-10-08 16:09:37 +01:00
|
|
|
"storj.io/storj/pkg/node"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-10-08 16:09:37 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-10-16 16:22:31 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-10-08 16:09:37 +01:00
|
|
|
)
|
|
|
|
|
2018-06-22 14:33:57 +01:00
|
|
|
// NodeErr is the class for all errors pertaining to node operations
|
|
|
|
var NodeErr = errs.Class("node error")
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
// BootstrapErr is the class for all errors pertaining to bootstrapping a node
|
|
|
|
var BootstrapErr = errs.Class("bootstrap node error")
|
|
|
|
|
2018-06-22 14:33:57 +01:00
|
|
|
//TODO: shouldn't default to TCP but not sure what to do yet
|
2018-10-15 13:04:21 +01:00
|
|
|
var defaultTransport = pb.NodeTransport_TCP_TLS_GRPC
|
2018-06-22 14:33:57 +01:00
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
// NodeNotFound is returned when a lookup can not produce the requested node
|
|
|
|
var NodeNotFound = NodeErr.New("node not found")
|
|
|
|
|
|
|
|
type lookupOpts struct {
|
2018-10-12 09:52:32 +01:00
|
|
|
amount int
|
|
|
|
bootstrap bool
|
2018-10-08 16:09:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 14:33:57 +01:00
|
|
|
// Kademlia is an implementation of kademlia adhering to the DHT interface.
|
|
|
|
type Kademlia struct {
|
2018-10-08 16:09:37 +01:00
|
|
|
alpha int // alpha is a system wide concurrency parameter
|
|
|
|
routingTable *RoutingTable
|
2018-09-18 05:39:06 +01:00
|
|
|
bootstrapNodes []pb.Node
|
2018-10-08 16:09:37 +01:00
|
|
|
address string
|
|
|
|
nodeClient node.Client
|
|
|
|
identity *provider.FullIdentity
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewKademlia returns a newly configured Kademlia instance
|
2018-10-09 17:47:05 +01:00
|
|
|
func NewKademlia(id dht.NodeID, bootstrapNodes []pb.Node, address string, identity *provider.FullIdentity, path string, kadconfig KadConfig) (*Kademlia, error) {
|
2018-10-08 16:09:37 +01:00
|
|
|
self := pb.Node{Id: id.String(), Address: &pb.NodeAddress{Address: address}}
|
2018-10-08 21:37:52 +01:00
|
|
|
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
if err := os.MkdirAll(path, 0777); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bucketIdentifier := id.String()[:5] // need a way to differentiate between nodes if running more than one simultaneously
|
2018-10-08 16:09:37 +01:00
|
|
|
rt, err := NewRoutingTable(&self, &RoutingOptions{
|
2018-10-08 21:37:52 +01:00
|
|
|
kpath: filepath.Join(path, fmt.Sprintf("kbucket_%s.db", bucketIdentifier)),
|
|
|
|
npath: filepath.Join(path, fmt.Sprintf("nbucket_%s.db", bucketIdentifier)),
|
2018-10-09 17:47:05 +01:00
|
|
|
idLength: kadconfig.DefaultIDLength,
|
|
|
|
bucketSize: kadconfig.DefaultBucketSize,
|
|
|
|
rcBucketSize: kadconfig.DefaultReplacementCacheSize,
|
2018-10-08 16:09:37 +01:00
|
|
|
})
|
2018-06-22 14:33:57 +01:00
|
|
|
if err != nil {
|
2018-10-08 16:09:37 +01:00
|
|
|
return nil, BootstrapErr.Wrap(err)
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
for _, v := range bootstrapNodes {
|
|
|
|
ok, err := rt.addNode(&v)
|
2018-10-10 16:05:05 +01:00
|
|
|
if err != nil {
|
2018-10-08 16:09:37 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-10 16:05:05 +01:00
|
|
|
if !ok {
|
|
|
|
zap.L().Warn("Failed to add node", zap.String("NodeID", v.Id))
|
|
|
|
}
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
k := &Kademlia{
|
2018-10-09 17:47:05 +01:00
|
|
|
alpha: kadconfig.Alpha,
|
2018-10-08 16:09:37 +01:00
|
|
|
routingTable: rt,
|
|
|
|
bootstrapNodes: bootstrapNodes,
|
|
|
|
address: address,
|
|
|
|
identity: identity,
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
nc, err := node.NewNodeClient(identity, self, k)
|
2018-06-22 14:33:57 +01:00
|
|
|
if err != nil {
|
2018-10-08 16:09:37 +01:00
|
|
|
return nil, BootstrapErr.Wrap(err)
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
2018-05-09 15:04:51 +01:00
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
k.nodeClient = nc
|
2018-05-09 15:04:51 +01:00
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
return k, nil
|
2018-05-09 15:04:51 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 14:33:57 +01:00
|
|
|
// Disconnect safely closes connections to the Kademlia network
|
2018-10-08 16:09:37 +01:00
|
|
|
func (k *Kademlia) Disconnect() error {
|
|
|
|
// TODO(coyle)
|
|
|
|
return errors.New("TODO Disconnect")
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
// GetNodes returns all nodes from a starting node up to a maximum limit
|
|
|
|
// stored in the local routing table limiting the result by the specified restrictions
|
2018-10-08 16:09:37 +01:00
|
|
|
func (k *Kademlia) GetNodes(ctx context.Context, start string, limit int, restrictions ...pb.Restriction) ([]*pb.Node, error) {
|
2018-10-16 16:22:31 +01:00
|
|
|
nodes := []*pb.Node{}
|
|
|
|
iteratorMethod := func(it storage.Iterator) error {
|
|
|
|
var item storage.ListItem
|
|
|
|
maxLimit := storage.LookupLimit
|
|
|
|
for ; maxLimit > 0 && it.Next(&item); maxLimit-- {
|
|
|
|
id := string(item.Key)
|
|
|
|
node := &pb.Node{}
|
|
|
|
err := proto.Unmarshal(item.Value, node)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
node.Id = id
|
|
|
|
if meetsRestrictions(restrictions, *node) {
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
if len(nodes) == limit {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := k.routingTable.iterate(
|
|
|
|
storage.IterateOptions{
|
|
|
|
First: storage.Key(start),
|
|
|
|
Recurse: true,
|
|
|
|
},
|
|
|
|
iteratorMethod,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return []*pb.Node{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
return nodes, nil
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRoutingTable provides the routing table for the Kademlia DHT
|
|
|
|
func (k *Kademlia) GetRoutingTable(ctx context.Context) (dht.RoutingTable, error) {
|
2018-10-08 16:09:37 +01:00
|
|
|
return k.routingTable, nil
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bootstrap contacts one of a set of pre defined trusted nodes on the network and
|
|
|
|
// begins populating the local Kademlia node
|
|
|
|
func (k *Kademlia) Bootstrap(ctx context.Context) error {
|
2018-10-08 16:09:37 +01:00
|
|
|
// What I want to do here is do a normal lookup for myself
|
|
|
|
// so call lookup(ctx, nodeImLookingFor)
|
|
|
|
if len(k.bootstrapNodes) == 0 {
|
|
|
|
return BootstrapErr.New("no bootstrap nodes provided")
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-12 09:52:32 +01:00
|
|
|
return k.lookup(ctx, node.IDFromString(k.routingTable.self.GetId()), lookupOpts{amount: 5, bootstrap: true})
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
2018-05-09 15:04:51 +01:00
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
func (k *Kademlia) lookup(ctx context.Context, target dht.NodeID, opts lookupOpts) error {
|
|
|
|
kb := k.routingTable.K()
|
|
|
|
// look in routing table for targetID
|
|
|
|
nodes, err := k.routingTable.FindNear(target, kb)
|
2018-06-22 14:33:57 +01:00
|
|
|
if err != nil {
|
2018-10-08 16:09:37 +01:00
|
|
|
return err
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
2018-05-09 15:04:51 +01:00
|
|
|
|
2018-10-12 09:52:32 +01:00
|
|
|
lookup := newSequentialLookup(k.routingTable, nodes, k.nodeClient, target, opts.amount, opts.bootstrap)
|
|
|
|
err = lookup.Run(ctx)
|
|
|
|
if err != nil {
|
|
|
|
zap.L().Warn("lookup failed", zap.Error(err))
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
// Ping checks that the provided node is still accessible on the network
|
|
|
|
func (k *Kademlia) Ping(ctx context.Context, node pb.Node) (pb.Node, error) {
|
|
|
|
// TODO(coyle)
|
|
|
|
return pb.Node{}, nil
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
// FindNode looks up the provided NodeID first in the local Node, and if it is not found
|
|
|
|
// begins searching the network for the NodeID. Returns and error if node was not found
|
|
|
|
func (k *Kademlia) FindNode(ctx context.Context, ID dht.NodeID) (pb.Node, error) {
|
|
|
|
//TODO(coyle)
|
|
|
|
return pb.Node{}, NodeErr.New("TODO FindNode")
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
// ListenAndServe connects the kademlia node to the network and listens for incoming requests
|
|
|
|
func (k *Kademlia) ListenAndServe() error {
|
|
|
|
identOpt, err := k.identity.ServerOption()
|
2018-06-22 14:33:57 +01:00
|
|
|
if err != nil {
|
2018-10-08 16:09:37 +01:00
|
|
|
return err
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
grpcServer := grpc.NewServer(identOpt)
|
|
|
|
mn := node.NewServer(k)
|
2018-06-22 14:33:57 +01:00
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
pb.RegisterNodesServer(grpcServer, mn)
|
|
|
|
lis, err := net.Listen("tcp", k.address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := grpcServer.Serve(lis); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer grpcServer.Stop()
|
2018-06-22 14:33:57 +01:00
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
return nil
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetIntroNode determines the best node to bootstrap a new node onto the network
|
2018-10-08 16:09:37 +01:00
|
|
|
func GetIntroNode(addr string) (*pb.Node, error) {
|
|
|
|
if addr == "" {
|
|
|
|
addr = "bootstrap.storj.io:8080"
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
return &pb.Node{
|
|
|
|
Address: &pb.NodeAddress{
|
2018-06-22 14:33:57 +01:00
|
|
|
Transport: defaultTransport,
|
|
|
|
Address: addr,
|
|
|
|
},
|
|
|
|
}, nil
|
2018-05-09 15:04:51 +01:00
|
|
|
}
|
2018-08-01 15:15:38 +01:00
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
// Restrict is used to limit nodes returned that don't match the miniumum storage requirements
|
|
|
|
func Restrict(r pb.Restriction, n []*pb.Node) []*pb.Node {
|
2018-08-01 15:15:38 +01:00
|
|
|
oper := r.GetOperand()
|
|
|
|
op := r.GetOperator()
|
|
|
|
val := r.GetValue()
|
|
|
|
var comp int64
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
results := []*pb.Node{}
|
2018-08-01 15:15:38 +01:00
|
|
|
for _, v := range n {
|
|
|
|
switch oper {
|
2018-09-18 05:39:06 +01:00
|
|
|
case pb.Restriction_freeBandwidth:
|
2018-08-01 15:15:38 +01:00
|
|
|
comp = v.GetRestrictions().GetFreeBandwidth()
|
2018-09-18 05:39:06 +01:00
|
|
|
case pb.Restriction_freeDisk:
|
2018-08-01 15:15:38 +01:00
|
|
|
comp = v.GetRestrictions().GetFreeDisk()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch op {
|
2018-09-18 05:39:06 +01:00
|
|
|
case pb.Restriction_EQ:
|
2018-10-16 16:22:31 +01:00
|
|
|
if comp == val {
|
2018-08-01 15:15:38 +01:00
|
|
|
results = append(results, v)
|
|
|
|
continue
|
|
|
|
}
|
2018-09-18 05:39:06 +01:00
|
|
|
case pb.Restriction_LT:
|
2018-08-01 15:15:38 +01:00
|
|
|
if comp < val {
|
|
|
|
results = append(results, v)
|
|
|
|
continue
|
|
|
|
}
|
2018-09-18 05:39:06 +01:00
|
|
|
case pb.Restriction_LTE:
|
2018-08-01 15:15:38 +01:00
|
|
|
if comp <= val {
|
|
|
|
results = append(results, v)
|
|
|
|
continue
|
|
|
|
}
|
2018-09-18 05:39:06 +01:00
|
|
|
case pb.Restriction_GT:
|
2018-08-01 15:15:38 +01:00
|
|
|
if comp > val {
|
|
|
|
results = append(results, v)
|
|
|
|
continue
|
|
|
|
}
|
2018-09-18 05:39:06 +01:00
|
|
|
case pb.Restriction_GTE:
|
2018-08-01 15:15:38 +01:00
|
|
|
if comp >= val {
|
|
|
|
results = append(results, v)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
2018-10-16 16:22:31 +01:00
|
|
|
|
|
|
|
func meetsRestrictions(rs []pb.Restriction, n pb.Node) bool {
|
|
|
|
for _, r := range rs {
|
|
|
|
oper := r.GetOperand()
|
|
|
|
op := r.GetOperator()
|
|
|
|
val := r.GetValue()
|
|
|
|
var comp int64
|
|
|
|
switch oper {
|
|
|
|
case pb.Restriction_freeBandwidth:
|
|
|
|
comp = n.GetRestrictions().GetFreeBandwidth()
|
|
|
|
case pb.Restriction_freeDisk:
|
|
|
|
comp = n.GetRestrictions().GetFreeDisk()
|
|
|
|
}
|
|
|
|
switch op {
|
|
|
|
case pb.Restriction_EQ:
|
|
|
|
if comp != val {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case pb.Restriction_LT:
|
|
|
|
if comp >= val {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case pb.Restriction_LTE:
|
|
|
|
if comp > val {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case pb.Restriction_GT:
|
|
|
|
if comp <= val {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case pb.Restriction_GTE:
|
|
|
|
if comp < val {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|