Skip to content

Commit 3ae9595

Browse files
authored
add jobIdHash and cancelTx (#1286)
* add jobIdHash and cancelTx
1 parent 1e9f67d commit 3ae9595

7 files changed

Lines changed: 38 additions & 7 deletions

File tree

src/@types/C2D/C2D.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ export interface DBComputeJobPayment {
245245
token: string
246246
lockTx: string
247247
claimTx: string
248+
cancelTx: string
248249
cost: number
249250
}
250251

@@ -271,6 +272,7 @@ export interface DBComputeJob extends ComputeJob {
271272
algoDuration: number // duration of the job in seconds
272273
encryptedDockerRegistryAuth?: string
273274
output?: string // this is always an ECIES encrypted string, that decodes to ComputeOutput interface
275+
jobIdHash: string
274276
}
275277

276278
// make sure we keep them both in sync

src/components/c2d/compute_engine_docker.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ export class C2DEngineDocker extends C2DEngine {
344344
const currentTimestamp = BigInt(Math.floor(Date.now() / 1000))
345345
const envs: string[] = []
346346
const envsChains: string[] = []
347-
348347
// Group jobs by operation type and chain for batch processing
349348
const jobsToClaim: Array<{
350349
job: DBComputeJob
@@ -413,7 +412,7 @@ export class C2DEngineDocker extends C2DEngine {
413412

414413
// Find matching lock
415414
const lock = locks.find(
416-
(lock) => BigInt(lock.jobId.toString()) === BigInt(create256Hash(job.jobId))
415+
(lock) => BigInt(lock.jobId.toString()) === BigInt(job.jobIdHash)
417416
)
418417

419418
if (!lock) {
@@ -509,8 +508,10 @@ export class C2DEngineDocker extends C2DEngine {
509508
if (txId) {
510509
// Update all jobs with the transaction ID
511510
for (const claim of claims) {
512-
claim.job.payment!.claimTx = txId
513-
claim.job.payment!.cost = claim.cost
511+
if (claim.job.payment) {
512+
claim.job.payment.claimTx = txId
513+
claim.job.payment.cost = claim.cost
514+
}
514515
claim.job.status = C2DStatusNumber.JobFinished
515516
claim.job.statusText = C2DStatusText.JobFinished
516517
await this.db.updateJob(claim.job)
@@ -535,8 +536,10 @@ export class C2DEngineDocker extends C2DEngine {
535536
claim.proof
536537
)
537538
if (txId) {
538-
claim.job.payment!.claimTx = txId
539-
claim.job.payment!.cost = claim.cost
539+
if (claim.job.payment) {
540+
claim.job.payment.claimTx = txId
541+
claim.job.payment.cost = claim.cost
542+
}
540543
claim.job.status = C2DStatusNumber.JobFinished
541544
claim.job.statusText = C2DStatusText.JobFinished
542545
await this.db.updateJob(claim.job)
@@ -579,6 +582,7 @@ export class C2DEngineDocker extends C2DEngine {
579582
if (txId) {
580583
// Update all jobs
581584
for (const job of jobsToCancelBatch) {
585+
if (job.payment) job.payment.cancelTx = txId
582586
job.status = C2DStatusNumber.JobFinished
583587
job.statusText = C2DStatusText.JobFinished
584588
await this.db.updateJob(job)
@@ -601,6 +605,7 @@ export class C2DEngineDocker extends C2DEngine {
601605
job.owner
602606
)
603607
if (txId) {
608+
if (job.payment) job.payment.cancelTx = txId
604609
job.status = C2DStatusNumber.JobFinished
605610
job.statusText = C2DStatusText.JobFinished
606611
await this.db.updateJob(job)
@@ -618,6 +623,10 @@ export class C2DEngineDocker extends C2DEngine {
618623
for (const job of jobsWithoutLock) {
619624
job.status = C2DStatusNumber.JobFinished
620625
job.statusText = C2DStatusText.JobFinished
626+
if (job.payment) {
627+
job.payment.cancelTx = 'nolock'
628+
job.payment.claimTx = 'nolock'
629+
}
621630
await this.db.updateJob(job)
622631
}
623632
}
@@ -1088,6 +1097,7 @@ export class C2DEngineDocker extends C2DEngine {
10881097
containerImage: image,
10891098
owner,
10901099
jobId,
1100+
jobIdHash: create256Hash(jobId),
10911101
dateCreated: String(Date.now() / 1000),
10921102
dateFinished: null,
10931103
status:

src/components/core/compute/startCompute.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ export class PaidComputeStartHandler extends CommonComputeHandler {
669669
token: task.payment.token,
670670
lockTx: agreementId,
671671
claimTx: null,
672+
cancelTx: null,
672673
cost: 0
673674
},
674675
jobId,

src/components/database/sqliteCompute.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ function getInternalStructure(job: DBComputeJob): any {
4646
payment: job.payment,
4747
algoDuration: job.algoDuration,
4848
queueMaxWaitTime: job.queueMaxWaitTime,
49-
output: job.output
49+
output: job.output,
50+
jobIdHash: job.jobIdHash
5051
}
5152
return internalBlob
5253
}

src/test/integration/compute.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import { DDOManager } from '@oceanprotocol/ddo-js'
8181
import Dockerode from 'dockerode'
8282
import { C2DEngineDocker } from '../../components/c2d/compute_engine_docker.js'
8383
import { createHashForSignature, safeSign } from '../utils/signature.js'
84+
import { create256Hash } from '../../utils/crypt.js'
8485

8586
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
8687

@@ -2733,6 +2734,7 @@ describe('Compute Access Restrictions', () => {
27332734
const testJob: DBComputeJob = {
27342735
owner: await consumerAccount.getAddress(),
27352736
jobId: testJobId,
2737+
jobIdHash: create256Hash(testJobId),
27362738
dateCreated: now,
27372739
status: C2DStatusNumber.PublishingResults,
27382740
statusText: C2DStatusText.PublishingResults,
@@ -2749,6 +2751,7 @@ describe('Compute Access Restrictions', () => {
27492751
token: paymentToken,
27502752
lockTx: '0x123',
27512753
claimTx: '',
2754+
cancelTx: '',
27522755
cost: 0
27532756
},
27542757
resources: [
@@ -2901,6 +2904,7 @@ describe('Compute Access Restrictions', () => {
29012904
const testJob: DBComputeJob = {
29022905
owner: await consumerAccount.getAddress(),
29032906
jobId: testJobId,
2907+
jobIdHash: create256Hash(testJobId),
29042908
dateCreated: now.toString(),
29052909
status: C2DStatusNumber.JobSettle,
29062910
statusText: C2DStatusText.JobSettle,
@@ -2917,6 +2921,7 @@ describe('Compute Access Restrictions', () => {
29172921
token: paymentToken,
29182922
lockTx: lockTx || '0x123',
29192923
claimTx: '',
2924+
cancelTx: '',
29202925
cost: 0
29212926
},
29222927
resources: [
@@ -2968,6 +2973,7 @@ describe('Compute Access Restrictions', () => {
29682973
const testJob: DBComputeJob = {
29692974
owner: await consumerAccount.getAddress(),
29702975
jobId: testJobId,
2976+
jobIdHash: create256Hash(testJobId),
29712977
dateCreated: now.toString(),
29722978
status: C2DStatusNumber.JobSettle,
29732979
statusText: C2DStatusText.JobSettle,
@@ -2984,6 +2990,7 @@ describe('Compute Access Restrictions', () => {
29842990
token: paymentToken,
29852991
lockTx: '0xexpired',
29862992
claimTx: '',
2993+
cancelTx: '',
29872994
cost: 0
29882995
},
29892996
resources: [
@@ -3032,6 +3039,7 @@ describe('Compute Access Restrictions', () => {
30323039
const testJob: DBComputeJob = {
30333040
owner: await consumerAccount.getAddress(),
30343041
jobId: testJobId,
3042+
jobIdHash: create256Hash(testJobId),
30353043
dateCreated: now,
30363044
status: C2DStatusNumber.JobSettle,
30373045
statusText: C2DStatusText.JobSettle,
@@ -3095,6 +3103,7 @@ describe('Compute Access Restrictions', () => {
30953103
const testJob: DBComputeJob = {
30963104
owner: await consumerAccount.getAddress(),
30973105
jobId: testJobId,
3106+
jobIdHash: create256Hash(testJobId),
30983107
dateCreated: now.toString(),
30993108
status: C2DStatusNumber.JobSettle,
31003109
statusText: C2DStatusText.JobSettle,

src/test/integration/getJobs.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
tearDownEnvironment
1818
} from '../utils/utils.js'
1919
import { streamToObject } from '../../utils/util.js'
20+
import { create256Hash } from '../../utils/crypt.js'
2021

2122
// Helper to create a minimal valid DBComputeJob
2223
function buildJob(overrides: Partial<DBComputeJob> = {}): DBComputeJob {
@@ -25,6 +26,9 @@ function buildJob(overrides: Partial<DBComputeJob> = {}): DBComputeJob {
2526
owner: overrides.owner || '0xowner_test',
2627
did: overrides.did,
2728
jobId: overrides.jobId || `job-${Date.now()}-${Math.random().toString(36).slice(2)}`,
29+
jobIdHash: create256Hash(
30+
overrides.jobId || `job-${Date.now()}-${Math.random().toString(36).slice(2)}`
31+
),
2832
dateCreated: overrides.dateCreated || nowSec,
2933
dateFinished: overrides.dateFinished || (null as unknown as string),
3034
status: overrides.status ?? C2DStatusNumber.JobStarted,

src/test/unit/compute.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ describe('Compute Jobs Database', () => {
6464
const job: DBComputeJob = {
6565
owner: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260',
6666
jobId: null,
67+
jobIdHash: null,
6768
dateCreated: null,
6869
dateFinished: null,
6970
status: C2DStatusNumber.JobStarted,
@@ -91,6 +92,7 @@ describe('Compute Jobs Database', () => {
9192
token: '0x123',
9293
lockTx: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260fdc',
9394
claimTx: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260fdc',
95+
cancelTx: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260fdc',
9496
chainId: 8996,
9597
cost: 0
9698
},
@@ -135,6 +137,7 @@ describe('Compute Jobs Database', () => {
135137
const job: DBComputeJob = {
136138
owner: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947261',
137139
jobId: null,
140+
jobIdHash: null,
138141
dateCreated: null,
139142
dateFinished: null,
140143
status: C2DStatusNumber.JobStarted,
@@ -162,6 +165,7 @@ describe('Compute Jobs Database', () => {
162165
token: '0x123',
163166
lockTx: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260fdc',
164167
claimTx: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260fdc',
168+
cancelTx: '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260fdc',
165169
chainId: 8996,
166170
cost: 0
167171
},

0 commit comments

Comments
 (0)