-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(decentralization): work in progress => requesting seeds after co…
…nnection
- Loading branch information
Showing
8 changed files
with
337 additions
and
39 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |