Skip to content

Commit

Permalink
Add Rpc prefix to all Rpc types (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
NullSoldier authored Jul 11, 2022
1 parent f152f91 commit 44642e8
Show file tree
Hide file tree
Showing 21 changed files with 127 additions and 125 deletions.
4 changes: 2 additions & 2 deletions ironfish-cli/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import {
ConfigOptions,
ConnectionError,
createRootLogger,
ErrorUtils,
IronfishSdk,
Logger,
RpcConnectionError,
} from '@ironfish/sdk'
import { Command, Config } from '@oclif/core'
import {
Expand Down Expand Up @@ -75,7 +75,7 @@ export abstract class IronfishCommand extends Command {
} catch (error: unknown) {
if (hasUserResponseError(error)) {
this.log(error.codeMessage)
} else if (error instanceof ConnectionError) {
} else if (error instanceof RpcConnectionError) {
this.log(`Cannot connect to your node, start your node first.`)
} else {
throw error
Expand Down
4 changes: 2 additions & 2 deletions ironfish-cli/src/commands/faucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { DEFAULT_DISCORD_INVITE, RequestError } from '@ironfish/sdk'
import { DEFAULT_DISCORD_INVITE, RpcRequestError } from '@ironfish/sdk'
import { CliUx, Flags } from '@oclif/core'
import { IronfishCommand } from '../command'
import { RemoteFlags } from '../flags'
Expand Down Expand Up @@ -74,7 +74,7 @@ export class FaucetCommand extends IronfishCommand {
email,
})
} catch (error: unknown) {
if (error instanceof RequestError) {
if (error instanceof RpcRequestError) {
CliUx.ux.action.stop(error.codeMessage)
} else {
CliUx.ux.action.stop(
Expand Down
4 changes: 2 additions & 2 deletions ironfish-cli/src/commands/service/faucet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { ConnectionError, Meter, PromiseUtils, RpcSocketClient, WebApi } from '@ironfish/sdk'
import { Meter, PromiseUtils, RpcConnectionError, RpcSocketClient, WebApi } from '@ironfish/sdk'
import { Flags } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'
Expand Down Expand Up @@ -66,7 +66,7 @@ export default class Faucet extends IronfishCommand {
try {
await this.startSyncing(client, api, speed)
} catch (e) {
if (e instanceof ConnectionError) {
if (e instanceof RpcConnectionError) {
this.log('Connection error... retrying in 5 seconds')
await PromiseUtils.sleep(5000)
continue
Expand Down
4 changes: 2 additions & 2 deletions ironfish-cli/src/commands/stop.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { ConnectionError, IronfishNode } from '@ironfish/sdk'
import { IronfishNode, RpcConnectionError } from '@ironfish/sdk'
import { IronfishCommand } from '../command'
import { RemoteFlags } from '../flags'

Expand All @@ -18,7 +18,7 @@ export default class StopCommand extends IronfishCommand {
await this.parse(StopCommand)

await this.sdk.client.connect().catch((e) => {
if (e instanceof ConnectionError) {
if (e instanceof RpcConnectionError) {
this.exit(0)
}
throw e
Expand Down
8 changes: 5 additions & 3 deletions ironfish-cli/src/utils/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { isResponseUserError, RequestError } from '@ironfish/sdk'
import { isRpcResponseUserError, RpcRequestError } from '@ironfish/sdk'

export function hasUserResponseError(error: unknown): error is RequestError {
export function hasUserResponseError(error: unknown): error is RpcRequestError {
return (
error instanceof RequestError && !!error.response && isResponseUserError(error.response)
error instanceof RpcRequestError &&
!!error.response &&
isRpcResponseUserError(error.response)
)
}
6 changes: 3 additions & 3 deletions ironfish/src/rpc/adapters/ipcAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os from 'os'
import * as yup from 'yup'
import { IronfishSdk } from '../../sdk'
import { RequestError, RpcSocketClient } from '../clients'
import { RpcRequestError, RpcSocketClient } from '../clients'
import { ALL_API_NAMESPACES } from '../routes'
import { ERROR_CODES, ValidationError } from './errors'
import { RpcIpcAdapter } from './ipcAdapter'
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('IpcAdapter', () => {
expect.assertions(3)
await response.waitForEnd()
} catch (error: unknown) {
if (!(error instanceof RequestError)) {
if (!(error instanceof RpcRequestError)) {
throw error
}
expect(error.status).toBe(402)
Expand All @@ -119,7 +119,7 @@ describe('IpcAdapter', () => {
expect.assertions(3)
await response.waitForEnd()
} catch (error: unknown) {
if (!(error instanceof RequestError)) {
if (!(error instanceof RpcRequestError)) {
throw error
}
expect(error.status).toBe(400)
Expand Down
6 changes: 3 additions & 3 deletions ironfish/src/rpc/adapters/ipcAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Assert } from '../../assert'
import { createRootLogger, Logger } from '../../logger'
import { Meter } from '../../metrics/meter'
import { YupUtils } from '../../utils/yup'
import { Request } from '../request'
import { RpcRequest } from '../request'
import { ApiNamespace, Router } from '../routes'
import { RpcServer } from '../server'
import { IRpcAdapter } from './adapter'
Expand Down Expand Up @@ -86,7 +86,7 @@ export class RpcIpcAdapter implements IRpcAdapter {
server: IpcServer | null = null
namespaces: ApiNamespace[]
logger: Logger
pending = new Map<IpcSocketId, Request[]>()
pending = new Map<IpcSocketId, RpcRequest[]>()
started = false
connection: IpcAdapterConnectionInfo
inboundTraffic = new Meter()
Expand Down Expand Up @@ -242,7 +242,7 @@ export class RpcIpcAdapter implements IRpcAdapter {
Assert.isNotNull(router)
Assert.isNotNull(server)

const request = new Request(
const request = new RpcRequest(
message.data,
(status: number, data?: unknown) => {
this.emitResponse(socket, message.mid, status, data)
Expand Down
16 changes: 8 additions & 8 deletions ironfish/src/rpc/adapters/memoryAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { Assert } from '../../assert'
import { PromiseUtils, SetTimeoutToken } from '../../utils'
import { RequestError } from '../clients/errors'
import { Request } from '../request'
import { Response } from '../response'
import { RpcRequestError } from '../clients/errors'
import { RpcRequest } from '../request'
import { RpcResponse } from '../response'
import { Router } from '../routes'
import { Stream } from '../stream'
import { ResponseError } from './errors'
Expand All @@ -30,7 +30,7 @@ export class RpcMemoryAdapter {
const stream = new Stream<TStream>()
const response = new MemoryResponse(promise, stream, null)

const request = new Request(
const request = new RpcRequest(
data,
(status: number, data?: unknown) => {
response.status = status
Expand All @@ -51,7 +51,7 @@ export class RpcMemoryAdapter {
// Set the response status to the errors status because RequsetError takes it from the response
response.status = e.status

const error = new RequestError(response, e.code, e.message, e.stack)
const error = new RpcRequestError(response, e.code, e.message, e.stack)

// Do this so in memory requests retain the original stack and are easier to debug
error.stack = error.codeStack ?? error.stack
Expand All @@ -66,8 +66,8 @@ export class RpcMemoryAdapter {
}
}

export class MemoryResponse<TEnd, TStream> extends Response<TEnd, TStream> {
request: Request<unknown, unknown> | null = null
export class MemoryResponse<TEnd, TStream> extends RpcResponse<TEnd, TStream> {
request: RpcRequest<unknown, unknown> | null = null
routePromise: Promise<void> | null = null

constructor(
Expand All @@ -78,7 +78,7 @@ export class MemoryResponse<TEnd, TStream> extends Response<TEnd, TStream> {
super(promise, stream, timeout)
}

end(...args: Parameters<Request['end']>): ReturnType<Request['end']> {
end(...args: Parameters<RpcRequest['end']>): ReturnType<RpcRequest['end']> {
Assert.isNotNull(this.request)
return this.request.end(args)
}
Expand Down
8 changes: 4 additions & 4 deletions ironfish/src/rpc/adapters/socketAdapter/socketAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { JSONUtils } from '../../../utils'
import { ErrorUtils } from '../../../utils/error'
import { YupUtils } from '../../../utils/yup'
import { MessageBuffer } from '../../messageBuffer'
import { Request } from '../../request'
import { RpcRequest } from '../../request'
import { ApiNamespace, Router } from '../../routes'
import { RpcServer } from '../../server'
import { IRpcAdapter } from '../adapter'
Expand All @@ -24,7 +24,7 @@ import {
type SocketClient = {
id: string
socket: net.Socket
requests: Map<string, Request>
requests: Map<string, RpcRequest>
messageBuffer: MessageBuffer
}

Expand Down Expand Up @@ -133,7 +133,7 @@ export abstract class RpcSocketAdapter implements IRpcAdapter {
}

onClientConnection(socket: net.Socket): void {
const requests = new Map<string, Request>()
const requests = new Map<string, RpcRequest>()
const client = { socket, requests, id: uuid(), messageBuffer: new MessageBuffer() }
this.clients.set(client.id, client)

Expand Down Expand Up @@ -183,7 +183,7 @@ export abstract class RpcSocketAdapter implements IRpcAdapter {
const message = result.result.data

const requestId = uuid()
const request = new Request(
const request = new RpcRequest(
message.data,
(status: number, data?: unknown) => {
this.emitResponse(client, this.constructMessage(message.mid, status, data), requestId)
Expand Down
Loading

0 comments on commit 44642e8

Please sign in to comment.