Skip to content

Commit d717211

Browse files
create snapshot query helpers
1 parent 02f51e8 commit d717211

File tree

1 file changed

+98
-11
lines changed

1 file changed

+98
-11
lines changed

wasm/src/programs/manager/execute.rs

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,24 @@ use crate::{
3939
},
4040
};
4141
use snarkvm_algorithms::snark::varuna::VarunaVersion;
42-
use snarkvm_console::network::{ConsensusVersion, Network};
42+
use snarkvm_console::{
43+
network::{ConsensusVersion},
44+
types::Field,
45+
};
4346
use snarkvm_ledger_query::QueryTrait;
44-
use snarkvm_synthesizer::prelude::{InclusionVersion, cost_in_microcredits_v1, execution_cost_v1, execution_cost_v2};
47+
use snarkvm_synthesizer::prelude::{
48+
InclusionVersion,
49+
cost_in_microcredits_v1,
50+
execution_cost_v1,
51+
execution_cost_v2,
52+
};
4553

4654
use core::ops::Add;
4755
use js_sys::{Array, Object};
4856
use rand::{SeedableRng, rngs::StdRng};
4957
use std::str::FromStr;
5058

59+
5160
#[wasm_bindgen]
5261
impl ProgramManager {
5362
/// Execute an arbitrary function locally
@@ -112,12 +121,23 @@ impl ProgramManager {
112121
let mut execution_response = if prove_execution {
113122
log("Preparing inclusion proofs for execution");
114123
if let Some(offline_query) = offline_query {
115-
trace.prepare_async(&offline_query).await.map_err(|err| err.to_string())?;
124+
trace.prepare_async(&offline_query).await.map_err(|e| e.to_string())?;
116125
} else {
117-
let query = SnapshotQuery::from(node_url);
118-
trace.prepare_async(&query).await.map_err(|err| err.to_string())?;
126+
// NEW: try snapshot query, fallback to QueryNative (TODO, for now)
127+
let commitments = snapshot_helpers::collect_commitments_from_trace(&trace)?;
128+
match snapshot_helpers::build_snapshot_query(node_url, &commitments).await {
129+
Ok(snapshot_query) => {
130+
trace.prepare_async(&snapshot_query).await.map_err(|e| e.to_string())?;
131+
}
132+
Err(_e) => {
133+
// TODO: remove this fallback once snapshot builder is implemented
134+
let query = QueryNative::from(node_url);
135+
trace.prepare_async(&query).await.map_err(|err| err.to_string())?;
136+
}
137+
}
119138
}
120139

140+
121141
log("Proving execution");
122142
let locator = program_native.id().to_string().add("/").add(function);
123143
let execution =
@@ -203,10 +223,20 @@ impl ProgramManager {
203223
if let Some(offline_query) = offline_query.as_ref() {
204224
trace.prepare_async(offline_query).await.map_err(|err| err.to_string())?;
205225
} else {
206-
let query = QueryNative::from(node_url);
207-
trace.prepare_async(&query).await.map_err(|err| err.to_string())?;
226+
// NEW: try snapshot query, fallback to QueryNative (TODO, for now)
227+
let commitments = snapshot_helpers::collect_commitments_from_trace(&trace)?;
228+
match snapshot_helpers::build_snapshot_query(node_url, &commitments).await {
229+
Ok(snapshot_query) => {
230+
trace.prepare_async(&snapshot_query).await.map_err(|e| e.to_string())?;
231+
}
232+
Err(_e) => {
233+
let query = QueryNative::from(node_url);
234+
trace.prepare_async(&query).await.map_err(|err| err.to_string())?;
235+
}
236+
}
208237
}
209238

239+
210240
log("Proving execution");
211241
let locator = program_native.id().to_string().add("/").add(function);
212242
let execution = trace
@@ -326,11 +356,23 @@ impl ProgramManager {
326356
trace.prepare_async(&offline_query).await.map_err(|err| err.to_string())?;
327357
block_height
328358
} else {
329-
let query = QueryNative::from(node_url);
330-
let block_height = query.current_block_height_async().await.map_err(|e| e.to_string())?;
331-
trace.prepare_async(&query).await.map_err(|err| err.to_string())?;
332-
block_height
359+
// try snapshot query, fallback to QueryNative (TODO, for now)
360+
let commitments = snapshot_helpers::collect_commitments_from_trace(&trace)?;
361+
match snapshot_helpers::build_snapshot_query(node_url, &commitments).await {
362+
Ok(snapshot_query) => {
363+
let bh = snapshot_query.current_block_height().map_err(|e| e.to_string())?;
364+
trace.prepare_async(&snapshot_query).await.map_err(|e| e.to_string())?;
365+
bh
366+
}
367+
Err(_e) => {
368+
let query = QueryNative::from(node_url);
369+
let bh = query.current_block_height_async().await.map_err(|e| e.to_string())?;
370+
trace.prepare_async(&query).await.map_err(|err| err.to_string())?;
371+
bh
372+
}
373+
}
333374
};
375+
334376
let execution =
335377
trace.prove_execution::<CurrentAleo, _>(&locator, VarunaVersion::V2, rng).map_err(|e| e.to_string())?;
336378

@@ -389,6 +431,51 @@ impl ProgramManager {
389431
}
390432
}
391433

434+
mod snapshot_helpers {
435+
use super::*;
436+
type StateRootNative = <CurrentNetwork as snarkvm_console::network::Network>::StateRoot;
437+
438+
pub fn collect_commitments_from_trace<T>(
439+
_trace: &T,
440+
) -> Result<Vec<Field<CurrentNetwork>>, String> {
441+
Ok(vec![])
442+
}
443+
444+
pub async fn build_snapshot_query(
445+
node_url: &str,
446+
commitments: &[Field<CurrentNetwork>],
447+
) -> Result<SnapshotQuery, String> {
448+
let (state_root, block_height) = snapshot_head(node_url).await?;
449+
let mut query = SnapshotQuery::new(block_height, &state_root_to_string(state_root))?;
450+
451+
for c in commitments {
452+
let c_str = c.to_string();
453+
let sp_str = fetch_state_path_at_root(node_url, &c_str, &state_root_to_string(state_root)).await?;
454+
query.add_state_path(&c_str, &sp_str)?;
455+
}
456+
Ok(query)
457+
}
458+
459+
// Prefer an API that returns both in one call; otherwise fetch root then height immediately.
460+
async fn snapshot_head(node_url: &str) -> Result<(StateRootNative, u32), String> {
461+
// TODO: call existing client / REST: (root, height)
462+
Err("snapshot_head() not implemented".into())
463+
}
464+
465+
async fn fetch_state_path_at_root(
466+
node_url: &str,
467+
commitment: &str,
468+
state_root: &str,
469+
) -> Result<String, String> {
470+
// TODO: call endpoint that returns a StatePath string for (commitment, state_root)
471+
Err("fetch_state_path_at_root() not implemented".into())
472+
}
473+
474+
fn state_root_to_string(root: StateRootNative) -> String {
475+
root.to_string()
476+
}
477+
}
478+
392479
#[cfg(test)]
393480
mod tests {
394481
use super::*;

0 commit comments

Comments
 (0)