Skip to content

Commit 5f60731

Browse files
authored
refactor: get_queue_elements (#1595)
* refactor: get_queue_elements * chore: fix test * fix: batch address test * fix: account compression tests
1 parent 051fe83 commit 5f60731

File tree

16 files changed

+350
-204
lines changed

16 files changed

+350
-204
lines changed

forester-utils/src/instructions.rs

Lines changed: 42 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ where
5959
})?
6060
.unwrap();
6161

62-
let (leaves_hash_chain, start_index, current_root, batch_size, full_batch_index) = {
62+
let (leaves_hash_chain, start_index, current_root, batch_size) = {
6363
let merkle_tree = BatchedMerkleTreeAccount::address_from_bytes(
6464
merkle_tree_account.data.as_mut_slice(),
6565
&merkle_tree_pubkey.into(),
@@ -75,13 +75,7 @@ where
7575
let current_root = *merkle_tree.root_history.last().unwrap();
7676
let batch_size = batch.zkp_batch_size as usize;
7777

78-
(
79-
leaves_hash_chain,
80-
start_index,
81-
current_root,
82-
batch_size,
83-
full_batch_index,
84-
)
78+
(leaves_hash_chain, start_index, current_root, batch_size)
8579
};
8680

8781
let batch_start_index = indexer
@@ -96,9 +90,8 @@ where
9690
let addresses = indexer
9791
.get_queue_elements(
9892
merkle_tree_pubkey.to_bytes(),
99-
full_batch_index,
100-
0,
10193
batch_size as u64,
94+
None
10295
)
10396
.await
10497
.map_err(|e| {
@@ -115,7 +108,7 @@ where
115108
let non_inclusion_proofs = indexer
116109
.get_multiple_new_address_proofs_h40(
117110
merkle_tree_pubkey.to_bytes(),
118-
addresses.clone(),
111+
addresses.iter().map(|x|x.account_hash).collect(),
119112
)
120113
.await
121114
.map_err(|e| {
@@ -151,6 +144,10 @@ where
151144
})?
152145
.try_into()
153146
.unwrap();
147+
let addresses = addresses
148+
.iter()
149+
.map(|x| x.account_hash)
150+
.collect::<Vec<[u8; 32]>>();
154151

155152
let inputs =
156153
get_batch_address_append_circuit_inputs::<{ DEFAULT_BATCH_ADDRESS_TREE_HEIGHT as usize }>(
@@ -227,7 +224,7 @@ pub async fn create_append_batch_ix_data<R: RpcConnection, I: Indexer<R>>(
227224
)
228225
};
229226

230-
let (zkp_batch_size, full_batch_index, num_inserted_zkps, leaves_hash_chain) = {
227+
let (zkp_batch_size, leaves_hash_chain) = {
231228
let mut output_queue_account = rpc.get_account(output_queue_pubkey).await.unwrap().unwrap();
232229
let output_queue =
233230
BatchedQueueAccount::output_from_bytes(output_queue_account.data.as_mut_slice())
@@ -242,41 +239,27 @@ pub async fn create_append_batch_ix_data<R: RpcConnection, I: Indexer<R>>(
242239
let leaves_hash_chain =
243240
output_queue.hash_chain_stores[full_batch_index as usize][num_inserted_zkps as usize];
244241

245-
(
246-
zkp_batch_size,
247-
full_batch_index,
248-
num_inserted_zkps,
249-
leaves_hash_chain,
250-
)
242+
(zkp_batch_size, leaves_hash_chain)
251243
};
252-
let start = num_inserted_zkps as usize * zkp_batch_size as usize;
253-
let end = start + zkp_batch_size as usize;
254244

255-
let leaves = indexer
256-
.get_queue_elements(
257-
merkle_tree_pubkey.to_bytes(),
258-
full_batch_index,
259-
start as u64,
260-
end as u64,
261-
)
245+
let indexer_response = indexer
246+
.get_queue_elements(output_queue_pubkey.to_bytes(), zkp_batch_size, None)
262247
.await
263248
.unwrap();
264249

265-
info!("Leaves: {:?}", leaves);
266-
267-
let (old_leaves, merkle_proofs) = {
268-
let mut old_leaves = vec![];
269-
let mut merkle_proofs = vec![];
270-
let indices =
271-
(merkle_tree_next_index..merkle_tree_next_index + zkp_batch_size).collect::<Vec<_>>();
272-
let proofs = indexer.get_proofs_by_indices(merkle_tree_pubkey, &indices);
273-
proofs.iter().for_each(|proof| {
274-
old_leaves.push(proof.leaf);
275-
merkle_proofs.push(proof.proof.clone());
276-
});
277-
278-
(old_leaves, merkle_proofs)
279-
};
250+
println!("indexer_response: {:?}", indexer_response);
251+
let old_leaves = indexer_response
252+
.iter()
253+
.map(|x| x.leaf)
254+
.collect::<Vec<[u8; 32]>>();
255+
let leaves = indexer_response
256+
.iter()
257+
.map(|x| x.account_hash)
258+
.collect::<Vec<[u8; 32]>>();
259+
let merkle_proofs = indexer_response
260+
.iter()
261+
.map(|x| x.proof.clone())
262+
.collect::<Vec<Vec<[u8; 32]>>>();
280263

281264
info!("Old leaves: {:?}", old_leaves);
282265

@@ -356,8 +339,11 @@ pub async fn create_nullify_batch_ix_data<R: RpcConnection, I: Indexer<R>>(
356339
(zkp_size, root, hash_chain)
357340
};
358341

359-
let leaf_indices_tx_hashes =
360-
indexer.get_leaf_indices_tx_hashes(merkle_tree_pubkey, zkp_batch_size as usize);
342+
let leaf_indices_tx_hashes = indexer
343+
.get_queue_elements(merkle_tree_pubkey.to_bytes(), zkp_batch_size, None)
344+
.await
345+
.unwrap();
346+
println!("leaf_indices_tx_hashes {:?}", leaf_indices_tx_hashes);
361347

362348
let mut leaves = Vec::new();
363349
let mut tx_hashes = Vec::new();
@@ -366,23 +352,19 @@ pub async fn create_nullify_batch_ix_data<R: RpcConnection, I: Indexer<R>>(
366352
let mut merkle_proofs = Vec::new();
367353
let mut nullifiers = Vec::new();
368354

369-
let proofs = indexer.get_proofs_by_indices(
370-
merkle_tree_pubkey,
371-
&leaf_indices_tx_hashes
372-
.iter()
373-
.map(|leaf_info| leaf_info.leaf_index as u64)
374-
.collect::<Vec<_>>(),
375-
);
376-
377-
for (leaf_info, proof) in leaf_indices_tx_hashes.iter().zip(proofs.iter()) {
378-
path_indices.push(leaf_info.leaf_index);
379-
leaves.push(leaf_info.leaf);
380-
old_leaves.push(proof.leaf);
381-
merkle_proofs.push(proof.proof.clone());
382-
tx_hashes.push(leaf_info.tx_hash);
355+
for leaf_info in leaf_indices_tx_hashes.iter() {
356+
path_indices.push(leaf_info.leaf_index as u32);
357+
leaves.push(leaf_info.account_hash);
358+
old_leaves.push(leaf_info.leaf);
359+
merkle_proofs.push(leaf_info.proof.clone());
360+
tx_hashes.push(leaf_info.tx_hash.unwrap());
383361
let index_bytes = leaf_info.leaf_index.to_be_bytes();
384-
let nullifier =
385-
Poseidon::hashv(&[&leaf_info.leaf, &index_bytes, &leaf_info.tx_hash]).unwrap();
362+
let nullifier = Poseidon::hashv(&[
363+
&leaf_info.account_hash,
364+
&index_bytes,
365+
&leaf_info.tx_hash.unwrap(),
366+
])
367+
.unwrap();
386368
nullifiers.push(nullifier);
387369
}
388370

forester/src/batch_processor/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ impl<R: RpcConnection, I: Indexer<R> + IndexerType<R>> BatchProcessor<R, I> {
158158

159159
async fn process_state_append(&self) -> Result<usize> {
160160
let mut rpc = self.context.rpc_pool.get_connection().await?;
161-
let (num_inserted_zkps, zkp_batch_size) = self.get_num_inserted_zkps(&mut rpc).await?;
162-
state::perform_append(&self.context, &mut rpc, num_inserted_zkps).await?;
161+
let (_, zkp_batch_size) = self.get_num_inserted_zkps(&mut rpc).await?;
162+
state::perform_append(&self.context, &mut rpc).await?;
163163
Ok(zkp_batch_size)
164164
}
165165

forester/src/batch_processor/state.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use crate::{
1818
pub(crate) async fn perform_append<R: RpcConnection, I: Indexer<R> + IndexerType<R>>(
1919
context: &BatchContext<R, I>,
2020
rpc: &mut R,
21-
num_inserted_zkps: u64,
2221
) -> Result<()> {
2322
let instruction_data = create_append_batch_ix_data(
2423
rpc,
@@ -53,7 +52,6 @@ pub(crate) async fn perform_append<R: RpcConnection, I: Indexer<R> + IndexerType
5352
context.indexer.clone(),
5453
context.merkle_tree,
5554
context.output_queue,
56-
num_inserted_zkps,
5755
)
5856
.await
5957
.expect("Failed to update test indexer after append");

forester/src/indexer_type.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ pub trait IndexerType<R: RpcConnection>: sealed::Sealed {
6969
indexer: &mut impl Indexer<R>,
7070
merkle_tree_pubkey: Pubkey,
7171
output_queue: Pubkey,
72-
num_inserted_zkps: u64,
7372
) where
7473
Self: Sized;
7574
}
@@ -150,18 +149,12 @@ impl<R: RpcConnection + light_client::rpc::merkle_tree::MerkleTreeExt> IndexerTy
150149
indexer: &mut impl Indexer<R>,
151150
merkle_tree_pubkey: Pubkey,
152151
output_queue: Pubkey,
153-
num_inserted_zkps: u64,
154152
) where
155153
Self: Sized,
156154
{
157155
if let Some(test_indexer) = (indexer as &mut dyn Any).downcast_mut::<TestIndexer<R>>() {
158156
test_indexer
159-
.update_test_indexer_after_append(
160-
rpc,
161-
merkle_tree_pubkey,
162-
output_queue,
163-
num_inserted_zkps,
164-
)
157+
.update_test_indexer_after_append(rpc, merkle_tree_pubkey, output_queue)
165158
.await;
166159
}
167160
}
@@ -209,7 +202,6 @@ impl<R: RpcConnection> IndexerType<R> for PhotonIndexer<R> {
209202
_indexer: &mut impl Indexer<R>,
210203
_merkle_tree_pubkey: Pubkey,
211204
_output_queue: Pubkey,
212-
_num_inserted_zkps: u64,
213205
) {
214206
// No-op for production indexer
215207
}
@@ -328,14 +320,12 @@ pub async fn update_test_indexer_after_append<R: RpcConnection, I: Indexer<R> +
328320
indexer: Arc<Mutex<I>>,
329321
merkle_tree_pubkey: Pubkey,
330322
output_queue: Pubkey,
331-
num_inserted_zkps: u64,
332323
) -> Result<(), ForesterError> {
333324
I::update_test_indexer_after_append(
334325
&mut *rpc,
335326
&mut *indexer.lock().await,
336327
merkle_tree_pubkey,
337328
output_queue,
338-
num_inserted_zkps,
339329
)
340330
.await;
341331

program-libs/batched-merkle-tree/tests/merkle_tree.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ pub async fn perform_address_update(
12731273
&mt_pubkey,
12741274
)
12751275
.unwrap();
1276-
let (input_res, new_root, pre_next_full_batch) = {
1276+
let (input_res, new_root, _pre_next_full_batch) = {
12771277
let mut account =
12781278
BatchedMerkleTreeAccount::address_from_bytes(mt_account_data, &mt_pubkey).unwrap();
12791279

@@ -1285,7 +1285,9 @@ pub async fn perform_address_update(
12851285
.batches
12861286
.get(next_full_batch as usize)
12871287
.unwrap();
1288-
let batch_start_index = batch.start_index;
1288+
let batch_start_index =
1289+
batch.start_index + batch.get_num_inserted_zkps() * batch.zkp_batch_size;
1290+
println!("batch start index {}", batch_start_index);
12891291
let leaves_hash_chain = account
12901292
.hash_chain_stores
12911293
.get(next_full_batch as usize)
@@ -1298,12 +1300,14 @@ pub async fn perform_address_update(
12981300
account.get_metadata().queue_batches.batch_size as u32,
12991301
account.get_metadata().queue_batches.zkp_batch_size as u32,
13001302
*leaves_hash_chain,
1301-
next_index as usize,
1303+
next_index as usize, // % batch.zkp_batch_size as usize
13021304
batch_start_index as usize,
13031305
*current_root,
13041306
)
13051307
.await
13061308
.unwrap();
1309+
1310+
mock_indexer.finalize_batch_address_update(10);
13071311
let instruction_data = InstructionDataBatchNullifyInputs {
13081312
new_root,
13091313
compressed_proof: CompressedProof {
@@ -1334,16 +1338,6 @@ pub async fn perform_address_update(
13341338
let account =
13351339
BatchedMerkleTreeAccount::address_from_bytes(mt_account_data, &mt_pubkey).unwrap();
13361340

1337-
{
1338-
let batch = account
1339-
.queue_batches
1340-
.batches
1341-
.get(pre_next_full_batch as usize)
1342-
.unwrap();
1343-
if batch.get_state() == BatchState::Inserted {
1344-
mock_indexer.finalize_batch_address_update(batch.batch_size as usize);
1345-
}
1346-
}
13471341
assert_address_merkle_tree_update(old_account, account, new_root);
13481342
}
13491343

program-tests/account-compression-test/tests/batched_merkle_tree_test.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,7 @@ async fn test_batch_address_merkle_trees() {
16991699
)
17001700
.await
17011701
.unwrap();
1702+
mock_indexer.finalize_batch_address_update(10);
17021703
}
17031704
// 5. Failing: invalid proof
17041705
// 6. Failing: invalid new root
@@ -2027,7 +2028,8 @@ pub async fn update_batch_address_tree(
20272028
.batches
20282029
.get(next_full_batch as usize)
20292030
.unwrap();
2030-
let batch_start_index = batch.start_index;
2031+
let batch_start_index =
2032+
batch.start_index + batch.get_num_inserted_zkps() * batch.zkp_batch_size;
20312033
let leaves_hash_chain = zero_copy_account
20322034
.hash_chain_stores
20332035
.get(next_full_batch as usize)

program-tests/registry-test/tests/tests.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,18 +1918,18 @@ async fn test_batch_address_tree() {
19181918
)
19191919
.await
19201920
.unwrap();
1921+
let mut account = rpc
1922+
.get_account(env.batch_address_merkle_tree)
1923+
.await
1924+
.unwrap()
1925+
.unwrap();
1926+
test_indexer
1927+
.finalize_batched_address_tree_update(
1928+
env.batch_address_merkle_tree,
1929+
account.data.as_mut_slice(),
1930+
)
1931+
.await;
19211932
}
1922-
let mut account = rpc
1923-
.get_account(env.batch_address_merkle_tree)
1924-
.await
1925-
.unwrap()
1926-
.unwrap();
1927-
test_indexer
1928-
.finalize_batched_address_tree_update(
1929-
env.batch_address_merkle_tree,
1930-
account.data.as_mut_slice(),
1931-
)
1932-
.await;
19331933
}
19341934

19351935
// Non eligible forester.
@@ -1966,6 +1966,17 @@ async fn test_batch_address_tree() {
19661966
)
19671967
.await
19681968
.unwrap();
1969+
let mut account = rpc
1970+
.get_account(env.batch_address_merkle_tree)
1971+
.await
1972+
.unwrap()
1973+
.unwrap();
1974+
test_indexer
1975+
.finalize_batched_address_tree_update(
1976+
env.batch_address_merkle_tree,
1977+
account.data.as_mut_slice(),
1978+
)
1979+
.await;
19691980
}
19701981
let mut account = rpc
19711982
.get_account(env.batch_address_merkle_tree)

program-tests/system-cpi-test/tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ async fn test_read_only_accounts() {
146146
// fails because of invalid leaves hash_chain in some iteration
147147
let instruction_data = create_batch_update_address_tree_instruction_data_with_proof(
148148
&mut e2e_env.rpc,
149-
&e2e_env.indexer,
149+
&mut e2e_env.indexer,
150150
env.batch_address_merkle_tree,
151151
)
152152
.await

program-tests/utils/src/e2e_test_env.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,12 +762,13 @@ where
762762
.indexer
763763
.get_queue_elements(
764764
merkle_tree_pubkey.to_bytes(),
765-
full_batch_index,
766-
0,
767765
batch.batch_size,
766+
None,
768767
)
769768
.await
770769
.unwrap();
770+
let addresses =
771+
addresses.iter().map(|x| x.account_hash).collect::<Vec<_>>();
771772
// // local_leaves_hash_chain is only used for a test assertion.
772773
// let local_nullifier_hash_chain = create_hash_chain_from_array(&addresses);
773774
// assert_eq!(leaves_hash_chain, local_nullifier_hash_chain);

0 commit comments

Comments
 (0)