Skip to content

Commit

Permalink
fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
mariacarmina committed Mar 11, 2024
2 parents 1c4dc2c + 4bd8e7d commit 21334e5
Show file tree
Hide file tree
Showing 27 changed files with 964 additions and 388 deletions.
9 changes: 6 additions & 3 deletions src/@types/commands.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ValidateParams } from '../components/httpRoutes/validateCommands.js'
import { DDO } from './DDO/DDO'
import { P2PCommandResponse } from './OceanNode'
import type { ComputeAsset, ComputeAlgorithm, ComputeOutput } from './C2D'
Expand Down Expand Up @@ -52,6 +53,7 @@ export interface ValidateDDOCommand extends Command {
}

export interface StatusCommand extends Command {}
export interface EchoCommand extends Command {}

export interface StopNodeCommand extends Command {
expiryTimestamp: number
Expand Down Expand Up @@ -82,12 +84,12 @@ export interface DecryptDDOCommand extends Command {

export interface EncryptCommand extends Command {
blob: string
encoding: string
encryptionType: EncryptMethod.AES | EncryptMethod.ECIES
encoding?: string
encryptionType?: EncryptMethod.AES | EncryptMethod.ECIES
}

export interface EncryptFileCommand extends Command {
encryptionType: EncryptMethod.AES | EncryptMethod.ECIES
encryptionType?: EncryptMethod.AES | EncryptMethod.ECIES
files?: BaseFileObject
rawData?: Buffer
// UrlFileObject | ArweaveFileObject | IpfsFileObject
Expand All @@ -106,6 +108,7 @@ export interface GetFeesCommand extends Command {

export interface ICommandHandler {
handle(command: Command): Promise<P2PCommandResponse>
validate(command: Command): ValidateParams
}

export interface BroadcastCommand {
Expand Down
51 changes: 51 additions & 0 deletions src/components/core/adminOperations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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'
import {
ValidateParams,
validateCommandParameters,
buildInvalidRequestMessage,
buildInvalidParametersResponse
} from '../httpRoutes/validateCommands.js'
import { validateSignature } from '../../utils/auth.js'

export class StopNodeHandler extends Handler {
validate(command: StopNodeCommand): ValidateParams {
const commandValidation = validateCommandParameters(command, [
'expiryTimestamp',
'signature'
])
if (!commandValidation.valid) {
CORE_LOGGER.logMessage(
`Command validation failed: ${JSON.stringify(commandValidation)}`
)
return commandValidation
}
if (
!validateSignature(command.expiryTimestamp, command.signature, this.getOceanNode())
) {
return buildInvalidRequestMessage('Expired authentication or invalid signature')
}
}

handle(task: StopNodeCommand): Promise<P2PCommandResponse> {
const validation = this.validate(task)
if (!validation.valid) {
return new Promise<P2PCommandResponse>((resolve, reject) => {
resolve(buildInvalidParametersResponse(validation))
})
}
CORE_LOGGER.logMessage(`Stopping node execution...`)
setTimeout(() => {
process.exit()
}, 2000)
return new Promise<P2PCommandResponse>((resolve, reject) => {
resolve({
status: { httpStatus: 200 },
stream: new ReadableString('EXIT OK')
})
})
}
}
39 changes: 22 additions & 17 deletions src/components/core/compute/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,33 @@ import { Handler } from '../handler.js'
import { ComputeGetEnvironmentsCommand } from '../../../@types/commands.js'
import { getConfiguration } from '../../../utils/config.js'
import { C2DEngine } from '../../c2d/compute_engines.js'
import {
ValidateParams,
buildInvalidParametersResponse,
buildInvalidRequestMessage,
validateCommandParameters
} from '../../httpRoutes/validateCommands.js'
export class ComputeGetEnvironmentsHandler extends Handler {
async handle(task: ComputeGetEnvironmentsCommand): Promise<P2PCommandResponse> {
try {
CORE_LOGGER.logMessage(
'ComputeGetEnvironmentsCommand received with arguments: ' +
JSON.stringify(task, null, 2),
true
)

if (isNaN(task.chainId) || task.chainId < 1) {
validate(command: ComputeGetEnvironmentsCommand): ValidateParams {
const validateCommand = validateCommandParameters(command, ['chainId'])
if (validateCommand.valid) {
if (isNaN(command.chainId) || command.chainId < 1) {
CORE_LOGGER.logMessage(
`Invalid chainId: ${task.chainId} on GET computeEnvironments request`,
`Invalid chainId: ${command.chainId} on GET computeEnvironments request`,
true
)
return {
stream: null,
status: {
httpStatus: 400,
error: 'Invalid chainId'
}
}
return buildInvalidRequestMessage('Invalid chainId')
}
}
return validateCommand
}

async handle(task: ComputeGetEnvironmentsCommand): Promise<P2PCommandResponse> {
const validation = this.validate(task)
if (!validation.valid) {
return buildInvalidParametersResponse(validation)
}
try {
const response: ComputeEnvironment[] = []
const config = await getConfiguration()
const { c2dClusters } = config
Expand Down
44 changes: 34 additions & 10 deletions src/components/core/compute/getResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,44 @@ import { Handler } from '../handler.js'
import { ComputeGetResultCommand } from '../../../@types/commands.js'
import { C2DEngine } from '../../c2d/compute_engines.js'
import { checkNonce, NonceResponse } from '../utils/nonceHandler.js'
import {
buildInvalidParametersResponse,
buildInvalidRequestMessage,
validateCommandParameters,
ValidateParams
} from '../../httpRoutes/validateCommands.js'
import { isAddress } from 'ethers'

export class ComputeGetResultHandler extends Handler {
async handle(task: ComputeGetResultCommand): Promise<P2PCommandResponse> {
CORE_LOGGER.logMessage(
'ComputeGetResultCommand received with arguments: ' + JSON.stringify(task, null, 2),
true
)
let error = null
if (!task.jobId) {
error = 'Invalid jobId'
validate(command: ComputeGetResultCommand): ValidateParams {
const validation = validateCommandParameters(command, [
'consumerAddress',
'signature',
'nonce',
'jobId',
'index'
])
if (validation.valid) {
if (command.consumerAddress && !isAddress(command.consumerAddress)) {
return buildInvalidRequestMessage(
'Parameter : "consumerAddress" is not a valid web3 address'
)
}
if (isNaN(command.index) || command.index < 1) {
return buildInvalidRequestMessage('Invalid result index')
}
}
if (isNaN(task.index) || task.index < 1) {
error = 'Invalid result index'
return validation
}

async handle(task: ComputeGetResultCommand): Promise<P2PCommandResponse> {
const validation = this.validate(task)
if (!validation.valid) {
return buildInvalidParametersResponse(validation)
}

let error = null

const nonceCheckResult: NonceResponse = await checkNonce(
this.getOceanNode().getDatabase().nonce,
task.consumerAddress,
Expand Down
43 changes: 27 additions & 16 deletions src/components/core/compute/getStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,37 @@ import { Handler } from '../handler.js'
import { ComputeGetStatusCommand } from '../../../@types/commands.js'
import { getConfiguration } from '../../../utils/config.js'
import { C2DEngine } from '../../c2d/compute_engines.js'
import {
ValidateParams,
buildInvalidParametersResponse,
buildInvalidRequestMessage,
validateCommandParameters
} from '../../httpRoutes/validateCommands.js'
import { isAddress } from 'ethers'

export class ComputeGetStatusHandler extends Handler {
async handle(task: ComputeGetStatusCommand): Promise<P2PCommandResponse> {
try {
CORE_LOGGER.logMessage(
'ComputeGetStatusCommand received with arguments: ' +
JSON.stringify(task, null, 2),
true
)
if (!task.consumerAddress && !task.jobId && !task.did) {
const error = `Missing jobId or consumerAddress or did`
validate(command: ComputeGetStatusCommand): ValidateParams {
const validation = validateCommandParameters(command, [])
if (validation.valid) {
if (command.consumerAddress && !isAddress(command.consumerAddress)) {
return buildInvalidRequestMessage(
'Parameter : "consumerAddress" is not a valid web3 address'
)
} else if (!command.consumerAddress && !command.jobId && !command.did) {
const error = 'Missing jobId or consumerAddress or did'
CORE_LOGGER.logMessage(error, true)
return {
stream: null,
status: {
httpStatus: 400,
error
}
}
return buildInvalidRequestMessage(error)
}
}
return validation
}

async handle(task: ComputeGetStatusCommand): Promise<P2PCommandResponse> {
const validation = this.validate(task)
if (!validation.valid) {
return buildInvalidParametersResponse(validation)
}
try {
const response: ComputeJob[] = []
// two scenarios here:
// 1. if we have a jobId, then we know what C2D Cluster to query
Expand Down
67 changes: 40 additions & 27 deletions src/components/core/compute/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,50 @@ import { validateOrderTransaction } from '../utils/validateOrders.js'
import { getExactComputeEnv } from './utils.js'
import { EncryptMethod } from '../../../@types/fileObject.js'
import { decrypt } from '../../../utils/crypt.js'
import {
ValidateParams,
buildInvalidParametersResponse,
buildInvalidRequestMessage,
validateCommandParameters
} from '../../httpRoutes/validateCommands.js'
import { isAddress } from 'ethers'
export class ComputeInitializeHandler extends Handler {
async handle(task: ComputeInitializeCommand): Promise<P2PCommandResponse> {
try {
CORE_LOGGER.logMessage(
'Initialize Compute Request recieved with arguments: ' +
JSON.stringify(task, null, 2),
true
)

const { validUntil } = task.compute
validate(command: ComputeInitializeCommand): ValidateParams {
const validation = validateCommandParameters(command, [
'datasets',
'algorithm',
'compute',
'consumerAddress'
])
if (validation.valid) {
if (command.consumerAddress && !isAddress(command.consumerAddress)) {
return buildInvalidRequestMessage(
'Parameter : "consumerAddress" is not a valid web3 address'
)
}
const { validUntil } = command.compute
if (validUntil <= new Date().getTime() / 1000) {
const errorMsg = `Error validating validUntil ${validUntil}. It is not in the future.`
CORE_LOGGER.error(errorMsg)
return {
stream: null,
status: {
httpStatus: 400,
error: errorMsg
}
}
}
if (!task.compute || !task.compute.env) {
CORE_LOGGER.logMessage(`Invalid compute environment: ${task.compute.env}`, true)
return {
stream: null,
status: {
httpStatus: 400,
error: `Invalid compute environment: ${task.compute.env}`
}
}
return buildInvalidRequestMessage(errorMsg)
} else if (!command.compute || !command.compute.env) {
CORE_LOGGER.error(`Invalid compute environment: ${command.compute.env}`)
return buildInvalidRequestMessage(
`Invalid compute environment: ${command.compute.env}`
)
}
}

return validation
}

async handle(task: ComputeInitializeCommand): Promise<P2PCommandResponse> {
const validation = this.validate(task)
if (!validation.valid) {
return buildInvalidParametersResponse(validation)
}

try {
let foundValidCompute = null
const node = this.getOceanNode()
const allFees: ProviderComputeInitializeResults = {
Expand Down Expand Up @@ -182,7 +195,7 @@ export class ComputeInitializeHandler extends Handler {
service,
bestValidUntil,
env,
validUntil
task.compute.validUntil
)
foundValidCompute = { txId: null, chainId: ddo.chainId }
}
Expand Down
45 changes: 31 additions & 14 deletions src/components/core/compute/startCompute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import { CORE_LOGGER } from '../../../utils/logging/common.js'
import { Handler } from '../handler.js'
import { ComputeStartCommand } from '../../../@types/commands.js'
import { C2DEngine } from '../../c2d/compute_engines.js'
import {
ValidateParams,
buildInvalidParametersResponse,
buildInvalidRequestMessage,
validateCommandParameters
} from '../../httpRoutes/validateCommands.js'
import { isAddress } from 'ethers'

import { DDO } from '../../../@types/DDO/DDO.js'
import { AssetUtils } from '../../../utils/asset.js'
import { EncryptMethod } from '../../../@types/fileObject.js'
Expand All @@ -13,24 +21,33 @@ import { verifyProviderFees } from '../utils/feesHandler.js'
import { getJsonRpcProvider } from '../../../utils/blockchain.js'
import { validateOrderTransaction } from '../utils/validateOrders.js'
export class ComputeStartHandler extends Handler {
validate(command: ComputeStartCommand): ValidateParams {
const commandValidation = validateCommandParameters(command, [
'consumerAddress',
'signature',
'nonce',
'environment',
'algorithm',
'dataset'
])
if (commandValidation.valid) {
if (!isAddress(command.consumerAddress)) {
return buildInvalidRequestMessage(
'Parameter : "consumerAddress" is not a valid web3 address'
)
}
}
return commandValidation
}

async handle(task: ComputeStartCommand): Promise<P2PCommandResponse> {
const validation = this.validate(task)
if (!validation.valid) {
return buildInvalidParametersResponse(validation)
}
try {
CORE_LOGGER.logMessage(
'ComputeStartCommand received with arguments: ' + JSON.stringify(task, null, 2),
true
)
// split compute env (which is already in hash-envId format) and get the hash
// then get env which might contain dashes as well
if (!task.environment) {
CORE_LOGGER.logMessage(`Invalid compute environment: ${task.environment}`, true)
return {
stream: null,
status: {
httpStatus: 400,
error: `Invalid compute environment: ${task.environment}`
}
}
}
const eIndex = task.environment.indexOf('-')
const hash = task.environment.slice(0, eIndex)
const envId = task.environment.slice(eIndex + 1)
Expand Down
Loading

0 comments on commit 21334e5

Please sign in to comment.