Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions forester/src/processor/v2/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,30 @@ impl StreamingAddressQueue {
hashchain_idx: usize,
) -> crate::Result<Option<AddressBatchSnapshot<HEIGHT>>> {
let available = self.wait_for_batch(end);
if start >= available {
if available < end || start >= end {
return Ok(None);
}
let actual_end = end.min(available);
let actual_end = end;
let data = lock_recover(&self.data, "streaming_address_queue.data");

let min_len = [
data.addresses.len(),
data.low_element_values.len(),
data.low_element_next_values.len(),
data.low_element_indices.len(),
data.low_element_next_indices.len(),
]
.into_iter()
.min()
.unwrap_or(0);
if min_len < actual_end {
return Err(anyhow!(
"incomplete batch data: min field length {} < required end {}",
min_len,
actual_end
));
}

let addresses = data.addresses[start..actual_end].to_vec();
if addresses.is_empty() {
return Err(anyhow!("Empty batch at start={}", start));
Expand Down Expand Up @@ -528,7 +546,11 @@ impl StreamingAddressQueue {
low_element_next_values: data.low_element_next_values[start..actual_end].to_vec(),
low_element_indices: data.low_element_indices[start..actual_end].to_vec(),
low_element_next_indices: data.low_element_next_indices[start..actual_end].to_vec(),
low_element_proofs: data.reconstruct_proofs::<HEIGHT>(start..actual_end)?,
low_element_proofs: data
.reconstruct_proofs::<HEIGHT>(start..actual_end)
.map_err(|error| {
anyhow!("incomplete batch data: failed to reconstruct proofs: {error}")
})?,
addresses,
leaves_hashchain,
}))
Expand Down Expand Up @@ -566,6 +588,10 @@ impl StreamingAddressQueue {
lock_recover(&self.data, "streaming_address_queue.data").start_index
}

pub fn tree_next_insertion_index(&self) -> u64 {
lock_recover(&self.data, "streaming_address_queue.data").tree_next_insertion_index
}

pub fn subtrees(&self) -> Vec<[u8; 32]> {
lock_recover(&self.data, "streaming_address_queue.data")
.subtrees
Expand Down
2 changes: 1 addition & 1 deletion forester/src/processor/v2/strategy/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl<R: Rpc> TreeStrategy<R> for AddressTreeStrategy {
}

let initial_root = streaming_queue.initial_root();
let start_index = streaming_queue.start_index();
let start_index = streaming_queue.tree_next_insertion_index();

let subtrees_arr: [[u8; 32]; DEFAULT_BATCH_ADDRESS_TREE_HEIGHT as usize] =
subtrees.try_into().map_err(|v: Vec<[u8; 32]>| {
Expand Down
2 changes: 1 addition & 1 deletion program-tests/utils/src/e2e_test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ where
// // local_leaves_hash_chain is only used for a test assertion.
// let local_nullifier_hash_chain = create_hash_chain_from_array(&addresses);
// assert_eq!(leaves_hash_chain, local_nullifier_hash_chain);
let start_index = address_queue.start_index as usize;
let start_index = address_queue.tree_next_insertion_index as usize;
assert!(
start_index >= 2,
"start index should be greater than 2 else tree is not inited"
Expand Down
11 changes: 6 additions & 5 deletions program-tests/utils/src/mock_batched_forester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ impl<const HEIGHT: usize> MockBatchedForester<HEIGHT> {

assert_eq!(computed_new_root, self.merkle_tree.root());

let proof_result = match ProofClient::local()
let proof_client = ProofClient::local();
let proof_result = match proof_client
.generate_batch_append_proof(circuit_inputs)
.await
{
Expand Down Expand Up @@ -207,9 +208,8 @@ impl<const HEIGHT: usize> MockBatchedForester<HEIGHT> {
batch_size,
&[],
)?;
let proof_result = ProofClient::local()
.generate_batch_update_proof(inputs)
.await?;
let proof_client = ProofClient::local();
let proof_result = proof_client.generate_batch_update_proof(inputs).await?;
let new_root = self.merkle_tree.root();
let proof = CompressedProof {
a: proof_result.0.proof.a,
Expand Down Expand Up @@ -318,7 +318,8 @@ impl<const HEIGHT: usize> MockBatchedAddressForester<HEIGHT> {
)));
}
};
let proof_result = match ProofClient::local()
let proof_client = ProofClient::local();
let proof_result = match proof_client
.generate_batch_address_append_proof(inputs)
.await
{
Expand Down
2 changes: 1 addition & 1 deletion program-tests/utils/src/test_batch_forester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ pub async fn create_batch_update_address_tree_instruction_data_with_proof<R: Rpc
// // local_leaves_hash_chain is only used for a test assertion.
// let local_nullifier_hash_chain = create_hash_chain_from_slice(addresses.as_slice()).unwrap();
// assert_eq!(leaves_hash_chain, local_nullifier_hash_chain);
let start_index = address_queue.start_index as usize;
let start_index = address_queue.tree_next_insertion_index as usize;
assert!(
start_index >= 1,
"start index should be greater than 2 else tree is not inited"
Expand Down
1 change: 0 additions & 1 deletion prover/client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub enum ProverClientError {

#[error("Integer conversion failed: {0}")]
IntegerConversion(String),

#[error("Hashchain mismatch: computed {computed:?} != expected {expected:?} (batch_size={batch_size}, next_index={next_index})")]
HashchainMismatch {
computed: [u8; 32],
Expand Down
4 changes: 2 additions & 2 deletions prover/client/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ pub fn bigint_to_u8_32(n: &BigInt) -> Result<[u8; 32], Box<dyn std::error::Error
pub fn compute_root_from_merkle_proof<const HEIGHT: usize>(
leaf: [u8; 32],
path_elements: &[[u8; 32]; HEIGHT],
path_index: u32,
path_index: usize,
) -> Result<([u8; 32], ChangelogEntry<HEIGHT>), ProverClientError> {
let mut changelog_entry = ChangelogEntry::default_with_index(path_index as usize);
let mut changelog_entry = ChangelogEntry::default_with_index(path_index);

let mut current_hash = leaf;
let mut current_index = path_index;
Expand Down
13 changes: 6 additions & 7 deletions prover/client/src/proof_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio::time::sleep;
use tracing::{debug, error, info, trace, warn};

use crate::{
constants::PROVE_PATH,
constants::{PROVE_PATH, SERVER_ADDRESS},
errors::ProverClientError,
proof::{
compress_proof, deserialize_gnark_proof_json, proof_from_json_struct, ProofCompressed,
Expand All @@ -17,14 +17,13 @@ use crate::{
batch_append::{BatchAppendInputsJson, BatchAppendsCircuitInputs},
batch_update::{update_inputs_string, BatchUpdateCircuitInputs},
},
prover::build_http_client,
};

const MAX_RETRIES: u32 = 10;
const BASE_RETRY_DELAY_SECS: u64 = 1;
const DEFAULT_POLLING_INTERVAL_MS: u64 = 100;
const DEFAULT_MAX_WAIT_TIME_SECS: u64 = 600;
const DEFAULT_LOCAL_SERVER: &str = "http://localhost:3001";

const INITIAL_POLL_DELAY_SMALL_CIRCUIT_MS: u64 = 200;
const INITIAL_POLL_DELAY_LARGE_CIRCUIT_MS: u64 = 200;

Expand Down Expand Up @@ -70,8 +69,8 @@ pub struct ProofClient {
impl ProofClient {
pub fn local() -> Self {
Self {
client: Client::new(),
server_address: DEFAULT_LOCAL_SERVER.to_string(),
client: build_http_client(),
server_address: SERVER_ADDRESS.to_string(),
polling_interval: Duration::from_millis(DEFAULT_POLLING_INTERVAL_MS),
max_wait_time: Duration::from_secs(DEFAULT_MAX_WAIT_TIME_SECS),
api_key: None,
Expand All @@ -93,7 +92,7 @@ impl ProofClient {
};

Self {
client: Client::new(),
client: build_http_client(),
server_address,
polling_interval,
max_wait_time,
Expand All @@ -111,7 +110,7 @@ impl ProofClient {
initial_poll_delay: Duration,
) -> Self {
Self {
client: Client::new(),
client: build_http_client(),
server_address,
polling_interval,
max_wait_time,
Expand Down
22 changes: 11 additions & 11 deletions prover/client/src/proof_types/batch_address_append/proof_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ pub fn get_batch_address_append_circuit_inputs<const HEIGHT: usize>(
}
let new_element_values = &new_element_values[..zkp_batch_size];
let mut new_root = [0u8; 32];
let mut low_element_circuit_merkle_proofs = Vec::with_capacity(new_element_values.len());
let mut new_element_circuit_merkle_proofs = Vec::with_capacity(new_element_values.len());
let mut patched_low_element_next_values = Vec::with_capacity(new_element_values.len());
let mut patched_low_element_next_indices = Vec::with_capacity(new_element_values.len());
let mut patched_low_element_values = Vec::with_capacity(new_element_values.len());
let mut patched_low_element_indices = Vec::with_capacity(new_element_values.len());
let mut low_element_circuit_merkle_proofs = Vec::with_capacity(zkp_batch_size);
let mut new_element_circuit_merkle_proofs = Vec::with_capacity(zkp_batch_size);
let mut patched_low_element_next_values = Vec::with_capacity(zkp_batch_size);
let mut patched_low_element_next_indices = Vec::with_capacity(zkp_batch_size);
let mut patched_low_element_values = Vec::with_capacity(zkp_batch_size);
let mut patched_low_element_indices = Vec::with_capacity(zkp_batch_size);

let computed_hashchain = create_hash_chain_from_slice(new_element_values).map_err(|e| {
ProverClientError::GenericError(format!("Failed to compute hashchain: {}", e))
Expand Down Expand Up @@ -261,7 +261,7 @@ pub fn get_batch_address_append_circuit_inputs<const HEIGHT: usize>(
let is_first_batch = indexed_changelog.is_empty();
let mut expected_root_for_low = current_root;

for i in 0..new_element_values.len() {
for i in 0..zkp_batch_size {
let mut changelog_index = 0;
let low_element_index = low_element_indices[i].try_into().map_err(|_| {
ProverClientError::IntegerConversion(format!(
Expand Down Expand Up @@ -342,7 +342,7 @@ pub fn get_batch_address_append_circuit_inputs<const HEIGHT: usize>(
let (computed_root, _) = compute_root_from_merkle_proof::<HEIGHT>(
old_low_leaf_hash,
&merkle_proof,
low_element.index as u32,
low_element.index,
)?;
if computed_root != expected_root_for_low {
let low_value_bytes = bigint_to_be_bytes_array::<32>(&low_element.value)
Expand Down Expand Up @@ -383,7 +383,7 @@ pub fn get_batch_address_append_circuit_inputs<const HEIGHT: usize>(
compute_root_from_merkle_proof::<HEIGHT>(
new_low_leaf_hash,
&merkle_proof,
new_low_element.index as u32,
new_low_element.index,
)?;

patcher.push_changelog_entry::<HEIGHT>(changelog, changelog_entry);
Expand Down Expand Up @@ -424,7 +424,7 @@ pub fn get_batch_address_append_circuit_inputs<const HEIGHT: usize>(
let (updated_root, changelog_entry) = compute_root_from_merkle_proof(
new_element_leaf_hash,
&merkle_proof_array,
current_index as u32,
current_index,
)?;

if i == 0 && changelog.len() == 1 {
Expand All @@ -451,7 +451,7 @@ pub fn get_batch_address_append_circuit_inputs<const HEIGHT: usize>(
let (root_with_zero, _) = compute_root_from_merkle_proof::<HEIGHT>(
zero_hash,
&merkle_proof_array,
current_index as u32,
current_index,
)?;
if root_with_zero != intermediate_root {
tracing::error!(
Expand Down
2 changes: 1 addition & 1 deletion prover/client/src/proof_types/batch_append/proof_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub fn get_batch_append_inputs<const HEIGHT: usize>(
let (updated_root, changelog_entry) = compute_root_from_merkle_proof(
final_leaf,
&merkle_proof_array,
start_index + i as u32,
start_index as usize + i,
)?;
new_root = updated_root;
changelog.push(changelog_entry);
Expand Down
2 changes: 1 addition & 1 deletion prover/client/src/proof_types/batch_update/proof_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub fn get_batch_update_inputs<const HEIGHT: usize>(
index_bytes[28..].copy_from_slice(&(*index).to_be_bytes());
let nullifier = Poseidon::hashv(&[leaf, &index_bytes, &tx_hashes[i]]).unwrap();
let (root, changelog_entry) =
compute_root_from_merkle_proof(nullifier, &merkle_proof_array, *index)?;
compute_root_from_merkle_proof(nullifier, &merkle_proof_array, *index as usize)?;
new_root = root;
changelog.push(changelog_entry);
circuit_merkle_proofs.push(
Expand Down
Loading
Loading