Skip to content

Commit c370632

Browse files
reorganize
1 parent d717211 commit c370632

File tree

2 files changed

+59
-100
lines changed

2 files changed

+59
-100
lines changed

wasm/src/programs/manager/execute.rs

Lines changed: 13 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use crate::{
2727
execute_program,
2828
log,
2929
process_inputs,
30-
programs::SnapshotQuery,
3130
types::native::{
3231
CurrentAleo,
3332
CurrentNetwork,
@@ -39,24 +38,15 @@ use crate::{
3938
},
4039
};
4140
use snarkvm_algorithms::snark::varuna::VarunaVersion;
42-
use snarkvm_console::{
43-
network::{ConsensusVersion},
44-
types::Field,
45-
};
41+
use snarkvm_console::network::{ConsensusVersion, Network};
4642
use snarkvm_ledger_query::QueryTrait;
47-
use snarkvm_synthesizer::prelude::{
48-
InclusionVersion,
49-
cost_in_microcredits_v1,
50-
execution_cost_v1,
51-
execution_cost_v2,
52-
};
43+
use snarkvm_synthesizer::prelude::{InclusionVersion, cost_in_microcredits_v1, execution_cost_v1, execution_cost_v2};
5344

5445
use core::ops::Add;
5546
use js_sys::{Array, Object};
5647
use rand::{SeedableRng, rngs::StdRng};
5748
use std::str::FromStr;
5849

59-
6050
#[wasm_bindgen]
6151
impl ProgramManager {
6252
/// Execute an arbitrary function locally
@@ -121,23 +111,13 @@ impl ProgramManager {
121111
let mut execution_response = if prove_execution {
122112
log("Preparing inclusion proofs for execution");
123113
if let Some(offline_query) = offline_query {
124-
trace.prepare_async(&offline_query).await.map_err(|e| e.to_string())?;
114+
trace.prepare_async(&offline_query).await.map_err(|err| err.to_string())?;
125115
} else {
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-
}
116+
let query = QueryNative::from(node_url);
117+
// todo change this to new query struct and to try_from rather than from
118+
trace.prepare_async(&query).await.map_err(|err| err.to_string())?;
138119
}
139120

140-
141121
log("Proving execution");
142122
let locator = program_native.id().to_string().add("/").add(function);
143123
let execution =
@@ -223,20 +203,10 @@ impl ProgramManager {
223203
if let Some(offline_query) = offline_query.as_ref() {
224204
trace.prepare_async(offline_query).await.map_err(|err| err.to_string())?;
225205
} else {
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-
}
206+
let query = QueryNative::from(node_url);
207+
trace.prepare_async(&query).await.map_err(|err| err.to_string())?;
237208
}
238209

239-
240210
log("Proving execution");
241211
let locator = program_native.id().to_string().add("/").add(function);
242212
let execution = trace
@@ -356,23 +326,11 @@ impl ProgramManager {
356326
trace.prepare_async(&offline_query).await.map_err(|err| err.to_string())?;
357327
block_height
358328
} else {
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-
}
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
374333
};
375-
376334
let execution =
377335
trace.prove_execution::<CurrentAleo, _>(&locator, VarunaVersion::V2, rng).map_err(|e| e.to_string())?;
378336

@@ -431,51 +389,6 @@ impl ProgramManager {
431389
}
432390
}
433391

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-
479392
#[cfg(test)]
480393
mod tests {
481394
use super::*;
@@ -517,4 +430,4 @@ mod tests {
517430

518431
assert!(transaction.is_execute());
519432
}
520-
}
433+
}

wasm/src/programs/snapshot_query.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,49 @@ impl QueryTrait<CurrentNetwork> for SnapshotQuery {
112112
Ok(self.block_height)
113113
}
114114
}
115+
116+
117+
mod snapshot_helpers {
118+
use super::*;
119+
type StateRootNative = <CurrentNetwork as snarkvm_console::network::Network>::StateRoot;
120+
121+
pub fn collect_commitments_from_trace<T>(
122+
_trace: &T,
123+
) -> Result<Vec<Field<CurrentNetwork>>, String> {
124+
Ok(vec![])
125+
}
126+
127+
pub async fn build_snapshot_query(
128+
node_url: &str,
129+
commitments: &[Field<CurrentNetwork>],
130+
) -> Result<SnapshotQuery, String> {
131+
let (state_root, block_height) = snapshot_head(node_url).await?;
132+
let mut query = SnapshotQuery::new(block_height, &state_root_to_string(state_root))?;
133+
134+
for c in commitments {
135+
let c_str = c.to_string();
136+
let sp_str = fetch_state_path_at_root(node_url, &c_str, &state_root_to_string(state_root)).await?;
137+
query.add_state_path(&c_str, &sp_str)?;
138+
}
139+
Ok(query)
140+
}
141+
142+
// Prefer an API that returns both in one call; otherwise fetch root then height immediately.
143+
async fn snapshot_head(node_url: &str) -> Result<(StateRootNative, u32), String> {
144+
// TODO: call existing client / REST: (root, height)
145+
Err("snapshot_head() not implemented".into())
146+
}
147+
148+
async fn fetch_state_path_at_root(
149+
node_url: &str,
150+
commitment: &str,
151+
state_root: &str,
152+
) -> Result<String, String> {
153+
// TODO: call endpoint that returns a StatePath string for (commitment, state_root)
154+
Err("fetch_state_path_at_root() not implemented".into())
155+
}
156+
157+
fn state_root_to_string(root: StateRootNative) -> String {
158+
root.to_string()
159+
}
160+
}

0 commit comments

Comments
 (0)