2018-08-20 19:24:11 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
package mocks
|
2018-08-20 19:24:11 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-08 16:09:37 +01:00
|
|
|
"fmt"
|
2018-08-20 19:24:11 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-08-20 19:24:11 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
|
|
|
)
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
// Overlay is a mocked overlay implementation
|
|
|
|
type Overlay struct {
|
2018-11-29 18:39:27 +00:00
|
|
|
nodes map[storj.NodeID]*pb.Node
|
2018-08-20 19:24:11 +01:00
|
|
|
}
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
// NewOverlay returns a newly initialized mock overlal
|
|
|
|
func NewOverlay(nodes []*pb.Node) *Overlay {
|
2018-11-29 18:39:27 +00:00
|
|
|
rv := &Overlay{nodes: map[storj.NodeID]*pb.Node{}}
|
2018-08-20 19:24:11 +01:00
|
|
|
for _, node := range nodes {
|
|
|
|
rv.nodes[node.Id] = node
|
|
|
|
}
|
|
|
|
return rv
|
2018-10-08 16:09:37 +01:00
|
|
|
|
2018-08-20 19:24:11 +01:00
|
|
|
}
|
|
|
|
|
2018-11-20 15:54:22 +00:00
|
|
|
//CtxKey Used as kademlia key
|
|
|
|
type CtxKey int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ctxKeyMockOverlay CtxKey = iota
|
|
|
|
)
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
// FindStorageNodes is the mock implementation
|
|
|
|
func (mo *Overlay) FindStorageNodes(ctx context.Context, req *pb.FindStorageNodesRequest) (resp *pb.FindStorageNodesResponse, err error) {
|
2018-09-18 05:39:06 +01:00
|
|
|
nodes := make([]*pb.Node, 0, len(mo.nodes))
|
2018-08-20 19:24:11 +01:00
|
|
|
for _, node := range mo.nodes {
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
if int64(len(nodes)) < req.Opts.GetAmount() {
|
|
|
|
return nil, errs.New("not enough farmers exist")
|
|
|
|
}
|
|
|
|
nodes = nodes[:req.Opts.GetAmount()]
|
2018-09-18 05:39:06 +01:00
|
|
|
return &pb.FindStorageNodesResponse{Nodes: nodes}, nil
|
2018-08-20 19:24:11 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
// Lookup finds a single storage node based on the request
|
2018-10-08 16:09:37 +01:00
|
|
|
func (mo *Overlay) Lookup(ctx context.Context, req *pb.LookupRequest) (
|
2018-09-18 05:39:06 +01:00
|
|
|
*pb.LookupResponse, error) {
|
2018-11-29 18:39:27 +00:00
|
|
|
return &pb.LookupResponse{Node: mo.nodes[req.NodeId]}, nil
|
2018-08-20 19:24:11 +01:00
|
|
|
}
|
|
|
|
|
2018-09-11 05:52:14 +01:00
|
|
|
//BulkLookup finds multiple storage nodes based on the requests
|
2018-10-08 16:09:37 +01:00
|
|
|
func (mo *Overlay) BulkLookup(ctx context.Context, reqs *pb.LookupRequests) (
|
2018-09-18 05:39:06 +01:00
|
|
|
*pb.LookupResponses, error) {
|
|
|
|
var responses []*pb.LookupResponse
|
2018-11-24 02:46:53 +00:00
|
|
|
for _, r := range reqs.LookupRequest {
|
2018-09-27 21:41:56 +01:00
|
|
|
// NOTE (Dylan): tests did not catch missing node case, need updating
|
2018-11-29 18:39:27 +00:00
|
|
|
n := mo.nodes[r.NodeId]
|
2018-09-27 21:41:56 +01:00
|
|
|
resp := &pb.LookupResponse{Node: n}
|
2018-09-11 05:52:14 +01:00
|
|
|
responses = append(responses, resp)
|
2018-09-18 05:39:06 +01:00
|
|
|
}
|
2018-11-24 02:46:53 +00:00
|
|
|
return &pb.LookupResponses{LookupResponse: responses}, nil
|
2018-09-11 05:52:14 +01:00
|
|
|
}
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
// Config specifies static nodes for mock overlay
|
|
|
|
type Config struct {
|
2018-08-20 19:24:11 +01:00
|
|
|
Nodes string `help:"a comma-separated list of <node-id>:<ip>:<port>" default:""`
|
|
|
|
}
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
// Run runs server with mock overlay
|
2018-10-08 16:09:37 +01:00
|
|
|
func (c Config) Run(ctx context.Context, server *provider.Provider) error {
|
2018-09-18 05:39:06 +01:00
|
|
|
var nodes []*pb.Node
|
2018-08-20 19:24:11 +01:00
|
|
|
for _, nodestr := range strings.Split(c.Nodes, ",") {
|
|
|
|
parts := strings.SplitN(nodestr, ":", 2)
|
|
|
|
if len(parts) != 2 {
|
2018-10-08 16:09:37 +01:00
|
|
|
return fmt.Errorf("malformed node config: %#v", nodestr)
|
2018-08-20 19:24:11 +01:00
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
id, err := storj.NodeIDFromString(parts[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
addr := parts[1]
|
2018-09-18 05:39:06 +01:00
|
|
|
nodes = append(nodes, &pb.Node{
|
2018-08-20 19:24:11 +01:00
|
|
|
Id: id,
|
2018-09-18 05:39:06 +01:00
|
|
|
Address: &pb.NodeAddress{
|
2018-10-15 13:04:21 +01:00
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
2018-08-20 19:24:11 +01:00
|
|
|
Address: addr,
|
|
|
|
}})
|
|
|
|
}
|
2018-11-20 15:54:22 +00:00
|
|
|
srv := NewOverlay(nodes)
|
|
|
|
pb.RegisterOverlayServer(server.GRPC(), srv)
|
|
|
|
ctx = context.WithValue(ctx, ctxKeyMockOverlay, srv)
|
2018-08-20 19:24:11 +01:00
|
|
|
return server.Run(ctx)
|
|
|
|
}
|
2018-11-20 15:54:22 +00:00
|
|
|
|
|
|
|
// LoadServerFromContext gives access to the overlay server from the context, or returns nil
|
|
|
|
func LoadServerFromContext(ctx context.Context) *Overlay {
|
|
|
|
if v, ok := ctx.Value(ctxKeyMockOverlay).(*Overlay); ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|