Skip to content

Commit d9ed266

Browse files
committed
chore(coprocessor): add Transaction::BatchInputProofs scenario
1 parent 728d9d8 commit d9ed266

File tree

4 files changed

+105
-8
lines changed

4 files changed

+105
-8
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"transaction": "BatchInputProofs",
4+
"kind": "Rate",
5+
"variant": "NoCMUX",
6+
"inputs": "NA",
7+
"is_dependent": "Independent",
8+
"contract_address": "0xa5880e99d86F081E8D3868A8C4732C8f65dfdB08",
9+
"user_address": "0xa0534e99d86F081E8D3868A8C4732C8f65dfdB07",
10+
"batch_size": 100,
11+
"scenario": [
12+
[
13+
1.0,
14+
1
15+
]
16+
]
17+
}
18+
]

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::{
2121
};
2222
use stress_test_generator::{
2323
args::parse_args, dex::dex_swap_claim_transaction, utils::new_transaction_id,
24+
zk_gen::generate_and_insert_inputs_batch,
2425
};
2526
use stress_test_generator::{
2627
auction::batch_submit_encrypted_bids,
@@ -600,6 +601,19 @@ async fn generate_transaction(
600601
let ctx = &new_ctx;
601602

602603
match scenario.transaction {
604+
Transaction::BatchInputProofs => {
605+
let batch_size = scenario.batch_size.unwrap_or(1);
606+
generate_and_insert_inputs_batch(
607+
ctx,
608+
batch_size,
609+
MAX_NUMBER_OF_BIDS as u8,
610+
&scenario.contract_address,
611+
&scenario.user_address,
612+
)
613+
.await?;
614+
615+
Ok((Handle::default(), Handle::default()))
616+
}
603617
Transaction::BatchSubmitEncryptedBids => {
604618
let batch_size = min(MAX_NUMBER_OF_BIDS, scenario.batch_size.unwrap_or(1));
605619

@@ -620,8 +634,7 @@ async fn generate_transaction(
620634
)
621635
.await?;
622636

623-
// TODO: how to make dependent if needed?
624-
Ok((e_total_payment, e_total_payment)) // TODO: return meaningful second handle
637+
Ok((e_total_payment, e_total_payment))
625638
}
626639
Transaction::BatchAllowHandles => {
627640
let mut handles = Vec::new();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ pub enum Transaction {
101101
GenUsrDecHandles,
102102
BatchAllowHandles,
103103
BatchSubmitEncryptedBids,
104+
BatchInputProofs,
104105
}
105106

106107
#[derive(Debug, serde::Deserialize, serde::Serialize, Eq, PartialEq, Clone)]

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

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,28 @@ impl ZkData {
5757
}
5858
}
5959

60+
#[allow(clippy::too_many_arguments)]
6061
async fn insert_proof(
6162
pool: &sqlx::PgPool,
6263
request_id: i64,
63-
zk_pok: &[u8],
64-
aux: &ZkData,
64+
zk_pok: Vec<u8>,
65+
aux: ZkData,
6566
db_notify_channel: &str,
6667
transaction_id: Handle,
68+
retry_count: i32,
6769
_block_number: u64,
6870
) -> Result<(), sqlx::Error> {
6971
// Insert ZkPok into database
7072
sqlx::query(
71-
"INSERT INTO verify_proofs (zk_proof_id, input, chain_id, contract_address, user_address, verified, transaction_id)
72-
VALUES ($1, $2, $3, $4, $5, NULL, $6)"
73+
"INSERT INTO verify_proofs (zk_proof_id, input, chain_id, contract_address, user_address, verified, transaction_id, retry_count)
74+
VALUES ($1, $2, $3, $4, $5, NULL, $6, $7)"
7375
).bind(request_id)
7476
.bind(zk_pok)
7577
.bind(aux.chain_id)
7678
.bind(aux.contract_address.clone())
7779
.bind(aux.user_address.clone())
7880
.bind(transaction_id.to_vec())
81+
.bind(retry_count)
7982
.execute(pool).await?;
8083
sqlx::query("SELECT pg_notify($1, '')")
8184
.bind(db_notify_channel)
@@ -190,11 +193,12 @@ pub async fn generate_random_handle_vec(
190193
insert_proof(
191194
&pool,
192195
zk_id,
193-
&zk_pok,
194-
&zk_data,
196+
zk_pok,
197+
zk_data,
195198
&ctx.args.zkproof_notify_channel,
196199
transaction_id,
197200
0,
201+
0,
198202
)
199203
.await?;
200204

@@ -205,6 +209,67 @@ pub async fn generate_random_handle_vec(
205209
Ok(handles)
206210
}
207211

212+
pub async fn generate_and_insert_inputs_batch(
213+
ctx: &Context,
214+
batch_size: usize,
215+
inputs_count: u8,
216+
contract_address: &String,
217+
user_address: &String,
218+
) -> Result<(), Box<dyn std::error::Error>> {
219+
assert!(inputs_count <= 254);
220+
let ecfg = EnvConfig::new();
221+
let pool = sqlx::postgres::PgPoolOptions::new()
222+
.max_connections(20)
223+
.connect(&ecfg.evgen_db_url)
224+
.await
225+
.unwrap();
226+
227+
let (pks, public_params) = query_and_save_pks(ecfg.tenant_id, &pool).await?;
228+
let mut db_inserts = vec![];
229+
230+
// Generate a batch of zkpoks
231+
for idx in 0..batch_size {
232+
let transaction_id = next_random_handle(DEF_TYPE);
233+
234+
let zk_data = ZkData {
235+
contract_address: contract_address.to_owned(),
236+
user_address: user_address.to_owned(),
237+
acl_contract_address: ecfg.acl_contract_address.clone(),
238+
chain_id: ecfg.chain_id,
239+
};
240+
let aux_data = zk_data.to_owned().assemble()?;
241+
242+
let mut builder = tfhe::ProvenCompactCiphertextList::builder(&pks);
243+
for _ in 0..inputs_count {
244+
builder.push(rand::rng().random::<u64>());
245+
}
246+
let zk_id = ZK_PROOF_ID.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
247+
info!(target: "tool", "zkpok id: {}, count = {:?}, seq_num = {} of {} , txn: {:?}, ", zk_id, inputs_count, idx, batch_size, transaction_id );
248+
let the_list = builder
249+
.build_with_proof_packed(&public_params, &aux_data, tfhe::zk::ZkComputeLoad::Proof)
250+
.unwrap();
251+
252+
let zk_pok = fhevm_engine_common::utils::safe_serialize(&the_list);
253+
254+
// retry_count = 100 to ensure the txn-sender will delete it after first try
255+
// If not deleted, txn-sender will report too many VerifyProofNotRequested errors
256+
db_inserts.push(insert_proof(
257+
&pool,
258+
zk_id,
259+
zk_pok.clone(),
260+
zk_data,
261+
&ctx.args.zkproof_notify_channel,
262+
transaction_id,
263+
100,
264+
0,
265+
));
266+
}
267+
268+
futures::future::try_join_all(db_inserts).await?;
269+
270+
Ok(())
271+
}
272+
208273
pub async fn get_inputs_vector(
209274
ctx: &Context,
210275
in_type: Inputs,

0 commit comments

Comments
 (0)