Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arced Program #18

Draft
wants to merge 2 commits into
base: mainnet-staging
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ledger/block/src/transaction/deployment/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl<N: Network> FromBytes for Deployment<N> {
// Read the edition.
let edition = u16::read_le(&mut reader)?;
// Read the program.
let program = Program::read_le(&mut reader)?;
let program = Arc::new(Program::read_le(&mut reader)?);

// Read the number of entries in the bundle.
let num_entries = u16::read_le(&mut reader)?;
Expand Down
10 changes: 6 additions & 4 deletions ledger/block/src/transaction/deployment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ use console::{
use synthesizer_program::Program;
use synthesizer_snark::{Certificate, VerifyingKey};

use std::sync::Arc;

#[derive(Clone, PartialEq, Eq)]
pub struct Deployment<N: Network> {
/// The edition.
edition: u16,
/// The program.
program: Program<N>,
program: Arc<Program<N>>,
/// The mapping of function names to their verifying key and certificate.
verifying_keys: Vec<(Identifier<N>, (VerifyingKey<N>, Certificate<N>))>,
}
Expand All @@ -41,7 +43,7 @@ impl<N: Network> Deployment<N> {
/// Initializes a new deployment.
pub fn new(
edition: u16,
program: Program<N>,
program: Arc<Program<N>>,
verifying_keys: Vec<(Identifier<N>, (VerifyingKey<N>, Certificate<N>))>,
) -> Result<Self> {
// Construct the deployment.
Expand Down Expand Up @@ -110,12 +112,12 @@ impl<N: Network> Deployment<N> {
}

/// Returns the program.
pub const fn program(&self) -> &Program<N> {
pub fn program(&self) -> &Arc<Program<N>> {
&self.program
}

/// Returns the program.
pub const fn program_id(&self) -> &ProgramID<N> {
pub fn program_id(&self) -> &ProgramID<N> {
self.program.id()
}

Expand Down
6 changes: 3 additions & 3 deletions ledger/block/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use console::{
#[derive(Clone, PartialEq, Eq)]
pub enum Transaction<N: Network> {
/// The deploy transaction publishes an Aleo program to the network.
Deploy(N::TransactionID, ProgramOwner<N>, Box<Deployment<N>>, Fee<N>),
Deploy(N::TransactionID, ProgramOwner<N>, Deployment<N>, Fee<N>),
/// The execute transaction represents a call to an Aleo program.
Execute(N::TransactionID, Execution<N>, Option<Fee<N>>),
/// The fee transaction represents a fee paid to the network, used for rejected transactions.
Expand All @@ -55,7 +55,7 @@ impl<N: Network> Transaction<N> {
// Ensure the owner signed the correct transaction ID.
ensure!(owner.verify(deployment_id), "Attempted to create a deployment transaction with an invalid owner");
// Construct the deployment transaction.
Ok(Self::Deploy(id.into(), owner, Box::new(deployment), fee))
Ok(Self::Deploy(id.into(), owner, deployment, fee))
}

/// Initializes a new execution transaction.
Expand Down Expand Up @@ -126,7 +126,7 @@ impl<N: Network> Transaction<N> {
#[inline]
pub fn deployment(&self) -> Option<&Deployment<N>> {
match self {
Self::Deploy(_, _, deployment, _) => Some(deployment.as_ref()),
Self::Deploy(_, _, deployment, _) => Some(deployment),
_ => None,
}
}
Expand Down
4 changes: 2 additions & 2 deletions ledger/block/src/transactions/rejected/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ use crate::{Deployment, Execution, Fee};
/// A wrapper around the rejected deployment or execution.
#[derive(Clone, PartialEq, Eq)]
pub enum Rejected<N: Network> {
Deployment(ProgramOwner<N>, Box<Deployment<N>>),
Deployment(ProgramOwner<N>, Deployment<N>),
Execution(Execution<N>),
}

impl<N: Network> Rejected<N> {
/// Initializes a rejected deployment.
pub fn new_deployment(program_owner: ProgramOwner<N>, deployment: Deployment<N>) -> Self {
Self::Deployment(program_owner, Box::new(deployment))
Self::Deployment(program_owner, deployment)
}

/// Initializes a rejected execution.
Expand Down
6 changes: 4 additions & 2 deletions ledger/query/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use console::{
use ledger_store::{BlockStorage, BlockStore};
use synthesizer_program::Program;

use std::sync::Arc;

#[derive(Clone)]
pub enum Query<N: Network, B: BlockStorage<N>> {
/// The block store from the VM.
Expand Down Expand Up @@ -142,7 +144,7 @@ impl<N: Network, B: BlockStorage<N>> QueryTrait<N> for Query<N, B> {

impl<N: Network, B: BlockStorage<N>> Query<N, B> {
/// Returns the program for the given program ID.
pub fn get_program(&self, program_id: &ProgramID<N>) -> Result<Program<N>> {
pub fn get_program(&self, program_id: &ProgramID<N>) -> Result<Arc<Program<N>>> {
match self {
Self::VM(block_store) => {
block_store.get_program(program_id)?.ok_or_else(|| anyhow!("Program {program_id} not found in storage"))
Expand All @@ -164,7 +166,7 @@ impl<N: Network, B: BlockStorage<N>> Query<N, B> {

/// Returns the program for the given program ID.
#[cfg(feature = "async")]
pub async fn get_program_async(&self, program_id: &ProgramID<N>) -> Result<Program<N>> {
pub async fn get_program_async(&self, program_id: &ProgramID<N>) -> Result<Arc<Program<N>>> {
match self {
Self::VM(block_store) => {
block_store.get_program(program_id)?.ok_or_else(|| anyhow!("Program {program_id} not found in storage"))
Expand Down
4 changes: 3 additions & 1 deletion ledger/src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

use super::*;

use std::sync::Arc;

impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
/// Returns the committee for the given `block height`.
pub fn get_committee(&self, block_height: u32) -> Result<Option<Committee<N>>> {
Expand Down Expand Up @@ -192,7 +194,7 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
}

/// Returns the program for the given program ID.
pub fn get_program(&self, program_id: ProgramID<N>) -> Result<Program<N>> {
pub fn get_program(&self, program_id: ProgramID<N>) -> Result<Arc<Program<N>>> {
match self.vm.block_store().get_program(&program_id)? {
Some(program) => Ok(program),
None => bail!("Missing program for ID {program_id}"),
Expand Down
4 changes: 3 additions & 1 deletion ledger/src/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

use super::*;

use std::sync::Arc;

impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
/// Returns an iterator over the state roots, for all blocks in `self`.
pub fn state_roots(&self) -> impl '_ + Iterator<Item = Cow<'_, N::StateRoot>> {
Expand All @@ -33,7 +35,7 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
}

/// Returns an iterator over the programs, for all transactions in `self`.
pub fn programs(&self) -> impl '_ + Iterator<Item = Cow<'_, Program<N>>> {
pub fn programs(&self) -> impl '_ + Iterator<Item = Cow<'_, Arc<Program<N>>>> {
self.vm.transaction_store().programs()
}

Expand Down
2 changes: 1 addition & 1 deletion ledger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
pub fn create_deploy<R: Rng + CryptoRng>(
&self,
private_key: &PrivateKey<N>,
program: &Program<N>,
program: &Arc<Program<N>>,
priority_fee_in_microcredits: u64,
query: Option<Query<N, C::BlockStorage>>,
rng: &mut R,
Expand Down
2 changes: 1 addition & 1 deletion ledger/store/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ impl<N: Network, B: BlockStorage<N>> BlockStore<N, B> {
}

/// Returns the program for the given `program ID`.
pub fn get_program(&self, program_id: &ProgramID<N>) -> Result<Option<Program<N>>> {
pub fn get_program(&self, program_id: &ProgramID<N>) -> Result<Option<Arc<Program<N>>>> {
self.storage.transaction_store().get_program(program_id)
}

Expand Down
6 changes: 4 additions & 2 deletions ledger/store/src/helpers/memory/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ use console::{
use synthesizer_program::Program;
use synthesizer_snark::{Certificate, Proof, VerifyingKey};

use std::sync::Arc;

/// An in-memory transaction storage.
#[derive(Clone)]
pub struct TransactionMemory<N: Network> {
Expand Down Expand Up @@ -98,7 +100,7 @@ pub struct DeploymentMemory<N: Network> {
/// The owner map.
owner_map: MemoryMap<(ProgramID<N>, u16), ProgramOwner<N>>,
/// The program map.
program_map: MemoryMap<(ProgramID<N>, u16), Program<N>>,
program_map: MemoryMap<(ProgramID<N>, u16), Arc<Program<N>>>,
/// The verifying key map.
verifying_key_map: MemoryMap<(ProgramID<N>, Identifier<N>, u16), VerifyingKey<N>>,
/// The certificate map.
Expand All @@ -113,7 +115,7 @@ impl<N: Network> DeploymentStorage<N> for DeploymentMemory<N> {
type EditionMap = MemoryMap<ProgramID<N>, u16>;
type ReverseIDMap = MemoryMap<(ProgramID<N>, u16), N::TransactionID>;
type OwnerMap = MemoryMap<(ProgramID<N>, u16), ProgramOwner<N>>;
type ProgramMap = MemoryMap<(ProgramID<N>, u16), Program<N>>;
type ProgramMap = MemoryMap<(ProgramID<N>, u16), Arc<Program<N>>>;
type VerifyingKeyMap = MemoryMap<(ProgramID<N>, Identifier<N>, u16), VerifyingKey<N>>;
type CertificateMap = MemoryMap<(ProgramID<N>, Identifier<N>, u16), Certificate<N>>;
type FeeStorage = FeeMemory<N>;
Expand Down
6 changes: 4 additions & 2 deletions ledger/store/src/helpers/rocksdb/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ use console::{
use synthesizer_program::Program;
use synthesizer_snark::{Certificate, Proof, VerifyingKey};

use std::sync::Arc;

/// A database transaction storage.
#[derive(Clone)]
pub struct TransactionDB<N: Network> {
Expand Down Expand Up @@ -108,7 +110,7 @@ pub struct DeploymentDB<N: Network> {
/// The program owner map.
owner_map: DataMap<(ProgramID<N>, u16), ProgramOwner<N>>,
/// The program map.
program_map: DataMap<(ProgramID<N>, u16), Program<N>>,
program_map: DataMap<(ProgramID<N>, u16), Arc<Program<N>>>,
/// The verifying key map.
verifying_key_map: DataMap<(ProgramID<N>, Identifier<N>, u16), VerifyingKey<N>>,
/// The certificate map.
Expand All @@ -123,7 +125,7 @@ impl<N: Network> DeploymentStorage<N> for DeploymentDB<N> {
type EditionMap = DataMap<ProgramID<N>, u16>;
type ReverseIDMap = DataMap<(ProgramID<N>, u16), N::TransactionID>;
type OwnerMap = DataMap<(ProgramID<N>, u16), ProgramOwner<N>>;
type ProgramMap = DataMap<(ProgramID<N>, u16), Program<N>>;
type ProgramMap = DataMap<(ProgramID<N>, u16), Arc<Program<N>>>;
type VerifyingKeyMap = DataMap<(ProgramID<N>, Identifier<N>, u16), VerifyingKey<N>>;
type CertificateMap = DataMap<(ProgramID<N>, Identifier<N>, u16), Certificate<N>>;
type FeeStorage = FeeDB<N>;
Expand Down
10 changes: 5 additions & 5 deletions ledger/store/src/transaction/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use synthesizer_snark::{Certificate, VerifyingKey};
use aleo_std_storage::StorageMode;
use anyhow::Result;
use core::marker::PhantomData;
use std::borrow::Cow;
use std::{borrow::Cow, sync::Arc};

/// A trait for deployment storage.
pub trait DeploymentStorage<N: Network>: Clone + Send + Sync {
Expand All @@ -44,7 +44,7 @@ pub trait DeploymentStorage<N: Network>: Clone + Send + Sync {
/// The mapping of `(program ID, edition)` to `ProgramOwner`.
type OwnerMap: for<'a> Map<'a, (ProgramID<N>, u16), ProgramOwner<N>>;
/// The mapping of `(program ID, edition)` to `program`.
type ProgramMap: for<'a> Map<'a, (ProgramID<N>, u16), Program<N>>;
type ProgramMap: for<'a> Map<'a, (ProgramID<N>, u16), Arc<Program<N>>>;
/// The mapping of `(program ID, function name, edition)` to `verifying key`.
type VerifyingKeyMap: for<'a> Map<'a, (ProgramID<N>, Identifier<N>, u16), VerifyingKey<N>>;
/// The mapping of `(program ID, function name, edition)` to `certificate`.
Expand Down Expand Up @@ -310,7 +310,7 @@ pub trait DeploymentStorage<N: Network>: Clone + Send + Sync {
}

/// Returns the program for the given `program ID`.
fn get_program(&self, program_id: &ProgramID<N>) -> Result<Option<Program<N>>> {
fn get_program(&self, program_id: &ProgramID<N>) -> Result<Option<Arc<Program<N>>>> {
// Check if the program ID is for 'credits.aleo'.
// This case is handled separately, as it is a default program of the VM.
// TODO (howardwu): After we update 'fee' rules and 'Ratify' in genesis, we can remove this.
Expand Down Expand Up @@ -581,7 +581,7 @@ impl<N: Network, D: DeploymentStorage<N>> DeploymentStore<N, D> {
}

/// Returns the program for the given `program ID`.
pub fn get_program(&self, program_id: &ProgramID<N>) -> Result<Option<Program<N>>> {
pub fn get_program(&self, program_id: &ProgramID<N>) -> Result<Option<Arc<Program<N>>>> {
self.storage.get_program(program_id)
}

Expand Down Expand Up @@ -646,7 +646,7 @@ impl<N: Network, D: DeploymentStorage<N>> DeploymentStore<N, D> {
}

/// Returns an iterator over the programs, for all deployments.
pub fn programs(&self) -> impl '_ + Iterator<Item = Cow<'_, Program<N>>> {
pub fn programs(&self) -> impl '_ + Iterator<Item = Cow<'_, Arc<Program<N>>>> {
self.storage.program_map().values_confirmed().map(|program| match program {
Cow::Borrowed(program) => Cow::Borrowed(program),
Cow::Owned(program) => Cow::Owned(program),
Expand Down
6 changes: 3 additions & 3 deletions ledger/store/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use synthesizer_snark::{Certificate, VerifyingKey};
use aleo_std_storage::StorageMode;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::{borrow::Cow, sync::Arc};

#[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TransactionType {
Expand Down Expand Up @@ -386,7 +386,7 @@ impl<N: Network, T: TransactionStorage<N>> TransactionStore<N, T> {
}

/// Returns the program for the given `program ID`.
pub fn get_program(&self, program_id: &ProgramID<N>) -> Result<Option<Program<N>>> {
pub fn get_program(&self, program_id: &ProgramID<N>) -> Result<Option<Arc<Program<N>>>> {
self.storage.deployment_store().get_program(program_id)
}

Expand Down Expand Up @@ -458,7 +458,7 @@ impl<N: Network, T: TransactionStorage<N>> TransactionStore<N, T> {
}

/// Returns an iterator over the programs, for all deployments.
pub fn programs(&self) -> impl '_ + Iterator<Item = Cow<'_, Program<N>>> {
pub fn programs(&self) -> impl '_ + Iterator<Item = Cow<'_, Arc<Program<N>>>> {
self.storage.deployment_store().programs()
}

Expand Down
6 changes: 4 additions & 2 deletions ledger/test-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use synthesizer_process::Process;
use synthesizer_program::Program;

use once_cell::sync::OnceCell;
use std::sync::Arc;

type CurrentNetwork = console::network::MainnetV0;
type CurrentAleo = circuit::network::AleoV0;
Expand Down Expand Up @@ -147,6 +148,7 @@ function compute:
)
.unwrap();
assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
let program = Arc::new(program);

// Construct the process.
let process = Process::load().unwrap();
Expand All @@ -163,7 +165,7 @@ function compute:
pub fn sample_rejected_deployment(is_fee_private: bool, rng: &mut TestRng) -> Rejected<CurrentNetwork> {
// Sample a deploy transaction.
let deployment = match crate::sample_deployment_transaction(is_fee_private, rng) {
Transaction::Deploy(_, _, deployment, _) => (*deployment).clone(),
Transaction::Deploy(_, _, deployment, _) => deployment.clone(),
_ => unreachable!(),
};

Expand Down Expand Up @@ -395,7 +397,7 @@ pub fn sample_large_execution_transaction(rng: &mut TestRng) -> Transaction<Curr
let execution = INSTANCE
.get_or_init(|| {
// Initialize a program that produces large transactions.
let program = large_transaction_program();
let program = Arc::new(large_transaction_program());

// Construct the process.
let mut process = synthesizer_process::Process::load().unwrap();
Expand Down
4 changes: 3 additions & 1 deletion synthesizer/process/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

use super::*;

use std::sync::Arc;

impl<N: Network> Process<N> {
/// Deploys the given program ID, if it does not exist.
#[inline]
pub fn deploy<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
&self,
program: &Program<N>,
program: &Arc<Program<N>>,
rng: &mut R,
) -> Result<Deployment<N>> {
let timer = timer!("Process::deploy");
Expand Down
4 changes: 2 additions & 2 deletions synthesizer/process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<N: Network> Process<N> {
/// Adds a new program to the process.
/// If you intend to `execute` the program, use `deploy` and `finalize_deployment` instead.
#[inline]
pub fn add_program(&mut self, program: &Program<N>) -> Result<()> {
pub fn add_program(&mut self, program: &Arc<Program<N>>) -> Result<()> {
// Initialize the 'credits.aleo' program ID.
let credits_program_id = ProgramID::<N>::from_str("credits.aleo")?;
// If the program is not 'credits.aleo', compute the program stack, and add it to the process.
Expand Down Expand Up @@ -223,7 +223,7 @@ impl<N: Network> Process<N> {

/// Returns the program for the given program ID.
#[inline]
pub fn get_program(&self, program_id: impl TryInto<ProgramID<N>>) -> Result<&Program<N>> {
pub fn get_program(&self, program_id: impl TryInto<ProgramID<N>>) -> Result<&Arc<Program<N>>> {
Ok(self.get_stack(program_id)?.program())
}

Expand Down
2 changes: 1 addition & 1 deletion synthesizer/process/src/stack/helpers/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use super::*;
impl<N: Network> Stack<N> {
/// Initializes a new stack, given the process and program.
#[inline]
pub(crate) fn initialize(process: &Process<N>, program: &Program<N>) -> Result<Self> {
pub(crate) fn initialize(process: &Process<N>, program: &Arc<Program<N>>) -> Result<Self> {
// Construct the stack for the program.
let mut stack = Self {
program: program.clone(),
Expand Down
Loading