2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-05-09 15:04:51 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package kademlia
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-08 16:09:37 +01:00
|
|
|
"fmt"
|
2018-12-20 21:45:06 +00:00
|
|
|
"math/rand"
|
2018-10-08 21:37:52 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-12-07 18:31:59 +00:00
|
|
|
"sync/atomic"
|
2018-12-20 21:45:06 +00:00
|
|
|
"time"
|
2018-12-07 18:31:59 +00:00
|
|
|
"unsafe"
|
2018-05-09 15:04:51 +01:00
|
|
|
|
2018-11-20 18:29:07 +00:00
|
|
|
"github.com/gogo/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-11-21 15:07:18 +00:00
|
|
|
|
2018-06-22 14:33:57 +01:00
|
|
|
"storj.io/storj/pkg/dht"
|
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-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-01-18 15:00:56 +00:00
|
|
|
"storj.io/storj/pkg/transport"
|
2018-10-16 16:22:31 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-10-18 17:20:23 +01:00
|
|
|
"storj.io/storj/storage/boltdb"
|
2018-10-08 16:09:37 +01:00
|
|
|
)
|
|
|
|
|
2018-11-01 17:03:46 +00:00
|
|
|
var (
|
|
|
|
// NodeErr is the class for all errors pertaining to node operations
|
|
|
|
NodeErr = errs.Class("node error")
|
|
|
|
// BootstrapErr is the class for all errors pertaining to bootstrapping a node
|
|
|
|
BootstrapErr = errs.Class("bootstrap node error")
|
|
|
|
// NodeNotFound is returned when a lookup can not produce the requested node
|
2018-12-04 21:39:28 +00:00
|
|
|
NodeNotFound = errs.Class("node not found")
|
2018-11-01 17:03:46 +00:00
|
|
|
// TODO: shouldn't default to TCP but not sure what to do yet
|
|
|
|
defaultTransport = pb.NodeTransport_TCP_TLS_GRPC
|
|
|
|
defaultRetries = 3
|
|
|
|
)
|
2018-10-08 16:09:37 +01:00
|
|
|
|
2018-11-01 17:03:46 +00:00
|
|
|
type discoveryOptions struct {
|
2018-11-20 16:54:52 +00:00
|
|
|
concurrency int
|
|
|
|
retries int
|
|
|
|
bootstrap bool
|
|
|
|
bootstrapNodes []pb.Node
|
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-12-18 15:13:32 +00:00
|
|
|
log *zap.Logger
|
2018-12-07 18:31:59 +00:00
|
|
|
alpha int // alpha is a system wide concurrency parameter
|
|
|
|
routingTable *RoutingTable
|
|
|
|
bootstrapNodes []pb.Node
|
2019-01-18 15:00:56 +00:00
|
|
|
dialer *Dialer
|
2018-12-07 18:31:59 +00:00
|
|
|
identity *provider.FullIdentity
|
|
|
|
bootstrapCancel unsafe.Pointer // context.CancelFunc
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// New returns a newly configured Kademlia instance
|
|
|
|
var New = NewKademlia
|
|
|
|
|
2018-06-22 14:33:57 +01:00
|
|
|
// NewKademlia returns a newly configured Kademlia instance
|
2019-01-02 10:57:06 +00:00
|
|
|
func NewKademlia(log *zap.Logger, nodeType pb.NodeType, bootstrapNodes []pb.Node, address string, metadata *pb.NodeMetadata, identity *provider.FullIdentity, path string, alpha int) (*Kademlia, error) {
|
2018-11-21 15:07:18 +00:00
|
|
|
self := pb.Node{
|
2019-01-02 10:57:06 +00:00
|
|
|
Id: identity.ID,
|
2018-11-29 14:57:00 +00:00
|
|
|
Type: nodeType,
|
2018-11-21 15:07:18 +00:00
|
|
|
Address: &pb.NodeAddress{Address: address},
|
|
|
|
Metadata: metadata,
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
2018-10-18 17:20:23 +01:00
|
|
|
|
2019-01-02 10:57:06 +00:00
|
|
|
bucketIdentifier := self.Id.String()[:5] // need a way to differentiate between nodes if running more than one simultaneously
|
2018-10-18 17:20:23 +01:00
|
|
|
dbpath := filepath.Join(path, fmt.Sprintf("kademlia_%s.db", bucketIdentifier))
|
|
|
|
|
|
|
|
dbs, err := boltdb.NewShared(dbpath, KademliaBucket, NodeBucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, BootstrapErr.Wrap(err)
|
|
|
|
}
|
|
|
|
kdb, ndb := dbs[0], dbs[1]
|
|
|
|
|
2018-12-22 00:48:53 +00:00
|
|
|
rt, err := NewRoutingTable(log, self, kdb, ndb)
|
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-12-18 15:13:32 +00:00
|
|
|
return NewKademliaWithRoutingTable(log, self, bootstrapNodes, identity, alpha, rt)
|
2018-10-26 17:54:00 +01:00
|
|
|
}
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// NewWith returns a newly configured Kademlia instance
|
|
|
|
var NewWith = NewKademliaWithRoutingTable
|
|
|
|
|
2018-10-26 17:54:00 +01:00
|
|
|
// NewKademliaWithRoutingTable returns a newly configured Kademlia instance
|
2018-12-18 15:13:32 +00:00
|
|
|
func NewKademliaWithRoutingTable(log *zap.Logger, self pb.Node, bootstrapNodes []pb.Node, identity *provider.FullIdentity, alpha int, rt *RoutingTable) (*Kademlia, error) {
|
2018-10-08 16:09:37 +01:00
|
|
|
k := &Kademlia{
|
2018-12-18 15:13:32 +00:00
|
|
|
log: log,
|
2018-10-26 17:54:00 +01:00
|
|
|
alpha: alpha,
|
2018-10-08 16:09:37 +01:00
|
|
|
routingTable: rt,
|
|
|
|
bootstrapNodes: bootstrapNodes,
|
|
|
|
identity: identity,
|
2019-01-02 18:47:34 +00:00
|
|
|
|
2019-01-18 15:00:56 +00:00
|
|
|
dialer: NewDialer(log.Named("dialer"), transport.NewClient(identity, rt)),
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
return k, nil
|
2018-05-09 15:04:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// Close closes all kademlia connections and prevents new ones from being created.
|
|
|
|
func (k *Kademlia) Close() error {
|
|
|
|
// Cancel the bootstrap context
|
|
|
|
ptr := atomic.LoadPointer(&k.bootstrapCancel)
|
|
|
|
if ptr != nil {
|
|
|
|
(*(*context.CancelFunc)(ptr))()
|
|
|
|
}
|
2019-01-18 15:00:56 +00:00
|
|
|
return k.dialer.Close()
|
2019-01-10 13:13:27 +00: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 {
|
2018-12-07 18:31:59 +00:00
|
|
|
// Cancel the bootstrap context
|
|
|
|
ptr := atomic.LoadPointer(&k.bootstrapCancel)
|
|
|
|
if ptr != nil {
|
|
|
|
(*(*context.CancelFunc)(ptr))()
|
|
|
|
}
|
2018-12-20 21:45:06 +00:00
|
|
|
return errs.Combine(
|
2019-01-18 15:00:56 +00:00
|
|
|
k.dialer.Close(),
|
2018-10-18 17:20:23 +01:00
|
|
|
k.routingTable.Close(),
|
|
|
|
)
|
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-11-29 18:39:27 +00:00
|
|
|
func (k *Kademlia) GetNodes(ctx context.Context, start storj.NodeID, 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-- {
|
2018-11-29 18:39:27 +00:00
|
|
|
var (
|
|
|
|
id storj.NodeID
|
|
|
|
node = &pb.Node{}
|
|
|
|
)
|
|
|
|
err := id.Unmarshal(item.Key)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
err = proto.Unmarshal(item.Value, node)
|
2018-10-16 16:22:31 +01:00
|
|
|
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{
|
2018-11-29 18:39:27 +00:00
|
|
|
First: storage.Key(start.Bytes()),
|
2018-10-16 16:22:31 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// SetBootstrapNodes sets the bootstrap nodes.
|
|
|
|
// Must be called before anything starting to use kademlia.
|
|
|
|
func (k *Kademlia) SetBootstrapNodes(nodes []pb.Node) { k.bootstrapNodes = nodes }
|
|
|
|
|
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
|
|
|
if len(k.bootstrapNodes) == 0 {
|
|
|
|
return BootstrapErr.New("no bootstrap nodes provided")
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
2019-01-23 15:48:46 +00:00
|
|
|
|
|
|
|
var errs errs.Group
|
|
|
|
for _, node := range k.bootstrapNodes {
|
|
|
|
_, err := k.dialer.Ping(ctx, node)
|
|
|
|
if err == nil {
|
|
|
|
// We have pinged successfully one bootstrap node.
|
|
|
|
// Clear any errors and break the cycle.
|
|
|
|
errs = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
errs.Add(err)
|
|
|
|
}
|
|
|
|
err := errs.Err()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-07 18:31:59 +00:00
|
|
|
bootstrapContext, bootstrapCancel := context.WithCancel(ctx)
|
|
|
|
atomic.StorePointer(&k.bootstrapCancel, unsafe.Pointer(&bootstrapCancel))
|
2019-01-08 16:01:22 +00:00
|
|
|
|
2019-01-23 15:48:46 +00:00
|
|
|
//find nodes most similar to self
|
2019-01-08 16:01:22 +00:00
|
|
|
k.routingTable.mutex.Lock()
|
|
|
|
id := k.routingTable.self.Id
|
|
|
|
k.routingTable.mutex.Unlock()
|
2019-01-23 15:48:46 +00:00
|
|
|
_, err = k.lookup(bootstrapContext, id, true)
|
2018-12-20 21:45:06 +00:00
|
|
|
return err
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2019-01-18 15:00:56 +00:00
|
|
|
ok, err := k.dialer.Ping(ctx, node)
|
2018-10-26 17:38:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return pb.Node{}, NodeErr.Wrap(err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return pb.Node{}, NodeErr.New("Failed pinging node")
|
|
|
|
}
|
|
|
|
return 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
|
2018-11-29 18:39:27 +00:00
|
|
|
func (k *Kademlia) FindNode(ctx context.Context, ID storj.NodeID) (pb.Node, error) {
|
2018-12-20 21:45:06 +00:00
|
|
|
return k.lookup(ctx, ID, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
//lookup initiates a kadmelia node lookup
|
|
|
|
func (k *Kademlia) lookup(ctx context.Context, ID storj.NodeID, isBootstrap bool) (pb.Node, error) {
|
2018-12-04 21:39:28 +00:00
|
|
|
kb := k.routingTable.K()
|
2018-12-20 21:45:06 +00:00
|
|
|
var nodes []*pb.Node
|
|
|
|
if isBootstrap {
|
|
|
|
for _, v := range k.bootstrapNodes {
|
|
|
|
nodes = append(nodes, &v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
nodes, err = k.routingTable.FindNear(ID, kb)
|
|
|
|
if err != nil {
|
|
|
|
return pb.Node{}, err
|
|
|
|
}
|
2018-12-04 21:39:28 +00:00
|
|
|
}
|
2019-01-18 15:00:56 +00:00
|
|
|
lookup := newPeerDiscovery(k.log, k.routingTable.Local(), nodes, k.dialer, ID, discoveryOptions{
|
2018-12-20 21:45:06 +00:00
|
|
|
concurrency: k.alpha, retries: defaultRetries, bootstrap: isBootstrap, bootstrapNodes: k.bootstrapNodes,
|
2018-11-20 16:54:52 +00:00
|
|
|
})
|
2018-12-05 14:32:37 +00:00
|
|
|
target, err := lookup.Run(ctx)
|
2018-11-20 16:54:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return pb.Node{}, err
|
|
|
|
}
|
2018-12-20 21:45:06 +00:00
|
|
|
bucket, err := k.routingTable.getKBucketID(ID)
|
|
|
|
if err != nil {
|
|
|
|
k.log.Warn("Error getting getKBucketID in kad lookup")
|
|
|
|
} else {
|
|
|
|
err = k.routingTable.SetBucketTimestamp(bucket[:], time.Now())
|
|
|
|
if err != nil {
|
|
|
|
k.log.Warn("Error updating bucket timestamp in kad lookup")
|
|
|
|
}
|
|
|
|
}
|
2018-12-05 14:32:37 +00:00
|
|
|
if target == nil {
|
2018-12-20 21:45:06 +00:00
|
|
|
if isBootstrap {
|
|
|
|
return pb.Node{}, nil
|
|
|
|
}
|
2018-12-05 14:32:37 +00:00
|
|
|
return pb.Node{}, NodeNotFound.New("")
|
2018-12-04 21:39:28 +00:00
|
|
|
}
|
2018-12-05 14:32:37 +00:00
|
|
|
return *target, nil
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-11-20 16:54:52 +00:00
|
|
|
// Seen returns all nodes that this kademlia instance has successfully communicated with
|
|
|
|
func (k *Kademlia) Seen() []*pb.Node {
|
|
|
|
nodes := []*pb.Node{}
|
|
|
|
k.routingTable.mutex.Lock()
|
|
|
|
for _, v := range k.routingTable.seen {
|
2018-11-29 18:39:27 +00:00
|
|
|
nodes = append(nodes, pb.CopyNode(v))
|
2018-11-20 16:54:52 +00:00
|
|
|
}
|
|
|
|
k.routingTable.mutex.Unlock()
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
},
|
2019-01-23 10:39:03 +00:00
|
|
|
Type: pb.NodeType_BOOTSTRAP,
|
2018-06-22 14:33:57 +01:00
|
|
|
}, nil
|
2018-05-09 15:04:51 +01:00
|
|
|
}
|
2018-08-01 15:15:38 +01:00
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// StartRefresh occasionally refreshes stale kad buckets
|
2018-12-20 21:45:06 +00:00
|
|
|
func (k *Kademlia) StartRefresh(ctx context.Context) {
|
|
|
|
go func() {
|
2019-01-18 13:54:08 +00:00
|
|
|
err := k.RunRefresh(ctx)
|
|
|
|
if err != nil && err != context.Canceled {
|
|
|
|
k.log.Error("refresh returned", zap.Error(err))
|
2018-12-20 21:45:06 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
// RunRefresh occasionally refreshes stale kad buckets
|
|
|
|
func (k *Kademlia) RunRefresh(ctx context.Context) error {
|
|
|
|
ticker := time.NewTicker(5 * time.Minute)
|
|
|
|
time.Sleep(time.Duration(rand.Intn(300)) * time.Second) //stagger
|
|
|
|
for {
|
|
|
|
if err := k.refresh(ctx); err != nil {
|
|
|
|
k.log.Warn("bucket refresh failed", zap.Error(err))
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
case <-ctx.Done():
|
|
|
|
ticker.Stop()
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// refresh updates each Kademlia bucket not contacted in the last hour
|
2018-12-20 21:45:06 +00:00
|
|
|
func (k *Kademlia) refresh(ctx context.Context) error {
|
|
|
|
bIDs, err := k.routingTable.GetBucketIds()
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
startID := bucketID{}
|
|
|
|
var errors errs.Group
|
|
|
|
for _, bID := range bIDs {
|
|
|
|
ts, tErr := k.routingTable.GetBucketTimestamp(bID)
|
|
|
|
if tErr != nil {
|
|
|
|
errors.Add(tErr)
|
|
|
|
} else if now.After(ts.Add(time.Hour)) {
|
|
|
|
rID, _ := randomIDInRange(startID, keyToBucketID(bID))
|
|
|
|
_, _ = k.FindNode(ctx, rID) // ignore node not found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Error.Wrap(errors.Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
// randomIDInRange finds a random node ID with a range (start..end]
|
|
|
|
func randomIDInRange(start, end bucketID) (storj.NodeID, error) {
|
|
|
|
randID := storj.NodeID{}
|
|
|
|
divergedHigh := false
|
|
|
|
divergedLow := false
|
|
|
|
for x := 0; x < len(randID); x++ {
|
|
|
|
s := byte(0)
|
|
|
|
if !divergedLow {
|
|
|
|
s = start[x]
|
|
|
|
}
|
|
|
|
e := byte(255)
|
|
|
|
if !divergedHigh {
|
|
|
|
e = end[x]
|
|
|
|
}
|
|
|
|
if s > e {
|
|
|
|
return storj.NodeID{}, errs.New("Random id range was invalid")
|
|
|
|
}
|
|
|
|
if s == e {
|
|
|
|
randID[x] = s
|
|
|
|
} else {
|
|
|
|
r := s + byte(rand.Intn(int(e-s))) + 1
|
|
|
|
if r < e {
|
|
|
|
divergedHigh = true
|
|
|
|
}
|
|
|
|
if r > s {
|
|
|
|
divergedLow = true
|
|
|
|
}
|
|
|
|
randID[x] = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !divergedLow {
|
|
|
|
if !divergedHigh { // start == end
|
|
|
|
return storj.NodeID{}, errs.New("Random id range was invalid")
|
|
|
|
} else if randID[len(randID)-1] == start[len(randID)-1] { // start == randID
|
|
|
|
randID[len(randID)-1] = start[len(randID)-1] + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return randID, nil
|
|
|
|
}
|
|
|
|
|
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-11-24 02:46:53 +00:00
|
|
|
case pb.Restriction_FREE_BANDWIDTH:
|
2018-08-01 15:15:38 +01:00
|
|
|
comp = v.GetRestrictions().GetFreeBandwidth()
|
2018-11-24 02:46:53 +00:00
|
|
|
case pb.Restriction_FREE_DISK:
|
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 {
|
2018-11-24 02:46:53 +00:00
|
|
|
case pb.Restriction_FREE_BANDWIDTH:
|
2018-10-16 16:22:31 +01:00
|
|
|
comp = n.GetRestrictions().GetFreeBandwidth()
|
2018-11-24 02:46:53 +00:00
|
|
|
case pb.Restriction_FREE_DISK:
|
2018-10-16 16:22:31 +01:00
|
|
|
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
|
|
|
|
}
|