Skip to content

Commit ae8c50b

Browse files
hardliner66hirschenberger
authored andcommitted
Use pathbuf for remote externalities (paritytech#8480)
* Combine SnapshotConfig string fields name and directory into single PathBuf field named path * Update Cargo.lock * fix test build failure
1 parent cec6165 commit ae8c50b

3 files changed

Lines changed: 15 additions & 68 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/frame/remote-externalities/src/lib.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,19 @@ impl<B: BlockT> OnlineConfig<B> {
181181
/// Configuration of the state snapshot.
182182
#[derive(Clone)]
183183
pub struct SnapshotConfig {
184-
// TODO: I could mix these two into one filed, but I think separate is better bc one can be
185-
// configurable while one not.
186-
/// File name.
187-
pub name: String,
188-
/// Base directory.
189-
pub directory: String,
184+
/// The path to the snapshot file.
185+
pub path: PathBuf,
190186
}
191187

192-
impl Default for SnapshotConfig {
193-
fn default() -> Self {
194-
Self { name: "SNAPSHOT".into(), directory: ".".into() }
188+
impl SnapshotConfig {
189+
pub fn new<P: Into<PathBuf>>(path: P) -> Self {
190+
Self { path: path.into() }
195191
}
196192
}
197193

198-
impl SnapshotConfig {
199-
fn path(&self) -> PathBuf {
200-
Path::new(&self.directory).join(self.name.clone())
194+
impl Default for SnapshotConfig {
195+
fn default() -> Self {
196+
Self { path: Path::new("SNAPSHOT").into() }
201197
}
202198
}
203199

@@ -319,12 +315,12 @@ impl<B: BlockT> Builder<B> {
319315

320316
async fn pre_build(mut self) -> Result<Vec<KeyPair>, &'static str> {
321317
let mut base_kv = match self.mode.clone() {
322-
Mode::Offline(config) => self.load_state_snapshot(&config.state_snapshot.path())?,
318+
Mode::Offline(config) => self.load_state_snapshot(&config.state_snapshot.path)?,
323319
Mode::Online(config) => {
324320
self.init_remote_client().await?;
325321
let kp = self.load_remote().await?;
326322
if let Some(c) = config.state_snapshot {
327-
self.save_state_snapshot(&kp, &c.path())?;
323+
self.save_state_snapshot(&kp, &c.path)?;
328324
}
329325
kp
330326
}
@@ -399,7 +395,7 @@ mod tests {
399395
init_logger();
400396
Builder::<Block>::new()
401397
.mode(Mode::Offline(OfflineConfig {
402-
state_snapshot: SnapshotConfig { name: "test_data/proxy_test".into(), ..Default::default() },
398+
state_snapshot: SnapshotConfig { path: "test_data/proxy_test".into() },
403399
}))
404400
.build()
405401
.await

utils/frame/try-runtime/cli/src/lib.rs

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,14 @@ pub struct TryRuntimeCmd {
6767
pub enum State {
6868
/// Use a state snapshot as state to run the migration.
6969
Snap {
70-
#[structopt(flatten)]
71-
snapshot_path: SnapshotPath,
70+
snapshot_path: PathBuf,
7271
},
7372

7473
/// Use a live chain to run the migration.
7574
Live {
7675
/// An optional state snapshot file to WRITE to. Not written if set to `None`.
7776
#[structopt(short, long)]
78-
snapshot_path: Option<SnapshotPath>,
77+
snapshot_path: Option<PathBuf>,
7978

8079
/// The block hash at which to connect.
8180
/// Will be latest finalized head if not provided.
@@ -118,31 +117,6 @@ fn parse_url(s: &str) -> Result<String, &'static str> {
118117
}
119118
}
120119

121-
#[derive(Debug, structopt::StructOpt)]
122-
pub struct SnapshotPath {
123-
/// The directory of the state snapshot.
124-
#[structopt(short, long, default_value = ".")]
125-
directory: String,
126-
127-
/// The file name of the state snapshot.
128-
#[structopt(default_value = "SNAPSHOT")]
129-
file_name: String,
130-
}
131-
132-
impl FromStr for SnapshotPath {
133-
type Err = &'static str;
134-
fn from_str(s: &str) -> Result<Self, Self::Err> {
135-
let p: PathBuf = s.parse().map_err(|_| "invalid path")?;
136-
let parent = p.parent();
137-
let file_name = p.file_name();
138-
139-
file_name.and_then(|file_name| Some(Self {
140-
directory: parent.map(|p| p.to_string_lossy().into()).unwrap_or(".".to_string()),
141-
file_name: file_name.to_string_lossy().into()
142-
})).ok_or("invalid path")
143-
}
144-
}
145-
146120
impl TryRuntimeCmd {
147121
pub async fn run<B, ExecDispatch>(&self, config: Configuration) -> sc_cli::Result<()>
148122
where
@@ -182,12 +156,8 @@ impl TryRuntimeCmd {
182156
use remote_externalities::{Builder, Mode, SnapshotConfig, OfflineConfig, OnlineConfig};
183157
let builder = match &self.state {
184158
State::Snap { snapshot_path } => {
185-
let SnapshotPath { directory, file_name } = snapshot_path;
186159
Builder::<B>::new().mode(Mode::Offline(OfflineConfig {
187-
state_snapshot: SnapshotConfig {
188-
name: file_name.into(),
189-
directory: directory.into(),
190-
},
160+
state_snapshot: SnapshotConfig::new(snapshot_path),
191161
}))
192162
},
193163
State::Live {
@@ -197,10 +167,7 @@ impl TryRuntimeCmd {
197167
modules
198168
} => Builder::<B>::new().mode(Mode::Online(OnlineConfig {
199169
uri: url.into(),
200-
state_snapshot: snapshot_path.as_ref().map(|c| SnapshotConfig {
201-
name: c.file_name.clone(),
202-
directory: c.directory.clone(),
203-
}),
170+
state_snapshot: snapshot_path.as_ref().map(SnapshotConfig::new),
204171
modules: modules.clone().unwrap_or_default(),
205172
at: match block_at {
206173
Some(b) => Some(b.parse().map_err(|e| format!("Could not parse hash: {:?}", e))?),

0 commit comments

Comments
 (0)