Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/chain_spec: remove code_substitutes hack #2127

Merged
merged 3 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bin/polkadot.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,5 +411,9 @@ mod tests {
let spec = &include_bytes!("chain_spec/example.json")[..];
let specs = ChainSpec::from_json_bytes(&spec).unwrap();
assert_eq!(specs.id(), "polkadot");

// code_substitutes field
assert_eq!(specs.client_spec.code_substitutes.get(&1), None);
assert!(specs.client_spec.code_substitutes.get(&5203203).is_some());
}
}
3 changes: 3 additions & 0 deletions src/chain_spec/example.json

Large diffs are not rendered by default.

50 changes: 6 additions & 44 deletions src/chain_spec/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,15 @@ pub(super) struct ClientSpec {
#[serde(default)]
pub(super) chain_type: ChainType,

/// Each key is a block hash. Values are a hex-encoded runtime code (normally found in the
/// `:code` storage key). The descendants of the block with the given hash that share the same
/// [`crate::executor::CoreVersionRef::spec_version`] as the Wasm runtime code in the value
/// should instead use that Wasm runtime code. In other words, the substitution stops when
/// the `spec_version` is modified.
/// The block with that hash uses its unmodified runtime code. Only its children can be
/// affected.
/// Mapping from a block number to a hex-encoded wasm runtime code (normally found in the
/// `:code` storage key).
///
/// This can be used in order to substitute faulty runtimes with functioning ones.
///
/// See also <https://github.com/paritytech/substrate/pull/8898>.
/// The given runtime code will be used to substitute the on-chain runtime code starting with
/// the given block number until the `spec_version`
/// ([`crate::executor::CoreVersionRef::spec_version`]) on chain changes.
#[serde(default)]
// TODO: make use of this
pub(super) code_substitutes: HashMap<NumberAsString, HexString, fnv::FnvBuildHasher>,
pub(super) code_substitutes: HashMap<u64, HexString, fnv::FnvBuildHasher>,
pub(super) boot_nodes: Vec<String>,
pub(super) telemetry_endpoints: Option<Vec<(String, u8)>>,
pub(super) protocol_id: Option<String>,
Expand Down Expand Up @@ -143,39 +138,6 @@ impl<'a> serde::Deserialize<'a> for HexString {
}
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(super) struct NumberAsString(pub(super) u64);

impl serde::Serialize for NumberAsString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}

impl<'a> serde::Deserialize<'a> for NumberAsString {
fn deserialize<D>(deserializer: D) -> Result<NumberAsString, D::Error>
where
D: serde::Deserializer<'a>,
{
let string = String::deserialize(deserializer)?;

if let Some(hex) = string.strip_prefix("0x") {
// TODO: the hexadecimal format support is just a complete hack during a transition period for https://github.com/paritytech/substrate/pull/10600 ; must be removed before we actually make use of the code substitutes
let _bytes = hex::decode(&hex).map_err(serde::de::Error::custom)?;
Ok(NumberAsString(0))
} else if let Ok(num) = string.parse() {
Ok(NumberAsString(num))
} else {
Err(serde::de::Error::custom(
"block number is neither hexadecimal nor decimal",
))
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
Expand Down