Skip to content

Commit e491c96

Browse files
[framework-snaps] Update snapshot loading (MystenLabs#21193)
Since deployed protocol versions are not guaranteed to be sequential, snapshots are not guaranteed to be sequential (e.g., 45 may exist, 46 may not, but 47 may). Previously when trying to load a snapshot for a protocol version we would only load a snapshot if there was one for that exact version, otherwise we'd default to the newest version of the framework. This is incorrect, and @dariorussi has run into problems with this. This updates snapshot loading so if a snapshot for a particular version `v` is not found, the least version greater than the given version is returned. ## Test plan CI --- ## Release notes Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required. For each box you select, include information after the relevant heading that describes the impact of your changes that a user might notice and any actions they must take to implement updates. - [ ] Protocol: - [ ] Nodes (Validators and Full nodes): - [ ] gRPC: - [ ] JSON-RPC: - [ ] GraphQL: - [ ] CLI: - [ ] Rust SDK: --------- Co-authored-by: ronny-mysten <[email protected]>
1 parent 52756b4 commit e491c96

File tree

1 file changed

+32
-3
lines changed
  • crates/sui-framework-snapshot/src

1 file changed

+32
-3
lines changed

crates/sui-framework-snapshot/src/lib.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use serde::{Deserialize, Serialize};
5-
use std::collections::BTreeMap;
5+
use std::collections::{BTreeMap, BTreeSet};
66
use std::{fs, io::Read, path::PathBuf};
77
use sui_framework::{SystemPackage, SystemPackageMetadata};
88
use sui_types::base_types::ObjectID;
@@ -100,8 +100,7 @@ pub fn update_bytecode_snapshot_manifest(
100100
}
101101

102102
pub fn load_bytecode_snapshot(protocol_version: u64) -> anyhow::Result<Vec<SystemPackage>> {
103-
let mut snapshot_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
104-
snapshot_path.extend(["bytecode_snapshot", protocol_version.to_string().as_str()]);
103+
let snapshot_path = snapshot_path_for_version(protocol_version)?;
105104
let mut snapshots: BTreeMap<ObjectID, SystemPackage> = fs::read_dir(&snapshot_path)?
106105
.flatten()
107106
.map(|entry| {
@@ -128,3 +127,33 @@ pub fn load_bytecode_snapshot(protocol_version: u64) -> anyhow::Result<Vec<Syste
128127
pub fn manifest_path() -> PathBuf {
129128
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("manifest.json")
130129
}
130+
131+
/// Given a protocol version:
132+
/// * The path to the snapshot directory for that version is returned, if it exists.
133+
/// * If the version is greater than the latest snapshot version, then `Ok(None)` is returned.
134+
/// * If the version does not exist, but there are snapshots present with versions greater than
135+
/// `version`, then the smallest snapshot number greater than `version` is returned.
136+
fn snapshot_path_for_version(version: u64) -> anyhow::Result<PathBuf> {
137+
let snapshot_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("bytecode_snapshot");
138+
let mut snapshots = BTreeSet::new();
139+
140+
for entry in fs::read_dir(&snapshot_dir)? {
141+
let entry = entry?;
142+
let path = entry.path();
143+
if path.is_dir() {
144+
if let Some(snapshot_number) = path
145+
.file_name()
146+
.and_then(|n| n.to_str())
147+
.and_then(|n| n.parse::<u64>().ok())
148+
{
149+
snapshots.insert(snapshot_number);
150+
}
151+
}
152+
}
153+
154+
snapshots
155+
.range(version..)
156+
.next()
157+
.map(|v| snapshot_dir.join(v.to_string()))
158+
.ok_or_else(|| anyhow::anyhow!("No snapshot found for version {}", version))
159+
}

0 commit comments

Comments
 (0)