Skip to content

Commit d78c71c

Browse files
committed
feat(coprocessor): implement additional stress-test scenarios
- add Transaction::ERC7984Transfer - add a simplified variant of the auction::submitEncryptedBids - add sample json data files
1 parent 3198da0 commit d78c71c

File tree

5 files changed

+117
-10
lines changed

5 files changed

+117
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"user_address": "0xa0534e99d86F081E8D3868A8C4732C8f65dfdB07",
1010
"scenario": [
1111
[
12-
20.0,
13-
120
12+
1.0,
13+
1
1414
]
1515
]
1616
}

coprocessor/fhevm-engine/stress-test-generator/src/bin/stress_generator.rs

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use fhevm_engine_common::utils::DatabaseURL;
99
use host_listener::database::tfhe_event_propagate::{Database as ListenerDatabase, Handle};
1010

1111
use sqlx::Postgres;
12-
use std::io::Write;
12+
use std::{cmp::min, io::Write};
1313
use std::{collections::HashMap, fmt, sync::atomic::AtomicU64};
1414
use std::{
1515
ops::{Add, Sub},
@@ -19,16 +19,25 @@ use std::{
1919
sync::atomic::Ordering,
2020
time::{Duration, SystemTime},
2121
};
22-
use stress_test_generator::utils::{
23-
default_dependence_cache_size, get_ciphertext_digests, Dependence, GeneratorKind, Transaction,
22+
use stress_test_generator::{
23+
args::parse_args, dex::dex_swap_claim_transaction, utils::new_transaction_id,
24+
};
25+
use stress_test_generator::{
26+
auction::batch_submit_encrypted_bids,
27+
zk_gen::{generate_input_verification_transaction, get_inputs_vector},
2428
};
25-
use stress_test_generator::zk_gen::{generate_input_verification_transaction, get_inputs_vector};
26-
use stress_test_generator::{args::parse_args, dex::dex_swap_claim_transaction};
2729
use stress_test_generator::{
2830
dex::dex_swap_request_transaction,
2931
erc20::erc20_transaction,
3032
utils::{EnvConfig, Job, Scenario},
3133
};
34+
use stress_test_generator::{
35+
erc7984,
36+
utils::{
37+
allow_handles, default_dependence_cache_size, get_ciphertext_digests, next_random_handle,
38+
Dependence, GeneratorKind, Transaction, DEF_TYPE,
39+
},
40+
};
3241
use stress_test_generator::{
3342
synthetics::{
3443
add_chain_transaction, generate_pub_decrypt_handles_types,
@@ -41,6 +50,7 @@ use tokio_util::sync::CancellationToken;
4150
use tracing::{error, info, warn};
4251

4352
const MAX_RETRIES: usize = 500;
53+
const MAX_NUMBER_OF_BIDS: usize = 10;
4454

4555
#[tokio::main]
4656
async fn main() {
@@ -56,6 +66,7 @@ async fn main() {
5666
args: args.clone(),
5767
ecfg: EnvConfig::new(),
5868
cancel_token: CancellationToken::new(),
69+
inputs_pool: vec![],
5970
};
6071

6172
if args.run_server {
@@ -584,7 +595,52 @@ async fn generate_transaction(
584595
},
585596
};
586597

598+
let mut new_ctx = ctx.clone();
599+
new_ctx.inputs_pool = inputs.clone();
600+
let ctx = &new_ctx;
601+
587602
match scenario.transaction {
603+
Transaction::BatchSubmitEncryptedBids => {
604+
let batch_size = min(MAX_NUMBER_OF_BIDS, scenario.batch_size.unwrap_or(1));
605+
606+
// reuse the existing inputs as bids
607+
let bids = inputs
608+
.iter()
609+
.take(batch_size)
610+
.copied()
611+
.collect::<Vec<Option<Handle>>>();
612+
613+
let e_total_payment = batch_submit_encrypted_bids(
614+
ctx,
615+
listener_event_to_db,
616+
None, // Transaction ID
617+
&scenario.contract_address,
618+
&scenario.user_address,
619+
&bids,
620+
)
621+
.await?;
622+
623+
// TODO: how to make dependent if needed?
624+
Ok((e_total_payment, e_total_payment)) // TODO: return meaningful second handle
625+
}
626+
Transaction::BatchAllowHandles => {
627+
let mut handles = Vec::new();
628+
for _ in 0..scenario.batch_size.unwrap_or(1) {
629+
handles.push(next_random_handle(DEF_TYPE).to_vec());
630+
}
631+
632+
info!(target: "tool", batch_size = handles.len(), "Batch allowing handles");
633+
634+
allow_handles(
635+
&handles,
636+
fhevm_engine_common::types::AllowEvents::AllowedAccount,
637+
scenario.user_address.to_string(),
638+
pool,
639+
true,
640+
)
641+
.await?;
642+
Ok((Handle::default(), Handle::default()))
643+
}
588644
Transaction::ERC20Transfer => {
589645
let (_, output_dependence) = erc20_transaction(
590646
ctx,
@@ -712,5 +768,24 @@ async fn generate_transaction(
712768
.await?;
713769
Ok((output_dependence1, output_dependence2))
714770
}
771+
Transaction::ERC7984Transfer => {
772+
let transaction_id = new_transaction_id();
773+
let e_amount = inputs
774+
.first()
775+
.unwrap()
776+
.expect("should be at least one input available");
777+
778+
info!(target: "tool", "ERC7984 Transaction: tx_id: {:?}", transaction_id);
779+
let e_total_paid = erc7984::confidential_transfer_from(
780+
ctx,
781+
transaction_id,
782+
listener_event_to_db,
783+
e_amount,
784+
scenario.user_address.as_str(),
785+
)
786+
.await?;
787+
788+
Ok((e_total_paid, e_total_paid))
789+
}
715790
}
716791
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use sqlx::Postgres;
77
use tracing::{error, info};
88

99
use crate::utils::{
10-
allow_handle, insert_tfhe_event, next_random_handle, tfhe_event, Context, ERCTransferVariant,
11-
FheType, DEF_TYPE,
10+
allow_handle, insert_tfhe_event, new_transaction_id, next_random_handle, tfhe_event, Context,
11+
ERCTransferVariant, FheType, DEF_TYPE,
1212
};
1313
use crate::zk_gen::generate_random_handle_amount_if_none;
1414

@@ -26,7 +26,7 @@ pub async fn erc20_transaction(
2626
user_address: &String,
2727
) -> Result<(Handle, Handle), Box<dyn std::error::Error>> {
2828
let caller = user_address.parse().unwrap();
29-
let transaction_id = transaction_id.unwrap_or(next_random_handle(DEF_TYPE));
29+
let transaction_id = transaction_id.unwrap_or(new_transaction_id());
3030

3131
info!(target: "tool", "ERC20 Transaction: tx_id: {:?}", transaction_id);
3232

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
pub mod auction;
12
pub mod dex;
23
pub mod erc20;
4+
pub mod erc7984;
35
pub mod synthetics;
46
pub mod utils;
57
pub mod zk_gen;

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,46 @@ pub fn next_random_handle(ct_type: FheType) -> Handle {
6363
handle_hash.update(rand::rng().random::<u64>().to_be_bytes());
6464
let mut handle = handle_hash.finalize().to_vec();
6565
assert_eq!(handle.len(), 32);
66+
67+
// Mark it as a mocked handle
68+
handle[0..3].copy_from_slice(&[0u8; 3]);
69+
6670
// Handle from computation
6771
handle[21] = 255u8;
6872
handle[22..30].copy_from_slice(&ecfg.chain_id.to_be_bytes());
6973
handle[30] = ct_type as u8;
7074
handle[31] = 0u8;
7175
Handle::from_slice(&handle)
7276
}
77+
78+
pub fn new_transaction_id() -> Handle {
79+
let mut handle_hash = Keccak256::new();
80+
handle_hash.update(rand::rng().random::<u64>().to_be_bytes());
81+
let mut txn_id = handle_hash.finalize().to_vec();
82+
assert_eq!(txn_id.len(), 32);
83+
// Mark it as a mocked transaction id
84+
txn_id[0..11].copy_from_slice(&[0u8; 11]);
85+
Handle::from_slice(&txn_id)
86+
}
7387
pub fn default_dependence_cache_size() -> u16 {
7488
128
7589
}
7690

7791
#[derive(Debug, serde::Deserialize, serde::Serialize, Eq, PartialEq, Clone)]
7892
pub enum Transaction {
7993
ERC20Transfer,
94+
ERC7984Transfer,
8095
DEXSwapRequest,
8196
DEXSwapClaim,
8297
MULChain,
8398
ADDChain,
8499
InputVerif,
85100
GenPubDecHandles,
86101
GenUsrDecHandles,
102+
BatchAllowHandles,
103+
BatchSubmitEncryptedBids,
87104
}
105+
88106
#[derive(Debug, serde::Deserialize, serde::Serialize, Eq, PartialEq, Clone)]
89107
pub enum ERCTransferVariant {
90108
Whitepaper,
@@ -118,6 +136,7 @@ pub struct Scenario {
118136
pub contract_address: String,
119137
pub user_address: String,
120138
pub scenario: Vec<(f64, u64)>,
139+
pub batch_size: Option<usize>,
121140
}
122141

123142
pub struct Job {
@@ -131,6 +150,8 @@ pub struct Context {
131150
pub args: Args,
132151
pub ecfg: EnvConfig,
133152
pub cancel_token: tokio_util::sync::CancellationToken,
153+
// Pre-generated inputs pool
154+
pub inputs_pool: Vec<Option<Handle>>,
134155
}
135156

136157
#[allow(dead_code)]
@@ -174,6 +195,7 @@ pub async fn allow_handles(
174195
event_type: AllowEvents,
175196
account_address: String,
176197
pool: &sqlx::Pool<Postgres>,
198+
disable_pbs_computations: bool,
177199
) -> Result<(), Box<dyn std::error::Error>> {
178200
let ecfg = EnvConfig::new();
179201
let tenant_id = vec![ecfg.tenant_id; handles.len()];
@@ -190,6 +212,10 @@ pub async fn allow_handles(
190212
)
191213
.execute(pool)
192214
.await?;
215+
216+
if disable_pbs_computations {
217+
return Ok(());
218+
}
193219
let _query = sqlx::query!(
194220
"INSERT INTO pbs_computations(tenant_id, handle)
195221
SELECT * FROM UNNEST($1::INTEGER[], $2::BYTEA[])
@@ -423,3 +449,7 @@ pub async fn insert_tfhe_event(
423449
tracing::debug!(target: "tool", duration = ?started_at.elapsed(), "TFHE event, db_query");
424450
Ok(())
425451
}
452+
453+
pub async fn pool(listener_event_to_db: &ListenerDatabase) -> sqlx::Pool<Postgres> {
454+
listener_event_to_db.pool.clone().read().await.clone()
455+
}

0 commit comments

Comments
 (0)