Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/libs/blockchain/routines/Sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ export async function fastSync(
peerlist = [singlePeer]
}
// Selecting the first peer
let firstPeer = peerlist[0]
console.log("[SYNC] First peer is " + firstPeer.connectionString)
let firstPeer: Peer = peerlist[0]
console.log("[SYNC] First peer is " + firstPeer.connection.string)

// Asking the first peer for the last block number
let blockNumberResponse = await demostdlib.remoteCall(
firstPeer.connectionString,
firstPeer.connection.string,
firstPeer,
"getLastBlockNumber",
"nodeCall",
Expand Down Expand Up @@ -122,7 +122,7 @@ export async function fastSync(
// TODO Test if it is more logic to just download the headers and, once a chain has been estabilished, download the blocks
// TODO And by test i mean we have to do it
let blockResponse = await demostdlib.remoteCall(
firstPeer.connectionString,
firstPeer.connection.string,
firstPeer,
"getBlockByNumber",
"nodeCall",
Expand Down Expand Up @@ -175,12 +175,12 @@ export async function fastSync(

// TODO Sync reimplementation
export async function Sync(peer: Peer): Promise<boolean> {
if (!peer.socket) {
console.log("[SYNC] Peer has no socket: " + peer.connectionString)
if (!peer.connection.socket) {
console.log("[SYNC] Peer has no socket: " + peer.connection.string)
return false
}
if(!peer.socket.connected) {
console.log("[SYNC] Peer socket is not connected: " + peer.connectionString)
if(!peer.connection.socket.connected) {
console.log("[SYNC] Peer socket is not connected: " + peer.connection.string)
return false
}
// Getting our last block number and hash
Expand Down Expand Up @@ -248,7 +248,7 @@ export async function Sync(peer: Peer): Promise<boolean> {

async function askLastBlockNumber(peer: Peer): Promise<number> {
let blockNumberResponse = await demostdlib.remoteCall(
peer.connectionString,
peer.connection.string,
peer,
"getLastBlockNumber",
"nodeCall",
Expand All @@ -266,7 +266,7 @@ async function askLastBlockNumber(peer: Peer): Promise<number> {

async function askBlockByNumber(peer: Peer, blockNumber: number): Promise<Block> {
let blockResponse = await demostdlib.remoteCall(
peer.connectionString,
peer.connection.string,
peer,
"getBlockByNumber",
"nodeCall",
Expand Down
8 changes: 4 additions & 4 deletions src/libs/communications/comlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ export default class ComLink {
console.log("[COMMUNICATIONS] Invalid message")
return [false, "Invalid message"]
}
if (peer.socket) {
if (peer.connection.socket) {
console.log(
"[COMMUNICATIONS] Sending message to peer " + peer.socket.id,
"[COMMUNICATIONS] Sending message to peer " + peer.connection.socket.id,
)
// NOTE Removing privated key from message if present
if (message.privateKey) {
Expand Down Expand Up @@ -149,7 +149,7 @@ export default class ComLink {
}
// INFO Broadcast a ComLink object to a peer (usually called by the above methods)
async broadcastToPeer(peer: Peer) {
let _socket = peer.socket
let _socket = peer.connection.socket
console.log("[COMMUNICATIONS] Broadcast message to peer")
// NOTE Here we make sure that we dont have private data in the message
if (this.chain.current.currentMessage.privateKey) {
Expand All @@ -165,7 +165,7 @@ export default class ComLink {
// INFO Support for sending to a socket directly
async broadcastToSocketPeer(socket: Socket) {
let compatible_peer = new Peer()
compatible_peer.socket = socket
compatible_peer.connection.socket = socket
await this.broadcastToPeer(compatible_peer)
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/consensus/mechanisms/BFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export default class QBFT {
let peer = peerlist[i]

const response = await new Promise(resolve => {
peer.socket.emit(
peer.connection.socket.emit(
"voteRequest",
{
parameter: parameter,
Expand Down
68 changes: 54 additions & 14 deletions src/libs/network/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import terminalkit from "terminal-kit"
import Peer from "../peer/Peer"
import ClientListeners from "./clientListeners"
import CommonListeners from "./commonListeners"

import log from "src/utilities/logger"
const term = terminalkit.terminal

// NOTE Sleep function
Expand All @@ -26,20 +26,34 @@ async function sleep(ms) {
}

export default class Client {

static async connectToPeerObject(peer: Peer): Promise<[boolean, Peer | null]> {
// Just a wrapper around the connectToPeer function
const address = peer.connectionString.split(">")[0]
const port = parseInt(peer.connectionString.split(">")[1])
const connectedPeer = await this.connectToPeer(address, port)
if (!connectedPeer) {
static async connectToPeerObject(
peer: Peer,
): Promise<[boolean, Peer | null]> {
// We can work with a connection string and it would be better to do so
if (peer.connection.string) {
log.info("[CLIENT] Peer has a connection string: trying to connect using it")
const address = peer.connection.string.split(">")[0]
const port = parseInt(peer.connection.string.split(">")[1])
const connectedPeer: Peer | null = await this.connectToPeer(address, port)
if (!connectedPeer) {
return [false, null]
} else {
return [true, connectedPeer]
}
}
// We can work with a simple socket anyway
else if (peer.connection.socket) {
log.info("[CLIENT] Peer has a socket but no connection string: trying to use it")
return [peer.connection.socket.connected, peer] // ? If it is not connected we should try to reconnect
}
// We can't work with a peer without a socket or a connection string
else {
log.error("[CLIENT] Peer has no socket or connection string: cannot connect")
return [false, null]
} else {
return [true, connectedPeer]
}
}

// ! Refactor to accept a Peer object (made with PeerManager.extractPeerFromString)
// ? Refactor to accept a Peer object (made with PeerManager.extractPeerFromString)
static async connectToPeer(
address: string = "localhost",
port: number = 53550,
Expand All @@ -52,12 +66,18 @@ export default class Client {

const connectionSocket = io(address + ":" + port)
connectionSocket.on("connect", async () => {
term.green(
term.yellow(
"[CLIENT] Connected to peer at " + address + ":" + port + "\n",
)
log.info("[CLIENT] Connecting to peer at " + address + ":" + port)
_peerForged.identity = "placeholder" // TODO Add identity filling and verification
_peerForged.socket = connectionSocket
_peerForged.connectionString = address + ">" + port
_peerForged.connection.socket = connectionSocket
_peerForged.connection.string = address + ">" + port
term.yellow(
"[CLIENT] Connection string set to " +
_peerForged.connection.string +
"\n",
)
const commonListeners = new CommonListeners(_peerForged)
const clientListeners = new ClientListeners(_peerForged)
await commonListeners.runListeners()
Expand All @@ -69,12 +89,32 @@ export default class Client {
//Timeout and timer for the connection (yes, a blocking one)
while (timeout > 0) {
if (connected) {
log.info(
"[CLIENT] Connected to peer at " + address + ":" + port,
)
term.green(
"[CLIENT] Connected to peer at " +
address +
":" +
port +
"\n",
)
return _peerForged
} else {
timeout -= 100
await sleep(100)
}
}
log.error(
"[CLIENT] Failed to connect to peer at " + address + ":" + port,
)
term.red(
"[CLIENT] Failed to connect to peer at " +
address +
":" +
port +
"\n",
)
return null
}

Expand Down
7 changes: 4 additions & 3 deletions src/libs/network/clientListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ KyneSys Labs: https://www.kynesys.xyz/
import terminalkit from "terminal-kit"

import { PeerManager } from "../peer"
import { Peer } from "../peer"

const term = terminalkit.terminal

export default class ClientListeners {
private peer: any
private peer: Peer

constructor(peer: any) {
constructor(peer: Peer) {
this.peer = peer
}

Expand All @@ -28,7 +29,7 @@ export default class ClientListeners {
}

private connectListener = async () => {
this.peer.socket.on("connect", async connected_socket => {
this.peer.connection.socket.on("connect", async () => {
term.green("[CLIENT] Connected to peer\n")
PeerManager.getInstance().addPeer(this.peer)
})
Expand Down
23 changes: 12 additions & 11 deletions src/libs/network/commonListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import ResponseRegistry from "../communications/responseRegistry"
import { cryptography } from "../crypto"
import getRemoteIP from "../network/routines/getRemoteIP"
import { PeerManager } from "../peer"
import { Peer } from "src/libs/peer"

const term = terminalkit.terminal

export default class CommonListeners {
private peer: any
private peer: Peer

constructor(peer: any) {
constructor(peer: Peer) {
this.peer = peer
}

Expand All @@ -37,7 +38,7 @@ export default class CommonListeners {
}

private disconnectListener = async () => {
this.peer.socket.on("disconnect", async () => {
this.peer.connection.socket.on("disconnect", async () => {
// Removing the peer from the list if it was in
term.yellow("[COMMON] Peer disconnected")
PeerManager.getInstance().removePeer(this.peer)
Expand All @@ -48,7 +49,7 @@ export default class CommonListeners {
// ? REVIEW Send the remote ip too
let remote_ip = await getRemoteIP()
let message = "Please authenticate me: " + remote_ip
this.peer.socket.on("auth_ask", async (data: { message: string }) => {
this.peer.connection.socket.on("auth_ask", async (data: { message: string }) => {
//console.log(data)
// REVIEW Signing data.message with the private key
let _signature = cryptography.sign(
Expand All @@ -61,13 +62,13 @@ export default class CommonListeners {
_signature,
sharedState.getInstance().identity.ed25519.publicKey,
]
this.peer.socket.emit("auth_reply", _sendBack)
this.peer.connection.socket.emit("auth_reply", _sendBack)
})
}

// INFO For non sensitive data
private publicListener = async () => {
this.peer.socket.on("public", request => async () => {
this.peer.connection.socket.on("public", request => async () => {
console.log("[PEER] Received")
let response = {
muid: request.muid,
Expand All @@ -84,12 +85,12 @@ export default class CommonListeners {
}
// Once the response should be processed, we need to send it back
// TODO Would be better to have requests with is_reply set to true or false instead of two listeners
this.peer.socket.emit("public_reply", response)
this.peer.connection.socket.emit("public_reply", response)
})
}

private comlinkReplyListener = async () => {
this.peer.socket.on("comlink_reply", async request => {
this.peer.connection.socket.on("comlink_reply", async request => {
// request is a ComLink object with the same structure as the comlink listener below
console.log("[PEER] Received reply to " + request.muid)
//console.log(JSON.stringify(request, null, 2))
Expand All @@ -109,7 +110,7 @@ export default class CommonListeners {
// Parsing the comlink
let parsed_comlink = await ComLinkUtils.parseComlink(
request,
this.peer.socket,
this.peer.connection.socket,
)
if (!parsed_comlink) {
return
Expand All @@ -126,14 +127,14 @@ export default class CommonListeners {
ResponseRegistry.getInstance().registerResponse(
request.chain.current.currentMessage,
request.muid,
this.peer.socket,
this.peer.connection.socket,
connection_string,
)
})
}

private errorListener = async () => {
this.peer.socket.on("error", async request => {
this.peer.connection.socket.on("error", async request => {
console.log("[PEER] Received error:")
//console.log(request)
})
Expand Down
2 changes: 1 addition & 1 deletion src/libs/network/routines/nodecalls/getPeerlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default async function getPeerlist(): Promise<Peer[]> {
let response = [] as Peer[]
// Filling response with peers without socket objects
for (let peer of socketized_response) {
peer.socket = null
peer.connection.socket = null
response.push(peer)
}
return response
Expand Down
2 changes: 1 addition & 1 deletion src/libs/network/routines/timeSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default async function getPeerTime(
id: any,
): Promise<number> {
// A peer object must have a valid socket
if (!peer.socket) {
if (!peer.connection.socket) {
return null
}

Expand Down
6 changes: 4 additions & 2 deletions src/libs/network/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ KyneSys Labs: https://www.kynesys.xyz/

*/

import { Server as ServerType } from "socket.io"
import { Server as ServerType, Socket as ServerSocket } from "socket.io"
import { Socket } from "socket.io-client"
import ServerListeners from "src/libs/network/serverListeners"
import { Peer } from "src/libs/peer"
import terminalkit from "terminal-kit"
Expand All @@ -22,7 +23,8 @@ export default class Server {
term.green("[SERVER] Peer connected\n")

const newPeer = new Peer()
newPeer.setSocket(peerSocket)
newPeer.connection.serverSocket = peerSocket // ? is this useful?
newPeer.connection.socket = peerSocket as any as Socket // ? If it works, don't touch it
//newPeer.setIdentity(identity.ed25519.publicKey)
//PeerManager.getInstance().addPeer(newPeer)
const serverListeners = new ServerListeners(newPeer)
Expand Down
Loading