1- import { ethers } from 'ethers' ;
21import {
3- DEFAULT_MAX_PRICE ,
2+ MAX_DESIRED_APP_ORDER_PRICE ,
3+ MAX_DESIRED_DATA_ORDER_PRICE ,
4+ MAX_DESIRED_WORKERPOOL_ORDER_PRICE ,
45 SCONE_TAG ,
56 WORKERPOOL_ADDRESS ,
67} from '../../config/config.js' ;
@@ -11,7 +12,7 @@ import {
1112} from '../../utils/errors.js' ;
1213import {
1314 checkUserVoucher ,
14- findWorkerpoolOrders ,
15+ filterWorkerpoolOrders ,
1516} from '../../utils/processProtectedData.models.js' ;
1617import { pushRequesterSecret } from '../../utils/pushRequesterSecret.js' ;
1718import {
@@ -45,7 +46,9 @@ export const processProtectedData = async ({
4546 protectedData,
4647 app,
4748 userWhitelist,
48- maxPrice = DEFAULT_MAX_PRICE ,
49+ dataMaxPrice = MAX_DESIRED_DATA_ORDER_PRICE ,
50+ appMaxPrice = MAX_DESIRED_APP_ORDER_PRICE ,
51+ workerpoolMaxPrice = MAX_DESIRED_WORKERPOOL_ORDER_PRICE ,
4952 path,
5053 args,
5154 inputFiles,
@@ -67,9 +70,15 @@ export const processProtectedData = async ({
6770 const vUserWhitelist = addressSchema ( )
6871 . label ( 'userWhitelist' )
6972 . validateSync ( userWhitelist ) ;
70- const vMaxPrice = positiveNumberSchema ( )
71- . label ( 'maxPrice' )
72- . validateSync ( maxPrice ) ;
73+ const vDataMaxPrice = positiveNumberSchema ( )
74+ . label ( 'dataMaxPrice' )
75+ . validateSync ( dataMaxPrice ) ;
76+ const vAppMaxPrice = positiveNumberSchema ( )
77+ . label ( 'appMaxPrice' )
78+ . validateSync ( appMaxPrice ) ;
79+ const vWorkerpoolMaxPrice = positiveNumberSchema ( )
80+ . label ( 'workerpoolMaxPrice' )
81+ . validateSync ( workerpoolMaxPrice ) ;
7382 const vPath = stringSchema ( ) . label ( 'path' ) . validateSync ( path ) ;
7483 const vInputFiles = urlArraySchema ( )
7584 . label ( 'inputFiles' )
@@ -136,76 +145,111 @@ export const processProtectedData = async ({
136145 }
137146
138147 vOnStatusUpdate ( {
139- title : 'FETCH_PROTECTED_DATA_ORDERBOOK ' ,
148+ title : 'FETCH_ORDERS ' ,
140149 isDone : false ,
141150 } ) ;
142- const datasetOrderbook = await iexec . orderbook . fetchDatasetOrderbook (
143- vProtectedData ,
144- {
145- app : vApp ,
146- workerpool : vWorkerpool ,
147- requester,
148- }
149- ) ;
150- const datasetorder = datasetOrderbook . orders [ 0 ] ?. order ; //The first order is the cheapest one
151- if ( ! datasetorder ) {
152- throw new Error ( `No dataset orders found` ) ;
153- }
154- vOnStatusUpdate ( {
155- title : 'FETCH_PROTECTED_DATA_ORDERBOOK' ,
156- isDone : true ,
157- } ) ;
151+ const [
152+ datasetorderForApp ,
153+ datasetorderForWhitelist ,
154+ apporder ,
155+ workerpoolorder ,
156+ ] = await Promise . all ( [
157+ // Fetch dataset order
158+ iexec . orderbook
159+ . fetchDatasetOrderbook ( vProtectedData , {
160+ app : vApp ,
161+ requester : requester ,
162+ } )
163+ . then ( ( datasetOrderbook ) => {
164+ const desiredPriceDataOrderbook = datasetOrderbook . orders . filter (
165+ ( order ) => order . order . datasetprice <= vDataMaxPrice
166+ ) ;
167+ return desiredPriceDataOrderbook [ 0 ] ?. order ; // may be undefined
168+ } ) ,
169+ // Fetch dataset order for whitelist
170+ iexec . orderbook
171+ . fetchDatasetOrderbook ( vProtectedData , {
172+ app : vUserWhitelist ,
173+ requester : requester ,
174+ } )
175+ . then ( ( datasetOrderbook ) => {
176+ const desiredPriceDataOrderbook = datasetOrderbook . orders . filter (
177+ ( order ) => order . order . datasetprice <= vDataMaxPrice
178+ ) ;
179+ return desiredPriceDataOrderbook [ 0 ] ?. order ; // may be undefined
180+ } ) ,
181+ // Fetch app order
182+ iexec . orderbook
183+ . fetchAppOrderbook ( vApp , {
184+ minTag : [ 'tee' , 'scone' ] ,
185+ maxTag : [ 'tee' , 'scone' ] ,
186+ workerpool : vWorkerpool ,
187+ } )
188+ . then ( ( appOrderbook ) => {
189+ const desiredPriceAppOrderbook = appOrderbook . orders . filter (
190+ ( order ) => order . order . appprice <= vAppMaxPrice
191+ ) ;
192+ const desiredPriceAppOrder = desiredPriceAppOrderbook [ 0 ] ?. order ;
193+ if ( ! desiredPriceAppOrder ) {
194+ throw new Error ( 'No App order found for the desired price' ) ;
195+ }
196+ return desiredPriceAppOrder ;
197+ } ) ,
198+ // Fetch workerpool order for App or AppWhitelist
199+ Promise . all ( [
200+ // for app
201+ iexec . orderbook . fetchWorkerpoolOrderbook ( {
202+ workerpool : vWorkerpool ,
203+ app : vApp ,
204+ dataset : vProtectedData ,
205+ requester : requester , // public orders + user specific orders
206+ isRequesterStrict : useVoucher , // If voucher, we only want user specific orders
207+ minTag : [ 'tee' , 'scone' ] ,
208+ maxTag : [ 'tee' , 'scone' ] ,
209+ category : 0 ,
210+ } ) ,
211+ // for app whitelist
212+ iexec . orderbook . fetchWorkerpoolOrderbook ( {
213+ workerpool : vWorkerpool ,
214+ app : vUserWhitelist ,
215+ dataset : vProtectedData ,
216+ requester : requester , // public orders + user specific orders
217+ isRequesterStrict : useVoucher , // If voucher, we only want user specific orders
218+ minTag : [ 'tee' , 'scone' ] ,
219+ maxTag : [ 'tee' , 'scone' ] ,
220+ category : 0 ,
221+ } ) ,
222+ ] ) . then (
223+ ( [ workerpoolOrderbookForApp , workerpoolOrderbookForAppWhitelist ] ) => {
224+ const desiredPriceWorkerpoolOrder = filterWorkerpoolOrders ( {
225+ workerpoolOrders : [
226+ ...workerpoolOrderbookForApp . orders ,
227+ ...workerpoolOrderbookForAppWhitelist . orders ,
228+ ] ,
229+ workerpoolMaxPrice : vWorkerpoolMaxPrice ,
230+ useVoucher : vUseVoucher ,
231+ userVoucher,
232+ } ) ;
233+ if ( ! desiredPriceWorkerpoolOrder ) {
234+ throw new Error ( 'No Workerpool order found for the desired price' ) ;
235+ }
236+ return desiredPriceWorkerpoolOrder ;
237+ }
238+ ) ,
239+ ] ) ;
158240
159- vOnStatusUpdate ( {
160- title : 'FETCH_APP_ORDERBOOK' ,
161- isDone : false ,
162- } ) ;
163- const appOrderbook = await iexec . orderbook . fetchAppOrderbook ( vApp , {
164- dataset : protectedData ,
165- requester,
166- minTag : SCONE_TAG ,
167- maxTag : SCONE_TAG ,
168- workerpool : vWorkerpool ,
169- } ) ;
170- const apporder = appOrderbook . orders [ 0 ] ?. order ; //The first order is the cheapest one
171- if ( ! apporder ) {
172- throw new Error ( `No app orders found` ) ;
241+ if ( ! workerpoolorder ) {
242+ throw new Error ( 'No Workerpool order found for the desired price' ) ;
173243 }
174- vOnStatusUpdate ( {
175- title : 'FETCH_APP_ORDERBOOK' ,
176- isDone : true ,
177- } ) ;
178244
179- vOnStatusUpdate ( {
180- title : 'FETCH_WORKERPOOL_ORDERBOOK' ,
181- isDone : false ,
182- } ) ;
183- const workerpoolOrderbook = await iexec . orderbook . fetchWorkerpoolOrderbook ( {
184- workerpool : vWorkerpool === ethers . ZeroAddress ? 'any' : vWorkerpool , // if address zero was chosen use any workerpool
185- app : vApp ,
186- dataset : vProtectedData ,
187- requester : requester ,
188- isRequesterStrict :
189- vVoucherOwner && vVoucherOwner . toLowerCase ( ) !== requester . toLowerCase ( )
190- ? false
191- : useVoucher ,
192- minTag : SCONE_TAG ,
193- maxTag : SCONE_TAG ,
194- category : 0 ,
195- } ) ;
196- const workerpoolOrder = findWorkerpoolOrders ( {
197- workerpoolOrders : [ ...workerpoolOrderbook . orders ] ,
198- useVoucher : vUseVoucher ,
199- userVoucher,
200- } ) ;
201- if ( ! workerpoolOrder ) {
202- throw new Error ( 'No Workerpool order found.' ) ;
245+ const datasetorder = datasetorderForApp || datasetorderForWhitelist ;
246+ if ( ! datasetorder ) {
247+ throw new Error ( 'No Dataset order found for the desired price' ) ;
203248 }
204249 vOnStatusUpdate ( {
205- title : 'FETCH_WORKERPOOL_ORDERBOOK ' ,
250+ title : 'FETCH_ORDERS ' ,
206251 isDone : true ,
207252 } ) ;
208-
209253 vOnStatusUpdate ( {
210254 title : 'PUSH_REQUESTER_SECRET' ,
211255 isDone : false ,
@@ -222,13 +266,13 @@ export const processProtectedData = async ({
222266 } ) ;
223267 const requestorderToSign = await iexec . order . createRequestorder ( {
224268 app : vApp ,
225- category : workerpoolOrder . category ,
269+ category : workerpoolorder . category ,
226270 dataset : vProtectedData ,
227271 appmaxprice : apporder . appprice ,
228272 datasetmaxprice : datasetorder . datasetprice ,
229- workerpoolmaxprice : workerpoolOrder . workerpoolprice ,
273+ workerpoolmaxprice : workerpoolorder . workerpoolprice ,
230274 tag : SCONE_TAG ,
231- workerpool : workerpoolOrder . workerpool ,
275+ workerpool : workerpoolorder . workerpool ,
232276 params : {
233277 iexec_input_files : vInputFiles ,
234278 iexec_secrets : secretsId ,
@@ -239,7 +283,7 @@ export const processProtectedData = async ({
239283
240284 const orders = {
241285 requestorder,
242- workerpoolorder : workerpoolOrder ,
286+ workerpoolorder : workerpoolorder ,
243287 apporder : apporder ,
244288 datasetorder : datasetorder ,
245289 } ;
@@ -248,20 +292,6 @@ export const processProtectedData = async ({
248292 ...( vVoucherOwner ? { voucherAddress : userVoucher ?. address } : { } ) ,
249293 } ;
250294
251- const estimatedMatchOrderPrice = await iexec . order . estimateMatchOrders (
252- orders ,
253- matchOptions
254- ) ;
255- if (
256- estimatedMatchOrderPrice . total
257- . sub ( estimatedMatchOrderPrice . sponsored )
258- . ltn ( vMaxPrice )
259- ) {
260- throw new Error (
261- `No orders found within the specified price limit ${ vMaxPrice } nRLC.`
262- ) ;
263- }
264-
265295 const { dealid, txHash } = await iexec . order . matchOrders (
266296 orders ,
267297 matchOptions
0 commit comments