sui_adapter_latest/static_programmable_transactions/linkage/
config.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{
5    data_store::PackageStore,
6    static_programmable_transactions::linkage::resolution::{
7        ResolutionTable, VersionConstraint, add_and_unify, get_package,
8    },
9};
10use move_binary_format::binary_config::BinaryConfig;
11use sui_types::{
12    MOVE_STDLIB_PACKAGE_ID, SUI_FRAMEWORK_PACKAGE_ID, SUI_SYSTEM_PACKAGE_ID, base_types::ObjectID,
13    error::ExecutionError,
14};
15
16/// These are the set of native packages in Sui -- importantly they can be used implicitly by
17/// different parts of the system and are not required to be explicitly imported always.
18/// Additionally, there is no versioning concerns around these as they are "stable" for a given
19/// epoch, and are the special packages that are always available, and updated in-place.
20const NATIVE_PACKAGE_IDS: &[ObjectID] = &[
21    SUI_FRAMEWORK_PACKAGE_ID,
22    SUI_SYSTEM_PACKAGE_ID,
23    MOVE_STDLIB_PACKAGE_ID,
24];
25
26/// Metadata and shared operations for the PTB linkage analysis.
27#[derive(Debug)]
28pub struct ResolutionConfig {
29    /// Config to use for the linkage analysis.
30    pub linkage_config: LinkageConfig,
31    /// Config to use for the binary analysis (needed for deserialization to determine if a
32    /// function is a non-public entry function).
33    pub binary_config: BinaryConfig,
34}
35
36/// Configuration for the linkage analysis.
37#[derive(Debug)]
38pub struct LinkageConfig {
39    /// Whether system packages should always be included as a member in the generated linkage.
40    /// This is almost always true except for system transactions and genesis transactions.
41    pub always_include_system_packages: bool,
42}
43
44impl ResolutionConfig {
45    pub fn new(linkage_config: LinkageConfig, binary_config: BinaryConfig) -> Self {
46        Self {
47            linkage_config,
48            binary_config,
49        }
50    }
51}
52
53impl LinkageConfig {
54    pub fn legacy_linkage_settings(always_include_system_packages: bool) -> Self {
55        Self {
56            always_include_system_packages,
57        }
58    }
59
60    pub(crate) fn resolution_table_with_native_packages(
61        &self,
62        store: &dyn PackageStore,
63    ) -> Result<ResolutionTable, ExecutionError> {
64        let mut resolution_table = ResolutionTable::empty();
65        if self.always_include_system_packages {
66            for id in NATIVE_PACKAGE_IDS {
67                #[cfg(debug_assertions)]
68                {
69                    let package = get_package(id, store)?;
70                    debug_assert_eq!(package.id(), *id);
71                    debug_assert_eq!(package.original_package_id(), *id);
72                }
73                add_and_unify(id, store, &mut resolution_table, VersionConstraint::exact)?;
74            }
75        }
76
77        Ok(resolution_table)
78    }
79}