Skip to content

Commit 05bcb00

Browse files
refactor: use configurable prover url for batch operations (#1831)
* refactor: use configurable prover url for batch operations * Update forester/src/epoch_manager.rs Co-authored-by: Swen Schäferjohann <42959314+SwenSchaeferjohann@users.noreply.github.com> --------- Co-authored-by: Swen Schäferjohann <42959314+SwenSchaeferjohann@users.noreply.github.com>
1 parent 9106a1d commit 05bcb00

File tree

7 files changed

+78
-41
lines changed

7 files changed

+78
-41
lines changed

forester-utils/src/instructions/address_batch_update.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::time::Duration;
2+
13
use account_compression::processor::initialize_address_merkle_tree::Pubkey;
24
use futures::future;
35
use light_batched_merkle_tree::{
@@ -27,6 +29,9 @@ pub async fn create_batch_update_address_tree_instruction_data<R, I>(
2729
rpc: &mut R,
2830
indexer: &mut I,
2931
merkle_tree_pubkey: &Pubkey,
32+
prover_url: String,
33+
polling_interval: Duration,
34+
max_wait_time: Duration,
3035
) -> Result<(Vec<InstructionDataBatchNullifyInputs>, u16), ForesterUtilsError>
3136
where
3237
R: Rpc,
@@ -253,7 +258,7 @@ where
253258
}
254259

255260
info!("Generating {} ZK proofs asynchronously", all_inputs.len());
256-
let proof_client = ProofClient::local();
261+
let proof_client = ProofClient::with_config(prover_url, polling_interval, max_wait_time);
257262
let proof_futures = all_inputs
258263
.into_iter()
259264
.map(|inputs| proof_client.generate_batch_address_append_proof(inputs));

forester-utils/src/instructions/state_batch_append.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::{sync::Arc, time::Duration};
2+
13
use account_compression::processor::initialize_address_merkle_tree::Pubkey;
24
use light_batched_merkle_tree::{
35
constants::DEFAULT_BATCH_STATE_TREE_HEIGHT,
@@ -22,6 +24,9 @@ pub async fn create_append_batch_ix_data<R: Rpc, I: Indexer>(
2224
indexer: &mut I,
2325
merkle_tree_pubkey: Pubkey,
2426
output_queue_pubkey: Pubkey,
27+
prover_url: String,
28+
polling_interval: Duration,
29+
max_wait_time: Duration,
2530
) -> Result<Vec<InstructionDataBatchAppendInputs>, ForesterUtilsError> {
2631
trace!("Creating append batch instruction data");
2732

@@ -84,6 +89,11 @@ pub async fn create_append_batch_ix_data<R: Rpc, I: Indexer>(
8489
let mut all_changelogs: Vec<ChangelogEntry<{ DEFAULT_BATCH_STATE_TREE_HEIGHT as usize }>> =
8590
Vec::new();
8691
let mut proof_futures = Vec::new();
92+
let proof_client = Arc::new(ProofClient::with_config(
93+
prover_url.clone(),
94+
polling_interval,
95+
max_wait_time,
96+
));
8797

8898
for (batch_idx, leaves_hash_chain) in leaves_hash_chains.iter().enumerate() {
8999
let start_idx = batch_idx * zkp_batch_size as usize;
@@ -134,7 +144,8 @@ pub async fn create_append_batch_ix_data<R: Rpc, I: Indexer>(
134144
bigint_to_be_bytes_array::<32>(&circuit_inputs.new_root.to_biguint().unwrap()).unwrap();
135145
all_changelogs.extend(batch_changelogs);
136146

137-
let proof_future = generate_zkp_proof(circuit_inputs);
147+
let client = Arc::clone(&proof_client);
148+
let proof_future = generate_zkp_proof(circuit_inputs, client);
138149

139150
proof_futures.push(proof_future);
140151
}
@@ -162,8 +173,8 @@ pub async fn create_append_batch_ix_data<R: Rpc, I: Indexer>(
162173
}
163174
async fn generate_zkp_proof(
164175
circuit_inputs: BatchAppendsCircuitInputs,
176+
proof_client: Arc<ProofClient>,
165177
) -> Result<(CompressedProof, [u8; 32]), ForesterUtilsError> {
166-
let proof_client = ProofClient::local();
167178
let (proof, new_root) = proof_client
168179
.generate_batch_append_proof(circuit_inputs)
169180
.await

forester-utils/src/instructions/state_batch_nullify.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::{sync::Arc, time::Duration};
2+
13
use account_compression::processor::initialize_address_merkle_tree::Pubkey;
24
use light_batched_merkle_tree::{
35
constants::DEFAULT_BATCH_STATE_TREE_HEIGHT,
@@ -19,6 +21,9 @@ pub async fn create_nullify_batch_ix_data<R: Rpc, I: Indexer>(
1921
rpc: &mut R,
2022
indexer: &mut I,
2123
merkle_tree_pubkey: Pubkey,
24+
prover_url: String,
25+
polling_interval: Duration,
26+
max_wait_time: Duration,
2227
) -> Result<Vec<InstructionDataBatchNullifyInputs>, ForesterUtilsError> {
2328
trace!("create_multiple_nullify_batch_ix_data");
2429
// Get the tree information and find out how many ZKP batches need processing
@@ -124,6 +129,11 @@ pub async fn create_nullify_batch_ix_data<R: Rpc, I: Indexer>(
124129

125130
let mut all_changelogs = Vec::new();
126131
let mut proof_futures = Vec::new();
132+
let proof_client = Arc::new(ProofClient::with_config(
133+
prover_url.clone(),
134+
polling_interval,
135+
max_wait_time,
136+
));
127137

128138
let mut current_root = old_root;
129139

@@ -205,48 +215,36 @@ pub async fn create_nullify_batch_ix_data<R: Rpc, I: Indexer>(
205215
ForesterUtilsError::Prover("Failed to convert new root to bytes".into())
206216
})?;
207217

208-
let proof_future = tokio::spawn(generate_nullify_zkp_proof(circuit_inputs));
218+
let client = Arc::clone(&proof_client);
219+
let proof_future = generate_nullify_zkp_proof(circuit_inputs, client);
209220
proof_futures.push(proof_future);
210221
}
211222

212-
// Wait for all proof generation to complete
213-
let mut results = Vec::new();
214-
215-
for (i, future) in futures::future::join_all(proof_futures)
216-
.await
217-
.into_iter()
218-
.enumerate()
219-
{
220-
match future {
221-
Ok(result) => match result {
222-
Ok((proof, new_root)) => {
223-
results.push(InstructionDataBatchNullifyInputs {
224-
new_root,
225-
compressed_proof: proof,
226-
});
227-
trace!("Successfully generated proof for batch {}", i);
228-
}
229-
Err(e) => {
230-
error!("Error generating proof for batch {}: {:?}", i, e);
231-
return Err(e);
232-
}
233-
},
223+
let proof_results = futures::future::join_all(proof_futures).await;
224+
let mut instruction_data_vec = Vec::new();
225+
226+
for (i, proof_result) in proof_results.into_iter().enumerate() {
227+
match proof_result {
228+
Ok((proof, new_root)) => {
229+
trace!("Successfully generated proof for batch {}", i);
230+
instruction_data_vec.push(InstructionDataBatchNullifyInputs {
231+
new_root,
232+
compressed_proof: proof,
233+
});
234+
}
234235
Err(e) => {
235-
error!("Task error for batch {}: {:?}", i, e);
236-
return Err(ForesterUtilsError::Prover(format!(
237-
"Task error for batch {}: {:?}",
238-
i, e
239-
)));
236+
error!("Failed to generate proof for batch {}: {:?}", i, e);
237+
return Err(e);
240238
}
241239
}
242240
}
243241

244-
Ok(results)
242+
Ok(instruction_data_vec)
245243
}
246244
async fn generate_nullify_zkp_proof(
247245
inputs: BatchUpdateCircuitInputs,
246+
proof_client: Arc<ProofClient>,
248247
) -> Result<(CompressedProof, [u8; 32]), ForesterUtilsError> {
249-
let proof_client = ProofClient::local();
250248
let (proof, new_root) = proof_client
251249
.generate_batch_update_proof(inputs)
252250
.await

forester/src/epoch_manager.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,14 @@ impl<R: Rpc, I: Indexer + IndexerType<R> + 'static> EpochManager<R, I> {
11301130
merkle_tree: tree_accounts.merkle_tree,
11311131
output_queue: tree_accounts.queue,
11321132
ixs_per_tx: self.config.transaction_config.batch_ixs_per_tx,
1133+
prover_url: self
1134+
.config
1135+
.external_services
1136+
.prover_url
1137+
.clone()
1138+
.unwrap_or_else(|| "http://127.0.0.1:3001".to_string()),
1139+
prover_polling_interval: Duration::from_secs(1),
1140+
prover_max_wait_time: Duration::from_secs(120),
11331141
};
11341142

11351143
process_batched_operations(batch_context, tree_accounts.tree_type)

forester/src/processor/v2/address.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pub(crate) async fn process_batch<R: Rpc, I: Indexer + IndexerType<R>>(
2323
&mut *rpc,
2424
&mut *context.indexer.lock().await,
2525
&context.merkle_tree,
26+
context.prover_url.clone(),
27+
context.prover_polling_interval,
28+
context.prover_max_wait_time,
2629
)
2730
.await
2831
.map_err(|e| {

forester/src/processor/v2/common.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::Arc;
1+
use std::{sync::Arc, time::Duration};
22

33
use forester_utils::rpc_pool::SolanaRpcPool;
44
use light_batched_merkle_tree::{
@@ -26,6 +26,9 @@ pub struct BatchContext<R: Rpc, I: Indexer> {
2626
pub merkle_tree: Pubkey,
2727
pub output_queue: Pubkey,
2828
pub ixs_per_tx: usize,
29+
pub prover_url: String,
30+
pub prover_polling_interval: Duration,
31+
pub prover_max_wait_time: Duration,
2932
}
3033

3134
#[derive(Debug)]

forester/src/processor/v2/state.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ pub(crate) async fn perform_append<R: Rpc, I: Indexer + IndexerType<R>>(
3636
&mut *context.indexer.lock().await,
3737
context.merkle_tree,
3838
context.output_queue,
39+
context.prover_url.clone(),
40+
context.prover_polling_interval,
41+
context.prover_max_wait_time,
3942
)
4043
.await
4144
.map_err(|e| {
@@ -141,13 +144,19 @@ pub(crate) async fn perform_nullify<R: Rpc, I: Indexer + IndexerType<R>>(
141144
rpc: &mut R,
142145
) -> Result<()> {
143146
let batch_index = get_batch_index(context, rpc).await?;
144-
let instruction_data_vec =
145-
create_nullify_batch_ix_data(rpc, &mut *context.indexer.lock().await, context.merkle_tree)
146-
.await
147-
.map_err(|e| {
148-
error!("Failed to create nullify batch instruction data: {}", e);
149-
BatchProcessError::InstructionData(e.to_string())
150-
})?;
147+
let instruction_data_vec = create_nullify_batch_ix_data(
148+
rpc,
149+
&mut *context.indexer.lock().await,
150+
context.merkle_tree,
151+
context.prover_url.clone(),
152+
context.prover_polling_interval,
153+
context.prover_max_wait_time,
154+
)
155+
.await
156+
.map_err(|e| {
157+
error!("Failed to create nullify batch instruction data: {}", e);
158+
BatchProcessError::InstructionData(e.to_string())
159+
})?;
151160

152161
if instruction_data_vec.is_empty() {
153162
debug!("No zkp batches to nullify");

0 commit comments

Comments
 (0)