server.go 6 KB
Newer Older
1
package supernode
2 3

import (
Jeromy's avatar
Jeromy committed
4
	"context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	"errors"
6 7
	"fmt"

8
	proxy "github.com/ipfs/go-ipfs/routing/supernode/proxy"
Jeromy's avatar
Jeromy committed
9
	dshelp "github.com/ipfs/go-ipfs/thirdparty/ds-help"
Jeromy's avatar
Jeromy committed
10

Łukasz Magiera's avatar
Łukasz Magiera committed
11 12
	dhtpb "gx/ipfs/QmNaAVhp2UXfeDTLhHRUxjB69Tpku38ovSmQegcAMoJXbY/go-libp2p-kad-dht/pb"
	datastore "gx/ipfs/QmSiN66ybp5udnQnvhb6euiWiiQWdGvwMhAWa95cC1DTCV/go-datastore"
13
	pb "gx/ipfs/QmWYCqr6UDqqD1bfRybaAPtbAqcN3TSJpveaBXMwbQ3ePZ/go-libp2p-record/pb"
14
	pstore "gx/ipfs/QmXZSd1qR5BxZkPyuwfT5jpqQFScZccoZvDneXsKzCNHWX/go-libp2p-peerstore"
Jeromy's avatar
Jeromy committed
15
	proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
16
	peer "gx/ipfs/QmdS9KpbDyPrieswibZhkod1oXqRwZJrUPzxCofAMWpFGq/go-libp2p-peer"
17 18 19 20 21
)

// Server handles routing queries using a database backend
type Server struct {
	local           peer.ID
Jeromy's avatar
Jeromy committed
22
	routingBackend  datastore.Datastore
Jeromy's avatar
Jeromy committed
23
	peerstore       pstore.Peerstore
24 25 26
	*proxy.Loopback // so server can be injected into client
}

27
// NewServer creates a new Supernode routing Server
Jeromy's avatar
Jeromy committed
28
func NewServer(ds datastore.Datastore, ps pstore.Peerstore, local peer.ID) (*Server, error) {
29
	s := &Server{local, ds, ps, nil}
30 31 32 33 34 35 36
	s.Loopback = &proxy.Loopback{
		Handler: s,
		Local:   local,
	}
	return s, nil
}

37 38 39 40
func (_ *Server) Bootstrap(ctx context.Context) error {
	return nil
}

41 42 43 44 45 46 47 48 49 50
// HandleLocalRequest implements the proxy.RequestHandler interface. This is
// where requests are received from the outside world.
func (s *Server) HandleRequest(ctx context.Context, p peer.ID, req *dhtpb.Message) *dhtpb.Message {
	_, response := s.handleMessage(ctx, p, req) // ignore response peer. it's local.
	return response
}

func (s *Server) handleMessage(
	ctx context.Context, p peer.ID, req *dhtpb.Message) (peer.ID, *dhtpb.Message) {

51
	defer log.EventBegin(ctx, "routingMessageReceived", req, p).Done()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
52

53 54 55 56
	var response = dhtpb.NewMessage(req.GetType(), req.GetKey(), req.GetClusterLevel())
	switch req.GetType() {

	case dhtpb.Message_GET_VALUE:
Jeromy's avatar
Jeromy committed
57
		rawRecord, err := getRoutingRecord(s.routingBackend, req.GetKey())
58 59 60
		if err != nil {
			return "", nil
		}
61
		response.Record = rawRecord
62 63 64
		return p, response

	case dhtpb.Message_PUT_VALUE:
Brian Tiger Chow's avatar
Brian Tiger Chow committed
65 66 67 68 69 70
		// FIXME: verify complains that the peer's ID is not present in the
		// peerstore. Mocknet problem?
		// if err := verify(s.peerstore, req.GetRecord()); err != nil {
		// 	log.Event(ctx, "validationFailed", req, p)
		// 	return "", nil
		// }
Jeromy's avatar
Jeromy committed
71
		putRoutingRecord(s.routingBackend, req.GetKey(), req.GetRecord())
72
		return p, req
73 74 75

	case dhtpb.Message_FIND_NODE:
		p := s.peerstore.PeerInfo(peer.ID(req.GetKey()))
76
		pri := []dhtpb.PeerRoutingInfo{
rht's avatar
rht committed
77
			{
78 79 80 81 82
				PeerInfo: p,
				// Connectedness: TODO
			},
		}
		response.CloserPeers = dhtpb.PeerRoutingInfosToPBPeers(pri)
83 84 85
		return p.ID, response

	case dhtpb.Message_ADD_PROVIDER:
86 87 88 89 90
		for _, provider := range req.GetProviderPeers() {
			providerID := peer.ID(provider.GetId())
			if providerID == p {
				store := []*dhtpb.Message_Peer{provider}
				storeProvidersToPeerstore(s.peerstore, p, store)
Jeromy's avatar
Jeromy committed
91
				if err := putRoutingProviders(s.routingBackend, req.GetKey(), store); err != nil {
92 93 94 95 96
					return "", nil
				}
			} else {
				log.Event(ctx, "addProviderBadRequest", p, req)
			}
97 98 99 100
		}
		return "", nil

	case dhtpb.Message_GET_PROVIDERS:
Jeromy's avatar
Jeromy committed
101
		providers, err := getRoutingProviders(s.routingBackend, req.GetKey())
102 103
		if err != nil {
			return "", nil
104
		}
105
		response.ProviderPeers = providers
106 107 108 109 110 111 112 113 114 115 116
		return p, response

	case dhtpb.Message_PING:
		return p, req
	default:
	}
	return "", nil
}

var _ proxy.RequestHandler = &Server{}
var _ proxy.Proxy = &Server{}
117

Jeromy's avatar
Jeromy committed
118
func getRoutingRecord(ds datastore.Datastore, k string) (*pb.Record, error) {
119
	dskey := dshelp.NewKeyFromBinary([]byte(k))
120 121
	val, err := ds.Get(dskey)
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
122
		return nil, err
123 124 125 126 127
	}
	recordBytes, ok := val.([]byte)
	if !ok {
		return nil, fmt.Errorf("datastore had non byte-slice value for %v", dskey)
	}
128
	var record pb.Record
129 130 131 132 133 134
	if err := proto.Unmarshal(recordBytes, &record); err != nil {
		return nil, errors.New("failed to unmarshal dht record from datastore")
	}
	return &record, nil
}

Jeromy's avatar
Jeromy committed
135
func putRoutingRecord(ds datastore.Datastore, k string, value *pb.Record) error {
136 137 138 139
	data, err := proto.Marshal(value)
	if err != nil {
		return err
	}
140
	dskey := dshelp.NewKeyFromBinary([]byte(k))
141
	// TODO namespace
142
	return ds.Put(dskey, data)
143 144
}

Jeromy's avatar
Jeromy committed
145 146
func putRoutingProviders(ds datastore.Datastore, k string, newRecords []*dhtpb.Message_Peer) error {
	log.Event(context.Background(), "putRoutingProviders")
147
	oldRecords, err := getRoutingProviders(ds, k)
148 149
	if err != nil {
		return err
150
	}
151 152 153 154 155 156 157
	mergedRecords := make(map[string]*dhtpb.Message_Peer)
	for _, provider := range oldRecords {
		mergedRecords[provider.GetId()] = provider // add original records
	}
	for _, provider := range newRecords {
		mergedRecords[provider.GetId()] = provider // overwrite old record if new exists
	}
158
	var protomsg dhtpb.Message
159 160 161 162
	protomsg.ProviderPeers = make([]*dhtpb.Message_Peer, 0, len(mergedRecords))
	for _, provider := range mergedRecords {
		protomsg.ProviderPeers = append(protomsg.ProviderPeers, provider)
	}
163 164 165 166
	data, err := proto.Marshal(&protomsg)
	if err != nil {
		return err
	}
167
	return ds.Put(providerKey(k), data)
168 169
}

Jeromy's avatar
Jeromy committed
170
func storeProvidersToPeerstore(ps pstore.Peerstore, p peer.ID, providers []*dhtpb.Message_Peer) {
171 172 173 174 175 176 177 178
	for _, provider := range providers {
		providerID := peer.ID(provider.GetId())
		if providerID != p {
			log.Errorf("provider message came from third-party %s", p)
			continue
		}
		for _, maddr := range provider.Addresses() {
			// as a router, we want to store addresses for peers who have provided
Jeromy's avatar
Jeromy committed
179
			ps.AddAddr(p, maddr, pstore.AddressTTL)
180 181 182 183
		}
	}
}

Jeromy's avatar
Jeromy committed
184 185
func getRoutingProviders(ds datastore.Datastore, k string) ([]*dhtpb.Message_Peer, error) {
	e := log.EventBegin(context.Background(), "getProviders")
186
	defer e.Done()
187
	var providers []*dhtpb.Message_Peer
188
	if v, err := ds.Get(providerKey(k)); err == nil {
189 190 191 192 193 194 195 196 197 198
		if data, ok := v.([]byte); ok {
			var msg dhtpb.Message
			if err := proto.Unmarshal(data, &msg); err != nil {
				return nil, err
			}
			providers = append(providers, msg.GetProviderPeers()...)
		}
	}
	return providers, nil
}
199

Jeromy's avatar
Jeromy committed
200 201
func providerKey(k string) datastore.Key {
	return datastore.KeyWithNamespaces([]string{"routing", "providers", k})
202
}