Skip to content

Commit c1094e2

Browse files
authored
feat: embed chainspecs in binary (#720)
To avoid runtime panics. Fixes KILTprotocol/ticket#3601 by embedding the well-known chainspecs into the binary. Fixes KILTprotocol/ticket#3597 by defaulting to "peregrine" if a chainspec path is specified that does not contain neither `peregrine` nor `spiritnet`.
1 parent be2c2d7 commit c1094e2

File tree

4 files changed

+11
-49
lines changed

4 files changed

+11
-49
lines changed

Dockerfile

-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,5 @@ VOLUME ["/data"]
3232

3333
COPY ./chainspecs /node/chainspecs
3434

35-
ENV CHAINSPECS_FOLDER=/node/chainspecs
36-
3735
ENTRYPOINT ["/usr/local/bin/node-executable"]
3836
CMD ["--help"]

nodes/parachain/src/chain_spec/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ impl FromStr for ParachainRuntime {
134134
// Any other Spiritnet-based chainspec
135135
s if s.contains("spiritnet") => Ok(Self::Spiritnet(SpiritnetRuntime::Other(s.to_string()))),
136136

137-
_ => Err(format!("Unknown chainspec id provided: {s}")),
137+
// Instead of panicking, we use the Peregrine runtime, since we don't expect Spiritnet to ever be used in
138+
// this way
139+
path => Ok(Self::Peregrine(PeregrineRuntime::Other(path.to_string()))),
138140
}
139141
}
140142
}

nodes/parachain/src/chain_spec/rilt/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,3 @@ const SAFE_XCM_VERSION: u32 = xcm::prelude::XCM_VERSION;
2525
/// Specialized `ChainSpec` for the normal parachain runtime.
2626
pub(crate) type ChainSpec =
2727
sc_service::GenericChainSpec<peregrine_runtime::RuntimeGenesisConfig, crate::chain_spec::Extensions>;
28-
29-
pub(crate) fn load_chain_spec(path: &str) -> Result<ChainSpec, String> {
30-
ChainSpec::from_json_file(path.into())
31-
}

nodes/parachain/src/chain_spec/utils.rs

+8-42
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
// If you feel like getting in touch with us, you can do so at info@botlabs.org
1818

19-
use std::path::{Path, PathBuf};
20-
2119
use runtime_common::{AccountId, AccountPublic};
2220
use sc_service::Properties;
2321
use sp_core::{Pair, Public};
@@ -57,18 +55,14 @@ pub(crate) fn load_spec(id: &str) -> Result<Box<dyn sc_service::ChainSpec>, Stri
5755
"rococo_local",
5856
))),
5957
PeregrineRuntime::New => Ok(Box::new(chain_spec::peregrine::new::generate_chain_spec())),
60-
PeregrineRuntime::Peregrine => Ok(Box::new(chain_spec::peregrine::load_chain_spec(
61-
get_chainspec_full_path("peregrine/peregrine-paseo.json")
62-
.to_str()
63-
.unwrap(),
58+
PeregrineRuntime::Peregrine => Ok(Box::new(chain_spec::peregrine::ChainSpec::from_json_bytes(
59+
include_bytes!("../../../../chainspecs/peregrine/peregrine-paseo.json").as_slice(),
6460
)?)),
65-
PeregrineRuntime::PeregrineStg => Ok(Box::new(chain_spec::peregrine::load_chain_spec(
66-
get_chainspec_full_path("peregrine-stg/peregrine-stg.json")
67-
.to_str()
68-
.unwrap(),
61+
PeregrineRuntime::PeregrineStg => Ok(Box::new(chain_spec::peregrine::ChainSpec::from_json_bytes(
62+
include_bytes!("../../../../chainspecs/peregrine-stg/peregrine-stg.json").as_slice(),
6963
)?)),
70-
PeregrineRuntime::Rilt => Ok(Box::new(chain_spec::rilt::load_chain_spec(
71-
get_chainspec_full_path("rilt/peregrine-rilt.json").to_str().unwrap(),
64+
PeregrineRuntime::Rilt => Ok(Box::new(chain_spec::peregrine::ChainSpec::from_json_bytes(
65+
include_bytes!("../../../../chainspecs/rilt/peregrine-rilt.json").as_slice(),
7266
)?)),
7367
PeregrineRuntime::RiltNew => Ok(Box::new(chain_spec::rilt::new::generate_chain_spec())),
7468
PeregrineRuntime::Other(s) => Ok(Box::new(chain_spec::peregrine::load_chain_spec(s.as_str())?)),
@@ -78,38 +72,10 @@ pub(crate) fn load_spec(id: &str) -> Result<Box<dyn sc_service::ChainSpec>, Stri
7872
"rococo_local",
7973
))),
8074
SpiritnetRuntime::New => Ok(Box::new(chain_spec::spiritnet::new::generate_chain_spec())),
81-
SpiritnetRuntime::Spiritnet => Ok(Box::new(chain_spec::spiritnet::load_chain_spec(
82-
get_chainspec_full_path("spiritnet/spiritnet.json").to_str().unwrap(),
75+
SpiritnetRuntime::Spiritnet => Ok(Box::new(chain_spec::spiritnet::ChainSpec::from_json_bytes(
76+
include_bytes!("../../../../chainspecs/spiritnet/spiritnet.json").as_slice(),
8377
)?)),
8478
SpiritnetRuntime::Other(s) => Ok(Box::new(chain_spec::spiritnet::load_chain_spec(s.as_str())?)),
8579
},
8680
}
8781
}
88-
89-
// Compile-time env variable used when running the binary with cargo or via
90-
// cargo (after `cargo build`).
91-
const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
92-
// Name of the runtime-time env variable that can be used to configure the
93-
// chainspecs folder path, useful especially when running the binary in a Docker
94-
// container.
95-
const CHAINSPECS_FOLDER_VAR_NAME: &str = "CHAINSPECS_FOLDER";
96-
97-
fn get_chainspec_full_path(path: &str) -> PathBuf {
98-
// Use the provided env variable, if present at runtime, or else uses the
99-
// compile-time `CARGO_MANIFEST_DIR` variable (e.g., if the binary is run via
100-
// cargo instead of in a Docker container).
101-
let chainspecs_root = match std::env::var(CHAINSPECS_FOLDER_VAR_NAME) {
102-
Ok(chainspecs_folder_name) => chainspecs_folder_name.to_owned(),
103-
Err(_) => Path::new(MANIFEST_DIR)
104-
.join("..")
105-
.join("..")
106-
.join("chainspecs")
107-
.to_string_lossy()
108-
.into_owned(),
109-
};
110-
111-
Path::new(chainspecs_root.as_str())
112-
.join(path)
113-
.canonicalize()
114-
.expect("Invalid path provided.")
115-
}

0 commit comments

Comments
 (0)