Skip to content

Commit 3d02f7f

Browse files
authored
Merge pull request ProvableHQ#2230 from AleoHQ/sample_response_checkdeployment
Use evaluate_function for CallStack::PackageRun; Sample response for CallStack::CheckDeployment
2 parents 8b43d18 + 53c69a8 commit 3d02f7f

File tree

6 files changed

+145
-27
lines changed

6 files changed

+145
-27
lines changed

synthesizer/process/src/stack/call/mod.rs

+51-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::{CallStack, Registers, RegistersCall, StackEvaluate, StackExecute};
15+
use crate::{stack::Address, CallStack, Registers, RegistersCall, StackEvaluate, StackExecute};
1616
use aleo_std::prelude::{finish, lap, timer};
1717
use console::{network::prelude::*, program::Request};
1818
use synthesizer_program::{
1919
Call,
2020
CallOperator,
21+
Operand,
2122
RegistersLoad,
2223
RegistersLoadCircuit,
2324
RegistersSigner,
@@ -241,7 +242,7 @@ impl<N: Network> CallTrait<N> for Call<N> {
241242
// Return the request and response.
242243
(request, response)
243244
}
244-
CallStack::CheckDeployment(_, private_key, ..) | CallStack::PackageRun(_, private_key, ..) => {
245+
CallStack::PackageRun(_, private_key, ..) => {
245246
// Compute the request.
246247
let request = Request::sign(
247248
&private_key,
@@ -257,8 +258,54 @@ impl<N: Network> CallTrait<N> for Call<N> {
257258
// Push the request onto the call stack.
258259
call_stack.push(request.clone())?;
259260

260-
// Execute the request.
261-
let response = substack.execute_function::<A, R>(call_stack, console_caller, rng)?;
261+
// Evaluate the request.
262+
let response = substack.evaluate_function::<A>(call_stack, console_caller)?;
263+
264+
// Return the request and response.
265+
(request, response)
266+
}
267+
CallStack::CheckDeployment(_, private_key, ..) => {
268+
// Compute the request.
269+
let request = Request::sign(
270+
&private_key,
271+
*substack.program_id(),
272+
*function.name(),
273+
inputs.iter(),
274+
&function.input_types(),
275+
rng,
276+
)?;
277+
278+
// Compute the address.
279+
let address = Address::try_from(&private_key)?;
280+
// Sample dummy outputs
281+
let outputs = function
282+
.outputs()
283+
.iter()
284+
.map(|output| substack.sample_value(&address, output.value_type(), rng))
285+
.collect::<Result<Vec<_>>>()?;
286+
// Map the output operands to registers.
287+
let output_registers = function
288+
.outputs()
289+
.iter()
290+
.map(|output| match output.operand() {
291+
Operand::Register(register) => Some(register.clone()),
292+
_ => None,
293+
})
294+
.collect::<Vec<_>>();
295+
296+
// Compute the response.
297+
let response = crate::Response::new(
298+
request.network_id(),
299+
substack.program().id(),
300+
function.name(),
301+
request.inputs().len(),
302+
request.tvk(),
303+
request.tcm(),
304+
outputs,
305+
&function.output_types(),
306+
&output_registers,
307+
)?;
308+
262309
// Return the request and response.
263310
(request, response)
264311
}

synthesizer/process/src/stack/evaluate.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ impl<N: Network> StackEvaluate<N> for Stack<N> {
106106
// Retrieve the next request, based on the call stack mode.
107107
let (request, call_stack) = match &call_stack {
108108
CallStack::Evaluate(authorization) => (authorization.next()?, call_stack),
109+
CallStack::PackageRun(requests, _, _) => {
110+
let last_request = requests.last().ok_or(anyhow!("CallStack does not contain request"))?.clone();
111+
(last_request, call_stack)
112+
}
109113
// If the evaluation is performed in the `Execute` mode, create a new `Evaluate` mode.
110114
// This is done to ensure that evaluation during execution is performed consistently.
111115
CallStack::Execute(authorization, _) => {
@@ -116,7 +120,9 @@ impl<N: Network> StackEvaluate<N> for Stack<N> {
116120
let call_stack = CallStack::Evaluate(authorization);
117121
(request, call_stack)
118122
}
119-
_ => bail!("Illegal operation: call stack must be `Evaluate` or `Execute` in `evaluate_function`."),
123+
_ => bail!(
124+
"Illegal operation: call stack must be `PackageRun`, `Evaluate` or `Execute` in `evaluate_function`."
125+
),
120126
};
121127
lap!(timer, "Retrieve the next request");
122128

synthesizer/process/src/stack/helpers/matches.rs

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
use super::*;
16-
use console::program::{Argument, FinalizeType};
1716

1817
impl<N: Network> StackMatches<N> for Stack<N> {
1918
/// Checks that the given value matches the layout of the value type.

synthesizer/process/src/stack/helpers/sample.rs

+52-21
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,6 @@
1515
use super::*;
1616

1717
impl<N: Network> Stack<N> {
18-
/// Returns a value for the given value type.
19-
pub fn sample_value<R: Rng + CryptoRng>(
20-
&self,
21-
burner_address: &Address<N>,
22-
value_type: &ValueType<N>,
23-
rng: &mut R,
24-
) -> Result<Value<N>> {
25-
match value_type {
26-
ValueType::Constant(plaintext_type)
27-
| ValueType::Public(plaintext_type)
28-
| ValueType::Private(plaintext_type) => Ok(Value::Plaintext(self.sample_plaintext(plaintext_type, rng)?)),
29-
ValueType::Record(record_name) => {
30-
Ok(Value::Record(self.sample_record(burner_address, record_name, rng)?))
31-
}
32-
ValueType::ExternalRecord(locator) => {
33-
bail!("Illegal operation: Cannot sample external records (for '{locator}.record').")
34-
}
35-
ValueType::Future(locator) => bail!("Illegal operation: Cannot sample futures (for '{locator}.future')."),
36-
}
37-
}
38-
3918
/// Returns a record for the given record name, with the given burner address.
4019
pub fn sample_record<R: Rng + CryptoRng>(
4120
&self,
@@ -64,6 +43,16 @@ impl<N: Network> Stack<N> {
6443
// Return the plaintext value.
6544
Ok(plaintext)
6645
}
46+
47+
/// Samples a future value according to the given future type.
48+
pub fn sample_future<R: Rng + CryptoRng>(&self, locator: &Locator<N>, rng: &mut R) -> Result<Future<N>> {
49+
// Sample a future value.
50+
let future = self.sample_future_internal(locator, 0, rng)?;
51+
// Ensure the future value matches the future type.
52+
self.matches_future(&future, locator)?;
53+
// Return the future value.
54+
Ok(future)
55+
}
6756
}
6857

6958
impl<N: Network> Stack<N> {
@@ -182,4 +171,46 @@ impl<N: Network> Stack<N> {
182171
// Return the plaintext.
183172
Ok(plaintext)
184173
}
174+
175+
/// Samples a future value according to the given locator.
176+
fn sample_future_internal<R: Rng + CryptoRng>(
177+
&self,
178+
locator: &Locator<N>,
179+
depth: usize,
180+
rng: &mut R,
181+
) -> Result<Future<N>> {
182+
// Retrieve the associated function.
183+
let function = match locator.program_id() == self.program_id() {
184+
true => self.get_function_ref(locator.resource())?,
185+
false => self.get_external_program(locator.program_id())?.get_function_ref(locator.resource())?,
186+
};
187+
188+
// Retrieve the finalize inputs.
189+
let inputs = match function.finalize_logic() {
190+
Some(finalize_logic) => finalize_logic.inputs(),
191+
None => bail!("Function '{locator}' does not have a finalize block"),
192+
};
193+
194+
let arguments = inputs
195+
.into_iter()
196+
.map(|input| {
197+
match input.finalize_type() {
198+
FinalizeType::Plaintext(plaintext_type) => {
199+
// Sample the plaintext value.
200+
let plaintext = self.sample_plaintext_internal(plaintext_type, depth + 1, rng)?;
201+
// Return the argument.
202+
Ok(Argument::Plaintext(plaintext))
203+
}
204+
FinalizeType::Future(locator) => {
205+
// Sample the future value.
206+
let future = self.sample_future_internal(locator, depth + 1, rng)?;
207+
// Return the argument.
208+
Ok(Argument::Future(future))
209+
}
210+
}
211+
})
212+
.collect::<Result<Vec<_>>>()?;
213+
214+
Ok(Future::new(*locator.program_id(), *locator.resource(), arguments))
215+
}
185216
}

synthesizer/process/src/stack/mod.rs

+26
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ use console::{
4141
account::{Address, PrivateKey},
4242
network::prelude::*,
4343
program::{
44+
Argument,
4445
Entry,
4546
EntryType,
47+
FinalizeType,
4648
Future,
4749
Identifier,
4850
Literal,
@@ -292,6 +294,30 @@ impl<N: Network> StackProgram<N> for Stack<N> {
292294
}
293295
Ok(num_calls)
294296
}
297+
298+
/// Returns a value for the given value type.
299+
fn sample_value<R: Rng + CryptoRng>(
300+
&self,
301+
burner_address: &Address<N>,
302+
value_type: &ValueType<N>,
303+
rng: &mut R,
304+
) -> Result<Value<N>> {
305+
match value_type {
306+
ValueType::Constant(plaintext_type)
307+
| ValueType::Public(plaintext_type)
308+
| ValueType::Private(plaintext_type) => Ok(Value::Plaintext(self.sample_plaintext(plaintext_type, rng)?)),
309+
ValueType::Record(record_name) => {
310+
Ok(Value::Record(self.sample_record(burner_address, record_name, rng)?))
311+
}
312+
ValueType::ExternalRecord(locator) => {
313+
// Retrieve the external stack.
314+
let stack = self.get_external_stack(locator.program_id())?;
315+
// Sample the output.
316+
Ok(Value::Record(stack.sample_record(burner_address, locator.resource(), rng)?))
317+
}
318+
ValueType::Future(locator) => Ok(Value::Future(self.sample_future(locator, rng)?)),
319+
}
320+
}
295321
}
296322

297323
impl<N: Network> StackProgramTypes<N> for Stack<N> {

synthesizer/program/src/traits/stack_and_registers.rs

+9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use console::{
3535
},
3636
types::{Address, Field},
3737
};
38+
use rand::{CryptoRng, Rng};
3839

3940
pub trait StackMatches<N: Network> {
4041
/// Checks that the given value matches the layout of the value type.
@@ -83,6 +84,14 @@ pub trait StackProgram<N: Network> {
8384

8485
/// Returns the expected number of calls for the given function name.
8586
fn get_number_of_calls(&self, function_name: &Identifier<N>) -> Result<usize>;
87+
88+
/// Samples a value for the given value_type.
89+
fn sample_value<R: Rng + CryptoRng>(
90+
&self,
91+
burner_address: &Address<N>,
92+
value_type: &ValueType<N>,
93+
rng: &mut R,
94+
) -> Result<Value<N>>;
8695
}
8796

8897
pub trait FinalizeRegistersState<N: Network> {

0 commit comments

Comments
 (0)