2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-29 18:39:27 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package pb
|
|
|
|
|
2019-01-02 18:47:34 +00:00
|
|
|
import (
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
)
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2018-12-12 21:24:08 +00:00
|
|
|
// NodeIDsToLookupRequests converts NodeIDs to LookupRequests
|
2018-11-29 18:39:27 +00:00
|
|
|
func NodeIDsToLookupRequests(nodeIDs storj.NodeIDList) *LookupRequests {
|
|
|
|
var rq []*LookupRequest
|
|
|
|
for _, v := range nodeIDs {
|
|
|
|
r := &LookupRequest{NodeId: v}
|
|
|
|
rq = append(rq, r)
|
|
|
|
}
|
|
|
|
return &LookupRequests{LookupRequest: rq}
|
|
|
|
}
|
|
|
|
|
2018-12-12 21:24:08 +00:00
|
|
|
// LookupResponsesToNodes converts LookupResponses to Nodes
|
2018-11-29 18:39:27 +00:00
|
|
|
func LookupResponsesToNodes(responses *LookupResponses) []*Node {
|
|
|
|
var nodes []*Node
|
|
|
|
for _, v := range responses.LookupResponse {
|
2018-12-12 21:24:08 +00:00
|
|
|
n := v.GetNode()
|
2018-11-29 18:39:27 +00:00
|
|
|
nodes = append(nodes, n)
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
// NodesToIDs extracts Node-s into a list of ids
|
|
|
|
func NodesToIDs(nodes []*Node) storj.NodeIDList {
|
|
|
|
ids := make(storj.NodeIDList, len(nodes))
|
|
|
|
for i, node := range nodes {
|
|
|
|
if node != nil {
|
|
|
|
ids[i] = node.Id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
// CopyNode returns a deep copy of a node
|
|
|
|
// It would be better to use `proto.Clone` but it is curently incompatible
|
|
|
|
// with gogo's customtype extension.
|
|
|
|
// (see https://github.com/gogo/protobuf/issues/147)
|
|
|
|
func CopyNode(src *Node) (dst *Node) {
|
2019-01-02 18:47:34 +00:00
|
|
|
src.Type.DPanicOnInvalid("copy node")
|
2018-11-29 18:39:27 +00:00
|
|
|
node := Node{Id: storj.NodeID{}}
|
|
|
|
copy(node.Id[:], src.Id[:])
|
|
|
|
if src.Address != nil {
|
|
|
|
node.Address = &NodeAddress{
|
|
|
|
Transport: src.Address.Transport,
|
|
|
|
Address: src.Address.Address,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if src.Metadata != nil {
|
|
|
|
node.Metadata = &NodeMetadata{
|
|
|
|
Email: src.Metadata.Email,
|
|
|
|
Wallet: src.Metadata.Wallet,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if src.Restrictions != nil {
|
|
|
|
node.Restrictions = &NodeRestrictions{
|
|
|
|
FreeBandwidth: src.Restrictions.FreeBandwidth,
|
|
|
|
FreeDisk: src.Restrictions.FreeDisk,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Type = src.Type
|
|
|
|
|
|
|
|
return &node
|
|
|
|
}
|
2019-01-02 18:47:34 +00:00
|
|
|
|
|
|
|
// DPanicOnInvalid panics if NodeType is invalid if zap is in development mode,
|
|
|
|
// otherwise it logs.
|
|
|
|
func (nt NodeType) DPanicOnInvalid(from string) {
|
|
|
|
// TODO: Remove all references
|
|
|
|
if nt == NodeType_INVALID {
|
|
|
|
zap.L().DPanic("INVALID NODE TYPE: " + from)
|
|
|
|
}
|
|
|
|
}
|