Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Mutable static proof.
Browse files Browse the repository at this point in the history
  • Loading branch information
pepyakin committed Mar 28, 2019
1 parent 9a243d8 commit 3d7b27f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions core/sr-api-macros/tests/runtime_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,12 @@ fn calling_with_native_else_wasm_and_fail_on_native_should_work() {
let block_id = BlockId::Number(client.info().unwrap().chain.best_number);
assert_eq!(runtime_api.fail_on_native(&block_id).unwrap(), 1);
}

#[test]
fn returns_mutable_static() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::AlwaysWasm);
let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().unwrap().chain.best_number);
assert_eq!(runtime_api.returns_mutable_static(&block_id).unwrap(), 33);
assert_eq!(runtime_api.returns_mutable_static(&block_id).unwrap(), 33);
}
20 changes: 19 additions & 1 deletion core/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ cfg_if! {
fn fail_on_wasm() -> u64;
fn benchmark_indirect_call() -> u64;
fn benchmark_direct_call() -> u64;
fn returns_mutable_static() -> u64;
}
}
} else {
Expand All @@ -254,6 +255,7 @@ cfg_if! {
fn fail_on_wasm() -> u64;
fn benchmark_indirect_call() -> u64;
fn benchmark_direct_call() -> u64;
fn returns_mutable_static() -> u64;
}
}
}
Expand All @@ -279,6 +281,10 @@ fn benchmark_add_one(i: u64) -> u64 {
#[cfg(not(feature = "std"))]
static BENCHMARK_ADD_ONE: runtime_io::ExchangeableFunction<fn(u64) -> u64> = runtime_io::ExchangeableFunction::new(benchmark_add_one);

/// Mutable static variables should be always observed to have
/// the initialized value at the start of a runtime call.
static mut MUTABLE_STATIC: u64 = 32;

cfg_if! {
if #[cfg(feature = "std")] {
impl_runtime_apis! {
Expand Down Expand Up @@ -372,6 +378,12 @@ cfg_if! {
fn benchmark_direct_call() -> u64 {
(0..1000).fold(0, |p, i| p + benchmark_add_one(i))
}
fn returns_mutable_static() -> u64 {
unsafe {
MUTABLE_STATIC += 1;
MUTABLE_STATIC
}
}
}

impl consensus_aura::AuraApi<Block> for Runtime {
Expand Down Expand Up @@ -475,11 +487,17 @@ cfg_if! {
fn benchmark_direct_call() -> u64 {
(0..10000).fold(0, |p, i| p + benchmark_add_one(i))
}
fn returns_mutable_static() -> u64 {
unsafe {
MUTABLE_STATIC += 1;
MUTABLE_STATIC
}
}
}

impl consensus_aura::AuraApi<Block> for Runtime {
fn slot_duration() -> u64 { 1 }
}
}
}
}
}
Binary file not shown.
Binary file not shown.

0 comments on commit 3d7b27f

Please sign in to comment.