Skip to content

Commit 6bfc04d

Browse files
chain-spec: always output backward compatible spec (tests check)
1 parent 3de595a commit 6bfc04d

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

substrate/client/chain-spec/src/chain_spec.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ enum Genesis<G> {
198198
StateRootHash(StorageData),
199199
/// Represents the full runtime genesis config in JSON format.
200200
/// The contained object is a JSON blob that can be parsed by a compatible runtime.
201+
#[serde(rename(serialize = "runtime"))]
201202
RuntimeGenesisConfig(json::Value),
202203
/// Represents a patch for the default runtime genesis config in JSON format.
203204
/// The contained value is a JSON object that can be parsed by a compatible runtime.
@@ -374,13 +375,24 @@ impl<G, E> ChainSpecBuilder<G, E> {
374375
code_substitutes: BTreeMap::new(),
375376
};
376377

378+
let code = self.code.expect("with code must be called.");
379+
380+
let genesis_build_action = match self.genesis_build_action {
381+
Some(GenesisBuildAction::Patch(patch)) => Some(GenesisBuildAction::Full(
382+
RuntimeCaller::new(&code[..])
383+
.get_config_for_patch(patch)
384+
.expect("patch should be correct"),
385+
)),
386+
_ => self.genesis_build_action,
387+
};
388+
377389
ChainSpec {
378390
client_spec,
379391
genesis: GenesisSource::GenesisBuilderApi(
380-
self.genesis_build_action
392+
genesis_build_action
381393
.expect("with_genesis_config_patch or with_genesis_config must be called."),
382394
),
383-
code: self.code.expect("with code must be called.").into(),
395+
code: code.into(),
384396
}
385397
}
386398
}

substrate/client/chain-spec/src/genesis_config_builder.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,29 +94,38 @@ impl<'a> GenesisConfigBuilderRuntimeCaller<'a> {
9494
Ok(ext.into_storages())
9595
}
9696

97-
/// Patch default `GenesisConfig` using given JSON patch and store it in the storage.
97+
/// Patch default `RuntimeGenesisConfig` using given JSON patch and store it in the storage.
9898
///
99-
/// This function generates the `GenesisConfig` for the runtime by applying a provided JSON
100-
/// patch. The patch modifies the default `GenesisConfig` allowing customization of the specific
101-
/// keys. The resulting `GenesisConfig` is then deserialized from the patched JSON
102-
/// representation and stored in the storage.
99+
/// Refer to [get_storage_for_patch] for info how patch is applied.
100+
pub fn get_storage_for_patch(&self, patch: Value) -> core::result::Result<Storage, String> {
101+
let config = self.get_config_for_patch(patch)?;
102+
self.get_storage_for_config(config)
103+
}
104+
105+
/// Creates a `RuntimeGenesisConfig`, by applying given `patch` to default
106+
/// `RuntimeGenesisConfig`.
107+
///
108+
/// This function generates the `RuntimeGenesisConfig` for the runtime by applying a provided
109+
/// JSON patch. The patch modifies the default `RuntimeGenesisConfig` allowing customization of
110+
/// the specific keys. The resulting `RuntimeGenesisConfig` is then returned.
103111
///
104112
/// If the provided JSON patch is incorrect or the deserialization fails the error will be
105113
/// returned.
106114
///
107-
/// The patching process modifies the default `GenesisConfig` according to the following rules:
115+
/// The patching process modifies the default `RuntimeGenesisConfig` according to the following
116+
/// rules:
108117
/// 1. Existing keys in the default configuration will be overridden by the corresponding values
109118
/// in the patch.
110119
/// 2. If a key exists in the patch but not in the default configuration, it will be added to
111-
/// the resulting `GenesisConfig`.
120+
/// the resulting `RuntimeGenesisConfig`.
112121
/// 3. Keys in the default configuration that have null values in the patch will be removed from
113-
/// the resulting `GenesisConfig`. This is helpful for changing enum variant value.
122+
/// the resulting `RuntimeGenesisConfig`. This is helpful for changing enum variant value.
114123
///
115-
/// Please note that the patch may contain full `GenesisConfig`.
116-
pub fn get_storage_for_patch(&self, patch: Value) -> core::result::Result<Storage, String> {
124+
/// Please note that the patch may contain full `RuntimeGenesisConfig`.
125+
pub fn get_config_for_patch(&self, patch: Value) -> core::result::Result<Value, String> {
117126
let mut config = self.get_default_config()?;
118127
json_patch::merge(&mut config, &patch);
119-
self.get_storage_for_config(config)
128+
Ok(config)
120129
}
121130
}
122131

0 commit comments

Comments
 (0)