Skip to content

Commit 0757aab

Browse files
committed
Replace usage of deprecated GenericChainSpec::from_genesis with GenericChainSpec::builder and work around some bugs
1 parent f825e01 commit 0757aab

File tree

20 files changed

+572
-509
lines changed

20 files changed

+572
-509
lines changed

Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/subspace-malicious-operator/src/chain_spec.rs

Lines changed: 94 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use evm_domain_runtime::{AccountId as AccountId20, EVMChainIdConfig, EVMConfig,
33
use hex_literal::hex;
44
use parity_scale_codec::Encode;
55
use sc_chain_spec::GenericChainSpec;
6-
use sc_service::{ChainSpec, ChainType, NoExtension};
6+
use sc_service::{ChainSpec, ChainType};
77
use sp_core::crypto::AccountId32;
88
use sp_core::{sr25519, Pair, Public};
99
use sp_domains::storage::RawGenesis;
@@ -34,64 +34,58 @@ fn endowed_accounts() -> Vec<(MultiAccountId, Balance)> {
3434
.collect()
3535
}
3636

37-
pub fn domain_dev_config() -> GenericChainSpec<evm_domain_runtime::RuntimeGenesisConfig> {
37+
pub fn domain_dev_config(
38+
) -> Result<GenericChainSpec<evm_domain_runtime::RuntimeGenesisConfig>, String> {
3839
// Alith is sudo account
3940
let sudo_account = AccountId20::from(hex!("f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"));
4041

41-
// TODO: Migrate once https://github.com/paritytech/polkadot-sdk/issues/2963 is un-broken
42-
#[allow(deprecated)]
43-
GenericChainSpec::from_genesis(
44-
// Name
45-
"Development",
46-
// ID
47-
"evm_domain_dev",
48-
ChainType::Development,
49-
move || {
50-
// This is the simplest bytecode to revert without returning any data.
51-
// We will pre-deploy it under all of our precompiles to ensure they can be called from
52-
// within contracts.
53-
// (PUSH1 0x00 PUSH1 0x00 REVERT)
54-
let revert_bytecode = vec![0x60, 0x00, 0x60, 0x00, 0xFD];
55-
56-
evm_domain_runtime::RuntimeGenesisConfig {
57-
system: evm_domain_runtime::SystemConfig::default(),
58-
sudo: evm_domain_runtime::SudoConfig {
59-
key: Some(sudo_account),
60-
},
61-
balances: evm_domain_runtime::BalancesConfig::default(),
62-
// this is set to default and chain_id will be set into genesis during the domain
63-
// instantiation on Consensus runtime.
64-
evm_chain_id: EVMChainIdConfig::default(),
65-
evm: EVMConfig {
66-
// We need _some_ code inserted at the precompile address so that
67-
// the evm will actually call the address.
68-
accounts: Precompiles::used_addresses()
69-
.into_iter()
70-
.map(|addr| {
71-
(
72-
addr,
73-
fp_evm::GenesisAccount {
74-
nonce: Default::default(),
75-
balance: Default::default(),
76-
storage: Default::default(),
77-
code: revert_bytecode.clone(),
78-
},
79-
)
80-
})
81-
.collect(),
82-
..Default::default()
83-
},
84-
..Default::default()
85-
}
86-
},
87-
vec![],
88-
None,
89-
None,
90-
None,
91-
None,
92-
None,
42+
Ok(GenericChainSpec::builder(
9343
evm_domain_runtime::WASM_BINARY.expect("WASM binary was not build, please build it!"),
44+
None,
9445
)
46+
.with_name("Development")
47+
.with_id("evm_domain_dev")
48+
.with_chain_type(ChainType::Development)
49+
.with_genesis_config({
50+
// This is the simplest bytecode to revert without returning any data.
51+
// We will pre-deploy it under all of our precompiles to ensure they can be called from
52+
// within contracts.
53+
// (PUSH1 0x00 PUSH1 0x00 REVERT)
54+
let revert_bytecode = vec![0x60, 0x00, 0x60, 0x00, 0xFD];
55+
56+
serde_json::to_value(evm_domain_runtime::RuntimeGenesisConfig {
57+
system: evm_domain_runtime::SystemConfig::default(),
58+
sudo: evm_domain_runtime::SudoConfig {
59+
key: Some(sudo_account),
60+
},
61+
balances: evm_domain_runtime::BalancesConfig::default(),
62+
// this is set to default and chain_id will be set into genesis during the domain
63+
// instantiation on Consensus runtime.
64+
evm_chain_id: EVMChainIdConfig::default(),
65+
evm: EVMConfig {
66+
// We need _some_ code inserted at the precompile address so that
67+
// the evm will actually call the address.
68+
accounts: Precompiles::used_addresses()
69+
.into_iter()
70+
.map(|addr| {
71+
(
72+
addr,
73+
fp_evm::GenesisAccount {
74+
nonce: Default::default(),
75+
balance: Default::default(),
76+
storage: Default::default(),
77+
code: revert_bytecode.clone(),
78+
},
79+
)
80+
})
81+
.collect(),
82+
..Default::default()
83+
},
84+
..Default::default()
85+
})
86+
.map_err(|error| format!("Failed to serialize genesis config: {error}"))?
87+
})
88+
.build())
9589
}
9690

9791
pub(crate) fn consensus_dev_sudo_account() -> AccountId32 {
@@ -103,7 +97,7 @@ pub fn create_domain_spec(
10397
raw_genesis: RawGenesis,
10498
) -> Result<Box<dyn sc_cli::ChainSpec>, String> {
10599
let mut chain_spec = match chain_id {
106-
"dev" => domain_dev_config(),
100+
"dev" => domain_dev_config()?,
107101
path => GenericChainSpec::<evm_domain_runtime::RuntimeGenesisConfig>::from_json_file(
108102
std::path::PathBuf::from(path),
109103
)?,
@@ -114,7 +108,7 @@ pub fn create_domain_spec(
114108

115109
pub fn load_domain_chain_spec(spec_id: &str) -> Result<Box<dyn sc_cli::ChainSpec>, String> {
116110
let chain_spec = match spec_id {
117-
"dev" => domain_dev_config(),
111+
"dev" => domain_dev_config()?,
118112
path => GenericChainSpec::<evm_domain_runtime::RuntimeGenesisConfig>::from_json_file(
119113
std::path::PathBuf::from(path),
120114
)?,
@@ -163,24 +157,20 @@ pub fn dev_config() -> Result<GenericChainSpec<subspace_runtime::RuntimeGenesisC
163157
.ok_or_else(|| "Development wasm not available".to_string())?;
164158

165159
let raw_genesis_storage = {
166-
let domain_genesis_config = domain_dev_config();
160+
let domain_genesis_config = domain_dev_config()?;
167161
let storage = domain_genesis_config
168162
.build_storage()
169163
.expect("Failed to build genesis storage from genesis runtime config");
170164
let raw_genesis = RawGenesis::from_storage(storage);
171165
raw_genesis.encode()
172166
};
173167

174-
// TODO: Migrate once https://github.com/paritytech/polkadot-sdk/issues/2963 is un-broken
175-
#[allow(deprecated)]
176-
Ok(GenericChainSpec::from_genesis(
177-
// Name
178-
"Subspace development",
179-
// ID
180-
"subspace_dev",
181-
ChainType::Development,
182-
move || {
183-
subspace_genesis_config(
168+
Ok(GenericChainSpec::builder(wasm_binary, None)
169+
.with_name("Subspace development")
170+
.with_id("subspace_dev")
171+
.with_chain_type(ChainType::Development)
172+
.with_genesis_config(patch_domain_runtime_version(
173+
serde_json::to_value(subspace_genesis_config(
184174
// Sudo account
185175
get_account_id_from_seed("Alice"),
186176
// Pre-funded accounts
@@ -214,22 +204,10 @@ pub fn dev_config() -> Result<GenericChainSpec<subspace_runtime::RuntimeGenesisC
214204
initial_balances: endowed_accounts(),
215205
permissioned_action_allowed_by: PermissionedActionAllowedBy::Anyone,
216206
},
217-
)
218-
},
219-
// Bootnodes
220-
vec![],
221-
// Telemetry
222-
None,
223-
// Protocol ID
224-
None,
225-
None,
226-
// Properties
227-
None,
228-
// Extensions
229-
NoExtension::None,
230-
// Code
231-
wasm_binary,
232-
))
207+
))
208+
.map_err(|error| format!("Failed to serialize genesis config: {error}"))?,
209+
))
210+
.build())
233211
}
234212

235213
/// Configure initial storage state for FRAME modules.
@@ -302,3 +280,36 @@ fn subspace_genesis_config(
302280
},
303281
}
304282
}
283+
284+
// TODO: Workaround for https://github.com/paritytech/polkadot-sdk/issues/4001
285+
fn patch_domain_runtime_version(mut genesis_config: serde_json::Value) -> serde_json::Value {
286+
let Some(runtime_version) = genesis_config
287+
.get_mut("domains")
288+
.and_then(|domains| domains.get_mut("genesisDomain"))
289+
.and_then(|genesis_domain| genesis_domain.get_mut("runtime_version"))
290+
else {
291+
return genesis_config;
292+
};
293+
294+
if let Some(spec_name) = runtime_version.get_mut("specName") {
295+
if let Some(spec_name_bytes) = spec_name
296+
.as_str()
297+
.map(|spec_name| spec_name.as_bytes().to_vec())
298+
{
299+
*spec_name = serde_json::to_value(spec_name_bytes)
300+
.expect("Bytes serialization doesn't fail; qed");
301+
}
302+
}
303+
304+
if let Some(impl_name) = runtime_version.get_mut("implName") {
305+
if let Some(impl_name_bytes) = impl_name
306+
.as_str()
307+
.map(|impl_name| impl_name.as_bytes().to_vec())
308+
{
309+
*impl_name = serde_json::to_value(impl_name_bytes)
310+
.expect("Bytes serialization doesn't fail; qed");
311+
}
312+
}
313+
314+
genesis_config
315+
}

0 commit comments

Comments
 (0)