11import { ethers } from 'ethers' ;
22import {
3- DEFAULT_MAX_PRICE ,
3+ MAX_DESIRED_APP_ORDER_PRICE ,
4+ MAX_DESIRED_DATA_ORDER_PRICE ,
5+ MAX_DESIRED_WORKERPOOL_ORDER_PRICE ,
46 SCONE_TAG ,
57 WORKERPOOL_ADDRESS ,
68} from '../../config/config.js' ;
@@ -11,7 +13,7 @@ import {
1113} from '../../utils/errors.js' ;
1214import {
1315 checkUserVoucher ,
14- findWorkerpoolOrders ,
16+ filterWorkerpoolOrders ,
1517} from '../../utils/processProtectedData.models.js' ;
1618import { pushRequesterSecret } from '../../utils/pushRequesterSecret.js' ;
1719import {
@@ -45,7 +47,9 @@ export const processProtectedData = async ({
4547 protectedData,
4648 app,
4749 userWhitelist,
48- maxPrice = DEFAULT_MAX_PRICE ,
50+ dataMaxPrice = MAX_DESIRED_DATA_ORDER_PRICE ,
51+ appMaxPrice = MAX_DESIRED_APP_ORDER_PRICE ,
52+ workerpoolMaxPrice = MAX_DESIRED_WORKERPOOL_ORDER_PRICE ,
4953 path,
5054 args,
5155 inputFiles,
@@ -67,9 +71,15 @@ export const processProtectedData = async ({
6771 const vUserWhitelist = addressSchema ( )
6872 . label ( 'userWhitelist' )
6973 . validateSync ( userWhitelist ) ;
70- const vMaxPrice = positiveNumberSchema ( )
71- . label ( 'maxPrice' )
72- . validateSync ( maxPrice ) ;
74+ const vDataMaxPrice = positiveNumberSchema ( )
75+ . label ( 'dataMaxPrice' )
76+ . validateSync ( dataMaxPrice ) ;
77+ const vAppMaxPrice = positiveNumberSchema ( )
78+ . label ( 'appMaxPrice' )
79+ . validateSync ( appMaxPrice ) ;
80+ const vWorkerpoolMaxPrice = positiveNumberSchema ( )
81+ . label ( 'workerpoolMaxPrice' )
82+ . validateSync ( workerpoolMaxPrice ) ;
7383 const vPath = stringSchema ( ) . label ( 'path' ) . validateSync ( path ) ;
7484 const vInputFiles = urlArraySchema ( )
7585 . label ( 'inputFiles' )
@@ -136,76 +146,111 @@ export const processProtectedData = async ({
136146 }
137147
138148 vOnStatusUpdate ( {
139- title : 'FETCH_PROTECTED_DATA_ORDERBOOK ' ,
149+ title : 'FETCH_ORDERS ' ,
140150 isDone : false ,
141151 } ) ;
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- } ) ;
152+ const [
153+ datasetorderForApp ,
154+ datasetorderForWhitelist ,
155+ apporder ,
156+ workerpoolorder ,
157+ ] = await Promise . all ( [
158+ // Fetch dataset order
159+ iexec . orderbook
160+ . fetchDatasetOrderbook ( vProtectedData , {
161+ app : vApp ,
162+ requester : requester ,
163+ } )
164+ . then ( ( datasetOrderbook ) => {
165+ const desiredPriceDataOrderbook = datasetOrderbook . orders . filter (
166+ ( order ) => order . order . datasetprice <= vDataMaxPrice
167+ ) ;
168+ return desiredPriceDataOrderbook [ 0 ] ?. order ; // may be undefined
169+ } ) ,
170+ // Fetch dataset order for whitelist
171+ iexec . orderbook
172+ . fetchDatasetOrderbook ( vProtectedData , {
173+ app : vUserWhitelist ,
174+ requester : requester ,
175+ } )
176+ . then ( ( datasetOrderbook ) => {
177+ const desiredPriceDataOrderbook = datasetOrderbook . orders . filter (
178+ ( order ) => order . order . datasetprice <= vDataMaxPrice
179+ ) ;
180+ return desiredPriceDataOrderbook [ 0 ] ?. order ; // may be undefined
181+ } ) ,
182+ // Fetch app order
183+ iexec . orderbook
184+ . fetchAppOrderbook ( vApp , {
185+ minTag : [ 'tee' , 'scone' ] ,
186+ maxTag : [ 'tee' , 'scone' ] ,
187+ workerpool : vWorkerpool ,
188+ } )
189+ . then ( ( appOrderbook ) => {
190+ const desiredPriceAppOrderbook = appOrderbook . orders . filter (
191+ ( order ) => order . order . appprice <= vAppMaxPrice
192+ ) ;
193+ const desiredPriceAppOrder = desiredPriceAppOrderbook [ 0 ] ?. order ;
194+ if ( ! desiredPriceAppOrder ) {
195+ throw new Error ( 'No App order found for the desired price' ) ;
196+ }
197+ return desiredPriceAppOrder ;
198+ } ) ,
199+ // Fetch workerpool order for App or AppWhitelist
200+ Promise . all ( [
201+ // for app
202+ iexec . orderbook . fetchWorkerpoolOrderbook ( {
203+ workerpool : vWorkerpool ,
204+ app : vApp ,
205+ dataset : vProtectedData ,
206+ requester : requester , // public orders + user specific orders
207+ isRequesterStrict : useVoucher , // If voucher, we only want user specific orders
208+ minTag : [ 'tee' , 'scone' ] ,
209+ maxTag : [ 'tee' , 'scone' ] ,
210+ category : 0 ,
211+ } ) ,
212+ // for app whitelist
213+ iexec . orderbook . fetchWorkerpoolOrderbook ( {
214+ workerpool : vWorkerpool === ethers . ZeroAddress ? 'any' : vWorkerpool ,
215+ app : vUserWhitelist ,
216+ dataset : vProtectedData ,
217+ requester : requester , // public orders + user specific orders
218+ isRequesterStrict : useVoucher , // If voucher, we only want user specific orders
219+ minTag : [ 'tee' , 'scone' ] ,
220+ maxTag : [ 'tee' , 'scone' ] ,
221+ category : 0 ,
222+ } ) ,
223+ ] ) . then (
224+ ( [ workerpoolOrderbookForApp , workerpoolOrderbookForAppWhitelist ] ) => {
225+ const desiredPriceWorkerpoolOrder = filterWorkerpoolOrders ( {
226+ workerpoolOrders : [
227+ ...workerpoolOrderbookForApp . orders ,
228+ ...workerpoolOrderbookForAppWhitelist . orders ,
229+ ] ,
230+ workerpoolMaxPrice : vWorkerpoolMaxPrice ,
231+ useVoucher : vUseVoucher ,
232+ userVoucher,
233+ } ) ;
234+ if ( ! desiredPriceWorkerpoolOrder ) {
235+ throw new Error ( 'No Workerpool order found for the desired price' ) ;
236+ }
237+ return desiredPriceWorkerpoolOrder ;
238+ }
239+ ) ,
240+ ] ) ;
158241
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` ) ;
242+ if ( ! workerpoolorder ) {
243+ throw new Error ( 'No Workerpool order found for the desired price' ) ;
173244 }
174- vOnStatusUpdate ( {
175- title : 'FETCH_APP_ORDERBOOK' ,
176- isDone : true ,
177- } ) ;
178245
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.' ) ;
246+ const datasetorder = datasetorderForApp || datasetorderForWhitelist ;
247+ if ( ! datasetorder ) {
248+ throw new Error ( 'No Dataset order found for the desired price' ) ;
203249 }
204250 vOnStatusUpdate ( {
205- title : 'FETCH_WORKERPOOL_ORDERBOOK ' ,
251+ title : 'FETCH_ORDERS ' ,
206252 isDone : true ,
207253 } ) ;
208-
209254 vOnStatusUpdate ( {
210255 title : 'PUSH_REQUESTER_SECRET' ,
211256 isDone : false ,
@@ -222,13 +267,13 @@ export const processProtectedData = async ({
222267 } ) ;
223268 const requestorderToSign = await iexec . order . createRequestorder ( {
224269 app : vApp ,
225- category : workerpoolOrder . category ,
270+ category : workerpoolorder . category ,
226271 dataset : vProtectedData ,
227272 appmaxprice : apporder . appprice ,
228273 datasetmaxprice : datasetorder . datasetprice ,
229- workerpoolmaxprice : workerpoolOrder . workerpoolprice ,
274+ workerpoolmaxprice : workerpoolorder . workerpoolprice ,
230275 tag : SCONE_TAG ,
231- workerpool : workerpoolOrder . workerpool ,
276+ workerpool : workerpoolorder . workerpool ,
232277 params : {
233278 iexec_input_files : vInputFiles ,
234279 iexec_secrets : secretsId ,
@@ -239,7 +284,7 @@ export const processProtectedData = async ({
239284
240285 const orders = {
241286 requestorder,
242- workerpoolorder : workerpoolOrder ,
287+ workerpoolorder : workerpoolorder ,
243288 apporder : apporder ,
244289 datasetorder : datasetorder ,
245290 } ;
@@ -248,20 +293,6 @@ export const processProtectedData = async ({
248293 ...( vVoucherOwner ? { voucherAddress : userVoucher ?. address } : { } ) ,
249294 } ;
250295
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-
265296 const { dealid, txHash } = await iexec . order . matchOrders (
266297 orders ,
267298 matchOptions
0 commit comments