Skip to content

Commit 79b4f8c

Browse files
entropidelicjotabulaciosMauroToscano
authored
fix(batcher): send correct responses to each client (#244)
Co-authored-by: jotabulacios <[email protected]> Co-authored-by: Mauro Toscano <[email protected]>
1 parent 969dd1d commit 79b4f8c

File tree

8 files changed

+288
-173
lines changed

8 files changed

+288
-173
lines changed

Makefile

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ anvil_start:
4848

4949
anvil_start_with_block_time:
5050
@echo "Starting Anvil..."
51-
anvil --load-state contracts/scripts/anvil/state/alignedlayer-deployed-anvil-state.json --block-time 2
51+
anvil --load-state contracts/scripts/anvil/state/alignedlayer-deployed-anvil-state.json --block-time 3
5252

5353
# TODO: Allow enviroment variables / different configuration files
5454
aggregator_start:
@@ -154,13 +154,13 @@ batcher_send_sp1_task:
154154
--vm_program test_files/sp1/sp1_fibonacci-elf \
155155
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657
156156

157-
batcher_send_sp1_burst_5:
157+
batcher_send_sp1_burst:
158158
@echo "Sending SP1 fibonacci task to Batcher..."
159159
@cd batcher/client/ && cargo run --release -- \
160160
--proving_system SP1 \
161161
--proof test_files/sp1/sp1_fibonacci.proof \
162162
--vm_program test_files/sp1/sp1_fibonacci-elf \
163-
--repetitions 5 \
163+
--repetitions 15 \
164164
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657
165165

166166
batcher_send_infinite_sp1:
@@ -176,6 +176,16 @@ batcher_send_plonk_bn254_task: batcher/client/target/release/batcher-client
176176
--vk test_files/plonk_bn254/plonk.vk \
177177
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657
178178

179+
batcher_send_plonk_bn254_burst: batcher/client/target/release/batcher-client
180+
@echo "Sending Groth16Bn254 1!=0 task to Batcher..."
181+
@cd batcher/client/ && cargo run --release -- \
182+
--proving_system GnarkPlonkBn254 \
183+
--proof test_files/plonk_bn254/plonk.proof \
184+
--public_input test_files/plonk_bn254/plonk_pub_input.pub \
185+
--vk test_files/plonk_bn254/plonk.vk \
186+
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657 \
187+
--repetitions 15
188+
179189
batcher_send_plonk_bls12_381_task: batcher/client/target/release/batcher-client
180190
@echo "Sending Groth16 BLS12-381 1!=0 task to Batcher..."
181191
@cd batcher/client/ && cargo run --release -- \
@@ -185,6 +195,16 @@ batcher_send_plonk_bls12_381_task: batcher/client/target/release/batcher-client
185195
--vk test_files/plonk_bls12_381/plonk.vk \
186196
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657
187197

198+
batcher_send_plonk_bls12_381_burst: batcher/client/target/release/batcher-client
199+
@echo "Sending Groth16 BLS12-381 1!=0 task to Batcher..."
200+
@cd batcher/client/ && cargo run --release -- \
201+
--proving_system GnarkPlonkBls12_381 \
202+
--proof test_files/plonk_bls12_381/plonk.proof \
203+
--public_input test_files/plonk_bls12_381/plonk_pub_input.pub \
204+
--vk test_files/plonk_bls12_381/plonk.vk \
205+
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657 \
206+
--repetitions 15
207+
188208

189209
batcher_send_groth16_bn254_task: batcher/client/target/release/batcher-client
190210
@echo "Sending Groth16Bn254 1!=0 task to Batcher..."
@@ -195,14 +215,14 @@ batcher_send_groth16_bn254_task: batcher/client/target/release/batcher-client
195215
--vk test_files/groth16/ineq_1_groth16.vk \
196216
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657
197217

198-
batcher_send_groth16_burst_5: batcher/client/target/release/batcher-client
218+
batcher_send_groth16_burst: batcher/client/target/release/batcher-client
199219
@echo "Sending Groth16Bn254 1!=0 task to Batcher..."
200220
@cd batcher/client/ && cargo run --release -- \
201221
--proving_system Groth16Bn254 \
202222
--proof test_files/groth16/ineq_1_groth16.proof \
203223
--public_input test_files/groth16/ineq_1_groth16.pub \
204224
--vk test_files/groth16/ineq_1_groth16.vk \
205-
--repetitions 5 \
225+
--repetitions 15 \
206226
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657
207227

208228
batcher_send_infinite_groth16: ./batcher/client/target/release/batcher-client ## Send a different Groth16 BN254 proof using the task sender every 3 seconds

batcher/client/src/main.rs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
use std::path::PathBuf;
1+
use std::{path::PathBuf, sync::Arc, time::Duration};
22

33
use alloy_primitives::Address;
44
use env_logger::Env;
5-
use futures_util::{future, SinkExt, StreamExt, TryStreamExt};
6-
use log::{info};
7-
use tokio_tungstenite::connect_async;
5+
use futures_util::{
6+
future,
7+
stream::{SplitSink, SplitStream},
8+
SinkExt, StreamExt, TryStreamExt,
9+
};
10+
use log::info;
11+
use tokio::{net::TcpStream, sync::Mutex};
12+
use tokio_tungstenite::{connect_async, MaybeTlsStream, WebSocketStream};
813

9-
use batcher::types::{parse_proving_system, VerificationData};
14+
use batcher::types::{parse_proving_system, BatchInclusionData, VerificationData};
1015

1116
use clap::Parser;
17+
use tungstenite::Message;
1218

1319
#[derive(Parser, Debug)]
1420
#[command(version, about, long_about = None)]
@@ -48,15 +54,14 @@ struct Args {
4854
long = "repetitions",
4955
default_value = "1"
5056
)]
51-
repetitions: u32,
57+
repetitions: usize,
5258

5359
#[arg(
5460
name = "Proof generator address",
5561
long = "proof_generator_addr",
5662
default_value = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
5763
)] // defaults to anvil address 1
5864
proof_generator_addr: String,
59-
6065
}
6166

6267
#[tokio::main]
@@ -99,7 +104,8 @@ async fn main() {
99104
info!("No VM program code file provided, continuing without VM program code...");
100105
}
101106

102-
let proof_generator_addr: Address = Address::parse_checksummed(&args.proof_generator_addr, None).unwrap();
107+
let proof_generator_addr: Address =
108+
Address::parse_checksummed(&args.proof_generator_addr, None).unwrap();
103109

104110
let verification_data = VerificationData {
105111
proving_system,
@@ -112,23 +118,41 @@ async fn main() {
112118

113119
let json_data = serde_json::to_string(&verification_data).expect("Failed to serialize task");
114120
for _ in 0..args.repetitions {
121+
// NOTE(marian): This sleep is only for ease of testing interactions between client and batcher,
122+
// it can be removed.
123+
std::thread::sleep(Duration::from_millis(500));
115124
ws_write
116125
.send(tungstenite::Message::Text(json_data.to_string()))
117126
.await
118127
.unwrap();
128+
info!("Message sent...")
119129
}
120130

131+
let num_responses = Arc::new(Mutex::new(0));
132+
let ws_write = Arc::new(Mutex::new(ws_write));
133+
134+
receive(ws_read, ws_write, args.repetitions, num_responses).await;
135+
}
136+
137+
async fn receive(
138+
ws_read: SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>,
139+
ws_write: Arc<Mutex<SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>>>,
140+
total_messages: usize,
141+
num_responses: Arc<Mutex<usize>>,
142+
) {
121143
ws_read
122-
.try_filter(|msg| future::ready(msg.is_text()))
123-
.for_each(|msg| async move {
124-
let data = msg.unwrap().into_text().unwrap();
125-
info!("Batch merkle root received: {}", data);
144+
.try_filter(|msg| future::ready(msg.is_text() || msg.is_binary()))
145+
.for_each(|msg| async {
146+
let mut num_responses_lock = num_responses.lock().await;
147+
*num_responses_lock += 1;
148+
let data = msg.unwrap().into_data();
149+
let deserialized_data: BatchInclusionData = serde_json::from_slice(&data).unwrap();
150+
info!("Batcher response received: {}", deserialized_data);
151+
152+
if *num_responses_lock == total_messages {
153+
info!("All messages responded. Closing connection...");
154+
ws_write.lock().await.close().await.unwrap();
155+
}
126156
})
127157
.await;
128-
129-
info!("Closing connection...");
130-
ws_write
131-
.close()
132-
.await
133-
.expect("Failed to close WebSocket connection");
134158
}

batcher/src/eth/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub async fn get_contract(
4141
}
4242

4343
pub async fn create_new_task(
44-
service_manager: AlignedLayerServiceManager,
44+
service_manager: &AlignedLayerServiceManager,
4545
batch_merkle_root: [u8; 32],
4646
batch_data_pointer: String,
4747
) -> Result<TransactionReceipt, anyhow::Error> {

0 commit comments

Comments
 (0)