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

Check contstraints for memory parameters #940

Merged
merged 5 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions bin/node/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();

Expand All @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions bin/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,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 = [
Expand Down
30 changes: 29 additions & 1 deletion bin/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,10 +1015,38 @@ impl_runtime_apis! {

#[cfg(test)]
mod tests {
use crate::VERSION;
use frame_support::traits::Get;
use primitives::HEAP_PAGES;
use smallvec::Array;
deuszx marked this conversation as resolved.
Show resolved Hide resolved

use super::*;

#[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 = <Runtime as pallet_contracts::Config>::Schedule::get()
.limits
.max_memory_size();
// Max call depth is CallStack::size() + 1
let max_call_depth = <Runtime as pallet_contracts::Config>::CallStack::size() as u32 + 1;
// Max code len
let max_code_len: u32 = <Runtime as pallet_contracts::Config>::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;
DamianStraszak marked this conversation as resolved.
Show resolved Hide resolved

assert!(lhs < rhs);
}
}
3 changes: 3 additions & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down