-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support chain spec generators * feat: enable specification of runtime versions (chain spec generation) * chore: add additional network config examples, used for manual testing * build(deps): bump zombienet * test: update integration test * test(up): add more integration tests * test(up): add tests
- Loading branch information
1 parent
7de050e
commit a14186d
Showing
12 changed files
with
1,080 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
use crate::{ | ||
up::{ | ||
sourcing::{ | ||
self, | ||
traits::{Source as _, *}, | ||
GitHub::*, | ||
Source, | ||
}, | ||
target, | ||
}, | ||
Binary, Error, | ||
}; | ||
use std::path::Path; | ||
use strum::{EnumProperty as _, VariantArray as _}; | ||
use strum_macros::{AsRefStr, EnumProperty, VariantArray}; | ||
|
||
/// A supported runtime. | ||
#[derive(AsRefStr, Debug, EnumProperty, PartialEq, VariantArray)] | ||
pub(super) enum Runtime { | ||
/// Kusama. | ||
#[strum(props( | ||
Repository = "https://github.com/r0gue-io/polkadot-runtimes", | ||
Binary = "chain-spec-generator", | ||
Chain = "kusama-local", | ||
Fallback = "v1.2.7" | ||
))] | ||
Kusama, | ||
/// Paseo. | ||
#[strum(props( | ||
Repository = "https://github.com/r0gue-io/paseo-runtimes", | ||
Binary = "chain-spec-generator", | ||
Chain = "paseo-local", | ||
Fallback = "v1.2.4" | ||
))] | ||
Paseo, | ||
/// Polkadot. | ||
#[strum(props( | ||
Repository = "https://github.com/r0gue-io/polkadot-runtimes", | ||
Binary = "chain-spec-generator", | ||
Chain = "polkadot-local", | ||
Fallback = "v1.2.7" | ||
))] | ||
Polkadot, | ||
} | ||
|
||
impl TryInto for &Runtime { | ||
/// Attempt the conversion. | ||
/// | ||
/// # Arguments | ||
/// * `tag` - If applicable, a tag used to determine a specific release. | ||
/// * `latest` - If applicable, some specifier used to determine the latest source. | ||
fn try_into(&self, tag: Option<String>, latest: Option<String>) -> Result<Source, Error> { | ||
Ok(match self { | ||
_ => { | ||
// Source from GitHub release asset | ||
let repo = crate::GitHub::parse(self.repository())?; | ||
let name = self.name().to_lowercase(); | ||
let binary = self.binary(); | ||
Source::GitHub(ReleaseArchive { | ||
owner: repo.org, | ||
repository: repo.name, | ||
tag, | ||
tag_format: self.tag_format().map(|t| t.into()), | ||
archive: format!("{binary}-{}.tar.gz", target()?), | ||
contents: vec![(binary, Some(format!("{name}-{binary}")))], | ||
latest, | ||
}) | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
impl Runtime { | ||
/// The chain spec identifier. | ||
fn chain(&self) -> &'static str { | ||
self.get_str("Chain").expect("expected specification of `Chain`") | ||
} | ||
|
||
/// The name of the runtime. | ||
fn name(&self) -> &str { | ||
self.as_ref() | ||
} | ||
} | ||
|
||
impl sourcing::traits::Source for Runtime {} | ||
|
||
pub(super) async fn chain_spec_generator( | ||
chain: &str, | ||
version: Option<&str>, | ||
cache: &Path, | ||
) -> Result<Option<Binary>, Error> { | ||
for runtime in Runtime::VARIANTS.iter().filter(|r| chain.to_lowercase().ends_with(r.chain())) { | ||
let name = format!("{}-{}", runtime.name().to_lowercase(), runtime.binary()); | ||
let releases = runtime.releases().await?; | ||
let tag = Binary::resolve_version(&name, version, &releases, cache); | ||
// Only set latest when caller has not explicitly specified a version to use | ||
let latest = version | ||
.is_none() | ||
.then(|| releases.iter().nth(0).map(|v| v.to_string())) | ||
.flatten(); | ||
let binary = Binary::Source { | ||
name: name.to_string(), | ||
source: TryInto::try_into(&runtime, tag, latest)?, | ||
cache: cache.to_path_buf(), | ||
}; | ||
return Ok(Some(binary)); | ||
} | ||
Ok(None) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use tempfile::tempdir; | ||
|
||
#[tokio::test] | ||
async fn kusama_works() -> anyhow::Result<()> { | ||
let expected = Runtime::Kusama; | ||
let version = "v1.2.7"; | ||
let temp_dir = tempdir()?; | ||
let binary = chain_spec_generator("kusama-local", Some(version), temp_dir.path()) | ||
.await? | ||
.unwrap(); | ||
assert!(matches!(binary, Binary::Source { name, source, cache } | ||
if name == format!("{}-{}", expected.name().to_lowercase(), expected.binary()) && | ||
source == Source::GitHub(ReleaseArchive { | ||
owner: "r0gue-io".to_string(), | ||
repository: "polkadot-runtimes".to_string(), | ||
tag: Some(version.to_string()), | ||
tag_format: None, | ||
archive: format!("chain-spec-generator-{}.tar.gz", target()?), | ||
contents: ["chain-spec-generator"].map(|b| (b, Some(format!("kusama-{b}").to_string()))).to_vec(), | ||
latest: binary.latest().map(|l| l.to_string()), | ||
}) && | ||
cache == temp_dir.path() | ||
)); | ||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn paseo_works() -> anyhow::Result<()> { | ||
let expected = Runtime::Paseo; | ||
let version = "v1.2.4"; | ||
let temp_dir = tempdir()?; | ||
let binary = chain_spec_generator("paseo-local", Some(version), temp_dir.path()) | ||
.await? | ||
.unwrap(); | ||
assert!(matches!(binary, Binary::Source { name, source, cache } | ||
if name == format!("{}-{}", expected.name().to_lowercase(), expected.binary()) && | ||
source == Source::GitHub(ReleaseArchive { | ||
owner: "r0gue-io".to_string(), | ||
repository: "paseo-runtimes".to_string(), | ||
tag: Some(version.to_string()), | ||
tag_format: None, | ||
archive: format!("chain-spec-generator-{}.tar.gz", target()?), | ||
contents: ["chain-spec-generator"].map(|b| (b, Some(format!("paseo-{b}").to_string()))).to_vec(), | ||
latest: binary.latest().map(|l| l.to_string()), | ||
}) && | ||
cache == temp_dir.path() | ||
)); | ||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn polkadot_works() -> anyhow::Result<()> { | ||
let expected = Runtime::Polkadot; | ||
let version = "v1.2.7"; | ||
let temp_dir = tempdir()?; | ||
let binary = chain_spec_generator("polkadot-local", Some(version), temp_dir.path()) | ||
.await? | ||
.unwrap(); | ||
assert!(matches!(binary, Binary::Source { name, source, cache } | ||
if name == format!("{}-{}", expected.name().to_lowercase(), expected.binary()) && | ||
source == Source::GitHub(ReleaseArchive { | ||
owner: "r0gue-io".to_string(), | ||
repository: "polkadot-runtimes".to_string(), | ||
tag: Some(version.to_string()), | ||
tag_format: None, | ||
archive: format!("chain-spec-generator-{}.tar.gz", target()?), | ||
contents: ["chain-spec-generator"].map(|b| (b, Some(format!("polkadot-{b}").to_string()))).to_vec(), | ||
latest: binary.latest().map(|l| l.to_string()), | ||
}) && | ||
cache == temp_dir.path() | ||
)); | ||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn chain_spec_generator_returns_none_when_no_match() -> anyhow::Result<()> { | ||
let temp_dir = tempdir()?; | ||
assert_eq!(chain_spec_generator("rococo-local", None, temp_dir.path()).await?, None); | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.