From 7bada081daa5f9fb7b0dc8863bc2e1f8cc73f1f9 Mon Sep 17 00:00:00 2001 From: Michal Swietek Date: Wed, 22 Feb 2023 16:13:32 +0100 Subject: [PATCH 1/4] Check contstraints for memory parameters --- Cargo.lock | 1 + bin/node/src/main.rs | 5 +---- bin/runtime/Cargo.toml | 1 + bin/runtime/src/lib.rs | 27 ++++++++++++++++++++++++++- primitives/src/lib.rs | 3 +++ 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0307f5a6df..88f42d98b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -325,6 +325,7 @@ dependencies = [ "primitives", "scale-info", "serde", + "smallvec", "sp-api", "sp-block-builder", "sp-consensus-aura", diff --git a/bin/node/src/main.rs b/bin/node/src/main.rs index 03ec6b8e9b..b6d8c4bffa 100644 --- a/bin/node/src/main.rs +++ b/bin/node/src/main.rs @@ -1,6 +1,7 @@ #[cfg(feature = "try-runtime")] use aleph_node::ExecutorDispatch; use aleph_node::{new_authority, new_full, new_partial, Cli, Subcommand}; +use aleph_primitives::HEAP_PAGES; #[cfg(feature = "try-runtime")] use aleph_runtime::Block; use log::warn; @@ -16,8 +17,6 @@ fn default_blocks_pruning() -> DatabasePruningMode { DatabasePruningMode::ArchiveCanonical } -const HEAP_PAGES: u64 = 4096; - fn pruning_changed(params: &PruningParams) -> bool { let state_pruning_changed = params.state_pruning != default_state_pruning(); @@ -26,8 +25,6 @@ fn pruning_changed(params: &PruningParams) -> bool { state_pruning_changed || blocks_pruning_changed } -// The default number of heap pages in substrate is 2048. Every heap page is 64KB, -// so value of 4096 gives 256MB memory for entire runtime. fn enforce_heap_pages(config: &mut Configuration) { config.default_heap_pages = Some(HEAP_PAGES); } diff --git a/bin/runtime/Cargo.toml b/bin/runtime/Cargo.toml index 8d1dd8e9a1..f414da4af1 100644 --- a/bin/runtime/Cargo.toml +++ b/bin/runtime/Cargo.toml @@ -14,6 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"] codec = { package = "parity-scale-codec", version = "3.0", default-features = false, features = ["derive"] } scale-info = { version = "2.0", default-features = false, features = ["derive"] } serde = { version = "1.0", optional = true, features = ["derive"] } +smallvec = { version = "1", default-features = false} frame-executive = { default-features = false, git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.35" } frame-support = { default-features = false, git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.35" } diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 8949983059..434ab4a1d3 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -1015,10 +1015,35 @@ impl_runtime_apis! { #[cfg(test)] mod tests { - use crate::VERSION; + use super::*; + use primitives::HEAP_PAGES; + use smallvec::Array; + use frame_support::traits::Get; + #[test] fn state_version_must_be_zero() { assert_eq!(0, VERSION.state_version); } + + #[test] + fn check_contracts_memory_parameters() { + // Memory limit of one instance of a runtime + const MAX_RUNTIME_MEM: u32 = HEAP_PAGES as u32 * 64 * 1024; + // Max stack size defined by wasmi - 1MB + const MAX_STACK_SIZE: u32 = 1024 * 1024; + // Max heap size is 16 mempages of 64KB each - 1MB + let max_heap_size = ::Schedule::get() + .limits + .max_memory_size(); + // Max call depth is CallStack::size() + 1 + let max_call_depth = ::CallStack::size() as u32 + 1; + // Max code len + let max_code_len: u32 = ::MaxCodeLen::get(); + + let lhs = max_call_depth * (72 * max_code_len + max_heap_size + MAX_STACK_SIZE); + let rhs = MAX_RUNTIME_MEM * 3 / 4; + + assert!(lhs < rhs); + } } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 554677fffb..48a94559f8 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -38,6 +38,9 @@ pub type BlockNumber = u32; pub type SessionCount = u32; pub type BlockCount = u32; +// Default number of heap pages that gives limit of 256MB for a runtime instance since each page is 64KB +pub const HEAP_PAGES: u64 = 4096; + pub const MILLISECS_PER_BLOCK: u64 = 1000; // We agreed to 5MB as the block size limit. pub const MAX_BLOCK_SIZE: u32 = 5 * 1024 * 1024; From e72ed47c108475bf34ce876a3b7b99aa46d361cf Mon Sep 17 00:00:00 2001 From: Michal Swietek Date: Wed, 22 Feb 2023 16:25:05 +0100 Subject: [PATCH 2/4] Add comments --- bin/runtime/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 434ab4a1d3..ef96096b42 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -1041,7 +1041,10 @@ mod tests { // Max code len let max_code_len: u32 = ::MaxCodeLen::get(); + // The factor comes from allocator, contracts representation, and wasmi let lhs = max_call_depth * (72 * max_code_len + max_heap_size + MAX_STACK_SIZE); + // We allocate only 75% of all runtime memory to contracts execution. Important: it's not + // enforeced in wasmtime let rhs = MAX_RUNTIME_MEM * 3 / 4; assert!(lhs < rhs); From cc397d1f4514199a8a998fdb0e8758a9166b9f87 Mon Sep 17 00:00:00 2001 From: Michal Swietek Date: Wed, 22 Feb 2023 16:25:22 +0100 Subject: [PATCH 3/4] Move smallvec to dev-dependencies --- bin/runtime/Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/runtime/Cargo.toml b/bin/runtime/Cargo.toml index f414da4af1..095317332f 100644 --- a/bin/runtime/Cargo.toml +++ b/bin/runtime/Cargo.toml @@ -14,7 +14,6 @@ targets = ["x86_64-unknown-linux-gnu"] codec = { package = "parity-scale-codec", version = "3.0", default-features = false, features = ["derive"] } scale-info = { version = "2.0", default-features = false, features = ["derive"] } serde = { version = "1.0", optional = true, features = ["derive"] } -smallvec = { version = "1", default-features = false} frame-executive = { default-features = false, git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.35" } frame-support = { default-features = false, git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.35" } @@ -65,6 +64,9 @@ pallet-elections = { path = "../../pallets/elections", default-features = false [build-dependencies] substrate-wasm-builder = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.35" } +[dev-dependencies] +smallvec = { version = "1", default-features = false} + [features] default = ["std"] std = [ From 61796aa6565cc885ef1aa5bac30bb9b324026c07 Mon Sep 17 00:00:00 2001 From: Michal Swietek Date: Wed, 22 Feb 2023 16:32:48 +0100 Subject: [PATCH 4/4] fmt --- bin/runtime/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index ef96096b42..c541077813 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -1015,11 +1015,11 @@ impl_runtime_apis! { #[cfg(test)] mod tests { - use super::*; + use frame_support::traits::Get; use primitives::HEAP_PAGES; use smallvec::Array; - use frame_support::traits::Get; + use super::*; #[test] fn state_version_must_be_zero() { @@ -1028,7 +1028,7 @@ mod tests { #[test] fn check_contracts_memory_parameters() { - // Memory limit of one instance of a runtime + // Memory limit of one instance of a runtime const MAX_RUNTIME_MEM: u32 = HEAP_PAGES as u32 * 64 * 1024; // Max stack size defined by wasmi - 1MB const MAX_STACK_SIZE: u32 = 1024 * 1024; @@ -1041,7 +1041,7 @@ mod tests { // Max code len let max_code_len: u32 = ::MaxCodeLen::get(); - // The factor comes from allocator, contracts representation, and wasmi + // The factor comes from allocator, contracts representation, and wasmi let lhs = max_call_depth * (72 * max_code_len + max_heap_size + MAX_STACK_SIZE); // We allocate only 75% of all runtime memory to contracts execution. Important: it's not // enforeced in wasmtime