leo_ast/constructor/
mod.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17mod admin;
18pub use admin::*;
19
20mod checksum;
21pub use checksum::*;
22
23mod noupgrade;
24pub use noupgrade::*;
25
26use crate::{Annotation, Block, Indent, IntegerType, Location, NetworkName, Node, NodeID, Type};
27use leo_span::{Span, sym};
28
29use anyhow::{anyhow, bail};
30use serde::{Deserialize, Serialize};
31use snarkvm::prelude::{Address, Literal, Locator, Network};
32use std::{fmt, str::FromStr};
33
34/// A constructor definition.
35#[derive(Clone, Default, Eq, PartialEq, Serialize, Deserialize)]
36pub struct Constructor {
37    /// Annotations on the constructor.
38    pub annotations: Vec<Annotation>,
39    /// The body of the constructor.
40    pub block: Block,
41    /// The entire span of the constructor definition.
42    pub span: Span,
43    /// The ID of the node.
44    pub id: NodeID,
45}
46
47/// The upgrade variant.
48#[derive(Clone, Debug, Eq, PartialEq)]
49pub enum UpgradeVariant {
50    Admin { address: String },
51    Custom,
52    Checksum { mapping: Location, key: String, key_type: Type },
53    NoUpgrade,
54}
55
56impl Constructor {
57    pub fn get_upgrade_variant_with_network(&self, network: NetworkName) -> anyhow::Result<UpgradeVariant> {
58        match network {
59            NetworkName::MainnetV0 => self.get_upgrade_variant::<snarkvm::prelude::MainnetV0>(),
60            NetworkName::TestnetV0 => self.get_upgrade_variant::<snarkvm::prelude::TestnetV0>(),
61            NetworkName::CanaryV0 => self.get_upgrade_variant::<snarkvm::prelude::CanaryV0>(),
62        }
63    }
64
65    /// Checks that the constructor's annotations are valid and returns the upgrade variant.
66    pub fn get_upgrade_variant<N: Network>(&self) -> anyhow::Result<UpgradeVariant> {
67        // Check that there is exactly one annotation.
68        if self.annotations.len() != 1 {
69            bail!(
70                "A constructor must have exactly one of the following annotations: `@admin`, `@checksum`, `@custom`, or `@noupgrade`."
71            );
72        }
73        // Get the annotation.
74        let annotation = &self.annotations[0];
75        match annotation.identifier.name {
76            sym::admin => {
77                // Parse the address string from the annotation.
78                let Some(address_string) = annotation.map.get(&sym::address) else {
79                    bail!("An `@admin` annotation must have an 'address' key.")
80                };
81                // Parse the address.
82                let address = Address::<N>::from_str(address_string)
83                    .map_err(|e| anyhow!("Invalid address in `@admin` annotation: `{e}`."))?;
84                Ok(UpgradeVariant::Admin { address: address.to_string() })
85            }
86            sym::checksum => {
87                // Parse the mapping string from the annotation.
88                let Some(mapping_string) = annotation.map.get(&sym::mapping) else {
89                    bail!("A `@checksum` annotation must have a 'mapping' key.")
90                };
91                // Parse the mapping string as a locator.
92                let mapping = Locator::<N>::from_str(mapping_string)
93                    .map_err(|e| anyhow!("Invalid mapping in `@checksum` annotation: `{e}`."))?;
94
95                // Parse the key string from the annotation.
96                let Some(key_string) = annotation.map.get(&sym::key) else {
97                    bail!("A `@checksum` annotation must have a 'key' key.")
98                };
99                // Parse the key as a plaintext value.
100                let key = Literal::<N>::from_str(key_string)
101                    .map_err(|e| anyhow!("Invalid key in `@checksum` annotation: `{e}`."))?;
102                // Get the literal type.
103                let key_type = get_type_from_snarkvm_literal(&key);
104                Ok(UpgradeVariant::Checksum { mapping: mapping.into(), key: key.to_string(), key_type })
105            }
106            sym::custom => Ok(UpgradeVariant::Custom),
107            sym::noupgrade => Ok(UpgradeVariant::NoUpgrade),
108            _ => bail!(
109                "Invalid annotation on constructor: `{}`. Expected one of `@admin`, `@checksum`, `@custom`, or `@noupgrade`.",
110                annotation.identifier.name
111            ),
112        }
113    }
114}
115
116impl fmt::Display for Constructor {
117    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
118        for annotation in &self.annotations {
119            writeln!(f, "{annotation}")?;
120        }
121
122        writeln!(f, "async constructor() {{")?;
123        for stmt in self.block.statements.iter() {
124            writeln!(f, "{}{}", Indent(stmt), stmt.semicolon())?;
125        }
126        write!(f, "}}")
127    }
128}
129
130impl fmt::Debug for Constructor {
131    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
132        write!(f, "{self}")
133    }
134}
135
136crate::simple_node_impl!(Constructor);
137
138// A helper function to get the type from a snarkVM literal.
139fn get_type_from_snarkvm_literal<N: Network>(literal: &Literal<N>) -> Type {
140    match literal {
141        Literal::Field(_) => Type::Field,
142        Literal::Group(_) => Type::Group,
143        Literal::Address(_) => Type::Address,
144        Literal::Scalar(_) => Type::Scalar,
145        Literal::Boolean(_) => Type::Boolean,
146        Literal::String(_) => Type::String,
147        Literal::I8(_) => Type::Integer(IntegerType::I8),
148        Literal::I16(_) => Type::Integer(IntegerType::I16),
149        Literal::I32(_) => Type::Integer(IntegerType::I32),
150        Literal::I64(_) => Type::Integer(IntegerType::I64),
151        Literal::I128(_) => Type::Integer(IntegerType::I128),
152        Literal::U8(_) => Type::Integer(IntegerType::U8),
153        Literal::U16(_) => Type::Integer(IntegerType::U16),
154        Literal::U32(_) => Type::Integer(IntegerType::U32),
155        Literal::U64(_) => Type::Integer(IntegerType::U64),
156        Literal::U128(_) => Type::Integer(IntegerType::U128),
157        Literal::Signature(_) => Type::Signature,
158    }
159}