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

[framework-snaps] Update snapshot loading #21193

Merged
merged 2 commits into from
Feb 13, 2025
Merged
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
35 changes: 32 additions & 3 deletions crates/sui-framework-snapshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::collections::{BTreeMap, BTreeSet};
use std::{fs, io::Read, path::PathBuf};
use sui_framework::{SystemPackage, SystemPackageMetadata};
use sui_types::base_types::ObjectID;
Expand Down Expand Up @@ -100,8 +100,7 @@ pub fn update_bytecode_snapshot_manifest(
}

pub fn load_bytecode_snapshot(protocol_version: u64) -> anyhow::Result<Vec<SystemPackage>> {
let mut snapshot_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
snapshot_path.extend(["bytecode_snapshot", protocol_version.to_string().as_str()]);
let snapshot_path = snapshot_path_for_version(protocol_version)?;
let mut snapshots: BTreeMap<ObjectID, SystemPackage> = fs::read_dir(&snapshot_path)?
.flatten()
.map(|entry| {
Expand All @@ -128,3 +127,33 @@ pub fn load_bytecode_snapshot(protocol_version: u64) -> anyhow::Result<Vec<Syste
pub fn manifest_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("manifest.json")
}

/// Given a protocol version:
/// * The path to the snapshot directory for that version is returned, if it exists.
/// * If the version is greater than the latest snapshot version, then `Ok(None)` is returned.
/// * If the version does not exist, but there are snapshots present with versions greater than
/// `version`, then the smallest snapshot number greater than `version` is returned.
fn snapshot_path_for_version(version: u64) -> anyhow::Result<PathBuf> {
let snapshot_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("bytecode_snapshot");
let mut snapshots = BTreeSet::new();

for entry in fs::read_dir(&snapshot_dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
if let Some(snapshot_number) = path
.file_name()
.and_then(|n| n.to_str())
.and_then(|n| n.parse::<u64>().ok())
{
snapshots.insert(snapshot_number);
}
}
}

snapshots
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you probably want the largest version that is <= the current version, instead of the smallest version that is >=. See sui::framework_versions::version_for_protocol

.range(version..)
.next()
.map(|v| snapshot_dir.join(v.to_string()))
.ok_or_else(|| anyhow::anyhow!("No snapshot found for version {}", version))
}
Loading