Skip to content

Commit

Permalink
Merge branch 'feat/relay-service' into 'dev'
Browse files Browse the repository at this point in the history
feat: add relay service

Closes #93

See merge request ergo/rosen-bridge/rosenet!58
  • Loading branch information
vorujack committed Nov 12, 2024
2 parents 80421b8 + f68a8c0 commit 71d33ff
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .changeset/tender-buckets-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
47 changes: 47 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"type": "module",
"workspaces": {
"packages": [
"services/*",
"tests/*",
"packages/*"
]
Expand Down
2 changes: 1 addition & 1 deletion packages/rosenet-node/lib/createRoseNetNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import sample from './utils/sample';

import RoseNetNodeError from './errors/RoseNetNodeError';

import packageJson from '../package.json' with { type: 'json' };
import packageJson from '../package.json' assert { type: 'json' };

import { PartialRoseNetNodeConfig } from './types';

Expand Down
2 changes: 1 addition & 1 deletion packages/rosenet-relay/lib/createRoseNetRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
RESERVATION_DURATION_LIMIT,
} from './constants';

import packageJson from '../package.json' with { type: 'json' };
import packageJson from '../package.json' assert { type: 'json' };

import { RoseNetRelayConfig } from './types';

Expand Down
1 change: 1 addition & 0 deletions services/relay/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
9 changes: 9 additions & 0 deletions services/relay/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# @rosen-bridge/relay-service

## Table of contents

- [Introduction](#introduction)

## Introduction

Rosen Bridge relay service
14 changes: 14 additions & 0 deletions services/relay/config/default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
logs:
- type: 'file'
path: './logs/'
maxSize: '10m'
maxFiles: '14d'
level: 'info'
- type: 'console'
level: 'info'
listen:
host: 0.0.0.0
port: 44123
whitelist: []
pubsubTopics: []
privateKeyFilePath: './rosenet-secret.json'
38 changes: 38 additions & 0 deletions services/relay/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@rosen-bridge/relay-service",
"version": "0.1.0",
"description": "Rosen Bridge relay service",
"repository": "",
"license": "GPL-3.0",
"author": "Rosen Team",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"lint": "eslint --fix . && npm run prettify",
"prettify": "prettier --write . --ignore-path ./.gitignore",
"start": "node --watch --loader ./ts-node-esm-loader.js --loader extensionless ./src/index.ts",
"start:prod": "node --loader extensionless ./dist/index.js",
"type-check": "tsc --noEmit"
},
"dependencies": {
"@rosen-bridge/abstract-logger": "^2.0.1",
"@rosen-bridge/rosenet-relay": "^0.1.1",
"@rosen-bridge/rosenet-utils": "^0.2.0",
"@rosen-bridge/winston-logger": "^1.0.2",
"config": "^3.3.7"
},
"devDependencies": {
"@types/config": "^0.0.41",
"@types/node": "^20.11.9",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"extensionless": "^1.9.6",
"prettier": "^3.2.4",
"ts-node": "^10.7.0",
"tsconfig-paths": "^4.1.2",
"typescript": "^5.3.3"
}
}
7 changes: 7 additions & 0 deletions services/relay/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { DefaultLoggerFactory } from '@rosen-bridge/abstract-logger';
import WinstonLogger from '@rosen-bridge/winston-logger';

import { logs } from './configs';

const winston = new WinstonLogger(logs);
DefaultLoggerFactory.init(winston);
14 changes: 14 additions & 0 deletions services/relay/src/configs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { TransportOptions } from '@rosen-bridge/winston-logger';
import config from 'config';

export const host = config.get<string>('listen.host') ?? '0.0.0.0';
export const port = config.get<number>('listen.port') ?? 44123;

export const whitelist = config.get<string[]>('whitelist') ?? [];

export const pubsubTopics = config.get<string[]>('pubsubTopics') ?? [];

export const logs = config.get<TransportOptions[]>('logs') ?? [];

export const privateKeyFilePath =
config.get<string>('privateKeyFilePath') ?? './rosenet-secret.json';
17 changes: 17 additions & 0 deletions services/relay/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import './bootstrap';

import { DefaultLoggerFactory } from '@rosen-bridge/abstract-logger';

import { startRelay } from './relay';

const logger = DefaultLoggerFactory.getInstance().getLogger(import.meta.url);

try {
await startRelay();
} catch (error) {
if (error instanceof Error) {
logger.error(`An error occurred while starting relay: [${error.message}]`);
} else {
logger.error(`An unknown error occurred while starting relay`);
}
}
38 changes: 38 additions & 0 deletions services/relay/src/relay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { DefaultLoggerFactory } from '@rosen-bridge/abstract-logger';
import { createRoseNetRelay } from '@rosen-bridge/rosenet-relay';
import { readPrivateKeyFromFile } from '@rosen-bridge/rosenet-utils';

import {
host,
port,
privateKeyFilePath,
pubsubTopics,
whitelist,
} from './configs';

const logger = DefaultLoggerFactory.getInstance().getLogger(import.meta.url);
const rosenetLogger = DefaultLoggerFactory.getInstance().getLogger('rosenet');

/**
* Start relay service
*/
const startRelay = async () => {
const privateKey = await readPrivateKeyFromFile(privateKeyFilePath);
const node = await createRoseNetRelay({
privateKey,
listen: {
host,
port,
},
whitelist,
logger: rosenetLogger,
});

await node.start();

pubsubTopics.forEach((topic) => node.subscribe(topic, () => {}));

logger.info('RoseNet relay started');
};

export { startRelay };
15 changes: 15 additions & 0 deletions services/relay/ts-node-esm-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { resolve as resolveTs } from 'ts-node/esm';
import * as tsConfigPaths from 'tsconfig-paths';
import { pathToFileURL } from 'url';

const { absoluteBaseUrl, paths } = tsConfigPaths.loadConfig();
const matchPath = tsConfigPaths.createMatchPath(absoluteBaseUrl, paths);

export function resolve(specifier, ctx, defaultResolve) {
const match = matchPath(specifier);
return match
? resolveTs(pathToFileURL(`${match}`).href, ctx, defaultResolve)
: resolveTs(specifier, ctx, defaultResolve);
}

export { load, transformSource } from 'ts-node/esm';
19 changes: 19 additions & 0 deletions services/relay/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist",
"baseUrl": ".",
},
"include": ["src"],
"ts-node": {
"esm": true,
},
"references": [
{
"path": "../../packages/rosenet-relay",
},
{
"path": "../../packages/utils",
},
],
}

0 comments on commit 71d33ff

Please sign in to comment.