Skip to content
Merged
Show file tree
Hide file tree
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
Next Next commit
Check contstraints for memory parameters
  • Loading branch information
mike1729 committed Feb 22, 2023
commit 7bada081daa5f9fb7b0dc8863bc2e1f8cc73f1f9
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
1 change: 1 addition & 0 deletions bin/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
27 changes: 26 additions & 1 deletion bin/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <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();

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);
}
}
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