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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{
    source_package::{
        layout::SourcePackageLayout,
        manifest_parser::{parse_move_manifest_string, parse_source_manifest},
        parsed_manifest::{
            Dependency, FileName, NamedAddress, PackageName, SourceManifest, SubstOrRename,
        },
    },
    BuildConfig,
};
use anyhow::{bail, Context, Result};
use move_command_line_common::files::find_move_filenames;
use move_core_types::account_address::AccountAddress;
use move_symbol_pool::Symbol;
use petgraph::{algo, graphmap::DiGraphMap};
use std::{
    cell::RefCell,
    collections::{BTreeMap, BTreeSet},
    path::PathBuf,
    rc::Rc,
};

pub type ResolvedTable = ResolutionTable<AccountAddress>;
pub type ResolvedPackage = ResolutionPackage<AccountAddress>;
pub type ResolvedGraph = ResolutionGraph<AccountAddress>;

// rename_to => (from_package name, from_address_name)
pub type Renaming = BTreeMap<NamedAddress, (PackageName, NamedAddress)>;
pub type GraphIndex = PackageName;

type ResolutionTable<T> = BTreeMap<NamedAddress, T>;
type ResolvingTable = ResolutionTable<ResolvingNamedAddress>;
type ResolvingGraph = ResolutionGraph<ResolvingNamedAddress>;
type ResolvingPackage = ResolutionPackage<ResolvingNamedAddress>;

#[derive(Debug, Clone)]
pub struct ResolvingNamedAddress {
    value: Rc<RefCell<Option<AccountAddress>>>,
}

/// A `ResolutionGraph` comes in two flavors:
/// 1. a `ResolutionGraph` during resolution (some named addresses may yet be instantiated)
/// 2. a `ResolvedGraph` which is a graph after resolution in which all named addresses have been
///    assigned a value.
///
/// Named addresses can be assigned values in a couple different ways:
/// 1. They can be assigned a value in the declaring package. In this case the value of that
///    named address will always be that value.
/// 2. Can be left unassigned in the declaring package. In this case it can receive its value
///    through unification across the package graph.
///
/// Named addresses can also be renamed in a package and will be re-exported under thes new names in this case.
#[derive(Debug, Clone)]
pub struct ResolutionGraph<T> {
    /// Build options
    pub build_options: BuildConfig,
    /// Root package
    pub root_package: SourceManifest,
    /// Dependency graph
    pub graph: DiGraphMap<PackageName, ()>,
    /// A mapping of package name to its resolution
    pub package_table: BTreeMap<PackageName, ResolutionPackage<T>>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ResolutionPackage<T> {
    /// Pointer into the `ResolutionGraph.graph`
    pub resolution_graph_index: GraphIndex,
    /// source manifest for this package
    pub source_package: SourceManifest,
    /// Where this package is located on the filesystem
    pub package_path: PathBuf,
    /// The renaming of addresses performed by this package
    pub renaming: Renaming,
    /// The mapping of addresses for this package (and that are in scope for it)
    pub resolution_table: ResolutionTable<T>,
    /// The digest of the contents of all files under the package root
    pub source_digest: String,
}

impl ResolvingGraph {
    pub fn new(
        root_package: SourceManifest,
        root_package_path: PathBuf,
        build_options: BuildConfig,
    ) -> Result<ResolvingGraph> {
        let mut resolution_graph = Self {
            build_options,
            root_package: root_package.clone(),
            graph: DiGraphMap::new(),
            package_table: BTreeMap::new(),
        };

        resolution_graph
            .build_resolution_graph(root_package.clone(), root_package_path, true)
            .with_context(|| {
                format!(
                    "Unable to resolve packages for package '{}'",
                    root_package.package.name
                )
            })?;
        Ok(resolution_graph)
    }

    pub fn resolve(self) -> Result<ResolvedGraph> {
        let ResolvingGraph {
            build_options,
            root_package,
            graph,
            package_table,
        } = self;

        let mut unresolved_addresses = Vec::new();

        let resolved_package_table = package_table
            .into_iter()
            .map(|(name, package)| {
                let ResolutionPackage {
                    resolution_graph_index,
                    source_package,
                    package_path,
                    renaming,
                    resolution_table,
                    source_digest,
                } = package;

                let resolved_table = resolution_table
                    .into_iter()
                    .filter_map(|(addr_name, instantiation_opt)| {
                        match *instantiation_opt.value.borrow() {
                            None => {
                                unresolved_addresses.push(format!(
                                    "Named address '{}' in package '{}'",
                                    addr_name, name
                                ));
                                None
                            }
                            Some(addr) => Some((addr_name, addr)),
                        }
                    })
                    .collect::<BTreeMap<_, _>>();
                let resolved_pkg = ResolvedPackage {
                    resolution_graph_index,
                    source_package,
                    package_path,
                    renaming,
                    resolution_table: resolved_table,
                    source_digest,
                };
                (name, resolved_pkg)
            })
            .collect::<BTreeMap<_, _>>();

        if !unresolved_addresses.is_empty() {
            bail!(
                "Unresolved addresses found: [\n{}\n]",
                unresolved_addresses.join("\n")
            )
        }

        Ok(ResolvedGraph {
            build_options,
            root_package,
            graph,
            package_table: resolved_package_table,
        })
    }

    fn build_resolution_graph(
        &mut self,
        package: SourceManifest,
        package_path: PathBuf,
        is_root_package: bool,
    ) -> Result<()> {
        let package_name = package.package.name;
        let package_node_id = match self.package_table.get(&package_name) {
            None => self.get_or_add_node(package_name)?,
            // Same package and we've already resolved it: OK, return early
            Some(other) if other.source_package == package => return Ok(()),
            // Different packages, with same name: Not OK
            Some(other) => {
                bail!(
                    "Conflicting dependencies found: package '{}' conflicts with '{}'",
                    other.source_package.package.name,
                    package.package.name,
                )
            }
        };

        let mut renaming = BTreeMap::new();
        let mut resolution_table = BTreeMap::new();

        // include dev dependencies if in dev mode
        let additional_deps = if !self.build_options.dev_mode {
            package.dev_dependencies.clone()
        } else {
            BTreeMap::new()
        };

        for (dep_name, dep) in package
            .dependencies
            .clone()
            .into_iter()
            .chain(additional_deps.into_iter())
        {
            let dep_node_id = self.get_or_add_node(dep_name).with_context(|| {
                format!(
                    "Cycle between packages {} and {} found",
                    package_name, dep_name
                )
            })?;
            self.graph.add_edge(package_node_id, dep_node_id, ());

            let (dep_renaming, dep_resolution_table) = self
                .process_dependency(dep_name, dep, package_path.clone())
                .with_context(|| {
                    format!(
                        "While resolving dependency '{}' in package '{}'",
                        dep_name, package_name
                    )
                })?;

            ResolutionPackage::extend_renaming(&mut renaming, &dep_name, dep_renaming.clone())
                .with_context(|| {
                    format!(
                        "While resolving address renames in dependency '{}' in package '{}'",
                        dep_name, package_name
                    )
                })?;

            ResolutionPackage::extend_resolution_table(
                &mut resolution_table,
                &dep_name,
                dep_resolution_table,
                dep_renaming,
            )
            .with_context(|| {
                format!(
                    "Resolving named addresses for dependency '{}' in package '{}'",
                    dep_name, package_name
                )
            })?;
        }

        self.unify_addresses_in_package(&package, &mut resolution_table, is_root_package)?;

        let package_path = package_path.canonicalize()?;
        let source_digest = checksumdir::checksumdir(&package_path.to_string_lossy().to_string())?;

        let resolved_package = ResolutionPackage {
            resolution_graph_index: package_node_id,
            source_package: package,
            package_path,
            renaming,
            resolution_table,
            source_digest,
        };

        self.package_table.insert(package_name, resolved_package);
        Ok(())
    }

    fn unify_addresses_in_package(
        &mut self,
        package: &SourceManifest,
        resolution_table: &mut ResolvingTable,
        is_root_package: bool,
    ) -> Result<()> {
        let package_name = &package.package.name;
        for (name, addr_opt) in package
            .addresses
            .clone()
            .unwrap_or_else(BTreeMap::new)
            .into_iter()
        {
            match resolution_table.get(&name) {
                Some(other) => {
                    other.unify(addr_opt).with_context(|| {
                        format!(
                            "Unable to resolve named address '{}' in\
                                package '{}' when resolving dependencies",
                            name, package_name
                        )
                    })?;
                }
                None => {
                    resolution_table.insert(name, ResolvingNamedAddress::new(addr_opt));
                }
            }
        }

        if self.build_options.dev_mode && is_root_package {
            for (name, addr) in package
                .dev_address_assignments
                .clone()
                .unwrap_or_else(BTreeMap::new)
                .into_iter()
            {
                match resolution_table.get(&name) {
                    Some(other) => {
                        other.unify(Some(addr)).with_context(|| {
                            format!(
                                "Unable to resolve named address '{}' in\
                                    package '{}' when resolving dependencies in dev mode",
                                name, package_name
                            )
                        })?;
                    }
                    None => {
                        bail!(
                            "Found unbound dev address assignment '{} = 0x{}' in root package '{}'. \
                             Dev addresses cannot introduce new named addresses",
                            name,
                            addr.short_str_lossless(),
                            package_name
                        );
                    }
                }
            }
        }
        Ok(())
    }

    // Process a dependency. `dep_name_in_pkg` is the name assigned to the dependent package `dep`
    // in the source manifest, and we check that this name matches the name of the dependency it is
    // assigned to.
    fn process_dependency(
        &mut self,
        dep_name_in_pkg: PackageName,
        dep: Dependency,
        root_path: PathBuf,
    ) -> Result<(Renaming, ResolvingTable)> {
        let (dep_package, dep_package_dir) =
            Self::parse_package_manifest(&dep, &dep_name_in_pkg, root_path)
                .with_context(|| format!("While processing dependency '{}'", dep_name_in_pkg))?;
        self.build_resolution_graph(dep_package.clone(), dep_package_dir, false)
            .with_context(|| {
                format!("Unable to resolve package dependency '{}'", dep_name_in_pkg)
            })?;

        if dep_name_in_pkg != dep_package.package.name {
            bail!("Name of dependency declared in package '{}' does not match dependency's package name '{}'",
                dep_name_in_pkg,
                dep_package.package.name
            );
        }

        let resolving_dep = &self.package_table[&dep_name_in_pkg];
        let mut renaming = BTreeMap::new();
        let mut resolution_table = resolving_dep.resolution_table.clone();

        // check that address being renamed exists in the dep that is being renamed/imported
        if let Some(dep_subst) = dep.subst {
            for (name, rename_from_or_assign) in dep_subst.into_iter() {
                match rename_from_or_assign {
                    SubstOrRename::RenameFrom(ident) => {
                        // Make sure dep has the address that we're importing
                        if !resolving_dep.resolution_table.contains_key(&ident) {
                            bail!(
                                "Tried to rename named address {0} from package '{1}'.\
                                However, {1} does not contain that address",
                                ident,
                                dep_name_in_pkg
                            );
                        }

                        // Apply the substitution, NB that the refcell for the address's value is kept!
                        if let Some(other_val) = resolution_table.remove(&ident) {
                            resolution_table.insert(name, other_val);
                        }

                        if renaming.insert(name, (dep_name_in_pkg, ident)).is_some() {
                            bail!("Duplicate renaming of named address '{0}' found for dependency {1}",
                                name,
                                dep_name_in_pkg,
                            );
                        }
                    }
                    SubstOrRename::Assign(value) => {
                        resolution_table
                            .get(&name)
                            .map(|named_addr| named_addr.unify(Some(value)))
                            .transpose()
                            .with_context(|| {
                                format!(
                                    "Unable to assign value to named address {} in dependency {}",
                                    name, dep_name_in_pkg
                                )
                            })?;
                    }
                }
            }
        }

        Ok((renaming, resolution_table))
    }

    fn get_or_add_node(&mut self, package_name: PackageName) -> Result<GraphIndex> {
        if self.graph.contains_node(package_name) {
            // If we encounter a node that we've already added we should check for cycles
            if algo::is_cyclic_directed(&self.graph) {
                // get the first cycle. Exists because we found a cycle above.
                let mut cycle = algo::kosaraju_scc(&self.graph)[0]
                    .iter()
                    .map(|node| node.as_str().to_string())
                    .collect::<Vec<_>>();
                // Add offending node at end to complete the cycle for display
                cycle.push(package_name.as_str().to_string());
                bail!("Found cycle between packages: {}", cycle.join(" -> "));
            }
            Ok(package_name)
        } else {
            Ok(self.graph.add_node(package_name))
        }
    }

    fn parse_package_manifest(
        dep: &Dependency,
        dep_name: &PackageName,
        mut root_path: PathBuf,
    ) -> Result<(SourceManifest, PathBuf)> {
        root_path.push(&dep.local);
        match std::fs::read_to_string(&root_path.join(SourcePackageLayout::Manifest.path())) {
            Ok(contents) => {
                let source_package: SourceManifest =
                    parse_move_manifest_string(contents).and_then(parse_source_manifest)?;
                Ok((source_package, root_path))
            }
            Err(_) => Err(anyhow::format_err!(
                "Unable to find package manifest for '{}' at {:?}",
                dep_name,
                SourcePackageLayout::Manifest.path().join(root_path),
            )),
        }
    }
}

impl ResolvingPackage {
    // Extend and check for duplicate names in rename_to
    fn extend_renaming(
        renaming: &mut Renaming,
        dep_name: &PackageName,
        dep_renaming: Renaming,
    ) -> Result<()> {
        for (rename_to, rename_from) in dep_renaming.into_iter() {
            // We cannot rename multiple named addresses to the same name. In the future we'll want
            // to support this.
            if renaming.insert(rename_to, rename_from).is_some() {
                bail!(
                    "Duplicate renaming of named address '{}' found in dependency '{}'",
                    rename_to,
                    dep_name
                );
            }
        }
        Ok(())
    }

    // The resolution table contains the transitive closure of addresses that are known in that
    // package. Extends the package's resolution table and checks for duplicate renamings that
    // conflict during this process.
    fn extend_resolution_table(
        resolution_table: &mut ResolvingTable,
        dep_name: &PackageName,
        dep_resolution_table: ResolvingTable,
        dep_renaming: Renaming,
    ) -> Result<()> {
        let renames = dep_renaming
            .into_iter()
            .map(|(rename_to, (_, rename_from))| (rename_from, rename_to))
            .collect::<BTreeMap<_, _>>();

        for (addr_name, addr_value) in dep_resolution_table.into_iter() {
            let addr_name = renames.get(&addr_name).cloned().unwrap_or(addr_name);
            if let Some(other) = resolution_table.insert(addr_name, addr_value.clone()) {
                // They need to be the same refcell so resolve to the same location if there are any
                // possible reassignments
                if other.value != addr_value.value {
                    bail!(
                        "Named address '{}' in dependency '{}' is already set to '{}' but was then reassigned to '{}'",
                        &addr_name,
                        dep_name,
                        match other.value.take() {
                            None => "unassigned".to_string(),
                            Some(addr) => format!("0x{}", addr.short_str_lossless()),
                        },
                        match addr_value.value.take() {
                            None => "unassigned".to_string(),
                            Some(addr) => format!("0x{}", addr.short_str_lossless()),
                        }
                    );
                }
            }
        }

        Ok(())
    }
}

impl ResolvingNamedAddress {
    pub fn new(address_opt: Option<AccountAddress>) -> Self {
        Self {
            value: Rc::new(RefCell::new(address_opt)),
        }
    }

    pub fn unify(&self, address_opt: Option<AccountAddress>) -> Result<()> {
        match address_opt {
            None => Ok(()),
            Some(addr_val) => match &mut *self.value.borrow_mut() {
                Some(current_value) if current_value != &addr_val =>
                    bail!("Attempted to assign a different value '0x{}' to an a already-assigned named address '0x{}'",
                        addr_val.short_str_lossless(), current_value.short_str_lossless()
                    ),
                Some(_) => Ok(()),
                x @ None => {
                    *x = Some(addr_val);
                    Ok(())
                }
            },
        }
    }
}

impl ResolvedGraph {
    pub fn get_package(&self, package_ident: &PackageName) -> &ResolvedPackage {
        self.package_table.get(package_ident).unwrap()
    }
}

impl ResolvedPackage {
    pub fn get_sources(&self, config: &BuildConfig) -> Result<Vec<FileName>> {
        let mut places_to_look = Vec::new();
        let mut add_path = |layout_path: SourcePackageLayout| {
            let path = self.package_path.join(layout_path.path());
            if layout_path.is_optional() && !path.exists() {
                return;
            }
            places_to_look.push(path.to_string_lossy().to_string())
        };

        add_path(SourcePackageLayout::Sources);
        add_path(SourcePackageLayout::Scripts);

        if config.dev_mode {
            add_path(SourcePackageLayout::Examples);
            add_path(SourcePackageLayout::Tests);
        }

        Ok(find_move_filenames(&places_to_look, false)?
            .into_iter()
            .map(Symbol::from)
            .collect())
    }

    /// Returns the transitive dependencies of this package in dependency order
    pub fn transitive_dependencies(&self, resolved_graph: &ResolvedGraph) -> Vec<PackageName> {
        let mut seen = BTreeSet::new();
        let resolve_package = |(package_name, _): (&PackageName, _)| {
            let mut package_deps = resolved_graph
                .package_table
                .get(package_name)
                .unwrap()
                .transitive_dependencies(resolved_graph);
            package_deps.push(*package_name);
            package_deps
        };

        let transitive_deps: Vec<_> = if resolved_graph.build_options.dev_mode {
            self.source_package
                .dependencies
                .iter()
                .chain(self.source_package.dev_dependencies.iter())
                .flat_map(resolve_package)
                .collect()
        } else {
            self.source_package
                .dependencies
                .iter()
                .flat_map(resolve_package)
                .collect()
        };

        transitive_deps
            .into_iter()
            .filter(|ident| {
                if !seen.contains(ident) {
                    seen.insert(*ident);
                    true
                } else {
                    false
                }
            })
            .collect()
    }
}