2014-07-23 04:48:30 -07:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
2014-08-07 14:16:24 -07:00
|
|
|
"bytes"
|
2014-08-05 09:38:26 -07:00
|
|
|
"encoding/json"
|
2014-08-08 18:09:21 -07:00
|
|
|
"errors"
|
2014-08-09 22:28:46 -07:00
|
|
|
"fmt"
|
2014-08-08 18:09:21 -07:00
|
|
|
"math/rand"
|
|
|
|
"time"
|
2014-07-30 17:46:56 -07:00
|
|
|
|
2014-08-03 17:35:12 -07:00
|
|
|
proto "code.google.com/p/goprotobuf/proto"
|
|
|
|
|
2014-08-05 09:38:26 -07:00
|
|
|
ma "github.com/jbenet/go-multiaddr"
|
|
|
|
|
2014-07-29 17:55:19 -07:00
|
|
|
peer "github.com/jbenet/go-ipfs/peer"
|
2014-08-08 19:58:42 -07:00
|
|
|
kb "github.com/jbenet/go-ipfs/routing/kbucket"
|
2014-07-29 17:55:19 -07:00
|
|
|
swarm "github.com/jbenet/go-ipfs/swarm"
|
|
|
|
u "github.com/jbenet/go-ipfs/util"
|
2014-07-23 04:48:30 -07:00
|
|
|
)
|
|
|
|
|
2014-08-03 21:46:01 -07:00
|
|
|
// Pool size is the number of nodes used for group find/set RPC calls
|
|
|
|
var PoolSize = 6
|
|
|
|
|
2014-07-29 19:33:51 -07:00
|
|
|
// TODO: determine a way of creating and managing message IDs
|
|
|
|
func GenerateMessageID() uint64 {
|
2014-08-05 18:32:22 -07:00
|
|
|
//return (uint64(rand.Uint32()) << 32) & uint64(rand.Uint32())
|
|
|
|
return uint64(rand.Uint32())
|
2014-07-29 19:33:51 -07:00
|
|
|
}
|
|
|
|
|
2014-07-23 04:48:30 -07:00
|
|
|
// This file implements the Routing interface for the IpfsDHT struct.
|
|
|
|
|
|
|
|
// Basic Put/Get
|
|
|
|
|
|
|
|
// PutValue adds value corresponding to given Key.
|
2014-08-07 18:06:50 -07:00
|
|
|
// This is the top level "Store" operation of the DHT
|
2014-08-09 22:28:46 -07:00
|
|
|
func (s *IpfsDHT) PutValue(key u.Key, value []byte) {
|
|
|
|
complete := make(chan struct{})
|
|
|
|
for i, route := range s.routes {
|
|
|
|
p := route.NearestPeer(kb.ConvertKey(key))
|
|
|
|
if p == nil {
|
|
|
|
s.network.Chan.Errors <- fmt.Errorf("No peer found on level %d", i)
|
|
|
|
continue
|
|
|
|
go func() {
|
|
|
|
complete <- struct{}{}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
err := s.putValueToNetwork(p, string(key), value)
|
|
|
|
if err != nil {
|
|
|
|
s.network.Chan.Errors <- err
|
|
|
|
}
|
|
|
|
complete <- struct{}{}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
for _, _ = range s.routes {
|
|
|
|
<-complete
|
2014-08-03 17:35:12 -07:00
|
|
|
}
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetValue searches for the value corresponding to given Key.
|
2014-08-06 21:36:56 -07:00
|
|
|
// If the search does not succeed, a multiaddr string of a closer peer is
|
|
|
|
// returned along with util.ErrSearchIncomplete
|
2014-07-23 04:48:30 -07:00
|
|
|
func (s *IpfsDHT) GetValue(key u.Key, timeout time.Duration) ([]byte, error) {
|
2014-08-09 22:28:46 -07:00
|
|
|
for _, route := range s.routes {
|
|
|
|
var p *peer.Peer
|
|
|
|
p = route.NearestPeer(kb.ConvertKey(key))
|
|
|
|
if p == nil {
|
|
|
|
return nil, errors.New("Table returned nil peer!")
|
|
|
|
}
|
2014-07-28 22:14:27 -07:00
|
|
|
|
2014-08-09 22:28:46 -07:00
|
|
|
b, err := s.getValueSingle(p, key, timeout)
|
|
|
|
if err == nil {
|
|
|
|
return b, nil
|
2014-08-07 18:06:50 -07:00
|
|
|
}
|
2014-08-09 22:28:46 -07:00
|
|
|
if err != u.ErrSearchIncomplete {
|
2014-08-08 18:09:21 -07:00
|
|
|
return nil, err
|
2014-08-03 17:35:12 -07:00
|
|
|
}
|
2014-07-28 22:14:27 -07:00
|
|
|
}
|
2014-08-09 22:28:46 -07:00
|
|
|
return nil, u.ErrNotFound
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value provider layer of indirection.
|
|
|
|
// This is what DSHTs (Coral and MainlineDHT) do to store large values in a DHT.
|
|
|
|
|
|
|
|
// Announce that this node can provide value for given key
|
2014-07-29 17:55:19 -07:00
|
|
|
func (s *IpfsDHT) Provide(key u.Key) error {
|
2014-08-08 19:58:42 -07:00
|
|
|
peers := s.routes[0].NearestPeers(kb.ConvertKey(key), PoolSize)
|
2014-08-03 21:46:01 -07:00
|
|
|
if len(peers) == 0 {
|
|
|
|
//return an error
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
pmes := DHTMessage{
|
|
|
|
Type: PBDHTMessage_ADD_PROVIDER,
|
|
|
|
Key: string(key),
|
2014-08-03 21:46:01 -07:00
|
|
|
}
|
|
|
|
pbmes := pmes.ToProtobuf()
|
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
for _, p := range peers {
|
2014-08-03 21:46:01 -07:00
|
|
|
mes := swarm.NewMessage(p, pbmes)
|
2014-08-08 18:09:21 -07:00
|
|
|
s.network.Chan.Outgoing <- mes
|
2014-08-03 21:46:01 -07:00
|
|
|
}
|
|
|
|
return nil
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// FindProviders searches for peers who can provide the value for given key.
|
2014-08-03 21:46:01 -07:00
|
|
|
func (s *IpfsDHT) FindProviders(key u.Key, timeout time.Duration) ([]*peer.Peer, error) {
|
2014-08-08 19:58:42 -07:00
|
|
|
p := s.routes[0].NearestPeer(kb.ConvertKey(key))
|
2014-08-03 21:46:01 -07:00
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
pmes := DHTMessage{
|
|
|
|
Type: PBDHTMessage_GET_PROVIDERS,
|
|
|
|
Key: string(key),
|
|
|
|
Id: GenerateMessageID(),
|
2014-08-03 21:46:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mes := swarm.NewMessage(p, pmes.ToProtobuf())
|
|
|
|
|
2014-08-09 22:28:46 -07:00
|
|
|
listenChan := s.ListenFor(pmes.Id, 1, time.Minute)
|
2014-08-05 18:32:22 -07:00
|
|
|
u.DOut("Find providers for: '%s'", key)
|
2014-08-08 18:09:21 -07:00
|
|
|
s.network.Chan.Outgoing <- mes
|
2014-08-03 21:46:01 -07:00
|
|
|
after := time.After(timeout)
|
|
|
|
select {
|
|
|
|
case <-after:
|
|
|
|
s.Unlisten(pmes.Id)
|
|
|
|
return nil, u.ErrTimeout
|
2014-08-09 22:28:46 -07:00
|
|
|
case resp := <-listenChan:
|
2014-08-05 18:32:22 -07:00
|
|
|
u.DOut("FindProviders: got response.")
|
2014-08-08 18:09:21 -07:00
|
|
|
pmes_out := new(PBDHTMessage)
|
2014-08-03 21:46:01 -07:00
|
|
|
err := proto.Unmarshal(resp.Data, pmes_out)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-05 18:32:22 -07:00
|
|
|
var addrs map[u.Key]string
|
|
|
|
err = json.Unmarshal(pmes_out.GetValue(), &addrs)
|
2014-08-05 09:38:26 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:32:22 -07:00
|
|
|
var prov_arr []*peer.Peer
|
2014-08-08 18:09:21 -07:00
|
|
|
for pid, addr := range addrs {
|
2014-08-05 18:32:22 -07:00
|
|
|
p := s.network.Find(pid)
|
2014-08-05 09:38:26 -07:00
|
|
|
if p == nil {
|
2014-08-08 18:09:21 -07:00
|
|
|
maddr, err := ma.NewMultiaddr(addr)
|
2014-08-05 09:38:26 -07:00
|
|
|
if err != nil {
|
|
|
|
u.PErr("error connecting to new peer: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
2014-08-05 18:32:22 -07:00
|
|
|
p, err = s.Connect(maddr)
|
2014-08-05 09:38:26 -07:00
|
|
|
if err != nil {
|
|
|
|
u.PErr("error connecting to new peer: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 18:32:22 -07:00
|
|
|
s.addProviderEntry(key, p)
|
|
|
|
prov_arr = append(prov_arr, p)
|
2014-08-05 09:38:26 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 18:32:22 -07:00
|
|
|
return prov_arr, nil
|
2014-08-03 21:46:01 -07:00
|
|
|
}
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find specific Peer
|
|
|
|
|
|
|
|
// FindPeer searches for a peer with given ID.
|
|
|
|
func (s *IpfsDHT) FindPeer(id peer.ID, timeout time.Duration) (*peer.Peer, error) {
|
2014-08-08 19:58:42 -07:00
|
|
|
p := s.routes[0].NearestPeer(kb.ConvertPeerID(id))
|
2014-08-03 21:46:01 -07:00
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
pmes := DHTMessage{
|
|
|
|
Type: PBDHTMessage_FIND_NODE,
|
|
|
|
Key: string(id),
|
|
|
|
Id: GenerateMessageID(),
|
2014-08-03 21:46:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mes := swarm.NewMessage(p, pmes.ToProtobuf())
|
|
|
|
|
2014-08-09 22:28:46 -07:00
|
|
|
listenChan := s.ListenFor(pmes.Id, 1, time.Minute)
|
2014-08-08 18:09:21 -07:00
|
|
|
s.network.Chan.Outgoing <- mes
|
2014-08-03 21:46:01 -07:00
|
|
|
after := time.After(timeout)
|
|
|
|
select {
|
|
|
|
case <-after:
|
|
|
|
s.Unlisten(pmes.Id)
|
|
|
|
return nil, u.ErrTimeout
|
2014-08-09 22:28:46 -07:00
|
|
|
case resp := <-listenChan:
|
2014-08-08 18:09:21 -07:00
|
|
|
pmes_out := new(PBDHTMessage)
|
2014-08-03 21:46:01 -07:00
|
|
|
err := proto.Unmarshal(resp.Data, pmes_out)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-05 20:31:48 -07:00
|
|
|
addr := string(pmes_out.GetValue())
|
|
|
|
maddr, err := ma.NewMultiaddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-06 10:02:53 -07:00
|
|
|
found_peer, err := s.Connect(maddr)
|
|
|
|
if err != nil {
|
|
|
|
u.POut("Found peer but couldnt connect.")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found_peer.ID.Equal(id) {
|
|
|
|
u.POut("FindPeer: searching for '%s' but found '%s'", id.Pretty(), found_peer.ID.Pretty())
|
|
|
|
return found_peer, u.ErrSearchIncomplete
|
|
|
|
}
|
|
|
|
|
|
|
|
return found_peer, nil
|
2014-08-03 21:46:01 -07:00
|
|
|
}
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
2014-08-06 18:37:45 -07:00
|
|
|
|
|
|
|
// Ping a peer, log the time it took
|
|
|
|
func (dht *IpfsDHT) Ping(p *peer.Peer, timeout time.Duration) error {
|
|
|
|
// Thoughts: maybe this should accept an ID and do a peer lookup?
|
|
|
|
u.DOut("Enter Ping.")
|
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
pmes := DHTMessage{Id: GenerateMessageID(), Type: PBDHTMessage_PING}
|
2014-08-06 18:37:45 -07:00
|
|
|
mes := swarm.NewMessage(p, pmes.ToProtobuf())
|
|
|
|
|
|
|
|
before := time.Now()
|
2014-08-07 18:06:50 -07:00
|
|
|
response_chan := dht.ListenFor(pmes.Id, 1, time.Minute)
|
2014-08-06 18:37:45 -07:00
|
|
|
dht.network.Chan.Outgoing <- mes
|
|
|
|
|
|
|
|
tout := time.After(timeout)
|
|
|
|
select {
|
|
|
|
case <-response_chan:
|
|
|
|
roundtrip := time.Since(before)
|
2014-08-08 18:09:21 -07:00
|
|
|
p.SetLatency(roundtrip)
|
2014-08-06 18:37:45 -07:00
|
|
|
u.POut("Ping took %s.", roundtrip.String())
|
|
|
|
return nil
|
|
|
|
case <-tout:
|
|
|
|
// Timed out, think about removing peer from network
|
|
|
|
u.DOut("Ping peer timed out.")
|
|
|
|
dht.Unlisten(pmes.Id)
|
|
|
|
return u.ErrTimeout
|
|
|
|
}
|
|
|
|
}
|
2014-08-07 14:16:24 -07:00
|
|
|
|
|
|
|
func (dht *IpfsDHT) GetDiagnostic(timeout time.Duration) ([]*diagInfo, error) {
|
|
|
|
u.DOut("Begin Diagnostic")
|
|
|
|
//Send to N closest peers
|
2014-08-08 19:58:42 -07:00
|
|
|
targets := dht.routes[0].NearestPeers(kb.ConvertPeerID(dht.self.ID), 10)
|
2014-08-07 14:16:24 -07:00
|
|
|
|
|
|
|
// TODO: Add timeout to this struct so nodes know when to return
|
2014-08-08 18:09:21 -07:00
|
|
|
pmes := DHTMessage{
|
|
|
|
Type: PBDHTMessage_DIAGNOSTIC,
|
|
|
|
Id: GenerateMessageID(),
|
2014-08-07 14:16:24 -07:00
|
|
|
}
|
|
|
|
|
2014-08-09 22:28:46 -07:00
|
|
|
listenChan := dht.ListenFor(pmes.Id, len(targets), time.Minute*2)
|
2014-08-07 14:16:24 -07:00
|
|
|
|
|
|
|
pbmes := pmes.ToProtobuf()
|
2014-08-08 18:09:21 -07:00
|
|
|
for _, p := range targets {
|
2014-08-07 14:16:24 -07:00
|
|
|
mes := swarm.NewMessage(p, pbmes)
|
2014-08-08 18:09:21 -07:00
|
|
|
dht.network.Chan.Outgoing <- mes
|
2014-08-07 14:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var out []*diagInfo
|
|
|
|
after := time.After(timeout)
|
|
|
|
for count := len(targets); count > 0; {
|
|
|
|
select {
|
|
|
|
case <-after:
|
|
|
|
u.DOut("Diagnostic request timed out.")
|
|
|
|
return out, u.ErrTimeout
|
2014-08-09 22:28:46 -07:00
|
|
|
case resp := <-listenChan:
|
2014-08-08 18:09:21 -07:00
|
|
|
pmes_out := new(PBDHTMessage)
|
2014-08-07 14:16:24 -07:00
|
|
|
err := proto.Unmarshal(resp.Data, pmes_out)
|
|
|
|
if err != nil {
|
|
|
|
// NOTE: here and elsewhere, need to audit error handling,
|
|
|
|
// some errors should be continued on from
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dec := json.NewDecoder(bytes.NewBuffer(pmes_out.GetValue()))
|
|
|
|
for {
|
|
|
|
di := new(diagInfo)
|
|
|
|
err := dec.Decode(di)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, di)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
return nil, nil
|
2014-08-07 14:16:24 -07:00
|
|
|
}
|