Skip to content

Commit

Permalink
feat(decentralization): work in progress => requesting seeds after co…
Browse files Browse the repository at this point in the history
…nnection
  • Loading branch information
tshiuan committed Nov 9, 2024
1 parent f9c222e commit 23c69c2
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 39 deletions.
242 changes: 203 additions & 39 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"mysql": "^2.18.1",
"request": "^2.88.2",
"socket.io": "^4.5.4",
"socket.io-client": "^4.8.1",
"typeorm": "^0.3.11"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ export const Config = {
cleanEveryXHours: 24,
cleanExpiredForXDays: 30,
},
sync: {
seed: ["http://localhost:5005"],
},
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import https from "https";
import { Config } from "./config";
import { AppDataSource } from "./database/data-source";
import { DatabaseModule } from "./database/typeorm";
import { ServerSyncLogic } from "./servers-sync/server-sync.logic";
import { SignatureRequestApi } from "./signature-request/signature-request.api";
import { SignatureRequestLogic } from "./signature-request/signature-request.logic";
import { SocketIoLogic } from "./socket-io.logic";
Expand Down Expand Up @@ -38,6 +39,7 @@ const startServer = (app: Express) => {
} else {
const httpServer = createServer(app);
SocketIoLogic.setup(httpServer);
ServerSyncLogic.initSync(httpServer);

httpServer.listen(Config.port.server, () => {
Logger.technical(
Expand Down
6 changes: 6 additions & 0 deletions src/servers-sync/server-sync-messages.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum ServerSyncMessage {
NEW_ENTRY = "newEntry",
NEW_SEED = "newSeed",
FULL_SYNC = "fullSync",
REQUEST_SEEDS = "requestSeeds",
}
35 changes: 35 additions & 0 deletions src/servers-sync/server-sync.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { SignatureRequest } from "../signature-request/signature-request.entity";
import { Signer } from "../signature-request/signer/signer.entity";

export interface NewSeedMessage {
ipAddress: string;
}

export enum NewEntryType {
TRANSACTION_BROADCASTED = "transactionBroadcasted",
NEW_SIGNATURE_REQUEST = "newSignatureRequest",
SIGNATURE_RECEIVED = "signatureReceived",
}

export interface NewEntryMessage {
type: NewEntryType;
}

export interface NewSignatureRequestEntry {
type: NewEntryType.NEW_SIGNATURE_REQUEST;
signatureRequest: SignatureRequest;
}

export interface SignatureReceivedEntry {
type: NewEntryType.SIGNATURE_RECEIVED;
signatureRequestId: SignatureRequest["id"];
signerId: Signer["id"];
signature: string;
}

export interface TransactionBroadcastedEntry {
type: NewEntryType.TRANSACTION_BROADCASTED;
signatureRequestId: SignatureRequest["id"];
}

export interface FullSyncMessage {}
69 changes: 69 additions & 0 deletions src/servers-sync/server-sync.logic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { DefaultEventsMap } from "@socket.io/component-emitter";
import { Server } from "socket.io";
import { connect, Socket } from "socket.io-client";
import { Config } from "../config";
import { ArrayUtils } from "../utils/array.utils";
import { ServerSyncMessage } from "./server-sync-messages.enum";

interface OutgoingConnection {
ipAddress: string;
socket: Socket;
}

let ioServer: Server;
let ioClients: OutgoingConnection[] = [];

let seeds = Config.sync.seed;

const initSync = (httpServer: any) => {
ioServer = new Server(httpServer, {
cors: {
origin: "*",
},
});
for (const serverIpAddress of seeds) {
connectToServer(serverIpAddress);
}

ioServer.on("connection", (socket) => {
// Check if outgoing client connection exist. If not connect

console.log(socket.handshake.headers.host);

const outgoingConnection = ioClients.find(
(outgoingConnection) =>
outgoingConnection.ipAddress === socket.handshake.address
);
if (!outgoingConnection) {
connectToServer(socket.handshake.address);
}
});

ioServer.on(ServerSyncMessage.REQUEST_SEEDS, handleSeedsRequest);
};

const connectToServer = (ipAddress: string) => {
const newClient = { ipAddress: ipAddress, socket: connect(ipAddress) };
ioClients.push(newClient);
requestSeeds(newClient.socket);
};

const requestSeeds = (socket: Socket<DefaultEventsMap, DefaultEventsMap>) => {
socket.emit(ServerSyncMessage.REQUEST_SEEDS, null, (newSeeds: string[]) => {
seeds = ArrayUtils.mergeWithoutDuplicate(seeds, newSeeds);
for (const serverIpAddress of seeds) {
const outgoingConnection = ioClients.find(
(outgoingConnection) => outgoingConnection.ipAddress === serverIpAddress
);
if (!outgoingConnection) {
connectToServer(serverIpAddress);
}
}
});
};

const handleSeedsRequest = (callback: (seeds: string[]) => void) => {
callback(seeds);
};

export const ServerSyncLogic = { initSync };
18 changes: 18 additions & 0 deletions src/utils/array.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mergeWithoutDuplicate = (a: any[], b: any[], key?: string) => {
var list = [...a, ...b];
for (var i = 0; i < list.length; ++i) {
for (var j = i + 1; j < list.length; ++j) {
if (key) {
if (list[i][key] === list[j][key]) list.splice(j--, 1);
} else {
if (list[i] === list[j]) list.splice(j--, 1);
}
}
}

return list;
};

export const ArrayUtils = {
mergeWithoutDuplicate,
};

0 comments on commit 23c69c2

Please sign in to comment.