Skip to content

Commit

Permalink
sdk/node: instantiate routing table for nodes, package away dns resol…
Browse files Browse the repository at this point in the history
…ve into util methods, have routing table only require specifying a public key it should position entries relative to
  • Loading branch information
lithdew committed Jun 17, 2020
1 parent c6118b7 commit f0d5abd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/jpillora/backoff v1.0.0
github.com/julienschmidt/httprouter v1.3.0
github.com/lithdew/bytesutil v0.0.0-20200409052507-d98389230a59
github.com/lithdew/kademlia v0.0.0-20200617090246-b0f19e4e7756
github.com/lithdew/kademlia v0.0.0-20200617103555-3b0f902b24c0
github.com/lithdew/monte v0.0.0-20200613174427-105bad77ff52
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ github.com/lithdew/kademlia v0.0.0-20200617081927-d292507b30d7 h1:CZd84KMw/wT0L2
github.com/lithdew/kademlia v0.0.0-20200617081927-d292507b30d7/go.mod h1:8kjcvkw5riLRd20sStElDh5j8GAKJtqy2iP/+5X25Po=
github.com/lithdew/kademlia v0.0.0-20200617090246-b0f19e4e7756 h1:7I+iFbO9cavDy/O9N9b//iM+SmPwOO6UpMYE3ycgeBw=
github.com/lithdew/kademlia v0.0.0-20200617090246-b0f19e4e7756/go.mod h1:8kjcvkw5riLRd20sStElDh5j8GAKJtqy2iP/+5X25Po=
github.com/lithdew/kademlia v0.0.0-20200617103555-3b0f902b24c0 h1:2Ar4IFLONvqQDX84ff8g4UQOXiuuN8PslVARM2KSE6g=
github.com/lithdew/kademlia v0.0.0-20200617103555-3b0f902b24c0/go.mod h1:8kjcvkw5riLRd20sStElDh5j8GAKJtqy2iP/+5X25Po=
github.com/lithdew/monte v0.0.0-20200613174427-105bad77ff52 h1:zYygDC3AydtQfkizBiglK6DEyRoZoTlZ/InYLaFUPFk=
github.com/lithdew/monte v0.0.0-20200613174427-105bad77ff52/go.mod h1:qisbxmkjwkE+XOLjCiJe+z1U4f95XxF5Hfrk21Rb1yc=
github.com/lithdew/reliable v0.0.0-20200506103725-7df64908b057 h1:CBhKVPym/7ZzY7ascOG93XSTlJuqrmIU/Hd0UDHC1TA=
Expand Down
4 changes: 2 additions & 2 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ func (n *Node) Start(addrs ...string) error {
Port: bindPort,
}

n.table = kademlia.NewTable(*n.id)
n.table = kademlia.NewTable(n.id.Pub)
} else {
n.table = kademlia.NewTable(kademlia.ZeroID)
n.table = kademlia.NewTable(kademlia.ZeroPublicKey)
}

n.providers = NewProviders()
Expand Down
28 changes: 16 additions & 12 deletions nodejs/src/flatend.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import "core-js";

import {DataPacket, HandshakePacket, ID, Opcode, ServiceRequestPacket, ServiceResponsePacket} from "./packet";
import {DataPacket, HandshakePacket, ID, Opcode, ServiceRequestPacket, ServiceResponsePacket, Table} from "./packet";
import nacl from "tweetnacl";
import assert from "assert";
import {MonteSocket} from "./monte";
import * as dns from "dns";
import * as net from "net";
import * as ip from "ip";
import {promisify} from "util";
import {Duplex, finished} from "stream";

Expand All @@ -20,8 +18,6 @@ const identityOpts = (opts: any): opts is NodeIdentityOpts => opts && ("keys" in

type NodeOpts = NodeIdentityOpts;

const lookup = async (hostname: string) => net.isIP(hostname) ? hostname : (await promisify(dns.lookup)(hostname)).address;

export class Context extends Duplex {
public headers: { [key: string]: string }

Expand Down Expand Up @@ -156,11 +152,23 @@ class Client {

type Handler = (ctx: Context) => void;

const resolve = async (addr: string): Promise<[string, number]> => {
const [host, port] = addr.trim().split(":");

const resolvedHost = (await promisify(dns.lookup)(host)).address;

const resolvedPort = parseInt(port);
assert(resolvedPort >= 0 && resolvedPort < 65536);

return [resolvedHost, resolvedPort];
}

export class Node {
#services: Map<string, WeakSet<Client>> = new Map<string, WeakSet<Client>>();
#providers: WeakMap<MonteSocket, Client> = new WeakMap<MonteSocket, Client>();
#clients: Map<string, Client> = new Map<string, Client>();
#handlers: Map<string, Handler> = new Map<string, Handler>();
#table: Table;

readonly #id: ID | null = null;
readonly #keys: nacl.SignKeyPair | null = null;
Expand All @@ -170,6 +178,8 @@ export class Node {
this.#id = opts.id;
this.#keys = opts.keys;
}

this.#table = new Table(this.#id?.publicKey ?? Buffer.alloc(nacl.sign.publicKeyLength));
}

get anonymous(): boolean {
Expand All @@ -186,13 +196,7 @@ export class Node {
}

public async dial(addr: string) {
const fields = addr.trim().split(":");
assert(fields.length === 2);

const host = await lookup(fields[0]);

const port = parseInt(fields[1]);
assert(port > 0 && port < 65536)
const [host, port] = await resolve(addr);

let client = this.#clients.get(addr);
if (!client) {
Expand Down
14 changes: 7 additions & 7 deletions nodejs/src/packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,22 +373,22 @@ const xor = (a: Uint8Array, b: Uint8Array): Uint8Array => {
}

export class Table {
id: ID;
pub: Uint8Array;
cap: number = 16;
length: number = 0;
buckets: Array<Array<ID>> = [...Array(nacl.sign.publicKeyLength * 8)].map(() => []);

public constructor(id: ID) {
this.id = id;
public constructor(pub: Uint8Array = Buffer.alloc(nacl.sign.publicKeyLength)) {
this.pub = pub;
}

private bucketIndex(pub: Uint8Array): number {
if (Buffer.compare(pub, this.id.publicKey) === 0) return 0;
return leadingZeros(xor(pub, this.id.publicKey));
if (Buffer.compare(pub, this.pub) === 0) return 0;
return leadingZeros(xor(pub, this.pub));
}

public update(id: ID): UpdateResult {
if (Buffer.compare(id.publicKey, this.id.publicKey) === 0) return UpdateResult.Fail;
if (Buffer.compare(id.publicKey, this.pub) === 0) return UpdateResult.Fail;

const bucket = this.buckets[this.bucketIndex(id.publicKey)];

Expand Down Expand Up @@ -439,7 +439,7 @@ export class Table {

fill(m);
while ((l >= 0 && fill(l)) || (r < this.buckets.length && fill(r))) {
[l, r] = [l-1, r+1];
[l, r] = [l - 1, r + 1];
}

return closest;
Expand Down

0 comments on commit f0d5abd

Please sign in to comment.