Skip to content

Commit

Permalink
Merge pull request #21 from LeiCoin-official/leicoin-pos-v2
Browse files Browse the repository at this point in the history
Leicoin pos v2
  • Loading branch information
LeiCraft authored Aug 28, 2024
2 parents adf4abe + 93a4211 commit 9a9c14a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 85 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
workflow_dispatch:
inputs:
version:
description: 'Release version'
description: 'Release version Format: x.y.z-[alpha|beta|pre].n'
required: true
release_name:
description: 'Release title'
Expand Down Expand Up @@ -66,7 +66,7 @@ jobs:
uses: ncipollo/release-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ github.event.inputs.version }}
tag: v${{ github.event.inputs.version }}
name: ${{ github.event.inputs.release_name }}
body: ${{ github.event.inputs.release_notes }}
prerelease: ${{ github.event.inputs.prerelease }}
Expand Down
2 changes: 1 addition & 1 deletion scripts/compile/commands/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CompileUtils {
const packageJSON = await Bun.file(process.cwd() + "/package.json").json() as {version: string};
return packageJSON.version;
} catch (err) {
console.log("Error reading package.json: " + err.message);
console.log("Error reading package.json: " + err.stack);
process.exit(1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/compile/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Compiler {
versionInFileName: boolean
) {
if (versionInFileName) {
this.command.outfile += `-${version}`;
this.command.outfile += `-v${version}`;
}

if (platform !== "auto") {
Expand Down
6 changes: 3 additions & 3 deletions src/leicoin-net/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export class LNConnections {

private static instance: LNConnections;

static createInstance() {
static createInstance(connections: UintMap<LNSocket> = new UintMap()) {
if (!this.instance) {
this.instance = new LNConnections();
this.instance = new LNConnections(connections);
}
return this.instance;
}

private constructor(
protected readonly connections: UintMap<LNSocket> = new UintMap()
protected readonly connections: UintMap<LNSocket>
) {}

public get size() {
Expand Down
34 changes: 21 additions & 13 deletions src/leicoin-net/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type TCPSocketListener } from "bun";
import { type BasicLNSocketHandler, LNSocketHandler, type SocketData } from "./socket.js";
import { LNSocketHandlerFactory, type BasicLNSocketHandler, type SocketData } from "./socket.js";
import cli from "../cli/cli.js";
import { LNConnections } from "./connections.js";
import { type EventEmitter } from "events";
Expand All @@ -16,7 +16,7 @@ export class LeiCoinNetNode implements ModuleLike<typeof LeiCoinNetNode> {
static async init() {
this.connections = LNConnections.createInstance();
Pipelines.registerPipelines();
this.socketHandler = LNSocketHandler.createInstance(this.connections);
this.socketHandler = LNSocketHandlerFactory.create(this.connections);
}

static async start(config: {
Expand All @@ -42,11 +42,15 @@ export class LeiCoinNetNode implements ModuleLike<typeof LeiCoinNetNode> {
}

private static async startServer(host: string, port: number) {
this.server = Bun.listen<SocketData>({
hostname: host,
port: port,
socket: this.socketHandler
});
try {
this.server = Bun.listen<SocketData>({
hostname: host,
port: port,
socket: this.socketHandler
});
} catch (error: any) {
cli.leicoin_net.error(`Failed to start server on ${host}:${port}, Error: ${error.stack}`);
}
}

/** @param peers Array of strings in the format "host:port" if no port is provided, the default port is 12200 */
Expand Down Expand Up @@ -75,12 +79,16 @@ export class LeiCoinNetNode implements ModuleLike<typeof LeiCoinNetNode> {
}

private static async connectToNode(host: string, port: number) {
const connection = await Bun.connect<SocketData>({
hostname: host,
port: port,
socket: this.socketHandler
})
this.connections.add(connection);
try {
const connection = await Bun.connect<SocketData>({
hostname: host,
port: port,
socket: this.socketHandler
})
this.connections.add(connection);
} catch (error: any) {
cli.leicoin_net.error(`Failed to connect to ${host}:${port}, Error: ${error.stack}`);
}
}

static async stop() {
Expand Down
113 changes: 49 additions & 64 deletions src/leicoin-net/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,71 +67,56 @@ export interface LNSocket extends Socket<SocketData> {
write(data: Buffer, byteOffset?: number, byteLength?: number): number;
}

class BasicLNSocketHandler implements SocketHandler<SocketData> {
readonly binaryType = "buffer";

constructor(
protected readonly connections: LNConnections
) {}

async open(socket: LNSocket) {
this.connections.add(socket);
cli.leicoin_net.info(
`A Connection was established with ${socket.data.uri}`
);
}

async close(socket: LNSocket) {
this.connections.remove(socket);
cli.leicoin_net.info(`Connection to ${socket.data.uri} closed.`);
}
async end(socket: LNSocket) {
this.connections.remove(socket);
cli.leicoin_net.info(`Connection to ${socket.data.uri} ended.`);
}

async timeout(socket: LNSocket) {
cli.leicoin_net.info(`Connection to ${socket.data.uri} timed out.`);
}

async error(socket: LNSocket, error: Error) {
cli.leicoin_net.info(`Connection Error: ${error.stack}`);
}
async connectError(socket: LNSocket, error: Error) {
cli.leicoin_net.info(
`Connection Error: ${error.stack} on ${socket.data.uri}`
);
}

async data(socket: LNSocket, data: Buffer) {
Pipelines.receiveData(data);
}

async drain(socket: LNSocket) {}

async handshake(
socket: LNSocket,
success: boolean,
authorizationError: Error | null
) {}
}

export { type BasicLNSocketHandler }

export class LNSocketHandler extends BasicLNSocketHandler {

static instance: LNSocketHandler;

static createInstance(connections: LNConnections) {
if (!this.instance) {
this.instance = new LNSocketHandler(connections);
export interface BasicLNSocketHandler extends SocketHandler<SocketData> {}

export class LNSocketHandlerFactory {
static create(connections: LNConnections) {

return new class LNSocketHandler implements BasicLNSocketHandler {
readonly binaryType = "buffer";

async open(socket: LNSocket) {
connections.add(socket);
cli.leicoin_net.info(
`A Connection was established with ${socket.data.uri}`
);
}

async close(socket: LNSocket) {
connections.remove(socket);
cli.leicoin_net.info(`Connection to ${socket.data.uri} closed.`);
}
async end(socket: LNSocket) {
connections.remove(socket);
cli.leicoin_net.info(`Connection to ${socket.data.uri} ended.`);
}

async timeout(socket: LNSocket) {
cli.leicoin_net.info(`Connection to ${socket.data.uri} timed out.`);
}

async error(socket: LNSocket, error: Error) {
cli.leicoin_net.info(`Connection Error: ${error.stack}`);
}
async connectError(socket: LNSocket, error: Error) {
cli.leicoin_net.info(
`Connection Error: ${error.stack}`
);
}

async data(socket: LNSocket, data: Buffer) {
Pipelines.receiveData(data);
}

async drain(socket: LNSocket) {}

async handshake(
socket: LNSocket,
success: boolean,
authorizationError: Error | null
) {}
}
return this.instance;
}

private constructor(connections: LNConnections) {
super(connections);
}
}

};

16 changes: 15 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class Utils {
}

//process.on("SIGINT", this.gracefulShutdown);
process.on("SIGTERM", this.gracefulShutdown);
process.once("SIGTERM", this.gracefulShutdown);

process.once("uncaughtException", this.uncaughtException);
process.once("unhandledRejection", this.unhandledRejection);
}

private static runStatus: "running" | "shutdown" | "shutdown_on_error" = "running";
Expand All @@ -38,6 +41,17 @@ class Utils {
}
}

private static async uncaughtException(error: Error) {
cli.default.error(`Uncaught Exception:\n${error.stack}`);
Utils.gracefulShutdown(1);
}

private static async unhandledRejection(reason: any) {
const error = reason.stack ? reason.stack : reason;
cli.default.error(`Unhandled Rejection:\n${error}`);
Utils.gracefulShutdown(1);
}

}

Utils.init();
Expand Down

0 comments on commit 9a9c14a

Please sign in to comment.