Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 Add rest api #11

Merged
merged 1 commit into from
Oct 21, 2021
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
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $ npm install -g polymesh-local
$ polymesh-local COMMAND
running command...
$ polymesh-local (-v|--version|version)
polymesh-local/2.1.0 linux-x64 node-v14.18.0
polymesh-local/2.1.0 darwin-arm64 node-v14.17.1
$ polymesh-local --help [COMMAND]
USAGE
$ polymesh-local COMMAND
Expand Down Expand Up @@ -151,22 +151,30 @@ USAGE
$ polymesh-local start [OPTIONS]

OPTIONS
-c, --clean Cleans state before starting
-h, --help show CLI help
-c, --clean Cleans state before starting
-h, --help show CLI help

-i, --image=image (Advanced) Specify a local docker image to use for Polymesh containers. Such an image
should be debian based and have the polymesh node binary set as its entrypoint
-i, --image=image (Advanced) Specify a local docker image to use for Polymesh containers. Such an
image should be debian based and have the polymesh node binary set as its
entrypoint

-o, --only=chain|subquery|gql [default: chain,subquery,gql] Run only some services
-o, --only=chain|subquery|gql|rest [default: chain,subquery,gql,rest] Run only some services

-s, --snapshot=snapshot Loads snapshot before starting. Current state used if not passed
-s, --snapshot=snapshot Loads snapshot before starting. Current state used if not passed

-v, --version=4.0.0 [default: 4.0.0] version of the containers to run
-v, --version=4.0.0 [default: 4.0.0] version of the containers to run

--chain=testnet-dev|ci-dev (Advanced) Specify a Polymesh runtime. ci-dev has reduced block times letting it
process transactions faster than testnet-dev
--chain=testnet-dev|ci-dev (Advanced) Specify a Polymesh runtime. ci-dev has reduced block times letting it
process transactions faster than testnet-dev

--verbose enables verbose output
--dids=dids [default: 0x0600000000000000000000000000000000000000000000000000000000000000]
Comma seperated list of dids available in the rest api. Defaults to
`0x0600000000000000000000000000000000000000000000000000000000000000`

--mnemonics=mnemonics [default: //Alice] Comma seperated list of mnemonics for dids. Defaults to
`//Alice`

--verbose enables verbose output
```

_See code: [src/commands/start.ts](https://github.com/PolymathNetwork/polymesh-local/blob/v2.1.0/src/commands/start.ts)_
Expand Down
12 changes: 11 additions & 1 deletion src/commands/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cli from 'cli-ux';
import { existsSync } from 'fs';

import { containersUp, containerTime, startContainers, stopContainers } from '../common/containers';
import { getRelayerEnvs } from '../common/rest';
import { createSnapshot, getMetadata, snapshotPath, writeMetadata } from '../common/snapshots';
import { dataDir, snapshotsDir } from '../consts';
import { noData } from '../errors';
Expand All @@ -27,6 +28,7 @@ export default class Save extends Command {
this.error(noData);
}

const restEnvs = await getRelayerEnvs();
const metadata = getMetadata();
const services = await containersUp();
if (services.length > 0) {
Expand All @@ -44,7 +46,15 @@ export default class Save extends Command {

if (services.length > 0) {
cli.action.start('Restarting services');
await startContainers(metadata.version, metadata.time, false, metadata.chain, services);
await startContainers(
metadata.version,
metadata.time,
false,
metadata.chain,
services,
restEnvs[0],
restEnvs[1]
);
metadata.startedAt = new Date().toISOString();
writeMetadata(metadata);
cli.action.stop();
Expand Down
43 changes: 36 additions & 7 deletions src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import {
startContainers,
stopContainers,
} from '../common/containers';
import { isRestUp, validateDidArgs } from '../common/rest';
import { getMetadata, loadSnapshot, Metadata, writeMetadata } from '../common/snapshots';
import { isSubqueryUp } from '../common/subquery';
import { isToolingUp } from '../common/tooling';
import { hostTime, printInfo, retry } from '../common/util';
import { dataDir } from '../consts';
import { chainRunningError } from '../errors';
import { chainRunningError, restArgsError } from '../errors';

export default class Start extends Command {
static description = 'Start all the services';
Expand Down Expand Up @@ -53,21 +54,30 @@ export default class Start extends Command {
}),
only: flags.string({
multiple: true,
default: ['chain', 'subquery', 'gql'],
default: ['chain', 'subquery', 'gql', 'rest'],
char: 'o',
description: 'Run only some services',
options: ['chain', 'subquery', 'gql'],
options: ['chain', 'subquery', 'gql', 'rest'],
}),
verbose: flags.boolean({
description: 'enables verbose output',
default: false,
}),
dids: flags.string({
description:
'Comma seperated list of dids available in the rest api. Defaults to `0x0600000000000000000000000000000000000000000000000000000000000000`',
default: '0x0600000000000000000000000000000000000000000000000000000000000000',
}),
mnemonics: flags.string({
description: 'Comma seperated list of mnemonics for dids. Defaults to `//Alice`',
default: '//Alice',
}),
};

async run(): Promise<void> {
const { flags: commandFlags } = this.parse(Start);
const { clean, snapshot, verbose, version, image, chain, only } = commandFlags;
const typedOnly = only as ('chain' | 'subquery' | 'gql')[];
const { clean, snapshot, verbose, version, image, chain, only, dids, mnemonics } = commandFlags;
const typedOnly = only as ('chain' | 'subquery' | 'gql' | 'rest')[];

if (await anyContainersUp()) {
this.error(chainRunningError);
Expand All @@ -79,6 +89,10 @@ export default class Start extends Command {
cli.action.stop();
}

if (!validateDidArgs(dids, mnemonics)) {
this.error(restArgsError);
}

if (!anyVolumes()) {
cli.action.start('No volumes detected. Initializing volumes');
createEmptyVolumes();
Expand Down Expand Up @@ -129,15 +143,30 @@ export default class Start extends Command {
return ['subquery'];
case 'gql':
return ['tooling'];
case 'rest':
return ['rest_api'];
}
return [];
});

cli.action.start('Starting the containers');
await startContainers(version, metadata.time, verbose, metadata.chain, services);
await startContainers(
version,
metadata.time,
verbose,
metadata.chain,
services,
dids,
mnemonics
);
cli.action.stop();

const allChecks = { chain: isChainUp, subquery: isSubqueryUp, gql: isToolingUp };
const allChecks = {
chain: isChainUp,
subquery: isSubqueryUp,
gql: isToolingUp,
rest: isRestUp,
};
const checks = typedOnly.map(o => allChecks[o]);

cli.action.start('Checking service liveness');
Expand Down
13 changes: 11 additions & 2 deletions src/common/containers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import compose from 'docker-compose';
import fs from 'fs';

import { Metadata } from '../common/snapshots';
import { dataDir, dateFmt, docker, localDir, postgres } from '../consts';
import { dataDir, dateFmt, docker, localDir, postgres, tooling } from '../consts';

export function prepareDockerfile(version: string, image?: string): void {
const template = fs.readFileSync(`${localDir}/mesh.Dockerfile.template`).toString();
Expand All @@ -21,7 +21,9 @@ export async function startContainers(
timestamp: string,
log: boolean,
chain: string,
services: string[]
services: string[],
dids: string,
mnemonics: string
): Promise<void> {
await compose.upMany(services, {
cwd: localDir,
Expand All @@ -38,6 +40,9 @@ export async function startContainers(
PG_DB: postgres.db,
FAKETIME: `@${timestamp}`,
CHAIN: chain,
TOOLING_API_KEY: tooling.apiKey,
RELAYER_DIDS: dids,
RELAYER_MNEMONICS: mnemonics,
},
});
}
Expand Down Expand Up @@ -66,6 +71,10 @@ export async function containersUp(): Promise<string[]> {
});
}

export function getContainerEnv(container: string, env: string): string {
return execSync(`docker exec ${container} bash -c 'echo "$${env}"'`).toString().trim();
}

export async function anyContainersUp(): Promise<boolean> {
return (await containersUp()).length > 0;
}
Expand Down
25 changes: 25 additions & 0 deletions src/common/rest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { containersUp, getContainerEnv } from '../common/containers';
import { returnsExpectedStatus } from '../common/util';
import { rest } from '../consts';

export async function isRestUp(): Promise<boolean> {
return returnsExpectedStatus(`http://${rest.url}`, 200);
}

export function validateDidArgs(dids: string, mnemonics: string): boolean {
return dids.split(',').length === mnemonics.split(',').length;
}

/**
* extracts the dids and mnemonics from the rest api so that it can be restarted during `save()
* @returns {Promise<[string, string]>}
*/
export async function getRelayerEnvs(): Promise<[string, string]> {
if ((await containersUp()).includes('rest_api')) {
const dids = getContainerEnv(rest.container, 'RELAYER_DIDS');
const mnemonics = getContainerEnv(rest.container, 'RELAYER_MNEMONICS');
return [dids, mnemonics];
} else {
return ['', ''];
}
}
3 changes: 2 additions & 1 deletion src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { execSync } from 'child_process';
import fetch from 'node-fetch';

import { getMetadata } from '../common/snapshots';
import { chain, checkSettings, dateFmt, postgres, tooling } from '../consts';
import { chain, checkSettings, dateFmt, postgres, rest, tooling } from '../consts';

async function sleep(time: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, time));
Expand Down Expand Up @@ -53,6 +53,7 @@ export function printInfo(cmd: Command): void {
cmd.log(`chain version ${metadata.version} running`);
cmd.log(`polymesh node listening at wss://${chain.url}`);
cmd.log(`postgreSQL listening at postgresql://localhost:${postgres.port}`);
cmd.log(`rest API listening at http://${rest.url}`);
cmd.log(`tooling-gql listening at http://${tooling.url}.`);
cmd.log(` note: tooling-gql requests need a header of: \`x-api-key: ${tooling.apiKey}\` set`);
}
Expand Down
5 changes: 5 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export const subquery = {
url: 'localhost:3002',
};

export const rest = {
url: 'localhost:3004',
container: 'local_rest_api_1',
};

export const tooling = {
apiKey: 'd41d8cd98f00b204e9800998ecf8427e',
url: 'localhost:3000/dev/graphql',
Expand Down
2 changes: 2 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ import { dataDir } from './consts';
export const chainRunningError =
'A running service was detected. First use the "stop" command and try this command again';
export const noData = `${dataDir} does not exist. Either load a snapshot or use "start" to resolve this`;
export const restArgsError =
'DIDs and Mnemonics must have the same amount of comma seperated values';
12 changes: 12 additions & 0 deletions src/local/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ services:
PG_PASSWORD: '${PG_PASSWORD}'
STAGE: dev

rest_api:
image: polymathnet/polymesh-rest-api:latest
restart: always
ports:
- 3004:3000
environment:
POLYMESH_NODE_URL: 'ws://alice:9944'
POLYMESH_MIDDLEWARE_URL: 'http://tooling:3000'
POLYMESH_MIDDLEWARE_API_KEY: '${TOOLING_API_KEY}'
RELAYER_DIDS: '${RELAYER_DIDS}'
RELAYER_MNEMONICS: '${RELAYER_MNEMONICS}'

volumes:
polymesh_alice:
external: true
Expand Down