Skip to content

Commit

Permalink
Merge pull request #172 from plasma-group/dev-cleanup-core
Browse files Browse the repository at this point in the history
Cleaned up imports and comments
  • Loading branch information
smartcontracts committed Apr 8, 2019
2 parents 3116d56 + 2c203ce commit 01ab313
Show file tree
Hide file tree
Showing 58 changed files with 196 additions and 103 deletions.
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
"async-lock": "^1.1.4",
"axios": "^0.18.0",
"bn.js": "^4.11.8",
"body-parser": "^1.18.3",
"debug": "^4.1.1",
"eth-lib": "^0.2.8",
"ethereumjs-tx": "^1.3.7",
"ethereumjs-vm": "^2.6.0",
"ethers": "^4.0.27",
"express": "^4.16.4",
"lodash": "^4.17.11",
"memdown": "^4.0.0",
"reflect-metadata": "^0.1.13",
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/app/common/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Internal Imports */
import { DebugLogger } from '../utils'
import { Process } from './process'

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/app/common/app/process.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* External Imports */
import { EventEmitter } from 'events'
import uuid = require('uuid')

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/app/common/db/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Internal Imports */
import {
Bucket,
Batch,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/app/common/db/db.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* External Imports */
import { AbstractOpenOptions, AbstractLevelDOWN } from 'abstract-leveldown'

/* Internal Imports */
import {
DB,
K,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/app/common/db/iterator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* External Imports */
import { AbstractIterator } from 'abstract-leveldown'

/* Internal Imports */
import { Iterator, IteratorOptions, K, V, KV, DB } from '../../../interfaces'

const defaultIteratorOptions: IteratorOptions = {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/app/common/db/key/key.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// TODO: Add bcoin acknowledgements for this code!
/* Internal Imports */
import { Key, KeyType } from '../../../../interfaces'
import { types } from './types'
import { makeID, assert } from './utils'
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/app/common/db/key/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeyType } from '../../../../interfaces'
/* Internal Imports */
import {
assertType,
assertLen,
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/app/common/db/key/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* tslint:disable:no-bitwise */
import { AssertionError } from 'assert'

// TODO: Add bcoin acknowledgements for this code!

Expand Down
15 changes: 9 additions & 6 deletions packages/core/src/app/common/net/rpc/json-rpc/json-rpc-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import { JsonRpcErrorResponse } from '../../../../../interfaces'
export const JSONRPC_ERRORS = {
PARSE_ERROR: {
code: -32700,
message: 'Parse error'
message: 'Parse error',
},
INVALID_REQUEST: {
code: -32600,
message: 'Invalid request'
message: 'Invalid request',
},
METHOD_NOT_FOUND: {
code: -32601,
message: 'Method not found'
message: 'Method not found',
},
INVALID_PARAMS: {
code: -32602,
message: 'Invalid params'
message: 'Invalid params',
},
INTERNAL_ERROR: {
code: -32603,
message: 'Internal error'
message: 'Internal error',
},
}

Expand All @@ -29,7 +29,10 @@ export const JSONRPC_ERRORS = {
* @param id ID for the response.
* @returns the response object.
*/
export const buildJsonRpcError = (type: keyof typeof JSONRPC_ERRORS, id: string | number): JsonRpcErrorResponse => {
export const buildJsonRpcError = (
type: keyof typeof JSONRPC_ERRORS,
id: string | number
): JsonRpcErrorResponse => {
return {
jsonrpc: '2.0',
error: JSONRPC_ERRORS[type],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* External Imports */
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'

/* Internal Imports */
import { HttpClient } from '../../../../../interfaces'

/**
Expand All @@ -10,13 +12,18 @@ export class AxiosHttpClient implements HttpClient {
baseURL: this.baseUrl,
})

/**
* Creates the client.
* @param baseUrl Base URL to make requests to.
*/
constructor(private baseUrl: string) {}

/**
* Sends an HTTP request to a server.
* @param data
* @param data Data to send to the server.
* @returns the HTTP response.
*/
async request(data: AxiosRequestConfig): Promise<AxiosResponse> {
public async request(data: AxiosRequestConfig): Promise<AxiosResponse> {
return this.http.request(data)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
/* External Imports */
import express = require('express')
import bodyParser = require('body-parser')

/* Internal Imports */
import { HttpServer } from '../../../../../interfaces'

/**
* HTTP server that uses Express under the hood.
*/
export class ExpressHttpServer {}
export class ExpressHttpServer implements HttpServer {
protected app = express()
private listening = false

/**
* Creates the server.
* @param port Port to listen on.
* @param hostname Hostname to listen on.
*/
constructor(private port: number, private hostname: string) {
this.app.use(bodyParser.json())
this.initRoutes()
}

/**
* Initializes any app routes.
* App has no routes by default.
*/
protected initRoutes(): void {
return
}

/**
* Starts the server.
*/
public async listen(): Promise<void> {
if (this.listening) {
return
}

return new Promise<void>((resolve, reject) => {
this.app.listen(this.port, this.hostname, () => {
this.listening = true
resolve()
})
})
}
}
4 changes: 4 additions & 0 deletions packages/core/src/app/common/utils/range-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface BlockRange extends Range {
export class RangeStore<T extends BlockRange> {
public ranges: T[]

/**
* Creates the store.
* @param ranges Initial ranges to store.
*/
constructor(ranges: T[] = []) {
this.ranges = ranges
}
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/app/common/utils/type-guards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { JsonRpcResponse, JsonRpcErrorResponse, JsonRpcRequest } from '../../../interfaces'
import {
JsonRpcResponse,
JsonRpcErrorResponse,
JsonRpcRequest,
} from '../../../interfaces'

/**
* Checks if a JSON-RPC response is an error response.
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/app/core/app/config-manager.process.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Internal Imports */
import { ConfigManager } from '../../../interfaces'
import { Process } from '../../common'
import { SimpleConfigManager } from './config-manager'
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/app/core/app/config-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { stringify, jsonify } from '../../common'
/* Internal Imports */
import { ConfigManager } from '../../../interfaces'
import { stringify, jsonify } from '../../common'

/**
* Simple config manager that stores configuration values
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/app/core/app/logger-manager.process.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Internal Imports */
import { LoggerManager } from '../../../interfaces'
import { Process } from '../../common'
import { DebugLoggerManager } from './logger-manager'
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/app/core/app/logger-manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Internal Imports */
import { LoggerManager } from '../../../interfaces'
import { DebugLogger } from '../../common'

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/app/core/core.app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* External Imports */
import { AbstractLevelDOWNConstructor } from 'abstract-leveldown'

/* Internal Imports */
import {
ConfigManager,
LoggerManager,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/app/core/db/db-manager.process.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Internal Imports */
import { ConfigManager, DBManager } from '../../../interfaces'
import { Process } from '../../common'
import { CORE_CONFIG_KEYS } from '../constants'
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/app/core/db/db-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* External Imports */
import path = require('path')
import { AbstractLevelDOWNConstructor } from 'abstract-leveldown'

/* Internal Imports */
import { BaseDB } from '../../common'
import { DBManager, DB } from '../../../interfaces'

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/app/core/eth/eth-client.process.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Internal Imports */
import { ConfigManager, EthClient } from '../../../interfaces'
import { Process } from '../../common'
import { CORE_CONFIG_KEYS } from '../constants'
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/app/core/eth/eth-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* External Imports */
import * as W3 from 'web3'
const Web3 = require('web3') // tslint:disable-line

/* Internal Imports */
import { EthClient } from '../../../interfaces'

/**
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/app/core/eth/key-manager.process.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Internal Imports */
import { DBManager, DB, KeyManager } from '../../../interfaces'
import { Process } from '../../common'
import { SimpleKeyManager } from './key-manager'
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/app/core/eth/key-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* External Imports */
import { sha3 } from 'web3-utils'
import { account as accountlib } from 'eth-lib'

/* Internal Imports */
import { KeyManager, KeyValueStore, Account } from '../../../interfaces'
import { BaseKey } from '../../common'

Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/app/core/net/rpc-server.process.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Internal Imports */
import { ConfigManager, RpcServer } from '../../../interfaces'
import { Process } from '../../common'
import { CORE_CONFIG_KEYS } from '../constants'
Expand All @@ -24,7 +25,9 @@ export class SimpleJsonRpcServerProcess extends Process<RpcServer> {
await this.config.waitUntilStarted()

const port = this.config.subject.get(CORE_CONFIG_KEYS.RPC_SERVER_PORT)
const hostname = this.config.subject.get(CORE_CONFIG_KEYS.RPC_SERVER_HOSTNAME)
const hostname = this.config.subject.get(
CORE_CONFIG_KEYS.RPC_SERVER_HOSTNAME
)
this.subject = new SimpleJsonRpcServer({}, port, hostname)
await this.subject.listen()
}
Expand Down
50 changes: 22 additions & 28 deletions packages/core/src/app/core/net/rpc-server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import express = require('express')
import bodyParser = require('body-parser')

import { JsonRpcRequest, JsonRpcSuccessResponse, RpcServer } from '../../../interfaces'
import { buildJsonRpcError, isJsonRpcRequest } from '../../common'
/* Internal Imports */
import {
JsonRpcRequest,
JsonRpcSuccessResponse,
RpcServer,
} from '../../../interfaces'
import {
buildJsonRpcError,
isJsonRpcRequest,
ExpressHttpServer,
} from '../../common'

/**
* Basic JSON-RPC server.
*/
export class SimpleJsonRpcServer implements RpcServer {
private app = express()
private listening = false

export class SimpleJsonRpcServer extends ExpressHttpServer
implements RpcServer {
/**
* Creates the server
* @param methods Methods to expose to the server.
Expand All @@ -19,10 +23,16 @@ export class SimpleJsonRpcServer implements RpcServer {
*/
constructor(
private methods: Record<string, Function> = {},
private port: number,
private hostname: string
port: number,
hostname: string
) {
this.app.use(bodyParser.json())
super(port, hostname)
}

/**
* Initializes app routes.
*/
protected initRoutes(): void {
this.app.get('/', async (req, res) => {
const request: JsonRpcRequest = req.body
if (!isJsonRpcRequest(request)) {
Expand All @@ -49,22 +59,6 @@ export class SimpleJsonRpcServer implements RpcServer {
})
}

/**
* Starts the server.
*/
public async listen(): Promise<void> {
if (this.listening) {
return
}

return new Promise<void>((resolve, reject) => {
this.app.listen(this.port, this.hostname, () => {
this.listening = true
resolve()
})
})
}

/**
* Registers a method so the server can expose it.
* @param name Name of the method to expose.
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/app/pg-core/db/chain-db.process.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Internal Imports */
import { DBManager, DB, AddressResolver, ChainDB } from '../../../interfaces'
import { Process } from '../../common'
import { PGChainDB } from './chain-db'
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/app/pg-core/db/chain-db.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Internal Imports */
import { KeyValueStore, ChainDB } from '../../../interfaces'
import { BaseKey } from '../../common'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Process } from '../../common'
/* Internal Imports */
import { EthClient, ConfigManager, AddressResolver } from '../../../interfaces'
import { Process } from '../../common'
import { PG_CORE_CONFIG_KEYS } from '../constants'
import { RegistryContractWrapper } from './registry-contract-wrapper'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* External Imports */
import { compiledPlasmaChain } from '@pigi/contracts'
import { isAddress } from 'web3-utils'
import Web3 from 'web3/types'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* External Imports */
import { compiledPlasmaRegistry } from '@pigi/contracts'
import Web3 from 'web3/types'
import { Contract } from 'web3-eth-contract/types'
Expand Down

0 comments on commit 01ab313

Please sign in to comment.