Skip to content

Commit

Permalink
fix more runtime errors
Browse files Browse the repository at this point in the history
  • Loading branch information
weezy20 committed Mar 8, 2024
1 parent 685f3c7 commit dcf82e4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 28 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ tracing-subscriber = { version = "0.3", optional = true }
url = { version = "2.5", optional = true }
zombienet-sdk = { git = "https://github.com/paritytech/zombienet-sdk", optional = true }
zombienet-support = { git = "https://github.com/paritytech/zombienet-sdk", optional = true }
uuid = { version = "1.7.0", features = ["v4"] }


[features]
Expand Down
17 changes: 13 additions & 4 deletions src/commands/add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) struct AddArgs {
#[command(subcommand)]
/// Pallet to add to the runtime
pub(crate) pallet: AddPallet,
#[arg(global = true, short)]
#[arg(global = true, short, long)]
/// Runtime path;
/// Cargo Manifest path will be inferred as `../Cargo.toml`
pub(crate) runtime: Option<String>,
Expand All @@ -35,11 +35,20 @@ pub(crate) struct FrameArgs {
impl AddArgs {
pub(crate) fn execute(&self) -> anyhow::Result<()> {
let runtime_path = match self.runtime {
Some(ref s) => PathBuf::from(s),
Some(ref s) => {
let path = PathBuf::from(s);
// println!("Using runtime path: {}", &path.display());
if !path.exists() {
anyhow::bail!("Runtime path does not exist: {}", path.display());
}
path
}
None => {
// TODO: Fetch runtime either from cache
// Fix: This is a placeholder path, should not be used
PathBuf::from("my-app/runtime/src/lib.rs")
unimplemented!(
"provide a runtime path until cache is implemented: --runtime <path>"
);
}
};
let pallet = match self.pallet {
Expand All @@ -48,7 +57,7 @@ impl AddArgs {
eprintln!("Sorry, frame pallets cannot be added right now");
std::process::exit(1);
// format!("FRAME-pallet-{name}")
},
}
};
pallet_engine::execute(self.pallet.clone(), runtime_path.clone())?;
println!("Added {}\n-> to {}", pallet, runtime_path.display());
Expand Down
23 changes: 16 additions & 7 deletions src/engines/pallet_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ pub struct PalletEngine {
/// Cursor for tracking where we are in the output
cursor: usize,
}
impl Drop for PalletEngine {
fn drop(&mut self) {
let output_dir = self.output.parent().unwrap();
let _ = fs::remove_dir_all(output_dir);
}
}

/// PalletDetails is data generated after parsing of a given `input` runtime file
/// This will make observations as to which pallets are there, if there are instances of the pallets
Expand Down Expand Up @@ -116,18 +122,19 @@ impl PalletEngine {
/// Call this to finalize edits
pub fn merge(self) -> anyhow::Result<()> {
fs::copy(&self.output, &self.input)?;
fs::remove_file(self.output);
fs::remove_file(&self.output);
Ok(())
}
/// Create a new PalletEngine
pub fn new(input: &PathBuf) -> anyhow::Result<Self> {
let tmp_dir = tempfile::TempDir::new()?;
let output: PathBuf = tmp_dir.path().join("temp_lib.rs");
let tmp_dir = PathBuf::from(format!("/tmp/pallet_engine_{}", uuid::Uuid::new_v4()));
fs::create_dir(&tmp_dir).context("Failed to create temporary directory for PalletEngine")?;
let output: PathBuf = tmp_dir.join("out_lib.rs");
// Open the file specified in `output`. If non-empty, delete its contents.
if output.exists() && output.is_file() {
std::fs::remove_file(output.as_path())?;
}
File::create(output.as_path()).context(format!(
File::create(&output).context(format!(
"Failed to create PalletEngine with output: {}",
output.display()
))?;
Expand Down Expand Up @@ -363,7 +370,10 @@ impl PalletEngine {
snip.push_str(&line?);
snip.push('\n');
}
let mut file = OpenOptions::new().append(true).open(&self.output)?;
let mut file = OpenOptions::new()
.append(true)
.open(&self.output)
.context("fn append_lines_from - cannot open output")?;
file.write_all(snip.as_bytes())?;
Ok(())
}
Expand Down Expand Up @@ -448,13 +458,12 @@ impl PalletEngine {
let mut ultimate = i
.pallets
.last()
.expect("Fatal: No pallets defined in construct_runtime!")
.ok_or(anyhow!("Fatal: No pallets defined in construct_runtime!"))?
.clone();
ultimate.index = new_pallet.index;
ultimate.path.inner.segments[0].ident = new_pallet.path;
ultimate.name = new_pallet.name;
i.pallets.push(ultimate);
Ok(())
}
RuntimeDeclaration::Explicit(e) => {
todo!()
Expand Down
1 change: 1 addition & 0 deletions src/engines/pallet_engine/pallet_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use syn::Ident;

/// Format containing necessary information for appending pallets
#[derive(Debug)]
pub(super) struct AddPalletEntry {
pub(super) index: Option<u8>,
pub(super) path: Ident,
Expand Down
43 changes: 26 additions & 17 deletions src/engines/pallet_engine/steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use log::{error, warn};
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use Steps::*;
use super::State;
/// Define the steps needed for a particular pallet insertion
#[derive(Debug)]
pub(super) enum Steps {
/// Import statements for pallet
RuntimePalletImport(TokenStream2),
Expand All @@ -25,13 +27,10 @@ pub(super) enum Steps {
ChainspecGenesisImport(TokenStream2),
/// Node specific imports if the above two are required
NodePalletDependency(Dependency),
/// PalletEngine Specific Commands
Commands,
}
enum Commands {
SwitchToConfig,
SwitchToCRT,
/// PalletEngine State transitions
SwitchTo(State),
}

macro_rules! steps {
($cmd:expr) => {
steps.push($cmd);
Expand All @@ -46,18 +45,18 @@ pub(super) fn step_builder(pallet: AddPallet) -> Result<Vec<Steps>> {
match pallet {
// Adding a pallet-parachain-template requires 5 distinct steps
AddPallet::Template => {
steps.push(RuntimePalletDependency(Dependency::runtime_template()));
// steps.push(RuntimePalletDependency(Dependency::runtime_template()));
steps.push(RuntimePalletImport(quote!(
pub use pallet_parachain_template;
)));
steps.push(SwitchToConfig);
steps.push(SwitchTo(State::Config));
steps.push(RuntimePalletConfiguration(quote!(
/// Configure the pallet template in pallets/template.
impl pallet_parachain_template::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}
)));
steps.push(SwitchToCrt);
steps.push(SwitchTo(State::ConstructRuntime));
steps.push(ConstructRuntimeEntry(AddPalletEntry::new(
// Index
None,
Expand All @@ -67,7 +66,7 @@ pub(super) fn step_builder(pallet: AddPallet) -> Result<Vec<Steps>> {
// TODO (high priority): implement name conflict resolution strategy
"Template",
)));
steps.push(NodePalletDependency(Dependency::node_template()))
// steps.push(NodePalletDependency(Dependency::node_template()))
}
AddPallet::Frame(_) => unimplemented!("Frame pallets not yet implemented"),
};
Expand All @@ -76,6 +75,7 @@ pub(super) fn step_builder(pallet: AddPallet) -> Result<Vec<Steps>> {
/// Execute steps on PalletEngine.
/// Each execution edits a file.
/// Sequence of steps matters so take care when ordering them
/// Works only for Template pallets at the moment.. See config and CRT inserts
pub(super) fn run_steps(mut pe: PalletEngine, steps: Vec<Steps>) -> Result<()> {
use super::State::*;
pe.prepare_output()?;
Expand All @@ -101,32 +101,40 @@ pub(super) fn run_steps(mut pe: PalletEngine, steps: Vec<Steps>) -> Result<()> {
}
};
}
SwitchToConfig => pe.prepare_config()?,
SwitchTo(State::Config) => pe.prepare_config()?,
RuntimePalletConfiguration(config) => {
if pe.state != Config {
// Not really a fatal error, but may cause unexpected behaviour
warn!("Engine not in Config state, executing config insertion anyways");
}
pe.insert_config(config)?
},
SwitchToCRT => pe.prepare_crt()?,
ConstructRuntimeEntry(p) => pe.add_pallet_runtime(p)?,
}
SwitchTo(State::ConstructRuntime) => pe.prepare_crt()?,
ConstructRuntimeEntry(_entry) => {
// TODO : Switch to add_pallet_runtime
// pe.add_pallet_runtime(entry)?
pe.insert_str_runtime("\t\tTemplate: pallet_parachain_template = 100,")?;
}
// ListBenchmarks(step) => pe.insert(step),
// ListBenchmarks(step) => pe.insert(step),
// ChainspecGenesisConfig(step) => pe.insert(step),
// ChainspecGenesisImport(step) => pe.insert(step),
// NodePalletDependency(step) => pe.insert(step),
_ => {
unimplemented!()
step => {
unimplemented!("{step:?} unimplemented")
}
}; // -- match --
} // -- for --
// Finalize runtime edits
pe.merge()?;
// TODO: Finalize toml and chainspec edits
Ok(())
}

mod dependency {
use strum_macros::{Display, EnumString};

#[derive(EnumString, Display)]
#[derive(EnumString, Display, Debug)]
pub(in crate::engines::pallet_engine) enum Features {
#[strum(serialize = "std")]
Std,
Expand All @@ -136,6 +144,7 @@ mod dependency {
TryRuntime,
Custom(String),
}
#[derive(Debug)]
pub(in crate::engines::pallet_engine) struct Dependency {
features: Vec<Features>,
path: String,
Expand Down

0 comments on commit dcf82e4

Please sign in to comment.