Skip to content

Commit a96a19d

Browse files
committed
Added validation for fields in initialize command and start compute. Fixed tests parsing.
1 parent 3a0a8f8 commit a96a19d

2 files changed

Lines changed: 100 additions & 21 deletions

File tree

src/commands.ts

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,6 @@ export class Commands {
315315
return;
316316
}
317317

318-
// TODO: check valid maxJobDuration
319-
320318
const computeEnvID = args[3];
321319
// NO chainId needed anymore (is not part of ComputeEnvironment spec anymore)
322320
// const chainComputeEnvs = computeEnvs[computeEnvID]; // was algoDdo.chainId
@@ -374,6 +372,20 @@ export class Commands {
374372
);
375373
return;
376374
}
375+
if (maxJobDuration < 0) {
376+
console.error(
377+
"Error initializing Provider for the compute job using dataset DID " +
378+
args[1] +
379+
" and algorithm DID " +
380+
args[2] +
381+
" because maxJobDuration is less than 0. It should be in seconds."
382+
);
383+
return;
384+
}
385+
let supportedMaxJobDuration: number = maxJobDuration;
386+
if (maxJobDuration > computeEnv.maxJobDuration) {
387+
supportedMaxJobDuration = computeEnv.maxJobDuration;
388+
}
377389
const paymentToken = args[5]
378390
if (!paymentToken) {
379391
console.error(
@@ -385,6 +397,31 @@ export class Commands {
385397
);
386398
return;
387399
}
400+
const chainId = await this.signer.getChainId()
401+
if (!computeEnv.fees.keys().includes(chainId)) {
402+
console.error(
403+
"Error starting paid compute using dataset DID " +
404+
args[1] +
405+
" and algorithm DID " +
406+
args[2] +
407+
" because chainId is not supported by compute environment. " +
408+
args[3] +
409+
". Supported chain IDs: " +
410+
computeEnv.fees.keys()
411+
);
412+
return;
413+
}
414+
if (computeEnv.fees[chainId].feeToken !== paymentToken) {
415+
console.error(
416+
"Error initializing Provider for the compute job using dataset DID " +
417+
args[1] +
418+
" and algorithm DID " +
419+
args[2] +
420+
" because paymentToken is not supported by this environment " +
421+
args[3]
422+
);
423+
return;
424+
}
388425
const resources = args[6] // resources object should be stringified in cli when calling initializeCompute
389426
if (!resources) {
390427
console.error(
@@ -396,16 +433,22 @@ export class Commands {
396433
);
397434
return;
398435
}
436+
const parsedResources = JSON.parse(resources);
437+
// for (const resource of parsedResources) {
438+
// if (resource.amount > computeEnv.resources[resource.id].amount) {
439+
440+
// }
441+
// }
399442
const providerInitializeComputeJob =
400443
await ProviderInstance.initializeCompute(
401444
assets,
402445
algo,
403446
computeEnv.id,
404447
paymentToken,
405-
maxJobDuration,
448+
supportedMaxJobDuration,
406449
providerURI,
407450
this.signer, // V1 was this.signer.getAddress()
408-
JSON.parse(resources)
451+
parsedResources
409452
);
410453
if (
411454
!providerInitializeComputeJob ||
@@ -594,7 +637,6 @@ export class Commands {
594637
}
595638
}
596639
// payment check
597-
// TODO: check valid maxJobDuration
598640
const maxJobDuration = Number(args[5])
599641
if (!maxJobDuration) {
600642
console.error(
@@ -606,21 +648,61 @@ export class Commands {
606648
);
607649
return;
608650
}
651+
if (maxJobDuration < 0) {
652+
console.error(
653+
"Error starting paid compute using dataset DID " +
654+
args[1] +
655+
" and algorithm DID " +
656+
args[2] +
657+
" because maxJobDuration is less than 0. It should be in seconds."
658+
);
659+
return;
660+
}
661+
let supportedMaxJobDuration: number = maxJobDuration;
662+
if (maxJobDuration > computeEnv.maxJobDuration) {
663+
supportedMaxJobDuration = computeEnv.maxJobDuration;
664+
}
665+
const chainId = await this.signer.getChainId()
609666
const paymentToken = args[6]
610667
if (!paymentToken) {
611668
console.error(
612-
"Error initializing Provider for the compute job using dataset DID " +
669+
"Error starting paid compute using dataset DID " +
613670
args[1] +
614671
" and algorithm DID " +
615672
args[2] +
616673
" because paymentToken was not provided."
617674
);
618675
return;
619676
}
677+
if (!computeEnv.fees.keys().includes(chainId)) {
678+
console.error(
679+
"Error starting paid compute using dataset DID " +
680+
args[1] +
681+
" and algorithm DID " +
682+
args[2] +
683+
" because chainId is not supported by compute environment. " +
684+
args[3] +
685+
". Supported chain IDs: " +
686+
computeEnv.fees.keys()
687+
);
688+
return;
689+
}
690+
691+
if (computeEnv.fees[chainId].feeToken !== paymentToken) {
692+
console.error(
693+
"Error starting paid compute using dataset DID " +
694+
args[1] +
695+
" and algorithm DID " +
696+
args[2] +
697+
" because paymentToken is not supported by this environment " +
698+
args[3]
699+
);
700+
return;
701+
}
620702
const resources = args[7] // resources object should be stringified in cli when calling initializeCompute
621703
if (!resources) {
622704
console.error(
623-
"Error initializing Provider for the compute job using dataset DID " +
705+
"Error starting paid compute using dataset DID " +
624706
args[1] +
625707
" and algorithm DID " +
626708
args[2] +
@@ -634,7 +716,7 @@ export class Commands {
634716
algo,
635717
computeEnv.id,
636718
paymentToken,
637-
maxJobDuration,
719+
supportedMaxJobDuration,
638720
providerURI,
639721
this.signer, // V1 was this.signer.getAddress()
640722
JSON.parse(resources)

test/computeFlow.test.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,15 @@ describe("Ocean CLI Compute", function() {
158158
expect(firstEnv).to.have.property("resources").that.is.an("array");
159159

160160
computeEnvId = firstEnv.id;
161+
console.log(`firstEnv.resources: ${JSON.stringify(firstEnv.resources)}`)
161162
resources = [
162163
{
163164
id: 'cpu',
164-
amount: firstEnv.resources[0].max - 1
165+
amount: firstEnv.resources[0].max - firstEnv.resources[0].inUse - 1
165166
},
166167
{
167168
id: 'ram',
168-
amount: firstEnv.resources[1].max - 1000
169+
amount: firstEnv.resources[1].max - firstEnv.resources[1].inUse - 1000
169170
},
170171
{
171172
id: 'disk',
@@ -183,11 +184,13 @@ describe("Ocean CLI Compute", function() {
183184
console.error("Raw output:", output);
184185
throw new Error("Could not find initialize response in the output");
185186
}
187+
const match = jsonMatch[0].match(/initialize compute details:\s*(.*)/s);
188+
const result = match ? match[1].trim() : null;
186189

187190
try {
188-
providerInitializeResponse = eval(`(${jsonMatch[1]})`);
191+
providerInitializeResponse = eval(result);
189192
} catch (error) {
190-
console.error("Extracted output:", jsonMatch[1]);
193+
console.error("Extracted output:", jsonMatch[0]);
191194
throw new Error("Failed to parse the extracted output:\n" + error);
192195
}
193196
console.log(`providerInitializeResponse: ${JSON.stringify(providerInitializeResponse)}`)
@@ -205,15 +208,9 @@ describe("Ocean CLI Compute", function() {
205208
console.error("Raw output:", output);
206209
throw new Error("Could not find initialize response in the output");
207210
}
208-
209-
let jobId;
210-
try {
211-
jobId = eval(`(${jsonMatch[0]})`);
212-
} catch (error) {
213-
console.error("Extracted output:", jsonMatch[0]);
214-
throw new Error("Failed to parse the extracted output:\n" + error);
215-
}
216-
computeJobId = jobId
211+
const match = jsonMatch[0].match(/JobID:\s*(.*)/s);
212+
const result = match ? match[1].trim() : null;
213+
computeJobId = result
217214
expect(computeJobId).to.be.a("string");
218215
});
219216

0 commit comments

Comments
 (0)