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

Stop node handler #329

Merged
merged 8 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions src/@types/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export interface ValidateDDOCommand extends Command {

export interface StatusCommand extends Command {}

export interface StopNodeCommand extends Command {
expiryTimestamp: number
signature: string
}

export interface QueryCommand extends Command {
query: Record<string, any>
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/core/coreHandlersRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
ComputeGetResultHandler,
ComputeInitializeHandler
} from './compute/index.js'
import { StopNodeHandler } from './dashboard.js'

export type HandlerRegistry = {
handlerName: string // name of the handler
Expand Down Expand Up @@ -98,6 +99,7 @@ export class CoreHandlersRegistry {
PROTOCOL_COMMANDS.COMPUTE_INITIALIZE,
new ComputeInitializeHandler(node)
)
this.registerCoreHandler(PROTOCOL_COMMANDS.STOP_NODE, new StopNodeHandler(node))
}

public static getInstance(node: OceanNode): CoreHandlersRegistry {
Expand Down
20 changes: 20 additions & 0 deletions src/components/core/dashboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Handler } from './handler.js'
import { P2PCommandResponse } from '../../@types/OceanNode.js'
import { StopNodeCommand } from '../../@types/commands.js'
import { CORE_LOGGER } from '../../utils/logging/common.js'
import { ReadableString } from '../P2P/handleProtocolCommands.js'

export class StopNodeHandler extends Handler {
paulo-ocean marked this conversation as resolved.
Show resolved Hide resolved
handle(task: StopNodeCommand): Promise<P2PCommandResponse> {
CORE_LOGGER.logMessage(`Stopping node execution...`)
setTimeout(() => {
process.exit()
}, 2000)
paulo-ocean marked this conversation as resolved.
Show resolved Hide resolved
return new Promise<P2PCommandResponse>((resolve, reject) => {
resolve({
status: { httpStatus: 200 },
stream: new ReadableString('EXIT OK')
})
})
}
}
1 change: 0 additions & 1 deletion src/components/core/utils/statusHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export async function status(
)
}
}

const status: OceanNodeStatus = {
id: undefined,
publicKey: undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/components/httpRoutes/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ broadcastCommandRoute.post(
'/broadcastCommand',
express.json(),
async (req: Request, res: Response): Promise<void> => {
const validate = validateBroadcastParameters(req.body)
const validate = validateBroadcastParameters(req.body, req.oceanNode)
if (!validate.valid) {
res.status(validate.status).send(validate.reason)
return
Expand All @@ -39,7 +39,7 @@ directCommandRoute.post(
'/directCommand',
express.json(),
async (req: Request, res: Response): Promise<void> => {
const validate = validateCommandAPIParameters(req.body)
const validate = validateCommandAPIParameters(req.body, req.oceanNode)
if (!validate.valid) {
// 'node' param is not mandatory for 'downloadURL' command for instance:
// https://github.com/oceanprotocol/ocean-node/issues/26
Expand Down
25 changes: 22 additions & 3 deletions src/components/httpRoutes/validateCommands.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { isAddress } from 'ethers'
import { SUPPORTED_PROTOCOL_COMMANDS, PROTOCOL_COMMANDS } from '../../utils/constants.js'
import { EncryptMethod } from '../../@types/fileObject.js'
import { validateSignature } from '../../utils/auth.js'
import { OceanNode } from '../../OceanNode.js'

export type ValidateParams = {
valid: boolean
reason?: string
status?: number
}

export function validateBroadcastParameters(requestBody: any): ValidateParams {
export function validateBroadcastParameters(
requestBody: any,
oceanNode: OceanNode
paulo-ocean marked this conversation as resolved.
Show resolved Hide resolved
): ValidateParams {
// for now we can use the same validation function,
// but later we might need to have separate validation functions
// if we many different commands of each type
return validateCommandAPIParameters(requestBody)
return validateCommandAPIParameters(requestBody, oceanNode)
paulo-ocean marked this conversation as resolved.
Show resolved Hide resolved
}
// add others when we add support
export function validateCommandAPIParameters(requestBody: any): ValidateParams {
export function validateCommandAPIParameters(
requestBody: any,
oceanNode: OceanNode
): ValidateParams {
// eslint-disable-next-line prefer-destructuring
const command: string = requestBody.command as string

Expand Down Expand Up @@ -146,6 +154,17 @@ export function validateCommandAPIParameters(requestBody: any): ValidateParams {
'Missing required parameter(s): "chaindId","datasets", "algorithm","compute", "consumerAddress"'
)
}
} else if (command === PROTOCOL_COMMANDS.STOP_NODE) {
if (!requestBody.expiryTimestamp || !requestBody.signature) {
return buildInvalidRequestMessage(
'Missing required parameter(s): "expiryTimestamp","signature"'
)
}
if (
!validateSignature(requestBody.expiryTimestamp, requestBody.signature, oceanNode)
) {
return buildInvalidRequestMessage('Expired authentication or invalid signature')
}
}
// only once is enough :-)
return {
Expand Down
6 changes: 4 additions & 2 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const PROTOCOL_COMMANDS = {
COMPUTE_STOP: 'stopCompute',
COMPUTE_GET_STATUS: 'getComputeStatus',
COMPUTE_GET_RESULT: 'getComputeResult',
COMPUTE_INITIALIZE: 'initializeCompute'
COMPUTE_INITIALIZE: 'initializeCompute',
STOP_NODE: 'stopNode'
}
// more visible, keep then close to make sure we always update both
export const SUPPORTED_PROTOCOL_COMMANDS: string[] = [
Expand All @@ -45,7 +46,8 @@ export const SUPPORTED_PROTOCOL_COMMANDS: string[] = [
PROTOCOL_COMMANDS.COMPUTE_STOP,
PROTOCOL_COMMANDS.COMPUTE_GET_STATUS,
PROTOCOL_COMMANDS.COMPUTE_GET_RESULT,
PROTOCOL_COMMANDS.COMPUTE_INITIALIZE
PROTOCOL_COMMANDS.COMPUTE_INITIALIZE,
PROTOCOL_COMMANDS.STOP_NODE
]

export const MetadataStates = {
Expand Down
Loading