2018-05-16 19:47:59 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package kademlia
|
|
|
|
|
|
|
|
import (
|
2018-06-05 12:48:19 +01:00
|
|
|
"encoding/hex"
|
|
|
|
"strconv"
|
2018-05-16 19:47:59 +01:00
|
|
|
"time"
|
|
|
|
|
2018-06-05 12:48:19 +01:00
|
|
|
bkad "github.com/coyle/kademlia"
|
|
|
|
|
2018-05-16 19:47:59 +01:00
|
|
|
"storj.io/storj/protos/overlay"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RouteTable implements the RoutingTable interface
|
|
|
|
type RouteTable struct {
|
2018-06-05 12:48:19 +01:00
|
|
|
ht *bkad.HashTable
|
|
|
|
dht *bkad.DHT
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRouteTable returns a newly configured instance of a RouteTable
|
|
|
|
func NewRouteTable(dht Kademlia) RouteTable {
|
|
|
|
return RouteTable{
|
|
|
|
ht: dht.dht.HT,
|
|
|
|
dht: dht.dht,
|
|
|
|
}
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// LocalID returns the local nodes ID
|
|
|
|
func (rt RouteTable) LocalID() NodeID {
|
2018-06-05 12:48:19 +01:00
|
|
|
return NodeID(rt.dht.GetSelfID())
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// K returns the currently configured maximum of nodes to store in a bucket
|
|
|
|
func (rt RouteTable) K() int {
|
2018-06-05 12:48:19 +01:00
|
|
|
return rt.dht.NumNodes()
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CacheSize returns the total current size of the cache
|
|
|
|
func (rt RouteTable) CacheSize() int {
|
2018-06-05 12:48:19 +01:00
|
|
|
//TODO: How is this calculated ? size of the routing table ? is it total bytes, mb, kb etc .?
|
2018-05-16 19:47:59 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucket retrieves a bucket from the local node
|
|
|
|
func (rt RouteTable) GetBucket(id string) (bucket Bucket, ok bool) {
|
2018-06-05 12:48:19 +01:00
|
|
|
i, err := hex.DecodeString(id)
|
|
|
|
if err != nil {
|
|
|
|
return KBucket{}, false
|
|
|
|
}
|
|
|
|
b := rt.ht.GetBucket(i)
|
|
|
|
if b == nil {
|
|
|
|
return KBucket{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return KBucket{
|
|
|
|
nodes: convertNetworkNodes(b),
|
|
|
|
}, true
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBuckets retrieves all buckets from the local node
|
2018-06-05 12:48:19 +01:00
|
|
|
func (rt RouteTable) GetBuckets() (k []Bucket, err error) {
|
|
|
|
bs := []Bucket{}
|
|
|
|
b := rt.ht.GetBuckets()
|
|
|
|
for i, v := range b {
|
|
|
|
bs[i] = KBucket{nodes: convertNetworkNodes(v)}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bs, nil
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FindNear finds all Nodes near the provided nodeID up to the provided limit
|
|
|
|
func (rt RouteTable) FindNear(id NodeID, limit int) ([]overlay.Node, error) {
|
2018-06-05 12:48:19 +01:00
|
|
|
return convertNetworkNodes(rt.ht.GetClosestContacts([]byte(id), limit)), nil
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectionSuccess handles the details of what kademlia should do when
|
|
|
|
// a successful connection is made to node on the network
|
|
|
|
func (rt RouteTable) ConnectionSuccess(id string, address overlay.NodeAddress) {
|
2018-06-05 12:48:19 +01:00
|
|
|
// TODO: What should we do ?
|
2018-05-16 19:47:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectionFailed handles the details of what kademlia should do when
|
|
|
|
// a connection fails for a node on the network
|
|
|
|
func (rt RouteTable) ConnectionFailed(id string, address overlay.NodeAddress) {
|
2018-06-05 12:48:19 +01:00
|
|
|
// TODO: What should we do ?
|
2018-05-16 19:47:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBucketTimestamp updates the last updated time for a bucket
|
|
|
|
func (rt RouteTable) SetBucketTimestamp(id string, now time.Time) error {
|
2018-06-05 12:48:19 +01:00
|
|
|
i, err := strconv.Atoi(id)
|
|
|
|
if err != nil {
|
|
|
|
return NodeErr.New("unable to convert id to int")
|
|
|
|
}
|
|
|
|
|
|
|
|
rt.ht.SetBucketTime(i, now)
|
|
|
|
|
2018-05-16 19:47:59 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketTimestamp retrieves the last updated time for a bucket
|
|
|
|
func (rt RouteTable) GetBucketTimestamp(id string, bucket Bucket) (time.Time, error) {
|
2018-06-05 12:48:19 +01:00
|
|
|
return rt.dht.GetExpirationTime([]byte(id)), nil
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|