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

Use pathbuf for remote externalities #8480

Merged
3 commits merged into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 0 additions & 16 deletions Cargo.lock

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

24 changes: 10 additions & 14 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
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