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

[DNM] Add offchain executor params #5278

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Merge and revert allocator changes
  • Loading branch information
s0me0ne-unkn0wn committed Dec 23, 2024
commit b46ed8f50243a375a9fb98c22fd335584e99ff4e
3 changes: 2 additions & 1 deletion cumulus/test/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -50,7 +50,8 @@ pub fn get_chain_spec_with_extra_endowed(
extra_endowed_accounts: Vec<AccountId>,
code: &[u8],
) -> ChainSpec {
let runtime_caller = GenesisConfigBuilderRuntimeCaller::<ParachainHostFunctions>::new(code);
let runtime_caller =
GenesisConfigBuilderRuntimeCaller::<ParachainHostFunctions>::new(code, Default::default());
let mut development_preset = runtime_caller
.get_named_preset(Some(&sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET.to_string()))
.expect("development preset is available on test runtime; qed");
96 changes: 1 addition & 95 deletions substrate/bin/utils/chain-spec-builder/bin/main.rs
Original file line number Diff line number Diff line change
@@ -32,99 +32,5 @@ fn inner_main() -> Result<(), String> {
sp_tracing::try_init_simple();

let builder = ChainSpecBuilder::parse();
let chain_spec_path = builder.chain_spec_path.to_path_buf();

match builder.command {
ChainSpecBuilderCmd::Create(cmd) => {
let chain_spec_json = generate_chain_spec_for_runtime(&cmd)?;
fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?;
},
ChainSpecBuilderCmd::UpdateCode(UpdateCodeCmd {
ref input_chain_spec,
ref runtime_wasm_path,
}) => {
let chain_spec = ChainSpec::from_json_file(input_chain_spec.clone())?;

let mut chain_spec_json =
serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(false)?)
.map_err(|e| format!("Conversion to json failed: {e}"))?;
update_code_in_json_chain_spec(
&mut chain_spec_json,
&fs::read(runtime_wasm_path.as_path())
.map_err(|e| format!("Wasm blob file could not be read: {e}"))?[..],
);

let chain_spec_json = serde_json::to_string_pretty(&chain_spec_json)
.map_err(|e| format!("to pretty failed: {e}"))?;
fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?;
},
ChainSpecBuilderCmd::AddCodeSubstitute(AddCodeSubstituteCmd {
ref input_chain_spec,
ref runtime_wasm_path,
block_height,
}) => {
let chain_spec = ChainSpec::from_json_file(input_chain_spec.clone())?;

let mut chain_spec_json =
serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(false)?)
.map_err(|e| format!("Conversion to json failed: {e}"))?;

set_code_substitute_in_json_chain_spec(
&mut chain_spec_json,
&fs::read(runtime_wasm_path.as_path())
.map_err(|e| format!("Wasm blob file could not be read: {e}"))?[..],
block_height,
);
let chain_spec_json = serde_json::to_string_pretty(&chain_spec_json)
.map_err(|e| format!("to pretty failed: {e}"))?;
fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?;
},
ChainSpecBuilderCmd::ConvertToRaw(ConvertToRawCmd { ref input_chain_spec }) => {
let chain_spec = ChainSpec::from_json_file(input_chain_spec.clone())?;

let chain_spec_json =
serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(true)?)
.map_err(|e| format!("Conversion to json failed: {e}"))?;

let chain_spec_json = serde_json::to_string_pretty(&chain_spec_json)
.map_err(|e| format!("Conversion to pretty failed: {e}"))?;
fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?;
},
ChainSpecBuilderCmd::Verify(VerifyCmd { ref input_chain_spec }) => {
let chain_spec = ChainSpec::from_json_file(input_chain_spec.clone())?;
let _ = serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(true)?)
.map_err(|e| format!("Conversion to json failed: {e}"))?;
},
ChainSpecBuilderCmd::ListPresets(ListPresetsCmd { runtime_wasm_path }) => {
let code = fs::read(runtime_wasm_path.as_path())
.map_err(|e| format!("wasm blob shall be readable {e}"))?;
let caller: GenesisConfigBuilderRuntimeCaller =
GenesisConfigBuilderRuntimeCaller::new(&code[..], Default::default());
let presets = caller
.preset_names()
.map_err(|e| format!("getting default config from runtime should work: {e}"))?;
let presets: Vec<String> = presets
.into_iter()
.map(|preset| {
String::from(
TryInto::<&str>::try_into(&preset)
.unwrap_or_else(|_| "cannot display preset id")
.to_string(),
)
})
.collect();
println!("{}", serde_json::json!({"presets":presets}).to_string());
},
ChainSpecBuilderCmd::DisplayPreset(DisplayPresetCmd { runtime_wasm_path, preset_name }) => {
let code = fs::read(runtime_wasm_path.as_path())
.map_err(|e| format!("wasm blob shall be readable {e}"))?;
let caller: GenesisConfigBuilderRuntimeCaller =
GenesisConfigBuilderRuntimeCaller::new(&code[..], Default::default());
let preset = caller
.get_named_preset(preset_name.as_ref())
.map_err(|e| format!("getting default config from runtime should work: {e}"))?;
println!("{preset}");
},
};
Ok(())
builder.run()
}
19 changes: 13 additions & 6 deletions substrate/bin/utils/chain-spec-builder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -20,7 +20,11 @@
docify::compile_markdown!("README.docify.md", "README.md");

use clap::{Parser, Subcommand};
use sc_chain_spec::{OffchainExecutorParams, ChainType, GenericChainSpec, GenesisConfigBuilderRuntimeCaller};
use sc_chain_spec::{
json_patch, set_code_substitute_in_json_chain_spec, update_code_in_json_chain_spec, ChainType,
GenericChainSpec, GenesisConfigBuilderRuntimeCaller, OffchainExecutorParams,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{
borrow::Cow,
@@ -79,7 +83,8 @@ pub struct CreateCmd {
/// errors will be reported.
#[arg(long, short = 'v')]
verify: bool,
/// Number of extra heap pages available to executor when calling into runtime to build chain spec.
/// Number of extra heap pages available to executor when calling into runtime to build chain
/// spec.
#[arg(long)]
extra_heap_pages: Option<u64>,
#[command(subcommand)]
@@ -288,7 +293,7 @@ impl ChainSpecBuilder {
let code = fs::read(runtime.as_path())
.map_err(|e| format!("wasm blob shall be readable {e}"))?;
let caller: GenesisConfigBuilderRuntimeCaller =
GenesisConfigBuilderRuntimeCaller::new(&code[..]);
GenesisConfigBuilderRuntimeCaller::new(&code[..], Default::default());
let presets = caller
.preset_names()
.map_err(|e| format!("getting default config from runtime should work: {e}"))?;
@@ -298,7 +303,7 @@ impl ChainSpecBuilder {
let code = fs::read(runtime.as_path())
.map_err(|e| format!("wasm blob shall be readable {e}"))?;
let caller: GenesisConfigBuilderRuntimeCaller =
GenesisConfigBuilderRuntimeCaller::new(&code[..]);
GenesisConfigBuilderRuntimeCaller::new(&code[..], Default::default());
let preset = caller
.get_named_preset(preset_name.as_ref())
.map_err(|e| format!("getting default config from runtime should work: {e}"))?;
@@ -347,8 +352,10 @@ fn process_action<T: Serialize + Clone + Sync + 'static>(
)?)
},
GenesisBuildAction::Default(DefaultCmd {}) => {
let caller: GenesisConfigBuilderRuntimeCaller =
GenesisConfigBuilderRuntimeCaller::new(&code[..], OffchainExecutorParams { extra_heap_pages: cmd.extra_heap_pages });
let caller: GenesisConfigBuilderRuntimeCaller = GenesisConfigBuilderRuntimeCaller::new(
&code,
OffchainExecutorParams { extra_heap_pages: cmd.extra_heap_pages },
);
let default_config = caller
.get_default_config()
.map_err(|e| format!("getting default config from runtime should work: {e}"))?;
6 changes: 3 additions & 3 deletions substrate/client/allocator/src/freeing_bump.rs
Original file line number Diff line number Diff line change
@@ -96,13 +96,13 @@ const LOG_TARGET: &str = "wasm-heap";
// The minimum possible allocation size is chosen to be 8 bytes because in that case we would have
// easier time to provide the guaranteed alignment of 8.
//
// The maximum possible allocation size is set in the primitives to 64MiB.
// The maximum possible allocation size is set in the primitives to 32MiB.
//
// N_ORDERS - represents the number of orders supported.
//
// This number corresponds to the number of powers between the minimum possible allocation and
// maximum possible allocation, or: 2^3...2^26 (both ends inclusive, hence 24).
const N_ORDERS: usize = 24;
// maximum possible allocation, or: 2^3...2^25 (both ends inclusive, hence 23).
const N_ORDERS: usize = 23;
const MIN_POSSIBLE_ALLOCATION: u32 = 8; // 2^3 bytes, 8 bytes

/// The exponent for the power of two sized block adjusted to the minimum size.
16 changes: 8 additions & 8 deletions substrate/client/chain-spec/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -18,10 +18,9 @@

//! Substrate chain configurations.
#![warn(missing_docs)]
use crate::OffchainExecutorParams;
use crate::{
extension::GetExtension, genesis_config_builder::HostFunctions, ChainType,
GenesisConfigBuilderRuntimeCaller as RuntimeCaller, Properties,
GenesisConfigBuilderRuntimeCaller as RuntimeCaller, OffchainExecutorParams, Properties,
};
use sc_network::config::MultiaddrWithPeerId;
use sc_telemetry::TelemetryEndpoints;
@@ -122,7 +121,8 @@ impl<EHF: HostFunctions> GenesisSource<EHF> {
code: code.clone(),
})),
Self::GenesisBuilderApi(GenesisBuildAction::NamedPreset(name, _), code) => {
let patch = RuntimeCaller::<EHF>::new(&code[..], Default::default()).get_named_preset(Some(name))?;
let patch = RuntimeCaller::<EHF>::new(&code[..], Default::default())
.get_named_preset(Some(name))?;
Ok(Genesis::RuntimeGenesis(RuntimeGenesisInner {
json_blob: RuntimeGenesisConfigJson::Patch(patch),
code: code.clone(),
@@ -439,9 +439,7 @@ impl<E, EHF> ChainSpecBuilder<E, EHF> {
code_substitutes: BTreeMap::new(),
};

let executor_params = OffchainExecutorParams {
extra_heap_pages: self.heap_pages,
};
let executor_params = OffchainExecutorParams { extra_heap_pages: self.heap_pages };

ChainSpec {
client_spec,
@@ -604,7 +602,8 @@ where
}),
) => {
let mut storage =
RuntimeCaller::<EHF>::new(&code[..], self.executor_params.clone()).get_storage_for_config(config)?;
RuntimeCaller::<EHF>::new(&code[..], self.executor_params.clone())
.get_storage_for_config(config)?;
storage.top.insert(sp_core::storage::well_known_keys::CODE.to_vec(), code);
RawGenesis::from(storage)
},
@@ -616,7 +615,8 @@ where
}),
) => {
let mut storage =
RuntimeCaller::<EHF>::new(&code[..], self.executor_params.clone()).get_storage_for_patch(patch)?;
RuntimeCaller::<EHF>::new(&code[..], self.executor_params.clone())
.get_storage_for_patch(patch)?;
storage.top.insert(sp_core::storage::well_known_keys::CODE.to_vec(), code);
RawGenesis::from(storage)
},
47 changes: 29 additions & 18 deletions substrate/client/chain-spec/src/genesis_config_builder.rs
Original file line number Diff line number Diff line change
@@ -64,9 +64,12 @@ where
/// This code is later referred to as `runtime`.
pub fn new(code: &'a [u8], executor_params: OffchainExecutorParams) -> Self {
let mut executor = WasmExecutor::<(sp_io::SubstrateHostFunctions, EHF)>::builder()
.with_allow_missing_host_functions(true);
.with_allow_missing_host_functions(true);
if let Some(heap_pages) = executor_params.extra_heap_pages {
executor = executor.with_offchain_heap_alloc_strategy(sc_executor::HeapAllocStrategy::Static { extra_pages: heap_pages as _ })
executor =
executor.with_offchain_heap_alloc_strategy(sc_executor::HeapAllocStrategy::Static {
extra_pages: heap_pages as _,
})
};
GenesisConfigBuilderRuntimeCaller {
code: code.into(),
@@ -188,30 +191,36 @@ mod tests {
#[test]
fn list_presets_works() {
sp_tracing::try_init_simple();
let presets =
<GenesisConfigBuilderRuntimeCaller>::new(substrate_test_runtime::wasm_binary_unwrap())
.preset_names()
.unwrap();
let presets = <GenesisConfigBuilderRuntimeCaller>::new(
substrate_test_runtime::wasm_binary_unwrap(),
Default::default(),
)
.preset_names()
.unwrap();
assert_eq!(presets, vec![PresetId::from("foobar"), PresetId::from("staging"),]);
}

#[test]
fn get_default_config_works() {
let config =
<GenesisConfigBuilderRuntimeCaller>::new(substrate_test_runtime::wasm_binary_unwrap())
.get_default_config()
.unwrap();
let config = <GenesisConfigBuilderRuntimeCaller>::new(
substrate_test_runtime::wasm_binary_unwrap(),
Default::default(),
)
.get_default_config()
.unwrap();
let expected = r#"{"babe": {"authorities": [], "epochConfig": {"allowed_slots": "PrimaryAndSecondaryVRFSlots", "c": [1, 4]}}, "balances": {"balances": []}, "substrateTest": {"authorities": []}, "system": {}}"#;
assert_eq!(from_str::<Value>(expected).unwrap(), config);
}

#[test]
fn get_named_preset_works() {
sp_tracing::try_init_simple();
let config =
<GenesisConfigBuilderRuntimeCaller>::new(substrate_test_runtime::wasm_binary_unwrap())
.get_named_preset(Some(&"foobar".to_string()))
.unwrap();
let config = <GenesisConfigBuilderRuntimeCaller>::new(
substrate_test_runtime::wasm_binary_unwrap(),
Default::default(),
)
.get_named_preset(Some(&"foobar".to_string()))
.unwrap();
let expected = r#"{"foo":"bar"}"#;
assert_eq!(from_str::<Value>(expected).unwrap(), config);
}
@@ -230,10 +239,12 @@ mod tests {
},
});

let storage =
<GenesisConfigBuilderRuntimeCaller>::new(substrate_test_runtime::wasm_binary_unwrap())
.get_storage_for_patch(patch)
.unwrap();
let storage = <GenesisConfigBuilderRuntimeCaller>::new(
substrate_test_runtime::wasm_binary_unwrap(),
Default::default(),
)
.get_storage_for_patch(patch)
.unwrap();

//Babe|Authorities
let value: Vec<u8> = storage
1 change: 0 additions & 1 deletion substrate/client/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -34,7 +34,6 @@ serde_json = { workspace = true, default-features = true }
thiserror = { workspace = true }
# personal fork here as workaround for: https://github.com/rust-bitcoin/rust-bip39/pull/64
bip39 = { package = "parity-bip39", version = "2.0.1", features = ["rand"] }
tokio = { features = ["parking_lot", "rt-multi-thread", "signal"], workspace = true, default-features = true }
sc-chain-spec = { workspace = true, default-features = true }
sc-client-api = { workspace = true, default-features = true }
sc-client-db = { workspace = true }
4 changes: 2 additions & 2 deletions substrate/primitives/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -407,9 +407,9 @@ macro_rules! impl_maybe_marker_std_or_serde {
}

/// The maximum number of bytes that can be allocated at one time.
// The maximum possible allocation size was chosen rather arbitrary, 64 MiB should be enough for
// The maximum possible allocation size was chosen rather arbitrary, 32 MiB should be enough for
// everybody.
pub const MAX_POSSIBLE_ALLOCATION: u32 = 67108864; // 2^26 bytes, 64 MiB
pub const MAX_POSSIBLE_ALLOCATION: u32 = 33554432; // 2^25 bytes, 32 MiB

/// Generates a macro for checking if a certain feature is enabled.
///
Original file line number Diff line number Diff line change
@@ -116,7 +116,7 @@ fn genesis_from_code<EHF: HostFunctions>(
sp_io::SubstrateHostFunctions,
frame_benchmarking::benchmarking::HostFunctions,
EHF,
)>::new(code);
)>::new(code, Default::default());

let mut preset_json = genesis_config_caller.get_named_preset(Some(genesis_builder_preset))?;
if let Some(patcher) = storage_patcher {
Loading
Oops, something went wrong.
You are viewing a condensed version of this merge commit. You can view the full changes here.