Skip to content

Commit b64295e

Browse files
committed
chore(coprocessor): batch queries within a DB transaction (stress-test) tool
1 parent d9ed266 commit b64295e

File tree

10 files changed

+218
-136
lines changed

10 files changed

+218
-136
lines changed

coprocessor/fhevm-engine/stress-test-generator/data/json/batch_bids_auction.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"batch_size": 1,
1111
"scenario": [
1212
[
13-
1.0,
14-
1
13+
2.0,
14+
1200
1515
]
1616
]
1717
}

coprocessor/fhevm-engine/stress-test-generator/data/json/batch_input_proofs.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
"is_dependent": "Independent",
88
"contract_address": "0xa5880e99d86F081E8D3868A8C4732C8f65dfdB08",
99
"user_address": "0xa0534e99d86F081E8D3868A8C4732C8f65dfdB07",
10-
"batch_size": 100,
10+
"batch_size": 4000,
1111
"scenario": [
1212
[
1313
1.0,
14-
1
14+
600
1515
]
1616
]
1717
}

coprocessor/fhevm-engine/stress-test-generator/src/auction.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ pub static CONTRACT_STATE: OnceLock<Arc<RwLock<ContractState>>> = OnceLock::new(
3232
#[allow(clippy::too_many_arguments)]
3333
pub async fn batch_submit_encrypted_bids(
3434
ctx: &Context,
35-
listener_event_to_db: &mut ListenerDatabase,
35+
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
36+
listener_event_to_db: &ListenerDatabase,
3637
transaction_id: Option<Handle>,
3738
user_address: &str,
3839
payment_token_address: &str,
@@ -51,6 +52,7 @@ pub async fn batch_submit_encrypted_bids(
5152

5253
// euint64 eTotalPaymentValue = FHE.asEuint64(0);
5354
let mut e_total_payment_value = generate_trivial_encrypt(
55+
tx,
5456
user_address,
5557
user_address,
5658
transaction_id,
@@ -68,6 +70,7 @@ pub async fn batch_submit_encrypted_bids(
6870

6971
let (e_paid, e_amount, price) = process_bid_entry(
7072
ctx,
73+
tx,
7174
e_amount.expect("should be a valid bid"),
7275
bid_price,
7376
transaction_id,
@@ -90,7 +93,7 @@ pub async fn batch_submit_encrypted_bids(
9093
result: result_handle,
9194
scalarByte: ScalarByte::from(false as u8),
9295
}));
93-
insert_tfhe_event(listener_event_to_db, transaction_id, event, false).await?;
96+
insert_tfhe_event(tx, listener_event_to_db, transaction_id, event, false).await?;
9497
e_total_payment_value = result_handle;
9598

9699
user_submitted_bids.push(BidEntry {
@@ -102,6 +105,7 @@ pub async fn batch_submit_encrypted_bids(
102105

103106
let e_is_payment_confirmed = process_batch_payment(
104107
ctx,
108+
tx,
105109
transaction_id,
106110
listener_event_to_db,
107111
user_address,
@@ -113,6 +117,7 @@ pub async fn batch_submit_encrypted_bids(
113117
// Confirm and finalize each bid based on the payment result
114118
for bid_entry in user_submitted_bids.iter() {
115119
confirm_and_finalize_bid(
120+
tx,
116121
transaction_id,
117122
listener_event_to_db,
118123
bid_entry,
@@ -136,16 +141,18 @@ pub async fn batch_submit_encrypted_bids(
136141
#[allow(clippy::too_many_arguments)]
137142
pub async fn process_bid_entry(
138143
_ctx: &Context,
144+
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
139145
mut e_amount: Handle,
140146
price: u64,
141147
transaction_id: Handle,
142-
listener_event_to_db: &mut ListenerDatabase,
148+
listener_event_to_db: &ListenerDatabase,
143149
user_address: &str,
144150
) -> Result<(Handle, Handle, u64), Box<dyn std::error::Error>> {
145151
let caller = user_address.parse().unwrap();
146152
info!(target: "tool", "Process Bid Entry: tx_id: {:?}", transaction_id);
147153

148154
let total_supply = generate_trivial_encrypt(
155+
tx,
149156
user_address,
150157
user_address,
151158
transaction_id,
@@ -164,9 +171,10 @@ pub async fn process_bid_entry(
164171
result: less_than_total_supply,
165172
scalarByte: ScalarByte::from(false as u8),
166173
}));
167-
insert_tfhe_event(listener_event_to_db, transaction_id, event, false).await?;
174+
insert_tfhe_event(tx, listener_event_to_db, transaction_id, event, false).await?;
168175

169176
let zero = generate_trivial_encrypt(
177+
tx,
170178
user_address,
171179
user_address,
172180
transaction_id,
@@ -188,10 +196,11 @@ pub async fn process_bid_entry(
188196
},
189197
));
190198

191-
insert_tfhe_event(listener_event_to_db, transaction_id, event, false).await?;
199+
insert_tfhe_event(tx, listener_event_to_db, transaction_id, event, false).await?;
192200
e_amount = result_handle;
193201

194202
let e_price = generate_trivial_encrypt(
203+
tx,
195204
user_address,
196205
user_address,
197206
transaction_id,
@@ -211,7 +220,7 @@ pub async fn process_bid_entry(
211220
result: e_paid,
212221
scalarByte: ScalarByte::from(false as u8),
213222
}));
214-
insert_tfhe_event(listener_event_to_db, transaction_id, event, false).await?;
223+
insert_tfhe_event(tx, listener_event_to_db, transaction_id, event, false).await?;
215224

216225
Ok((e_paid, e_amount, price))
217226
}
@@ -225,8 +234,9 @@ pub async fn process_bid_entry(
225234
// eIsPaymentConfirmed = FHE.eq(eTotalPaid, eTotalValue);
226235
pub async fn process_batch_payment(
227236
ctx: &Context,
237+
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
228238
transaction_id: Handle,
229-
listener_event_to_db: &mut ListenerDatabase,
239+
listener_event_to_db: &ListenerDatabase,
230240
user_address: &str,
231241
payment_token_address: &str,
232242
e_total_value: Handle,
@@ -238,6 +248,7 @@ pub async fn process_batch_payment(
238248

239249
let e_total_paid = crate::erc7984::confidential_transfer_from(
240250
ctx,
251+
tx,
241252
transaction_id,
242253
listener_event_to_db,
243254
e_total_value,
@@ -246,11 +257,11 @@ pub async fn process_batch_payment(
246257
.await?;
247258

248259
allow_handle(
260+
tx,
249261
&e_total_paid.to_vec(),
250262
AllowEvents::AllowedAccount,
251263
payment_token_address.to_string(),
252264
transaction_id,
253-
&pool,
254265
)
255266
.await?;
256267

@@ -262,14 +273,15 @@ pub async fn process_batch_payment(
262273
result: e_is_payment_confirmed,
263274
scalarByte: ScalarByte::from(false as u8),
264275
}));
265-
insert_tfhe_event(listener_event_to_db, transaction_id, event, false).await?;
276+
insert_tfhe_event(tx, listener_event_to_db, transaction_id, event, false).await?;
266277

267278
Ok(e_is_payment_confirmed)
268279
}
269280

270281
pub async fn confirm_and_finalize_bid(
282+
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
271283
transaction_id: Handle,
272-
listener_event_to_db: &mut ListenerDatabase,
284+
listener_event_to_db: &ListenerDatabase,
273285
bid_entry: &BidEntry,
274286
user_address: &str,
275287
e_is_payment_confirmed: Handle,
@@ -281,6 +293,7 @@ pub async fn confirm_and_finalize_bid(
281293
info!(target: "tool", "Confirm and Finalize Bid: tx_id: {:?}", transaction_id);
282294

283295
let zero = generate_trivial_encrypt(
296+
tx,
284297
user_address,
285298
user_address,
286299
transaction_id,
@@ -307,7 +320,7 @@ pub async fn confirm_and_finalize_bid(
307320
result: result_handle,
308321
},
309322
));
310-
insert_tfhe_event(listener_event_to_db, transaction_id, event, true).await?;
323+
insert_tfhe_event(tx, listener_event_to_db, transaction_id, event, true).await?;
311324
bid_entry.e_amount = result_handle;
312325

313326
let result_handle = next_random_handle(DEF_TYPE);
@@ -320,7 +333,7 @@ pub async fn confirm_and_finalize_bid(
320333
result: result_handle,
321334
},
322335
));
323-
insert_tfhe_event(listener_event_to_db, transaction_id, event, true).await?;
336+
insert_tfhe_event(tx, listener_event_to_db, transaction_id, event, true).await?;
324337
bid_entry.e_paid = result_handle;
325338

326339
// Update contract state
@@ -358,7 +371,7 @@ pub async fn confirm_and_finalize_bid(
358371
.or_insert_with(|| bid_entry.e_amount);
359372

360373
if let Some(event) = event {
361-
insert_tfhe_event(listener_event_to_db, transaction_id, event, true)
374+
insert_tfhe_event(tx, listener_event_to_db, transaction_id, event, true)
362375
.await
363376
.unwrap();
364377
}
@@ -371,11 +384,11 @@ pub async fn confirm_and_finalize_bid(
371384

372385
// FHE.allowThis(updatedTotalAmount);
373386
allow_handle(
387+
tx,
374388
&updated_total_amount,
375389
AllowEvents::AllowedAccount,
376390
user_address.to_string(),
377391
transaction_id,
378-
pool,
379392
)
380393
.await?;
381394

@@ -409,7 +422,7 @@ pub async fn confirm_and_finalize_bid(
409422
.or_insert_with(|| bid_entry.e_amount);
410423

411424
if let Some(event) = event {
412-
insert_tfhe_event(listener_event_to_db, transaction_id, event, true)
425+
insert_tfhe_event(tx, listener_event_to_db, transaction_id, event, true)
413426
.await
414427
.unwrap();
415428
}
@@ -422,11 +435,11 @@ pub async fn confirm_and_finalize_bid(
422435

423436
// FHE.allowThis(updatedTotalAmountByTokenPrice);
424437
allow_handle(
438+
tx,
425439
&updated_total_amount_by_token_price,
426440
AllowEvents::AllowedAccount,
427441
user_address.to_string(),
428442
transaction_id,
429-
pool,
430443
)
431444
.await?;
432445
}
@@ -438,20 +451,20 @@ pub async fn confirm_and_finalize_bid(
438451
FHE.allow(_bid.ePaid, auctionConfig.complianceAddress);
439452
*/
440453
allow_handle(
454+
tx,
441455
bid_entry.e_amount.to_vec().as_ref(),
442456
AllowEvents::AllowedAccount,
443457
user_address.to_string(),
444458
transaction_id,
445-
pool,
446459
)
447460
.await?;
448461

449462
allow_handle(
463+
tx,
450464
bid_entry.e_paid.to_vec().as_ref(),
451465
AllowEvents::AllowedAccount,
452466
user_address.to_string(),
453467
transaction_id,
454-
pool,
455468
)
456469
.await?;
457470

0 commit comments

Comments
 (0)