pint_pkg/
build.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Given a compilation [`Plan`][crate::plan::Plan], build all packages in the graph.

use crate::{
    manifest,
    plan::{Graph, NodeIx, Pinned, PinnedManifests, Plan},
};
use essential_types::{
    contract::Contract, predicate::Predicate as CompiledPredicate, ContentAddress,
};
use pint_abi_types::ContractABI;
use pintc::asm_gen::compile_contract;
use std::{
    collections::HashMap,
    path::{Path, PathBuf},
};
use thiserror::Error;

/// A context that allows for iteratively compiling packages within a given compilation `Plan`.
pub struct PlanBuilder<'p> {
    /// The plan that is being executed by this builder.
    pub plan: &'p Plan,
    built_pkgs: BuiltPkgs,
    order: std::slice::Iter<'p, NodeIx>,
}

/// A package that is ready to be built.
pub struct PrebuiltPkg<'p, 'b> {
    pub plan: &'p Plan,
    built_pkgs: &'b mut BuiltPkgs,
    /// The node of the package to be built.
    n: NodeIx,
}

/// A mapping from the node index to the associated built package.
pub type BuiltPkgs = HashMap<NodeIx, BuiltPkg>;

/// A successfully built package.
#[derive(Debug)]
pub enum BuiltPkg {
    /// A built contract.
    Contract(BuiltContract),
    /// A built library.
    Library(BuiltLibrary),
}

/// A successfully built contract package.
#[derive(Debug)]
pub struct BuiltContract {
    /// All the emitted warnings.
    pub warnings: pintc::warning::Warnings,
    /// All built predicates.
    pub predicate_metadata: Vec<PredicateMetadata>,
    /// The salt of this contract.
    pub contract: Contract,
    /// The content address of the contract.
    pub ca: ContentAddress,
    /// The entry-point into the temp library submodules used to provide the CAs.
    pub lib_entry_point: PathBuf,
    /// The ABI for the contract.
    pub abi: ContractABI,
    /// The optimized contract.
    pub optimized: pintc::predicate::Contract,
}

/// An predicate built as a part of a contract.
#[derive(Debug)]
pub struct BuiltPredicate {
    /// The content address of the predicate.
    pub ca: ContentAddress,
    /// The name of the predicate from the code.
    pub name: String,
    pub predicate: CompiledPredicate,
}

/// An predicate built as a part of a contract.
#[derive(Debug)]
pub struct PredicateMetadata {
    /// The content address of the predicate.
    pub ca: ContentAddress,
    /// The name of the predicate from the code.
    pub name: String,
}

/// A successfully built library package.
#[derive(Debug)]
pub struct BuiltLibrary {
    /// All the emitted warnings.
    pub warnings: pintc::warning::Warnings,
    /// The compiled contract.
    pub contract: pintc::predicate::Contract,
}

/// An error occurred while building according to a compilation plan.
#[derive(Debug)]
pub struct BuildError {
    /// Packages that were successfully built.
    pub built_pkgs: BuiltPkgs,
    /// The package error that occurred.
    pub pkg_err: BuildPkgError,
}

/// An error was produced while building a package.
#[derive(Debug)]
pub struct BuildPkgError {
    /// The pintc error handler.
    pub handler: pintc::error::Handler,
    /// The kind of build error that occurred.
    pub kind: BuildPkgErrorKind,
}

#[derive(Debug, Error)]
pub enum BuildPkgErrorKind {
    #[error("`pintc` encountered an error: {0}")]
    Pintc(#[from] PintcError),
    #[error("failed to create lib providing contract and predicate CAs for {0:?}: {1}")]
    ContractLibrary(String, std::io::Error),
}

#[derive(Debug, Error)]
pub enum PintcError {
    #[error("parse error")]
    Parse,
    #[error("type check error")]
    TypeCheck,
    #[error("flattening error")]
    Flatten,
    #[error("abi gen")]
    ABIGen,
    #[error("asm-gen error")]
    AsmGen,
}

/// An error occurred while writing a built package's output artifacts.
#[derive(Debug, Error)]
pub enum WriteError {
    /// Failed to serialize contract or ABI.
    #[error("failed to serialize contract or ABI: {0}")]
    SerdeJson(#[from] serde_json::Error),
    /// An I/O error occurred.
    #[error("an I/O error occurred: {0}")]
    Io(#[from] std::io::Error),
}

impl<'p> PlanBuilder<'p> {
    /// Produce the next package that is to be built.
    pub fn next_pkg(&mut self) -> Option<PrebuiltPkg> {
        let &n = self.order.next()?;
        Some(PrebuiltPkg {
            plan: self.plan,
            built_pkgs: &mut self.built_pkgs,
            n,
        })
    }

    /// Access the set of packages that have been built so far.
    pub fn built_pkgs(&self) -> &BuiltPkgs {
        &self.built_pkgs
    }

    /// Build all remaining packages.
    pub fn build_all(mut self, skip_optimize: bool) -> Result<BuiltPkgs, BuildError> {
        while let Some(prebuilt) = self.next_pkg() {
            if let Err(pkg_err) = prebuilt.build(skip_optimize) {
                let built_pkgs = self.built_pkgs;
                return Err(BuildError {
                    built_pkgs,
                    pkg_err,
                });
            }
        }
        Ok(self.built_pkgs)
    }
}

impl<'p, 'b> PrebuiltPkg<'p, 'b> {
    /// The index of the package within the plan's package graph.
    pub fn node_ix(&self) -> NodeIx {
        self.n
    }

    /// This package's pinned representation from within the compilation graph.
    pub fn pinned(&self) -> &'p Pinned {
        &self.plan.graph()[self.n]
    }

    /// Build this package.
    pub fn build(self, skip_optimize: bool) -> Result<&'b BuiltPkg, BuildPkgError> {
        let Self {
            plan,
            built_pkgs,
            n,
        } = self;
        let built = build_pkg(plan, built_pkgs, n, skip_optimize)?;
        built_pkgs.insert(n, built);
        Ok(&built_pkgs[&n])
    }
}

impl BuildPkgError {
    /// Consume `self` and print the errors and the warnings.
    pub fn print_diagnostics(self) {
        let (errors, warnings) = self.handler.consume();
        pintc::error::print_errors(&pintc::error::Errors(errors));
        pintc::warning::print_warnings(&pintc::warning::Warnings(warnings));
    }
}

impl BuiltPkg {
    /// Write the built artifacts for this package to the given directory.
    pub fn write_to_dir(&self, name: &str, path: &Path) -> Result<(), WriteError> {
        match self {
            Self::Library(_) => (),
            Self::Contract(built) => {
                // Write the contract.
                let contract_string = serde_json::to_string_pretty(&built.contract)?;
                let contract_path = path.join(name).with_extension("json");
                std::fs::write(contract_path, contract_string)?;

                // Write the ABI.
                let abi_string = serde_json::to_string_pretty(&built.abi)?;
                let file_stem = format!("{}-abi", name);
                let abi_path = path.join(file_stem).with_extension("json");
                std::fs::write(abi_path, abi_string)?;
            }
        }
        Ok(())
    }

    /// Print all emitted warnings.
    pub fn print_warnings(&self) {
        let (Self::Contract(BuiltContract { warnings, .. })
        | Self::Library(BuiltLibrary { warnings, .. })) = self;

        pintc::warning::print_warnings(warnings);
    }
}

/// Collect the immediate dependencies of the given package.
fn dependencies<'a>(
    n: NodeIx,
    g: &Graph,
    manifests: &'a PinnedManifests,
    built_pkgs: &'a BuiltPkgs,
) -> HashMap<String, PathBuf> {
    use petgraph::{visit::EdgeRef, Direction};
    g.edges_directed(n, Direction::Outgoing)
        .map(|e| {
            let name = e.weight().name.to_string();
            let dep_n = e.target();
            let pinned = &g[dep_n];
            let manifest = &manifests[&pinned.id()];
            let entry_point = match &built_pkgs[&dep_n] {
                BuiltPkg::Library(_lib) => manifest.entry_point(),
                BuiltPkg::Contract(contract) => contract.lib_entry_point.clone(),
            };
            (name, entry_point)
        })
        .collect()
}

/// Given a built contract, generate a library with a module and constant for
/// each predicate's contract address along with a constant for the contract's
/// content address.
///
/// Returns the entry point to the library.
fn contract_dep_lib(
    ca: &ContentAddress,
    predicates: &[BuiltPredicate],
) -> std::io::Result<PathBuf> {
    // Temporary directory for the contract project.
    let temp_dir = std::env::temp_dir().join(format!("{:x}", ca));
    std::fs::create_dir_all(&temp_dir)?;

    // Write the contract's CA to the library root.
    let lib_str = format!("const ADDRESS: b256 = 0x{:x};", ca);
    let lib_path = temp_dir.join("lib.pnt");
    std::fs::write(&lib_path, lib_str.as_bytes())?;

    // Write the predicate CAs to submodules.
    for predicate in predicates {
        let submod_str = format!("const ADDRESS: b256 = 0x{:x};", predicate.ca);

        // Create the path to the submodule from the predicate name.
        let mut submod: Vec<&str> = predicate.name.split("::").collect();
        // The root predicate is nameless when output from pint, so we give it a name.
        if matches!(&submod[..], &[""]) {
            submod = vec!["root"];
        }
        let mut submod_path = temp_dir.clone();
        submod_path.extend(submod.clone());
        submod_path.set_extension("pnt");
        std::fs::create_dir_all(submod_path.parent().expect("submod has no parent dir"))?;
        std::fs::write(&submod_path, submod_str.as_bytes())?;
    }

    Ok(lib_path)
}

/// Build the package at the given index, assuming all dependencies are already built.
fn build_pkg(
    plan: &Plan,
    built_pkgs: &BuiltPkgs,
    n: NodeIx,
    skip_optimize: bool,
) -> Result<BuiltPkg, BuildPkgError> {
    let graph = plan.graph();
    let pinned = &graph[n];
    let manifest = &plan.manifests()[&pinned.id()];
    let entry_point = manifest.entry_point();
    let handler = pintc::error::Handler::default();
    let deps = dependencies(n, graph, plan.manifests(), built_pkgs);

    // Parse the package from the entry point.
    let deps = deps
        .iter()
        .map(|(name, path)| (name.as_str(), path.as_path()))
        .collect();
    let Ok(parsed) = pintc::parser::parse_project(&handler, &deps, &entry_point) else {
        let kind = BuildPkgErrorKind::from(PintcError::Parse);
        return Err(BuildPkgError { handler, kind });
    };

    // Type check the package.
    let Ok(contract) = handler.scope(|handler| parsed.type_check(handler)) else {
        let kind = BuildPkgErrorKind::from(PintcError::TypeCheck);
        return Err(BuildPkgError { handler, kind });
    };

    let built_pkg = match manifest.pkg.kind {
        manifest::PackageKind::Library => {
            // TODO: Add checks here to make sure the library is sane.. E.g., the library is
            // stateless, etc.
            let lib = BuiltLibrary {
                warnings: pintc::warning::Warnings(handler.consume().1),
                contract,
            };
            BuiltPkg::Library(lib)
        }
        manifest::PackageKind::Contract => {
            // Flatten the contract to flat pint (the IR).
            let Ok(flattened) = handler.scope(|handler| contract.flatten(handler)) else {
                let kind = BuildPkgErrorKind::from(PintcError::Flatten);
                return Err(BuildPkgError { handler, kind });
            };

            // Perform optimizations on the flattened contract.
            let optimized = if skip_optimize {
                flattened
            } else {
                flattened.optimize(&handler)
            };

            // Produce the ABI for the flattened contract.
            let Ok(abi) = optimized.abi(&handler) else {
                let kind = BuildPkgErrorKind::from(PintcError::ABIGen);
                return Err(BuildPkgError { handler, kind });
            };

            // Generate the assembly and the predicates.
            let Ok(contract) = handler.scope(|h| compile_contract(h, &optimized)) else {
                let kind = BuildPkgErrorKind::from(PintcError::AsmGen);
                return Err(BuildPkgError { handler, kind });
            };

            // Collect the predicates alongside their content addresses.
            let predicates: Vec<_> = contract
                .predicates
                .into_iter()
                .zip(contract.names)
                .map(|(predicate, name)| {
                    let ca = essential_hash::content_addr(&predicate);
                    BuiltPredicate {
                        ca,
                        name,
                        predicate,
                    }
                })
                .collect();

            // The CA of the contract.
            let ca = essential_hash::contract_addr::from_predicate_addrs(
                predicates.iter().map(|predicate| predicate.ca.clone()),
                &contract.salt,
            );

            // Generate a temp lib for providing the contract and predicate CAs to dependents.
            let lib_entry_point = match contract_dep_lib(&ca, &predicates) {
                Ok(path) => path,
                Err(e) => {
                    let kind = BuildPkgErrorKind::ContractLibrary(pinned.name.clone(), e);
                    return Err(BuildPkgError { handler, kind });
                }
            };

            let (predicate_metadata, predicates) = predicates
                .into_iter()
                .map(
                    |BuiltPredicate {
                         ca,
                         name,
                         predicate,
                     }| (PredicateMetadata { ca, name }, predicate),
                )
                .unzip();

            let contract = BuiltContract {
                warnings: pintc::warning::Warnings(handler.consume().1),
                ca,
                predicate_metadata,
                contract: Contract {
                    predicates,
                    salt: contract.salt,
                },
                lib_entry_point,
                abi,
                optimized,
            };
            BuiltPkg::Contract(contract)
        }
    };

    Ok(built_pkg)
}

/// Given a compilation [`Plan`][crate::plan::Plan], return a [`PlanBuilder`]
/// that may be used to compile all packages within the graph.
pub fn build_plan(plan: &Plan) -> PlanBuilder {
    PlanBuilder {
        built_pkgs: BuiltPkgs::default(),
        plan,
        order: plan.compilation_order().iter(),
    }
}