Skip to content

Commit

Permalink
initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
kunal047 committed Aug 1, 2022
1 parent 3be593c commit 38c87c7
Show file tree
Hide file tree
Showing 108 changed files with 18,553 additions and 0 deletions.
128 changes: 128 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@

version: "3.1"

volumes:
redis-data:
mongo-data:

services:
meta-entry-point:
image: gasless-meta-entry-point:latest
container_name: meta-entry-point
command: /bin/sh -c "yarn run dev"
env_file:
- ./envs/.common.env
- ./envs/.meta-entry-point.env
ports:
- "0.0.0.0:3000:3000"
depends_on:
- mongo
- redis
- centrifugo
volumes:
- ../meta-entry-point:/app/
- /app/node_modules
restart: always

utils-service:
image: gasless-utils-service:latest
container_name: utils-service
command: /bin/sh -c "yarn run dev"
env_file:
- ./envs/.common.env
- ./envs/.utils-service.env
ports:
- "0.0.0.0:3001:3000"
depends_on:
- mongo
- redis
- centrifugo
volumes:
- ../utils-service:/app/
- /app/node_modules
restart: always

transaction-listener:
image: gasless-transaction-listener:latest
container_name: transaction-listener
command: /bin/sh -c "yarn run dev"
env_file:
- ./envs/.common.env
- ./envs/.transaction-listener.env
ports:
- "0.0.0.0:3001:3000"
depends_on:
- mongo
- redis
- centrifugo
volumes:
- ../gasless-transaction-listener:/app/
- /app/node_modules
restart: always

transaction-handler:
image: gasless-transaction-handler:latest
container_name: transaction-handler
command: /bin/sh -c "yarn run dev"
env_file:
- ./envs/.common.env
- ./envs/.transaction-handler.env
ports:
- "0.0.0.0:3001:3000"
depends_on:
- mongo
- redis
- centrifugo
- transaction-listener
volumes:
- ../transaction-handler:/app/
- /app/node_modules
restart: always

relayers-service:
image: gasless-relayers-service:latest
container_name: relayers-service
command: /bin/sh -c "yarn run dev"
env_file:
- ./envs/.common.env
- ./envs/.relayers-service.env
depends_on:
- mongo
- redis
- centrifugo
- transaction-handler
volumes:
- ../relayers-service:/app/
- /app/node_modules
restart: always

redis:
image: redis:6.0
container_name: redis
command: ["redis-server", "--appendonly", "yes"]
volumes:
- redis-data:/data
restart: always

centrifugo:
container_name: centrifugo
image: centrifugo/centrifugo:latest
volumes:
- ./config.json:/centrifugo/config.json
command: centrifugo -c config.json
ulimits:
nofile:
soft: 65535
hard: 65535

mongo:
image: mongo:latest
container_name: mongo
environment:
MONGO_INITDB_DATABASE: biconomy
MONGO_INITDB_ROOT_PASSWORD: biconomy
MONGO_INITDB_ROOT_USERNAME: biconomy
restart: always
volumes:
- ./setup/users.js:/docker-entrypoint-initdb.d/001_users.js:ro
- mongo-data:/data/db
3 changes: 3 additions & 0 deletions relayer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

node_modules

32 changes: 32 additions & 0 deletions relayer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
FROM node:16.3.0-alpine

# install dependencies
RUN apk update && apk add git openssl openssh

# arguments
ARG SSH_PRIV_KEY
ARG SSH_PUB_KEY
ARG PORT=3000

RUN mkdir -p /root/.ssh && chmod 0700 /root/.ssh && ssh-keyscan github.com > /root/.ssh/known_hosts
RUN echo "$SSH_PRIV_KEY" > /root/.ssh/id_rsa && \
echo "$SSH_PUB_KEY" > /root/.ssh/id_rsa.pub && \
chmod 600 /root/.ssh/id_rsa && \
chmod 600 /root/.ssh/id_rsa.pub

RUN mkdir -p /app
WORKDIR /app

COPY package.json yarn.lock ./

# install packages
RUN yarn install
COPY . /app

# generate openssl cert
RUN openssl req -nodes -new -subj "/C=/ST=/O=/CN=" -x509 -keyout server.key -out server.cert

RUN yarn run build
EXPOSE 3000

CMD ["yarn", "run", "start"]
119 changes: 119 additions & 0 deletions relayer/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { hostname } from 'os';
import { diff } from 'deep-object-diff';
import _ from 'lodash';
import { redisClient, redisPubSub } from './src/db';
import { getRelayerServiceConfiguration } from './src/utils/cache-utils';
import { init } from './src/utils/tracing';

const { tracer } = init('relayers-service');

interface LooseObject {
[key: string]: any
}

// eslint-disable-next-line import/no-mutable-exports
let config: LooseObject = {};

const getNodePathIndex = async () => {
const r = await redisClient.get('NODE_PATH_INDEX');
const nodePathIndex = JSON.parse(r || '{}');
const hostName = hostname();
const index = parseInt(hostName.split('-')[2] || '0', 10);
nodePathIndex[hostName] = index;
await redisClient.set('NODE_PATH_INDEX', JSON.stringify(nodePathIndex));
return index;
};

const envConfig: LooseObject = {
mongoUrl: process.env.MONGO_URL,
slack: {
token: process.env.SLACK_TOKEN,
},
socketService: {
connectionHttp: process.env.WEB_SOCKET_API_URL,
connectionWs: process.env.WEB_SOCKET_URL,
secret: process.env.HMAC_SECRET_KEY,
apiKey: process.env.CF_API_KEY,
},
relayerService: {
EVENT_EXPIRED_STRING: `__keyspace@*__:TxId_*_${hostname()}`,
RESUBMIT_EXPIRED_STRING: `__keyspace@*__:ResubmitTxId_*_*_${hostname()}`,
ETH_ACCOUNT_PASS: process.env.ETH_ACCOUNT_PASS,
queueUrl: process.env.RELAYER_QUEUE_URL,
queueExchange: process.env.RELAYER_QUEUE_EXCHANGE,
masterSeed: process.env.RELAYERS_MASTER_SEED,
},
};

let supportedNetworks = process.env.SUPPORTED_NETWORKS;

try {
if (supportedNetworks) {
supportedNetworks = JSON.parse(supportedNetworks);
}
} catch (error) {
throw new Error('Supported networks array not found in process env');
}

envConfig.supportedNetworks = supportedNetworks;

let eip1559SupportedNetworks = process.env.EIP1559_SUPPORTED_NETWORKS;

try {
if (eip1559SupportedNetworks) {
eip1559SupportedNetworks = JSON.parse(eip1559SupportedNetworks);
}
} catch (error) {
throw new Error('Supported networks array not found in process env');
}

envConfig.eip1559SupportedNetworks = eip1559SupportedNetworks;

// parameter that can change in config
// TODO: relayerFundingAmount, relayerMinimumBalanceThreshold,
// txnHashKeyExpiryTimePerNetwork, autoCreatedRelayersLengthPerNetwork, provider

export const configChangeListeners: any = {
relayerManagerService: [],
};

const parseConfiguration = (configuration: string) => {
try {
const o = JSON.parse(configuration);
if (o && typeof o === 'object') {
return o;
}
} catch (error) {
console.log('invalid json');
}
return false;
};

const setupConfig = async () => {
const staticConfig = await redisClient.get(getRelayerServiceConfiguration()) || '';
envConfig.relayerService.nodePathIndex = await getNodePathIndex();
console.log(`hostname of the server is ${hostname()} using node path index of ${envConfig.relayerService.nodePathIndex}`);

config = _.merge(envConfig, JSON.parse(staticConfig));
await redisPubSub.subscribe('configuration_relayer_service', (message, channel) => {
const c = parseConfiguration(message);
if (c) {
console.log(channel, message);
const diffConfig = diff(config, JSON.parse(message));
for (const confKey of Object.keys(diffConfig)) {
if (configChangeListeners[confKey]) {
for (const listener of configChangeListeners[confKey]) {
listener(JSON.parse(message));
}
}
}
}
});
return 'done';
};

export {
setupConfig,
config,
tracer,
};
5 changes: 5 additions & 0 deletions relayer/log-config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { logger } from './transports/server-logs';

export {
logger,
};
59 changes: 59 additions & 0 deletions relayer/log-config/transports/server-logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable no-param-reassign */
import winston from 'winston';
import { context, Span, trace } from '@opentelemetry/api';
import { hostname } from 'os';

const consoleTransport = new winston.transports.Console({
level: process.env.LOG_LEVEL || 'debug',
});

const transports = [
consoleTransport,
];

const serverTransport = (path: string) => winston.createLogger({
// silent: false,
format: process.env.NODE_ENV !== 'development'
? winston.format.combine(
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss SSS',
}),
winston.format((info: any) => {
const span: Span | undefined = trace.getSpan(context.active());
if (span) {
const { traceId } = span.spanContext();
info.traceId = traceId;
}
info.path = path;
info.hostname = hostname();
return info;
})(),
winston.format.json(),
)
: winston.format.combine(
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss SSS',
}),
winston.format.printf((_info: any) => {
const info = _info;
const span: Span | undefined = trace.getSpan(context.active());
let spanData = '';
if (span) {
const { traceId } = span.spanContext();
spanData = ` [tid-${traceId}] `;
}
if (info.message.constructor === Object) {
info.message = JSON.stringify(info.message, null, 4);
}
return `${info.timestamp}${spanData} [${info.level}] ${path} - ${info.message}`;
}),
),
transports,
});

const logger = (module: { filename: string; }) => {
const path = module.filename.split('/').slice(-4).join('/');
return serverTransport(path);
};

export { logger };
Loading

0 comments on commit 38c87c7

Please sign in to comment.