Skip to content

Commit c9faa2f

Browse files
authored
Fix consistency for job id generation within codebase. (#934)
* Fix consistency for job id generation within codebase. * Fix type for jobId. * Make chainId optional for job id generation. * Fix unit tests env config. * fix imports. * Fix condition. * Fix review. * Added logs for debugging validating order. * Remove log. * Fix bigint serialization. * Cleanup logs.
1 parent e25a483 commit c9faa2f

6 files changed

Lines changed: 70 additions & 19 deletions

File tree

src/components/c2d/compute_engine_base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ export abstract class C2DEngine {
5959
owner: string,
6060
maxJobDuration: number,
6161
resources: ComputeResourceRequest[],
62-
payment: DBComputeJobPayment
62+
payment: DBComputeJobPayment,
63+
jobId: string
6364
): Promise<ComputeJob[]>
6465

6566
public abstract stopComputeJob(

src/components/c2d/compute_engine_docker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
} from 'fs'
3737
import { pipeline } from 'node:stream/promises'
3838
import { CORE_LOGGER } from '../../utils/logging/common.js'
39-
import { generateUniqueID } from '../database/sqliteCompute.js'
4039
import { AssetUtils } from '../../utils/asset.js'
4140
import { FindDdoHandler } from '../core/handler/ddoHandler.js'
4241
import { OceanNode } from '../../OceanNode.js'
@@ -310,11 +309,12 @@ export class C2DEngineDocker extends C2DEngine {
310309
owner: string,
311310
maxJobDuration: number,
312311
resources: ComputeResourceRequest[],
313-
payment: DBComputeJobPayment
312+
payment: DBComputeJobPayment,
313+
jobId: string
314314
): Promise<ComputeJob[]> {
315315
if (!this.docker) return []
316316
const isFree: boolean = !(payment && payment.lockTx)
317-
const jobId = generateUniqueID()
317+
318318
// C2D - Check image, check arhitecture, etc
319319
const image = getAlgorithmImage(algorithm)
320320
// ex: node@sha256:1155995dda741e93afe4b1c6ced2d01734a6ec69865cc0997daf1f4db7259a36

src/components/core/compute/startCompute.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { FindDdoHandler } from '../handler/ddoHandler.js'
3333
import { isOrderingAllowedForAsset } from '../handler/downloadHandler.js'
3434
import { DDOManager } from '@oceanprotocol/ddo-js'
3535
import { getNonceAsNumber, checkNonce, NonceResponse } from '../utils/nonceHandler.js'
36-
import { createHash } from 'crypto'
36+
import { generateUniqueID } from '../../database/sqliteCompute.js'
3737

3838
export class PaidComputeStartHandler extends CommandHandler {
3939
validate(command: PaidComputeStartCommand): ValidateParams {
@@ -353,12 +353,7 @@ export class PaidComputeStartHandler extends CommandHandler {
353353
resources
354354
}
355355
// job ID unicity
356-
const timestamp =
357-
BigInt(Date.now()) * 1_000_000n + (process.hrtime.bigint() % 1_000_000n)
358-
const random = Math.random()
359-
const jobId = createHash('sha256')
360-
.update(JSON.stringify(s) + timestamp.toString() + random.toString())
361-
.digest('hex')
356+
const jobId = generateUniqueID(s)
362357
// let's calculate payment needed based on resources request and maxJobDuration
363358
const cost = engine.calculateResourcesCost(
364359
task.payment.resources,
@@ -400,7 +395,8 @@ export class PaidComputeStartHandler extends CommandHandler {
400395
token: task.payment.token,
401396
lockTx: agreementId,
402397
claimTx: null
403-
}
398+
},
399+
jobId
404400
)
405401
CORE_LOGGER.logMessage(
406402
'ComputeStartCommand Response: ' + JSON.stringify(response, null, 2),
@@ -561,6 +557,16 @@ export class FreeComputeStartHandler extends CommandHandler {
561557
error: null
562558
}
563559
} */
560+
const s = {
561+
assets: task.datasets,
562+
algorithm: task.algorithm,
563+
output: task.output,
564+
environment: task.environment,
565+
owner: task.consumerAddress,
566+
maxJobDuration: task.maxJobDuration,
567+
resources: task.resources
568+
}
569+
const jobId = generateUniqueID(s)
564570
const response = await engine.startComputeJob(
565571
task.datasets,
566572
task.algorithm,
@@ -569,7 +575,8 @@ export class FreeComputeStartHandler extends CommandHandler {
569575
task.consumerAddress,
570576
task.maxJobDuration,
571577
task.resources,
572-
null
578+
null,
579+
jobId
573580
)
574581

575582
CORE_LOGGER.logMessage(

src/components/core/utils/validateOrders.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ export async function validateOrderTransaction(
134134
const currentTimestamp = Math.floor(Date.now() / 1000)
135135

136136
const timeElapsed = currentTimestamp - eventTimestamp
137+
CORE_LOGGER.logMessage(
138+
`serviceTimeout ${serviceTimeout} vs. timeElapsed ${timeElapsed} when validating order.`
139+
)
137140

138141
if (serviceTimeout !== 0 && timeElapsed > serviceTimeout) {
139142
return {

src/components/database/sqliteCompute.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '../../@types/C2D/C2D.js'
88
import sqlite3, { RunResult } from 'sqlite3'
99
import { DATABASE_LOGGER } from '../../utils/logging/common.js'
10-
import { v4 as uuidv4 } from 'uuid'
10+
import { createHash } from 'crypto'
1111

1212
interface ComputeDatabaseProvider {
1313
newJob(job: DBComputeJob): Promise<string>
@@ -18,8 +18,14 @@ interface ComputeDatabaseProvider {
1818
getFinishedJobs(): Promise<DBComputeJob[]>
1919
}
2020

21-
export function generateUniqueID(): string {
22-
return uuidv4()
21+
export function generateUniqueID(jobStructure: any): string {
22+
const timestamp =
23+
BigInt(Date.now()) * 1_000_000n + (process.hrtime.bigint() % 1_000_000n)
24+
const random = Math.random()
25+
const jobId = createHash('sha256')
26+
.update(JSON.stringify(jobStructure) + timestamp.toString() + random.toString())
27+
.digest('hex')
28+
return jobId
2329
}
2430

2531
function getInternalStructure(job: DBComputeJob): any {
@@ -138,8 +144,25 @@ export class SQLiteCompute implements ComputeDatabaseProvider {
138144
)
139145
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
140146
`
141-
const jobId = job.jobId || generateUniqueID()
142-
job.jobId = jobId
147+
let jobId: string
148+
if (!job.jobId) {
149+
const jobStructure = {
150+
assets: job.assets,
151+
algorithm: job.algorithm,
152+
output: {},
153+
environment: job.environment,
154+
owner: job.owner,
155+
maxJobDuration: job.maxJobDuration,
156+
chainId: job.payment?.chainId || null,
157+
agreementId: job.agreementId,
158+
resources: job.resources
159+
}
160+
jobId = generateUniqueID(jobStructure)
161+
job.jobId = jobId
162+
} else {
163+
jobId = job.jobId
164+
}
165+
143166
return new Promise<string>((resolve, reject) => {
144167
this.db.run(
145168
insertSQL,

src/test/unit/compute.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
convertStringToArray,
2020
STRING_SEPARATOR
2121
} from '../../components/database/sqliteCompute.js'
22+
import os from 'os'
2223
import {
2324
buildEnvOverrideConfig,
2425
OverrideEnvConfig,
@@ -29,7 +30,6 @@ import { OceanNodeConfig } from '../../@types/OceanNode.js'
2930
import { ENVIRONMENT_VARIABLES } from '../../utils/constants.js'
3031
import { completeDBComputeJob, dockerImageManifest } from '../data/assets.js'
3132
import { omitDBComputeFieldsFromComputeJob } from '../../components/c2d/index.js'
32-
import os from 'os'
3333
import { checkManifestPlatform } from '../../components/c2d/compute_engine_docker.js'
3434

3535
describe('Compute Jobs Database', () => {
@@ -46,6 +46,7 @@ describe('Compute Jobs Database', () => {
4646
documentId: 'did:op:12345',
4747
serviceId: '0x12345abc'
4848
}
49+
4950
before(async () => {
5051
envOverrides = buildEnvOverrideConfig(
5152
[ENVIRONMENT_VARIABLES.DOCKER_COMPUTE_ENVIRONMENTS],
@@ -83,6 +84,14 @@ describe('Compute Jobs Database', () => {
8384
isStarted: false,
8485
containerImage: 'some container image',
8586
resources: [],
87+
environment: 'some environment',
88+
agreementId: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260fdc',
89+
payment: {
90+
token: '0x123',
91+
lockTx: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260fdc',
92+
claimTx: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260fdc',
93+
chainId: 8996
94+
},
8695
isFree: false,
8796
algoStartTimestamp: '0',
8897
algoStopTimestamp: '0'
@@ -139,10 +148,18 @@ describe('Compute Jobs Database', () => {
139148
stopRequested: false,
140149
algorithm,
141150
assets: [dataset],
151+
environment: 'some environment',
142152
isRunning: false,
143153
isStarted: false,
144154
containerImage: 'another container image',
145155
resources: [],
156+
agreementId: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260fdc',
157+
payment: {
158+
token: '0x123',
159+
lockTx: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260fdc',
160+
claimTx: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260fdc',
161+
chainId: 8996
162+
},
146163
isFree: false,
147164
algoStartTimestamp: '0',
148165
algoStopTimestamp: '0'

0 commit comments

Comments
 (0)