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

moved sui::framework_versions to sui_package_management::system_package_version #21189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/sui-package-management/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ tracing.workspace = true
sui-json-rpc-types.workspace = true
sui-sdk.workspace = true
sui-types.workspace = true
sui-protocol-config.workspace = true

move-core-types.workspace = true
move-package.workspace = true
move-symbol-pool.workspace = true

[build-dependencies]
sui-framework-snapshot.workspace = true
anyhow.workspace = true
16 changes: 8 additions & 8 deletions crates/sui/build.rs → crates/sui-package-management/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use std::{
};
use sui_framework_snapshot::{load_bytecode_snapshot_manifest, manifest_path};

/// Output a file `OUT_DIR/framework_version_table.rs` containing the contents of the manifest as a
/// rust literal of type `[(ProtocolVersion, FrameworkVersion)]`. This is included as the
/// static [framework_versions::VERSION_TABLE]
fn generate_framework_version_table() -> anyhow::Result<()> {
/// Output a file `OUT_DIR/system_packages_version_table.rs` containing the contents of the manifest as a
/// rust literal of type `[(ProtocolVersion, SystemPackages)]`. This is included as the
/// static [system_packaes::VERSION_TABLE]
fn generate_system_packages_version_table() -> anyhow::Result<()> {
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("framework_version_table.rs");
let dest_path = Path::new(&out_dir).join("system_packages_version_table.rs");

let manifest_path = manifest_path().to_string_lossy().into_owned();
let manifest = load_bytecode_snapshot_manifest();
Expand All @@ -26,14 +26,14 @@ fn generate_framework_version_table() -> anyhow::Result<()> {
let hash = &entry.git_revision;
writeln!(
&mut file,
" (ProtocolVersion::new( {version:>2} ), FrameworkVersion {{"
" (ProtocolVersion::new( {version:>2} ), SystemPackagesVersion {{"
)?;
writeln!(&mut file, " git_revision: \"{hash}\".into(),")?;
writeln!(&mut file, " packages: [")?;
for package in entry.packages.iter() {
writeln!(
&mut file,
" FrameworkPackage {{ package_name: \"{}\".into(), repo_path: \"{}\".into() }},",
" SystemPackage {{ package_name: \"{}\".into(), repo_path: \"{}\".into() }},",
package.name,
package.path,
)?;
Expand All @@ -50,5 +50,5 @@ fn generate_framework_version_table() -> anyhow::Result<()> {
}

fn main() {
generate_framework_version_table().unwrap();
generate_system_packages_version_table().unwrap();
}
2 changes: 2 additions & 0 deletions crates/sui-package-management/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use sui_json_rpc_types::{get_new_package_obj_from_response, SuiTransactionBlockR
use sui_sdk::wallet_context::WalletContext;
use sui_types::base_types::ObjectID;

pub mod system_package_versions;

const PUBLISHED_AT_MANIFEST_FIELD: &str = "published-at";

pub enum LockCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@ use std::{collections::BTreeMap, sync::LazyLock};
use anyhow::Context;
use sui_protocol_config::ProtocolVersion;

/// Static mapping from protocol versions to the metadata for the framework
// Generated by [generate_framework_version_table] in build.rs
static VERSION_TABLE: LazyLock<BTreeMap<ProtocolVersion, FrameworkVersion>> = LazyLock::new(|| {
BTreeMap::from(include!(concat!(
env!("OUT_DIR"),
"/framework_version_table.rs"
)))
});
/// Static mapping from protocol versions to the metadata for the system packages
// Generated by [generate_system_packages_version_table] in build.rs
static VERSION_TABLE: LazyLock<BTreeMap<ProtocolVersion, SystemPackagesVersion>> =
LazyLock::new(|| {
BTreeMap::from(include!(concat!(
env!("OUT_DIR"),
"/system_packages_version_table.rs"
)))
});

#[derive(Debug)]
pub struct FrameworkVersion {
pub struct SystemPackagesVersion {
pub git_revision: String,
pub packages: Vec<FrameworkPackage>,
pub packages: Vec<SystemPackage>,
}

#[derive(Debug)]
pub struct FrameworkPackage {
pub struct SystemPackage {
/// The name of the package, e.g. "Sui"
pub package_name: String,

Expand All @@ -31,92 +32,99 @@ pub struct FrameworkPackage {
pub repo_path: String,
}

impl PartialEq for FrameworkVersion {
impl PartialEq for SystemPackagesVersion {
fn eq(&self, other: &Self) -> bool {
self.git_revision == other.git_revision
}
}

/// Return the framework snapshot for the latest known protocol version
pub fn latest_framework() -> &'static FrameworkVersion {
/// Return the system packages snapshot for the latest known protocol version
pub fn latest_system_packages() -> &'static SystemPackagesVersion {
VERSION_TABLE
.last_key_value()
.expect("known framework version table should be nonempty")
.expect("known system package version table should be nonempty")
.1
}

/// Return the latest protocol version that is not newer than the requested `version`
/// (or `Err` if there is no such version).
///
/// The returned [ProtocolVersion] is the protocol version that introduced the returned
/// [FrameworkVersion]; this may be older than the requested `version` if either:
/// 1. the framework did not change when `version` was released, or
/// [SystemPackagesVersion]; this may be older than the requested `version` if either:
/// 1. the system packages did not change when `version` was released, or
/// 2. this binary is older than the requested version and therefore doesn't know about the latest
/// framework version
/// version of the system packages
///
/// You can distinguish these cases by comparing `version` with [ProtocolVersion::MAX].
pub fn framework_for_protocol(
pub fn system_packages_for_protocol(
version: ProtocolVersion,
) -> anyhow::Result<(&'static FrameworkVersion, ProtocolVersion)> {
let (protocol, framework) = VERSION_TABLE
) -> anyhow::Result<(&'static SystemPackagesVersion, ProtocolVersion)> {
let (protocol, system_packages) = VERSION_TABLE
.range(..=version)
.next_back()
.context(format!("Unrecognized protocol version {version:?}"))?;
Ok((framework, *protocol))
Ok((system_packages, *protocol))
}

#[test]
/// There is at least one known framework version
/// There is at least one known version of the system packages
fn test_nonempty_version_table() {
assert!(!VERSION_TABLE.is_empty());
}

#[test]
/// the hash for a specific version that we have one for is correctly returned
fn test_exact_version() {
let (framework, protocol) = framework_for_protocol(4.into()).unwrap();
let (system_packages, protocol) = system_packages_for_protocol(4.into()).unwrap();
assert_eq!(
framework.git_revision,
system_packages.git_revision,
"f5d26f1b3ae89f68cb66f3a007e90065e5286905"
);
assert_eq!(protocol, 4.into());
assert!(framework.packages.iter().any(|p| p.package_name == "Sui"));
assert!(system_packages
.packages
.iter()
.any(|p| p.package_name == "Sui"));
}

#[test]
/// we get the right hash for a version that we don't have an exact entry for
fn test_gap_version() {
// versions 56 and 57 are missing in the manifest; version 55 should be returned
assert_eq!(
framework_for_protocol(56.into()).unwrap(),
framework_for_protocol(55.into()).unwrap(),
system_packages_for_protocol(56.into()).unwrap(),
system_packages_for_protocol(55.into()).unwrap(),
);
assert_eq!(
framework_for_protocol(57.into()).unwrap(),
framework_for_protocol(55.into()).unwrap(),
system_packages_for_protocol(57.into()).unwrap(),
system_packages_for_protocol(55.into()).unwrap(),
);
// version 58 is present though!
assert_ne!(
framework_for_protocol(58.into()).unwrap(),
framework_for_protocol(55.into()).unwrap(),
system_packages_for_protocol(58.into()).unwrap(),
system_packages_for_protocol(55.into()).unwrap(),
);
}

#[test]
/// we get the correct hash for the latest known protocol version
fn test_version_latest() {
assert_eq!(
framework_for_protocol(ProtocolVersion::MAX).unwrap().0,
latest_framework()
system_packages_for_protocol(ProtocolVersion::MAX)
.unwrap()
.0,
latest_system_packages()
);
assert_eq!(
framework_for_protocol(ProtocolVersion::MAX + 1).unwrap().0,
latest_framework()
system_packages_for_protocol(ProtocolVersion::MAX + 1)
.unwrap()
.0,
latest_system_packages()
);
}

#[test]
/// we get an error if the protocol version is too small or too large
fn test_version_errors() {
assert!(framework_for_protocol(0.into()).is_err());
assert!(system_packages_for_protocol(0.into()).is_err());
}
4 changes: 0 additions & 4 deletions crates/sui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ sui-test-transaction-builder.workspace = true
sui-protocol-config.workspace = true
serde_json.workspace = true

[build-dependencies]
sui-framework-snapshot.workspace = true
anyhow.workspace = true

[target.'cfg(msim)'.dependencies]
msim.workspace = true

Expand Down
1 change: 0 additions & 1 deletion crates/sui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod clever_error_rendering;
pub mod console;
pub mod displays;
pub mod fire_drill;
pub mod framework_versions;
pub mod genesis_ceremony;
pub mod genesis_inspector;
pub mod key_identity;
Expand Down
Loading