Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Use pathbuf for remote externalities (#8480)
Browse files Browse the repository at this point in the history
* Combine SnapshotConfig string fields name and directory into single PathBuf field named path

* Update Cargo.lock

* fix test build failure
  • Loading branch information
hardliner66 authored and kianenigma committed Jul 4, 2021
1 parent 3655f9b commit f0b8bf8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 52 deletions.
26 changes: 11 additions & 15 deletions utils/frame/remote-externalities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,23 +181,19 @@ impl<B: BlockT> OnlineConfig<B> {
/// Configuration of the state snapshot.
#[derive(Clone)]
pub struct SnapshotConfig {
// TODO: I could mix these two into one filed, but I think separate is better bc one can be
// configurable while one not.
/// File name.
pub name: String,
/// Base directory.
pub directory: String,
/// The path to the snapshot file.
pub path: PathBuf,
}

impl Default for SnapshotConfig {
fn default() -> Self {
Self { name: "SNAPSHOT".into(), directory: ".".into() }
impl SnapshotConfig {
pub fn new<P: Into<PathBuf>>(path: P) -> Self {
Self { path: path.into() }
}
}

impl SnapshotConfig {
fn path(&self) -> PathBuf {
Path::new(&self.directory).join(self.name.clone())
impl Default for SnapshotConfig {
fn default() -> Self {
Self { path: Path::new("SNAPSHOT").into() }
}
}

Expand Down Expand Up @@ -319,12 +315,12 @@ impl<B: BlockT> Builder<B> {

async fn pre_build(mut self) -> Result<Vec<KeyPair>, &'static str> {
let mut base_kv = match self.mode.clone() {
Mode::Offline(config) => self.load_state_snapshot(&config.state_snapshot.path())?,
Mode::Offline(config) => self.load_state_snapshot(&config.state_snapshot.path)?,
Mode::Online(config) => {
self.init_remote_client().await?;
let kp = self.load_remote().await?;
if let Some(c) = config.state_snapshot {
self.save_state_snapshot(&kp, &c.path())?;
self.save_state_snapshot(&kp, &c.path)?;
}
kp
}
Expand Down Expand Up @@ -399,7 +395,7 @@ mod tests {
init_logger();
Builder::<Block>::new()
.mode(Mode::Offline(OfflineConfig {
state_snapshot: SnapshotConfig { name: "test_data/proxy_test".into(), ..Default::default() },
state_snapshot: SnapshotConfig { path: "test_data/proxy_test".into() },
}))
.build()
.await
Expand Down
41 changes: 4 additions & 37 deletions utils/frame/try-runtime/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,14 @@ pub struct TryRuntimeCmd {
pub enum State {
/// Use a state snapshot as state to run the migration.
Snap {
#[structopt(flatten)]
snapshot_path: SnapshotPath,
snapshot_path: PathBuf,
},

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

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

#[derive(Debug, structopt::StructOpt)]
pub struct SnapshotPath {
/// The directory of the state snapshot.
#[structopt(short, long, default_value = ".")]
directory: String,

/// The file name of the state snapshot.
#[structopt(default_value = "SNAPSHOT")]
file_name: String,
}

impl FromStr for SnapshotPath {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let p: PathBuf = s.parse().map_err(|_| "invalid path")?;
let parent = p.parent();
let file_name = p.file_name();

file_name.and_then(|file_name| Some(Self {
directory: parent.map(|p| p.to_string_lossy().into()).unwrap_or(".".to_string()),
file_name: file_name.to_string_lossy().into()
})).ok_or("invalid path")
}
}

impl TryRuntimeCmd {
pub async fn run<B, ExecDispatch>(&self, config: Configuration) -> sc_cli::Result<()>
where
Expand Down Expand Up @@ -182,12 +156,8 @@ impl TryRuntimeCmd {
use remote_externalities::{Builder, Mode, SnapshotConfig, OfflineConfig, OnlineConfig};
let builder = match &self.state {
State::Snap { snapshot_path } => {
let SnapshotPath { directory, file_name } = snapshot_path;
Builder::<B>::new().mode(Mode::Offline(OfflineConfig {
state_snapshot: SnapshotConfig {
name: file_name.into(),
directory: directory.into(),
},
state_snapshot: SnapshotConfig::new(snapshot_path),
}))
},
State::Live {
Expand All @@ -197,10 +167,7 @@ impl TryRuntimeCmd {
modules
} => Builder::<B>::new().mode(Mode::Online(OnlineConfig {
uri: url.into(),
state_snapshot: snapshot_path.as_ref().map(|c| SnapshotConfig {
name: c.file_name.clone(),
directory: c.directory.clone(),
}),
state_snapshot: snapshot_path.as_ref().map(SnapshotConfig::new),
modules: modules.clone().unwrap_or_default(),
at: match block_at {
Some(b) => Some(b.parse().map_err(|e| format!("Could not parse hash: {:?}", e))?),
Expand Down

0 comments on commit f0b8bf8

Please sign in to comment.