Skip to content

Commit 847e990

Browse files
added input generation and burner private key, removing the need to provide them to the function
1 parent 844640c commit 847e990

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

wasm/src/programs/manager/execute.rs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
use super::*;
1818

1919
use crate::{
20+
Address,
2021
ExecutionResponse,
2122
OfflineQuery,
23+
Plaintext,
2224
PrivateKey,
2325
RecordPlaintext,
2426
SnapshotQuery,
@@ -42,10 +44,12 @@ use crate::{
4244
use snarkvm_algorithms::snark::varuna::VarunaVersion;
4345
use snarkvm_console::{
4446
network::{ConsensusVersion, Network},
45-
program::Value,
47+
program::{ArrayType, PlaintextType, Value, ValueType},
4648
};
4749
use snarkvm_ledger_query::QueryTrait;
4850
use snarkvm_synthesizer::prelude::{InclusionVersion, execution_cost, execution_cost_for_authorization};
51+
use snarkvm_synthesizer_program::FunctionCore;
52+
use wasm_bindgen::JsValue;
4953

5054
use crate::types::native::{PrivateKeyNative, ViewKeyNative};
5155
use core::ops::Add;
@@ -307,10 +311,8 @@ impl ProgramManager {
307311
#[wasm_bindgen(js_name = estimateExecutionFee)]
308312
#[allow(clippy::too_many_arguments)]
309313
pub async fn estimate_execution_fee(
310-
private_key: &PrivateKey,
311314
program: &str,
312315
function: &str,
313-
inputs: Array,
314316
url: Option<String>,
315317
imports: Option<Object>,
316318
offline_query: Option<OfflineQuery>,
@@ -327,6 +329,34 @@ impl ProgramManager {
327329
log("Check program imports are valid and add them to the process");
328330
let program_native = ProgramNative::from_str(program).map_err(|e| e.to_string())?;
329331

332+
let rng = &mut StdRng::from_entropy();
333+
// Initialize a burner private key.
334+
let burner_private_key = PrivateKey::new();
335+
// Compute the burner address.
336+
let burner_address = Address::from_private_key(&burner_private_key);
337+
338+
let function_native =
339+
program_native.get_function(IdentifierNative::from_str(function)).map_err(|e| e.to_string())?;
340+
341+
let mut inputs: Vec<Value<CurrentNetwork>> = vec![];
342+
for input_type in function_native.input_types() {
343+
match input_type {
344+
ValueType::ExternalRecord(locator) => {
345+
let stack = process.get_stack(locator.program_id()).map_err(|e| e.to_string())?;
346+
inputs.push(
347+
stack
348+
.sample_value(&burner_address, &ValueType::Record(*locator.resource()).into(), rng)
349+
.map_err(|e| e.to_string())?,
350+
);
351+
}
352+
_ => {
353+
let stack = process.get_stack(program_native.id()).map_err(|e| e.to_string())?;
354+
inputs
355+
.push(stack.sample_value(&burner_address, &input_type.into(), rng).map_err(|e| e.to_string())?);
356+
}
357+
}
358+
}
359+
330360
let program_id = program_native.id();
331361
let edition = edition.unwrap_or(1);
332362

@@ -338,30 +368,16 @@ impl ProgramManager {
338368
}
339369

340370
ProgramManager::resolve_imports(process, &program_native, imports)?;
341-
let rng = &mut StdRng::from_entropy();
342371

343372
let authorization = process
344-
.authorize::<CurrentAleo, StdRng>(
345-
private_key,
346-
program_id,
347-
function,
348-
inputs.iter().map(|v| Value::from_str(&v.as_string().unwrap_or_default())),
349-
rng,
350-
)
373+
.authorize::<CurrentAleo, StdRng>(&burner_private_key, program_id, function, inputs.iter(), rng)
351374
.map_err(|e| e.to_string())?;
352375

353376
let node_url = url.as_deref().unwrap_or(DEFAULT_URL);
354377
let latest_height = if let Some(offline_query) = offline_query.as_ref() {
355378
offline_query.current_block_height().map_err(|e| e.to_string())?
356379
} else {
357-
let function_name = IdentifierNative::from_str(function).map_err(|err| err.to_string())?;
358-
let view_key =
359-
ViewKeyNative::try_from(PrivateKeyNative::from(private_key)).map_err(|err| err.to_string())?;
360-
let query =
361-
SnapshotQuery::try_from_inputs(node_url, &program_native, &function_name, &view_key, &inputs.to_vec())
362-
.await
363-
.map_err(|err| err.to_string())?;
364-
query.current_block_height().map_err(|e| e.to_string())?
380+
latest_block_height(node_url).await.map_err(|err| err.to_string())?
365381
};
366382

367383
let consensus_version =

website/src/workers/worker.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,8 @@ self.addEventListener("message", (ev) => {
182182

183183
// Estimate the execution fee
184184
let executeFee = await aleo.ProgramManagerBase.estimateExecutionFee(
185-
privateKeyObject,
186185
remoteProgram,
187186
aleoFunction,
188-
inputs,
189187
url,
190188
imports,
191189
undefined,

0 commit comments

Comments
 (0)