Skip to content

Commit

Permalink
fix: general fixes
Browse files Browse the repository at this point in the history
- added getBuckets test for distance and lastUpdated order
- resetting buckets work
- changing utility names
  • Loading branch information
CMCDragonkai authored and tegefaulkes committed Jun 1, 2022
1 parent 9987a1f commit 9836ed0
Show file tree
Hide file tree
Showing 45 changed files with 3,056 additions and 1,105 deletions.
8 changes: 6 additions & 2 deletions src/bin/nodes/CommandGetAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ class CommandGetAll extends CommandPolykey {
let output: any = {};
for (const [bucketIndex, bucket] of result.getBucketsMap().entries()) {
output[bucketIndex] = {};
for (const [encodedId, address] of bucket.getNodeTableMap().entries()) {
for (const [encodedId, address] of bucket
.getNodeTableMap()
.entries()) {
output[bucketIndex][encodedId] = {};
output[bucketIndex][encodedId].host = address.getHost();
output[bucketIndex][encodedId].port = address.getPort();
}
}
if (options.format === 'human') output = [result.getBucketsMap().getEntryList()];
if (options.format === 'human') {
output = [result.getBucketsMap().getEntryList()];
}
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
Expand Down
18 changes: 11 additions & 7 deletions src/client/service/nodesGetAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import type { Authenticate } from '../types';
import type { NodeGraph } from '../../nodes';
import type { KeyManager } from '../../keys';
import type { NodeId } from '../../nodes/types';
import type * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb';
import { IdInternal } from '@matrixai/id';
import { utils as nodesUtils } from '../../nodes';
import { utils as grpcUtils } from '../../grpc';
import * as nodesPB from '../../proto/js/polykey/v1/nodes/nodes_pb';
import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb';

/**
* Retrieves all nodes from all buckets in the NodeGraph.
Expand All @@ -29,24 +29,28 @@ function nodesGetAll({
const response = new nodesPB.NodeBuckets();
const metadata = await authenticate(call.metadata);
call.sendMetadata(metadata);
const buckets = await nodeGraph.getAllBuckets();
// FIXME:
// const buckets = await nodeGraph.getAllBuckets();
const buckets: any = [];
for (const b of buckets) {
let index;
for (const id of Object.keys(b)) {
const encodedId = nodesUtils.encodeNodeId(IdInternal.fromString<NodeId>(id));
const encodedId = nodesUtils.encodeNodeId(
IdInternal.fromString<NodeId>(id),
);
const address = new nodesPB.Address()
.setHost(b[id].address.host)
.setPort(b[id].address.port);
// For every node in every bucket, add it to our message
if (!index) {
index = nodesUtils.calculateBucketIndex(
index = nodesUtils.bucketIndex(
keyManager.getNodeId(),
IdInternal.fromString<NodeId>(id)
IdInternal.fromString<NodeId>(id),
);
}
// Need to either add node to an existing bucket, or create a new
// Need to either add node to an existing bucket, or create a new
// bucket (if doesn't exist)
let bucket = response.getBucketsMap().get(index);
const bucket = response.getBucketsMap().get(index);
if (bucket) {
bucket.getNodeTableMap().set(encodedId, address);
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/network/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ function isHostname(hostname: any): hostname is Hostname {

/**
* Ports must be numbers between 0 and 65535 inclusive
* If connect is true, then port must be a number between 1 and 65535 inclusive
*/
function isPort(port: any): port is Port {
function isPort(port: any, connect: boolean = false): port is Port {
if (typeof port !== 'number') return false;
if (port < 0 || port > 65535) return false;
if (connect && port === 0) return false;
return true;
}

Expand Down
50 changes: 29 additions & 21 deletions src/nodes/NodeConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
NodeId,
NodeIdString,
SeedNodes,
NodeEntry,
} from './types';
import type { DBTransaction } from '@matrixai/db';
import { withF } from '@matrixai/resources';
Expand Down Expand Up @@ -367,7 +368,7 @@ class NodeConnectionManager {
public async findNode(targetNodeId: NodeId): Promise<NodeAddress> {
// First check if we already have an existing ID -> address record

let address = await this.nodeGraph.getNode(targetNodeId);
let address = (await this.nodeGraph.getNode(targetNodeId))?.address;
// Otherwise, attempt to locate it by contacting network
if (address == null) {
address = await this.getClosestGlobalNodes(targetNodeId);
Expand Down Expand Up @@ -461,7 +462,7 @@ class NodeConnectionManager {
// getClosestGlobalNodes()?
const contacted: { [nodeId: string]: boolean } = {};
// Iterate until we've found and contacted k nodes
while (Object.keys(contacted).length <= this.nodeGraph.maxNodesPerBucket) {
while (Object.keys(contacted).length <= this.nodeGraph.nodeBucketLimit) {
// While (!foundTarget) {
// Remove the node from the front of the array
const nextNode = shortlist.shift();
Expand Down Expand Up @@ -492,27 +493,31 @@ class NodeConnectionManager {
);
// Check to see if any of these are the target node. At the same time, add
// them to the shortlist
for (const nodeData of foundClosest) {
for (const [nodeId, nodeData] of foundClosest) {
// Ignore any nodes that have been contacted
if (contacted[nodeData.id]) {
if (contacted[nodeId]) {
continue;
}
if (nodeData.id.equals(targetNodeId)) {
await this.nodeGraph.setNode(nodeData.id, nodeData.address);
if (nodeId.equals(targetNodeId)) {
await this.nodeGraph.setNode(nodeId, nodeData.address);
foundAddress = nodeData.address;
// We have found the target node, so we can stop trying to look for it
// in the shortlist
break;
}
shortlist.push(nodeData);
shortlist.push([nodeId, nodeData]);
}
// To make the number of jumps relatively short, should connect to the nodes
// closest to the target first, and ask if they know of any closer nodes
// than we can simply unshift the first (closest) element from the shortlist
shortlist.sort(function (a: NodeData, b: NodeData) {
if (a.distance > b.distance) {
const distance = (nodeId: NodeId) =>
nodesUtils.nodeDistance(targetNodeId, nodeId);
shortlist.sort(function ([nodeIdA], [nodeIdB]) {
const distanceA = distance(nodeIdA);
const distanceB = distance(nodeIdB);
if (distanceA > distanceB) {
return 1;
} else if (a.distance < b.distance) {
} else if (distanceA < distanceB) {
return -1;
} else {
return 0;
Expand All @@ -533,28 +538,30 @@ class NodeConnectionManager {
public async getRemoteNodeClosestNodes(
nodeId: NodeId,
targetNodeId: NodeId,
): Promise<Array<NodeData>> {
): Promise<Array<[NodeId, NodeData]>> {
// Construct the message
const nodeIdMessage = new nodesPB.Node();
nodeIdMessage.setNodeId(nodesUtils.encodeNodeId(targetNodeId));
// Send through client
return this.withConnF(nodeId, async (connection) => {
const client = await connection.getClient();
const response = await client.nodesClosestLocalNodesGet(nodeIdMessage);
const nodes: Array<NodeData> = [];
const nodes: Array<[NodeId, NodeData]> = [];
// Loop over each map element (from the returned response) and populate nodes
response.getNodeTableMap().forEach((address, nodeIdString: string) => {
const nodeId = nodesUtils.decodeNodeId(nodeIdString);
// If the nodeId is not valid we don't add it to the list of nodes
if (nodeId != null) {
nodes.push({
id: nodeId,
address: {
host: address.getHost() as Host | Hostname,
port: address.getPort() as Port,
nodes.push([
nodeId,
{
address: {
host: address.getHost() as Host | Hostname,
port: address.getPort() as Port,
},
lastUpdated: 0, // FIXME?
},
distance: nodesUtils.calculateDistance(targetNodeId, nodeId),
});
]);
}
});
return nodes;
Expand Down Expand Up @@ -588,8 +595,9 @@ class NodeConnectionManager {
seedNodeId,
this.keyManager.getNodeId(),
);
for (const n of nodes) {
await this.nodeGraph.setNode(n.id, n.address);
for (const [nodeId, nodeData] of nodes) {
// FIXME: this should be the `nodeManager.setNode`
await this.nodeGraph.setNode(nodeId, nodeData.address);
}
}
}
Expand Down
Loading

0 comments on commit 9836ed0

Please sign in to comment.