minor changes

This commit is contained in:
Sergio Chouhy 2025-07-18 20:34:07 -03:00
parent af7d3675a1
commit 36834f192c
7 changed files with 35 additions and 39 deletions

View File

@ -34,7 +34,7 @@ fn main() {
let [private_account_user_1, private_account_user_2] = USER_CLIENTS[1]
.transfer_private(private_account_user_1, &addresses[2], 8, &mut sequencer)
.unwrap();
println!("📝 Balances after shielded execution");
println!("📝 Balances after private execution");
print_accounts(&sequencer, &[&private_account_user_1, &private_account_user_2]);
// A deshielded execution of the Transfer Program
@ -77,7 +77,11 @@ fn main() {
println!("📝 Balances after private piñata execution");
print_accounts(
&sequencer,
&[&private_account_user_1, &private_account_user_2, &private_account_user_0],
&[
&private_account_user_1,
&private_account_user_2,
&private_account_user_0,
],
);
println!("Ok!");

View File

@ -13,15 +13,11 @@ pub mod transfer_shielded;
/// A client that creates and submits transfer transactions
pub struct MockedClient {
user_private_key: Key,
private_accounts: Vec<Account>,
}
impl MockedClient {
pub const fn new(user_private_key: Key) -> Self {
Self {
user_private_key,
private_accounts: Vec::new(),
}
Self { user_private_key }
}
pub fn user_address(&self) -> Address {
@ -39,8 +35,7 @@ impl MockedClient {
) -> Result<Vec<Account>, ()> {
// Execute and generate proof of the outer program
let (receipt, private_outputs) =
nssa::invoke_privacy_execution::<P>(input_accounts, instruction_data, visibilities, commitment_tree_root)
.unwrap();
nssa::execute_offchain::<P>(input_accounts, instruction_data, visibilities, commitment_tree_root).unwrap();
// Send proof to the sequencer
sequencer.process_privacy_execution(receipt)?;

View File

@ -17,11 +17,8 @@ pub struct MockedSequencer {
accounts: BTreeMap<Address, Account>,
commitment_tree: SparseMerkleTree,
nullifier_set: HashSet<Nullifier>,
deployed_program_ids: HashSet<ProgramId>,
}
/// List of deployed programs
const DEPLOYED_PROGRAM_IDS: [ProgramId; 3] = [TRANSFER_ID, TRANSFER_MULTIPLE_ID, PINATA_ID];
/// The initial balance of the genesis accounts
const INITIAL_BALANCE: u128 = 150;
/// The address of the piñata program account
@ -45,7 +42,6 @@ impl MockedSequencer {
accounts,
commitment_tree,
nullifier_set,
deployed_program_ids: DEPLOYED_PROGRAM_IDS.iter().cloned().collect(),
}
}

View File

@ -16,7 +16,7 @@ impl MockedSequencer {
.collect::<Result<_, _>>()?;
// Execute the program
let inputs_outputs = nssa::execute::<P>(&input_accounts, instruction_data)?;
let inputs_outputs = nssa::execute_onchain::<P>(&input_accounts, instruction_data)?;
// Perform consistency checks
if !self.inputs_outputs_are_consistent(&input_accounts, &inputs_outputs) {

View File

@ -51,7 +51,8 @@ fn main() {
let balance_to_move = vec![30, 40];
// Execute and prove the outer program for the TransferMultipleProgram.
let (receipt, _) = nssa::invoke_privacy_execution::<TransferMultipleProgram>(
// This is executed off-chain by the sender.
let (receipt, _) = nssa::execute_offchain::<TransferMultipleProgram>(
&[sender, receiver_1, receiver_2],
balance_to_move,
&visibilities,

View File

@ -20,7 +20,7 @@ pub fn main() {
let balance_to_move = vec![10, 20];
let inputs_outputs =
nssa::execute::<TransferMultipleProgram>(&[sender, receiver_1, receiver_2], balance_to_move).unwrap();
nssa::execute_onchain::<TransferMultipleProgram>(&[sender, receiver_1, receiver_2], balance_to_move).unwrap();
println!("OK!");
}

View File

@ -50,30 +50,9 @@ fn execute_and_prove_inner<P: Program>(
Ok((receipt, inputs_outputs))
}
/// Executes the program `P` without generating a proof.
/// Returns the list of accounts pre and post states.
pub fn execute<P: Program>(
input_accounts: &[Account],
instruction_data: P::InstructionData,
) -> Result<Vec<Account>, ()> {
// Write inputs to the program
let mut env_builder = ExecutorEnv::builder();
write_inputs::<P>(input_accounts, instruction_data, &mut env_builder)?;
let env = env_builder.build().unwrap();
// Execute the program (without proving)
let executor = default_executor();
let session_info = executor.execute(env, P::PROGRAM_ELF).map_err(|_| ())?;
// Get (inputs and) outputs
let inputs_outputs: Vec<Account> = session_info.journal.decode().map_err(|_| ())?;
Ok(inputs_outputs)
}
/// Builds the private outputs from the results of the execution of an inner program.
/// Populates the nonces with the ones provided.
pub fn build_private_outputs_from_inner_results(
fn build_private_outputs_from_inner_results(
inputs_outputs: &[Account],
num_inputs: usize,
visibilities: &[InputVisibiility],
@ -93,10 +72,31 @@ pub fn build_private_outputs_from_inner_results(
.collect()
}
/// Executes the program `P` without generating a proof.
/// Returns the list of accounts pre and post states.
pub fn execute_onchain<P: Program>(
input_accounts: &[Account],
instruction_data: P::InstructionData,
) -> Result<Vec<Account>, ()> {
// Write inputs to the program
let mut env_builder = ExecutorEnv::builder();
write_inputs::<P>(input_accounts, instruction_data, &mut env_builder)?;
let env = env_builder.build().unwrap();
// Execute the program (without proving)
let executor = default_executor();
let session_info = executor.execute(env, P::PROGRAM_ELF).map_err(|_| ())?;
// Get (inputs and) outputs
let inputs_outputs: Vec<Account> = session_info.journal.decode().map_err(|_| ())?;
Ok(inputs_outputs)
}
/// Executes and proves the inner program `P` and executes and proves the outer program on top of it.
/// Returns the proof of execution of the outer program and the list of new private accounts
/// resulted from this execution.
pub fn invoke_privacy_execution<P: Program>(
pub fn execute_offchain<P: Program>(
inputs: &[Account],
instruction_data: P::InstructionData,
visibilities: &[InputVisibiility],