Skip to content

Commit

Permalink
fix: cleaning up TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Jun 7, 2022
1 parent 78b6ce5 commit 1f6a96c
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 50 deletions.
15 changes: 7 additions & 8 deletions src/client/service/nodesGetAll.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type * as grpc from '@grpc/grpc-js';
import type { Authenticate } from '../types';
import type { KeyManager } from '../../keys';
import type KeyManager from '../../keys/KeyManager';
import type { NodeId } from '../../nodes/types';
import type * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb';
import type NodeGraph from '../../nodes/NodeGraph';
import { IdInternal } from '@matrixai/id';
import { utils as nodesUtils } from '../../nodes';
import { utils as grpcUtils } from '../../grpc';
Expand All @@ -12,11 +13,11 @@ import * as nodesPB from '../../proto/js/polykey/v1/nodes/nodes_pb';
* Retrieves all nodes from all buckets in the NodeGraph.
*/
function nodesGetAll({
// NodeGraph,
nodeGraph,
keyManager,
authenticate,
}: {
// NodeGraph: NodeGraph;
nodeGraph: NodeGraph;
keyManager: KeyManager;
authenticate: Authenticate;
}) {
Expand All @@ -28,10 +29,8 @@ function nodesGetAll({
const response = new nodesPB.NodeBuckets();
const metadata = await authenticate(call.metadata);
call.sendMetadata(metadata);
// FIXME:
// const buckets = await nodeGraph.getAllBuckets();
const buckets: any = [];
for (const b of buckets) {
const buckets = nodeGraph.getBuckets();
for await (const b of buckets) {
let index;
for (const id of Object.keys(b)) {
const encodedId = nodesUtils.encodeNodeId(
Expand All @@ -48,7 +47,7 @@ function nodesGetAll({
);
}
// Need to either add node to an existing bucket, or create a new
// bucket (if doesn't exist)
// bucket (if it doesn't exist)
const bucket = response.getBucketsMap().get(index);
if (bucket) {
bucket.getNodeTableMap().set(encodedId, address);
Expand Down
7 changes: 1 addition & 6 deletions src/nodes/NodeConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class NodeConnectionManager {
this.nodeManager = nodeManager;
for (const nodeIdEncoded in this.seedNodes) {
const nodeId = nodesUtils.decodeNodeId(nodeIdEncoded)!;
await this.nodeGraph.setNode(nodeId, this.seedNodes[nodeIdEncoded]); // FIXME: also fine implicit transactions
await this.nodeGraph.setNode(nodeId, this.seedNodes[nodeIdEncoded]);
}
this.logger.info(`Started ${this.constructor.name}`);
}
Expand Down Expand Up @@ -264,7 +264,6 @@ class NodeConnectionManager {
)}`,
);
// Creating the connection and set in map
// FIXME: this is fine, just use the implicit tran. fix this when adding optional transactions
const targetAddress = await this.findNode(targetNodeId);
if (targetAddress == null) {
throw new nodesErrors.ErrorNodeGraphNodeIdNotFound();
Expand Down Expand Up @@ -411,7 +410,6 @@ class NodeConnectionManager {
return address;
}

// FIXME: getClosestNodes was moved to NodeGraph? that needs to be updated.
/**
* Attempts to locate a target node in the network (using Kademlia).
* Adds all discovered, active nodes to the current node's database (up to k
Expand All @@ -438,7 +436,6 @@ class NodeConnectionManager {
// Let foundTarget: boolean = false;
let foundAddress: NodeAddress | undefined = undefined;
// Get the closest alpha nodes to the target node (set as shortlist)
// FIXME: no tran
const shortlist = await this.nodeGraph.getClosestNodes(
targetNodeId,
this.initialClosestNodes,
Expand Down Expand Up @@ -473,7 +470,6 @@ class NodeConnectionManager {
try {
// Add the node to the database so that we can find its address in
// call to getConnectionToNode
// FIXME: no tran
await this.nodeGraph.setNode(nextNodeId, nextNodeAddress.address);
await this.getConnection(nextNodeId, timer);
} catch (e) {
Expand All @@ -496,7 +492,6 @@ class NodeConnectionManager {
continue;
}
if (nodeId.equals(targetNodeId)) {
// FIXME: no tran
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
Expand Down
2 changes: 0 additions & 2 deletions src/nodes/NodeGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,6 @@ class NodeGraph {
* current node has less than k nodes in all of its buckets, in which case it
* returns all nodes it has knowledge of)
*/
// FIXME: this is still operating on assumptions from old code.
// I can't get the gt/lt to work on the iterator.
@ready(new nodesErrors.ErrorNodeGraphNotRunning())
public async getClosestNodes(
nodeId: NodeId,
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
} from '../nodes/types';
import type { ClaimEncoded } from '../claims/types';
import type { Timer } from '../types';
import type { PromiseType } from '../utils/utils';
import type { PromiseDeconstructed } from '../utils/utils';
import type { AbortSignal } from 'node-abort-controller';
import Logger from '@matrixai/logger';
import { StartStop, ready } from '@matrixai/async-init/dist/StartStop';
Expand Down Expand Up @@ -47,8 +47,8 @@ class NodeManager {
protected refreshBucketQueue: Set<NodeBucketIndex> = new Set();
protected refreshBucketQueueRunning: boolean = false;
protected refreshBucketQueueRunner: Promise<void>;
protected refreshBucketQueuePlug_: PromiseType<void> = promise();
protected refreshBucketQueueDrained_: PromiseType<void> = promise();
protected refreshBucketQueuePlug_: PromiseDeconstructed<void> = promise();
protected refreshBucketQueueDrained_: PromiseDeconstructed<void> = promise();
protected refreshBucketQueueAbortController: AbortController;

constructor({
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/Queue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PromiseType } from '../utils';
import type { PromiseDeconstructed } from '../utils';
import Logger from '@matrixai/logger';
import { StartStop, ready } from '@matrixai/async-init/dist/StartStop';
import * as nodesErrors from './errors';
Expand All @@ -11,8 +11,8 @@ class Queue {
protected end: boolean = false;
protected queue: Array<() => Promise<void>> = [];
protected runner: Promise<void>;
protected plug_: PromiseType<void> = promise();
protected drained_: PromiseType<void> = promise();
protected plug_: PromiseDeconstructed<void> = promise();
protected drained_: PromiseDeconstructed<void> = promise();

constructor({ logger }: { logger?: Logger }) {
this.logger = logger ?? new Logger(this.constructor.name);
Expand Down
17 changes: 0 additions & 17 deletions src/nodes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ type NodeBucketMeta = {
count: number;
};

// Type NodeBucketMetaProps = NonFunctionProperties<NodeBucketMeta>;

// Just make the bucket entries also
// bucketIndex anot as a key
// but as the domain
Expand All @@ -45,20 +43,8 @@ type NodeData = {
lastUpdated: number;
};

// Type NodeBucketEntry = {
// address: NodeAddress;
// lastUpdated: Date;
// };

type SeedNodes = Record<NodeIdEncoded, NodeAddress>;

// FIXME: should have a proper name
type NodeEntry = {
id: NodeId;
address: NodeAddress;
distance: BigInt;
};

/**
* A claim made on a node. That is, can be either:
* - a claim from a node -> node
Expand Down Expand Up @@ -106,9 +92,6 @@ export type {
NodeBucketMeta,
NodeBucket,
NodeData,
NodeEntry,
// NodeBucketEntry,

NodeGraphOp,
NodeGraphSpace,
};
15 changes: 7 additions & 8 deletions src/nodes/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import type {
} from './types';
import { IdInternal } from '@matrixai/id';
import lexi from 'lexicographic-integer';
import { utils as dbUtils } from '@matrixai/db';
import { bytes2BigInt } from '../utils';
import * as keysUtils from '../keys/utils';

// FIXME:
const prefixBuffer = Buffer.from([33]);
// Const prefixBuffer = Buffer.from(dbUtils.prefix);
const sepBuffer = dbUtils.sep;

/**
* Encodes the NodeId as a `base32hex` string
Expand Down Expand Up @@ -94,9 +93,9 @@ function bucketKey(bucketIndex: NodeBucketIndex): string {
*/
function bucketsDbKey(bucketIndex: NodeBucketIndex, nodeId: NodeId): Buffer {
return Buffer.concat([
prefixBuffer,
sepBuffer,
Buffer.from(bucketKey(bucketIndex)),
prefixBuffer,
sepBuffer,
bucketDbKey(nodeId),
]);
}
Expand All @@ -117,9 +116,9 @@ function lastUpdatedBucketsDbKey(
nodeId: NodeId,
): Buffer {
return Buffer.concat([
prefixBuffer,
sepBuffer,
Buffer.from(bucketKey(bucketIndex)),
prefixBuffer,
sepBuffer,
lastUpdatedBucketDbKey(lastUpdated, nodeId),
]);
}
Expand Down Expand Up @@ -313,7 +312,7 @@ function generateRandomNodeIdForBucket(
}

export {
prefixBuffer,
sepBuffer,
encodeNodeId,
decodeNodeId,
bucketIndex,
Expand Down
6 changes: 3 additions & 3 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function promisify<
};
}

type PromiseType<T> = {
type PromiseDeconstructed<T> = {
p: Promise<T>;
resolveP: (value: T | PromiseLike<T>) => void;
rejectP: (reason?: any) => void;
Expand All @@ -179,7 +179,7 @@ type PromiseType<T> = {
/**
* Deconstructed promise
*/
function promise<T = void>(): PromiseType<T> {
function promise<T = void>(): PromiseDeconstructed<T> {
let resolveP, rejectP;
const p = new Promise<T>((resolve, reject) => {
resolveP = resolve;
Expand Down Expand Up @@ -310,7 +310,7 @@ function debounce<P extends any[]>(
};
}

export type { PromiseType };
export type { PromiseDeconstructed };
export {
getDefaultNodePath,
never,
Expand Down

0 comments on commit 1f6a96c

Please sign in to comment.