Skip to content

Commit 3d9c6ba

Browse files
refactor: merge grantAccess standard and bulk workflows
1 parent 0258279 commit 3d9c6ba

File tree

4 files changed

+64
-114
lines changed

4 files changed

+64
-114
lines changed

packages/sdk/src/lib/dataProtectorCore/grantAccess.ts

Lines changed: 57 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ZeroAddress } from 'ethers';
2-
import { NULL_ADDRESS } from 'iexec/utils';
3-
import { createBulkOrder } from '../../utils/createBulkOrder.js';
2+
import { DATASET_INFINITE_VOLUME, NULL_ADDRESS } from 'iexec/utils';
43
import {
54
ValidationError,
65
WorkflowError,
@@ -49,7 +48,7 @@ export const grantAccess = async ({
4948
protectedData,
5049
authorizedApp,
5150
authorizedUser,
52-
pricePerAccess,
51+
pricePerAccess = 0,
5352
numberOfAccess,
5453
allowBulk = false,
5554
onStatusUpdate = () => {},
@@ -65,10 +64,10 @@ export const grantAccess = async ({
6564
const vAuthorizedUser = addressOrEnsSchema()
6665
.label('authorizedUser')
6766
.validateSync(authorizedUser);
68-
const vPricePerAccess = positiveIntegerStringSchema()
67+
let vPricePerAccess = positiveIntegerStringSchema()
6968
.label('pricePerAccess')
7069
.validateSync(pricePerAccess);
71-
const vNumberOfAccess = positiveStrictIntegerStringSchema()
70+
let vNumberOfAccess = positiveStrictIntegerStringSchema()
7271
.label('numberOfAccess')
7372
.validateSync(numberOfAccess);
7473
const vAllowBulk = booleanSchema().label('allowBulk').validateSync(allowBulk);
@@ -77,6 +76,25 @@ export const grantAccess = async ({
7776
onStatusUpdate
7877
);
7978

79+
// Validate consistency between allowBulk, pricePerAccess and numberOfAccess
80+
if (vAllowBulk) {
81+
if (vPricePerAccess && vPricePerAccess !== '0') {
82+
throw new ValidationError(
83+
'allowBulk requires pricePerAccess to be 0 or undefined'
84+
);
85+
}
86+
vPricePerAccess = '0';
87+
if (
88+
vNumberOfAccess &&
89+
vNumberOfAccess !== DATASET_INFINITE_VOLUME.toString()
90+
) {
91+
throw new ValidationError(
92+
`allowBulk requires numberOfAccess to be ${DATASET_INFINITE_VOLUME.toString()} or undefined`
93+
);
94+
}
95+
vNumberOfAccess = DATASET_INFINITE_VOLUME.toString();
96+
}
97+
8098
if (vAuthorizedApp && isEnsTest(vAuthorizedApp)) {
8199
const resolved = await iexec.ens.resolveName(vAuthorizedApp);
82100
if (!resolved) {
@@ -127,95 +145,52 @@ export const grantAccess = async ({
127145
});
128146
}
129147

130-
let datasetorder;
131-
132-
if (vAllowBulk) {
133-
vOnStatusUpdate({
134-
title: 'CREATE_BULK_ORDER',
135-
isDone: false,
136-
});
148+
vOnStatusUpdate({
149+
title: 'CREATE_DATASET_ORDER',
150+
isDone: false,
151+
});
137152

138-
datasetorder = await createBulkOrder(iexec, {
153+
const datasetorder = await iexec.order
154+
.createDatasetorder({
139155
dataset: vProtectedData,
140-
app: vAuthorizedApp,
141-
requester: vAuthorizedUser,
156+
apprestrict: vAuthorizedApp,
157+
requesterrestrict: vAuthorizedUser,
158+
datasetprice: vPricePerAccess,
159+
volume: vNumberOfAccess,
142160
tag,
143-
}).catch((e) => {
144-
throw new WorkflowError({
145-
message: 'Failed to create bulk order',
146-
errorCause: e,
147-
});
148-
});
149-
150-
vOnStatusUpdate({
151-
title: 'CREATE_BULK_ORDER',
152-
isDone: true,
153-
});
154-
155-
vOnStatusUpdate({
156-
title: 'PUBLISH_BULK_ORDER',
157-
isDone: false,
158-
});
159-
160-
await iexec.order.publishDatasetorder(datasetorder).catch((e) => {
161-
handleIfProtocolError(e);
161+
})
162+
.then((datasetorderTemplate) =>
163+
iexec.order.signDatasetorder(datasetorderTemplate)
164+
)
165+
.catch((e) => {
162166
throw new WorkflowError({
163-
message: 'Failed to publish bulk order',
167+
message: 'Failed to sign data access',
164168
errorCause: e,
165169
});
166170
});
167171

168-
vOnStatusUpdate({
169-
title: 'PUBLISH_BULK_ORDER',
170-
isDone: true,
171-
});
172-
} else {
173-
vOnStatusUpdate({
174-
title: 'CREATE_DATASET_ORDER',
175-
isDone: false,
176-
});
177-
178-
datasetorder = await iexec.order
179-
.createDatasetorder({
180-
dataset: vProtectedData,
181-
apprestrict: vAuthorizedApp,
182-
requesterrestrict: vAuthorizedUser,
183-
datasetprice: vPricePerAccess,
184-
volume: vNumberOfAccess,
185-
tag,
186-
})
187-
.then((datasetorderTemplate) =>
188-
iexec.order.signDatasetorder(datasetorderTemplate)
189-
)
190-
.catch((e) => {
191-
throw new WorkflowError({
192-
message: 'Failed to sign data access',
193-
errorCause: e,
194-
});
195-
});
172+
vOnStatusUpdate({
173+
title: 'CREATE_DATASET_ORDER',
174+
isDone: true,
175+
});
196176

197-
vOnStatusUpdate({
198-
title: 'CREATE_DATASET_ORDER',
199-
isDone: true,
200-
});
177+
vOnStatusUpdate({
178+
title: 'PUBLISH_DATASET_ORDER',
179+
isDone: false,
180+
});
201181

202-
vOnStatusUpdate({
203-
title: 'PUBLISH_DATASET_ORDER',
204-
isDone: false,
182+
await iexec.order.publishDatasetorder(datasetorder).catch((e) => {
183+
handleIfProtocolError(e);
184+
throw new WorkflowError({
185+
message: 'Failed to publish data access',
186+
errorCause: e,
205187
});
188+
});
206189

207-
await iexec.order.publishDatasetorder(datasetorder).catch((e) => {
208-
handleIfProtocolError(e);
209-
throw new WorkflowError({
210-
message: 'Failed to publish data access',
211-
errorCause: e,
212-
});
213-
});
190+
vOnStatusUpdate({
191+
title: 'PUBLISH_DATASET_ORDER',
192+
isDone: true,
193+
});
214194

215-
vOnStatusUpdate({
216-
title: 'PUBLISH_DATASET_ORDER',
217-
isDone: true,
218-
});
219-
}
220195
return formatGrantedAccess(datasetorder, parseInt(datasetorder.volume));
221196
};

packages/sdk/src/lib/types/coreTypes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ export type GrantAccessParams = {
193193

194194
/**
195195
* Enable bulk processing for the granted access
196+
*
197+
* Bulk processing allows multiple protected data to be processed in a single task without paying per access.
198+
* `pricePerAccess` and `numberOfAccess` should be left undefined when `allowBulk` is true.
196199
*/
197200
allowBulk?: boolean;
198201

packages/sdk/src/utils/createBulkOrder.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

packages/sdk/tests/e2e/dataProtectorCore/grantAccess.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,19 +279,19 @@ describe('dataProtectorCore.grantAccess()', () => {
279279
expect(grantedAccess.volume).toBe('9007199254740991'); // Number.MAX_SAFE_INTEGER
280280
expect(grantedAccess.datasetprice).toBe('0'); // Price should be 0 for bulk orders
281281
expect(onStatusUpdateMock).toHaveBeenNthCalledWith(1, {
282-
title: 'CREATE_BULK_ORDER',
282+
title: 'CREATE_DATASET_ORDER',
283283
isDone: false,
284284
});
285285
expect(onStatusUpdateMock).toHaveBeenNthCalledWith(2, {
286-
title: 'CREATE_BULK_ORDER',
286+
title: 'CREATE_DATASET_ORDER',
287287
isDone: true,
288288
});
289289
expect(onStatusUpdateMock).toHaveBeenNthCalledWith(3, {
290-
title: 'PUBLISH_BULK_ORDER',
290+
title: 'PUBLISH_DATASET_ORDER',
291291
isDone: false,
292292
});
293293
expect(onStatusUpdateMock).toHaveBeenNthCalledWith(4, {
294-
title: 'PUBLISH_BULK_ORDER',
294+
title: 'PUBLISH_DATASET_ORDER',
295295
isDone: true,
296296
});
297297
},

0 commit comments

Comments
 (0)