Skip to content
Snippets Groups Projects
Commit b1ff23ac authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

add what is needed to build gtest chainspecs

parent ca6c877e
No related branches found
No related tags found
1 merge request!171prepare gtest runtime
...@@ -67,7 +67,7 @@ An example of genesis configuration file: `resources/gdev.json`. Paste your sess ...@@ -67,7 +67,7 @@ An example of genesis configuration file: `resources/gdev.json`. Paste your sess
### Generate raw spec ### Generate raw spec
```docker ```docker
docker run -v $HOME/dev/duniter-v2s/resources:/var/lib/duniter/resources -e DUNITER_GENESIS_CONFIG=/var/lib/duniter/resources/gdev.json --rm duniter/duniter-v2s:TAG -- build-spec -lerror --chain=gdev-gl --raw > name-raw.json docker run -v $HOME/dev/duniter-v2s/resources:/var/lib/duniter/resources -e DUNITER_GENESIS_CONFIG=/var/lib/duniter/resources/gdev.json --rm duniter/duniter-v2s:TAG -- build-spec -lerror --chain=gdev_live --raw > name-raw.json
``` ```
```bash ```bash
......
...@@ -98,6 +98,9 @@ impl From<Cert> for (String, Option<u32>) { ...@@ -98,6 +98,9 @@ impl From<Cert> for (String, Option<u32>) {
} }
} }
/// generate genesis data from a json file
/// takes DUNITER_GENESIS_CONFIG env var if present or duniter-gen-conf.json by default
// this function is targeting dev chainspecs, do not use in production network
pub fn generate_genesis_data<CS, P, SK, F>( pub fn generate_genesis_data<CS, P, SK, F>(
f: F, f: F,
maybe_force_authority: Option<Vec<u8>>, maybe_force_authority: Option<Vec<u8>>,
......
...@@ -84,8 +84,33 @@ fn session_keys( ...@@ -84,8 +84,33 @@ fn session_keys(
} }
} }
// can not use ClientSpec which is private :'(
// // client specs used in chainspecs
// static client_spec_dev = ClientSpec {
// name: "ĞTest Development",
// id: "gtest_dev",
// chain_type: ChainType::Development,
// boot_nodes: vec![],
// telemetry_endpoints: None,
// protocol_id: None,
// fork_id: None,
// properties: Some(
// serde_json::json!({
// "tokenDecimals": TOKEN_DECIMALS,
// "tokenSymbol": TOKEN_SYMBOL,
// })
// .as_object()
// .expect("must be a map")
// .clone()),
// extensions: None,
// consensus_engine: (),
// genesis: Default::default(),
// code_substitutes: BTreeMap::new(),
// };
/// generate development chainspec with Alice validator /// generate development chainspec with Alice validator
pub fn development_chain_spec() -> Result<ChainSpec, String> { // there is some code duplication because we can not use ClientSpec
pub fn development_chainspecs() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or_else(|| "wasm not available".to_string())?; let wasm_binary = WASM_BINARY.ok_or_else(|| "wasm not available".to_string())?;
// custom genesis when DUNITER_GTEST_GENESIS is set // custom genesis when DUNITER_GTEST_GENESIS is set
...@@ -146,7 +171,7 @@ pub fn development_chain_spec() -> Result<ChainSpec, String> { ...@@ -146,7 +171,7 @@ pub fn development_chain_spec() -> Result<ChainSpec, String> {
ChainType::Development, ChainType::Development,
// constructor // constructor
move || { move || {
gen_genesis_for_local_chain( generate_genesis(
wasm_binary, wasm_binary,
// Initial authorities len // Initial authorities len
1, 1,
...@@ -156,7 +181,6 @@ pub fn development_chain_spec() -> Result<ChainSpec, String> { ...@@ -156,7 +181,6 @@ pub fn development_chain_spec() -> Result<ChainSpec, String> {
4, 4,
// Sudo account // Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"), get_account_id_from_seed::<sr25519::Public>("Alice"),
true,
) )
}, },
// Bootnodes // Bootnodes
...@@ -183,39 +207,47 @@ pub fn development_chain_spec() -> Result<ChainSpec, String> { ...@@ -183,39 +207,47 @@ pub fn development_chain_spec() -> Result<ChainSpec, String> {
} }
} }
pub fn local_testnet_config( /// generate live chainspecs from DUNITER_GTEST_GENESIS
initial_authorities_len: usize, /// one smith must have session keys
initial_smiths_len: usize, pub fn live_chainspecs() -> Result<ChainSpec, String> {
initial_identities_len: usize,
) -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or_else(|| "wasm not available".to_string())?; let wasm_binary = WASM_BINARY.ok_or_else(|| "wasm not available".to_string())?;
let genesis_json_path =
std::env::var("DUNITER_GTEST_GENESIS").expect("DUNITER_GTEST_GENESIS needed");
// log
log::info!("loading genesis from {genesis_json_path}");
// return chainspecs
Ok(ChainSpec::from_genesis( Ok(ChainSpec::from_genesis(
// Name // Name
"ĞTest Local Testnet", "ĞTest",
// ID // ID
"gtest_local", "gtest",
ChainType::Local, // chain type
sc_service::ChainType::Live,
// genesis config constructor
move || { move || {
gen_genesis_for_local_chain( super::gtest_genesis::build_genesis(
// path of json genesis
&genesis_json_path,
// wasm binary
wasm_binary, wasm_binary,
// Initial authorities len // do not replace session keys
initial_authorities_len, None,
// Initial smiths len,
initial_smiths_len,
// Initial identities len
initial_identities_len,
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
true,
) )
.expect("genesis building failed")
}, },
// Bootnodes // Bootnodes (TODO add bootnodes)
vec![], vec![],
// Telemetry // Telemetry (by default, enable telemetry, can be disabled with argument)
None, Some(
// Protocol ID sc_service::config::TelemetryEndpoints::new(vec![(
None, "wss://telemetry.polkadot.io/submit/".to_owned(),
0,
)])
.expect("invalid telemetry endpoints"),
), // Protocol ID
Some("gtest1"),
//Fork ID //Fork ID
None, None,
// Properties // Properties
...@@ -233,13 +265,13 @@ pub fn local_testnet_config( ...@@ -233,13 +265,13 @@ pub fn local_testnet_config(
)) ))
} }
fn gen_genesis_for_local_chain( /// generate a genesis with given number of smith and identities
fn generate_genesis(
wasm_binary: &[u8], wasm_binary: &[u8],
initial_authorities_len: usize, initial_authorities_len: usize,
initial_smiths_len: usize, initial_smiths_len: usize,
initial_identities_len: usize, initial_identities_len: usize,
root_key: AccountId, root_key: AccountId,
_enable_println: bool,
) -> GenesisConfig { ) -> GenesisConfig {
assert!(initial_identities_len <= 6); assert!(initial_identities_len <= 6);
assert!(initial_smiths_len <= initial_identities_len); assert!(initial_smiths_len <= initial_identities_len);
......
...@@ -103,37 +103,45 @@ impl SubstrateCli for Cli { ...@@ -103,37 +103,45 @@ impl SubstrateCli for Cli {
fn load_spec(&self, id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> { fn load_spec(&self, id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> {
Ok(match id { Ok(match id {
// === GDEV ===
// developement chainspec with Alice validator
// optionally from DUNITER_GENESIS_CONFIG file otherwise generated
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
"dev" => Box::new(chain_spec::gdev::development_chain_spec()?), "dev" => Box::new(chain_spec::gdev::development_chain_spec()?),
// local testnet with generated genesis and Alice validator
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
"local" | "gdev_local" => Box::new(chain_spec::gdev::local_testnet_config(1, 3, 4)?), "gdev_local" => Box::new(chain_spec::gdev::local_testnet_config(1, 3, 4)?),
// chainspecs for live network (generated or DUNITER_GENESIS_CONFIG)
// but must have a smith with declared session keys
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
"local2" => Box::new(chain_spec::gdev::local_testnet_config(2, 3, 4)?), "gdev_live" => Box::new(chain_spec::gdev::gen_live_conf()?),
#[cfg(feature = "gdev")] // hardcoded previously generated raw chainspecs
"local3" => Box::new(chain_spec::gdev::local_testnet_config(3, 3, 4)?), // yields a pointlessly heavy binary because of hexadecimal-text-encoded values
#[cfg(feature = "gdev")]
"local4" => Box::new(chain_spec::gdev::local_testnet_config(4, 4, 5)?),
#[cfg(feature = "gdev")]
"gdev-gl" | "gdev_gl" => Box::new(chain_spec::gdev::gen_live_conf()?),
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
"gdev" => Box::new(chain_spec::gdev::ChainSpec::from_json_bytes( "gdev" => Box::new(chain_spec::gdev::ChainSpec::from_json_bytes(
&include_bytes!("../specs/gdev-raw.json")[..], &include_bytes!("../specs/gdev-raw.json")[..],
)?), )?),
// config used for benckmarks
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
"gdev-benchmark" => Box::new(chain_spec::gdev::benchmark_chain_spec()?), "gdev-benchmark" => Box::new(chain_spec::gdev::benchmark_chain_spec()?),
// === GTEST ===
// dev chainspecs with Alice validator
// provide DUNITER_GTEST_GENESIS env var if you want to build genesis from json
// otherwise you get a local testnet with generated genesis
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
"gtest_dev" => Box::new(chain_spec::gtest::development_chain_spec()?), "gtest_dev" => Box::new(chain_spec::gtest::development_chainspecs()?),
// chainspecs for live network (must set DUNITER_GTEST_GENESIS)
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
"gtest_local" => Box::new(chain_spec::gtest::local_testnet_config(2, 3, 4)?), "gtest_live" => Box::new(chain_spec::gtest::live_chainspecs()?),
#[cfg(feature = "gtest")] // hardcoded previously generated raw chainspecs
"gtest_local3" => Box::new(chain_spec::gtest::local_testnet_config(3, 3, 4)?),
#[cfg(feature = "gtest")]
"gtest_local4" => Box::new(chain_spec::gtest::local_testnet_config(4, 4, 5)?),
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
"gtest" => { "gtest" => {
unimplemented!() unimplemented!();
//Box::new(chain_spec::gtest::ChainSpec::from_json_file(file_path)?) // Box::new(chain_spec::gtest::ChainSpec::from_json_bytes(
// &include_bytes!("../specs/gtest-raw.json")[..],
// )?)
} }
// === G1 ===
#[cfg(feature = "g1")] #[cfg(feature = "g1")]
"g1" => { "g1" => {
unimplemented!() unimplemented!()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment