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

Append overlay optimization. #1223

Merged
merged 59 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 57 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
0466d8c
append overlay optimization.
cheme Aug 28, 2023
341987f
Merge branch 'master' into append
cheme Aug 28, 2023
ac2b72c
update lock
cheme Aug 28, 2023
1fdd3c6
no more companion
cheme Aug 29, 2023
c94dde4
clippy
cheme Aug 29, 2023
a022d9b
fmt
cheme Aug 29, 2023
d8f21a0
Merge branch 'master' into append
cheme Nov 9, 2023
e261624
license
cheme Nov 9, 2023
31eebd3
Merge branch 'master' into append
cheme Jan 15, 2024
0c9aedc
lock update
cheme Jan 15, 2024
5c44ad3
trying prdoc
cheme Jan 15, 2024
9541b33
mut
cheme Jan 15, 2024
70b94cc
mut
cheme Jan 15, 2024
b28527f
fuzzing in its own file.
cheme Jan 16, 2024
bd3c28e
trying to return error
cheme Jan 16, 2024
7b9face
Revert "trying to return error" not too sure about this direction
cheme Jan 16, 2024
3bdd2f0
minor enhance
cheme Jan 16, 2024
066a8c2
fix
cheme Jan 16, 2024
0fba977
fix
cheme Jan 16, 2024
a105d3d
code change from review
cheme Feb 1, 2024
94c262c
renamings and comment
cheme Feb 1, 2024
eaf89f4
Merge branch 'master' into append
cheme Feb 1, 2024
c0e2da7
update lock
cheme Feb 1, 2024
e8f5a21
test of restore lower append size.
cheme Feb 1, 2024
3fe1c9f
Bump node version to 1.11.0 in polkadot-cli
EgorPopelyaev Apr 24, 2024
168bccb
Bump crate versions in: cumulus/polkadot-parachain/Cargo.toml
EgorPopelyaev Apr 24, 2024
a85bdb5
Bump spec_version
EgorPopelyaev Apr 25, 2024
0d65b3f
Move prdocs to the release folder
EgorPopelyaev Apr 25, 2024
082e23e
Update Cargo.lock
EgorPopelyaev Apr 25, 2024
49b27c7
add environment var to the matrix job
EgorPopelyaev Apr 25, 2024
0bb6249
Bump transaction_version
EgorPopelyaev Apr 25, 2024
9b12bab
Litep2p version 0.5.0 backport to 1.11.0 (#4605)
lexnv May 28, 2024
9db7629
`CheckMetadataHash` extension backport to 1.11.0 (#4584)
bkchr May 28, 2024
320cea6
Add omni bencher & chain-spec-builder bins to release (#4557)
EgorPopelyaev May 29, 2024
fc127b7
fix the release draft flow
EgorPopelyaev May 29, 2024
503aee6
Merge remote-tracking branch 'origin/release-polkadot-v1.11.0' into a…
bkchr May 30, 2024
310efa0
Fixes
bkchr May 30, 2024
8d2256f
Merge remote-tracking branch 'origin/master' into append
bkchr May 30, 2024
df03d26
Fixes
bkchr May 30, 2024
36bf1dc
Fixes
bkchr May 30, 2024
d45a374
Fixes
bkchr May 31, 2024
4305573
Some optimizations
bkchr May 31, 2024
03432c3
Try some stuff
bkchr May 31, 2024
5d9386a
Some small fixes
bkchr Jun 3, 2024
9287edd
Improve docs and naming
bkchr Jun 3, 2024
7ddc302
More renamings
bkchr Jun 3, 2024
11ff0c7
Docs
bkchr Jun 3, 2024
711bb1c
Remove the warning
bkchr Jun 3, 2024
d03e9e5
Fixes
bkchr Jun 3, 2024
4a84215
FMT
bkchr Jun 3, 2024
5505a0f
Fix
bkchr Jun 3, 2024
4f968fb
PRDOC
bkchr Jun 3, 2024
58013c9
Merge remote-tracking branch 'origin/master' into append
bkchr Jun 3, 2024
6fdfec0
Add test and comment about append merge on commit
skunert Jun 11, 2024
adc8c83
Update substrate/primitives/state-machine/src/fuzzing.rs
bkchr Jun 11, 2024
4561991
Update substrate/primitives/state-machine/src/overlayed_changes/chang…
bkchr Jun 11, 2024
078e116
Merge branch 'master' into append
bkchr Jun 11, 2024
318dc62
Update substrate/primitives/state-machine/src/overlayed_changes/chang…
bkchr Jun 11, 2024
18513b3
Update substrate/primitives/state-machine/src/overlayed_changes/chang…
bkchr Jun 11, 2024
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
15 changes: 15 additions & 0 deletions Cargo.lock

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

12 changes: 6 additions & 6 deletions polkadot/node/core/pvf/common/src/executor_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,19 @@ type HostFunctions = (
struct ValidationExternalities(sp_externalities::Extensions);

impl sp_externalities::Externalities for ValidationExternalities {
fn storage(&self, _: &[u8]) -> Option<Vec<u8>> {
fn storage(&mut self, _: &[u8]) -> Option<Vec<u8>> {
panic!("storage: unsupported feature for parachain validation")
}

fn storage_hash(&self, _: &[u8]) -> Option<Vec<u8>> {
fn storage_hash(&mut self, _: &[u8]) -> Option<Vec<u8>> {
panic!("storage_hash: unsupported feature for parachain validation")
}

fn child_storage_hash(&self, _: &ChildInfo, _: &[u8]) -> Option<Vec<u8>> {
fn child_storage_hash(&mut self, _: &ChildInfo, _: &[u8]) -> Option<Vec<u8>> {
panic!("child_storage_hash: unsupported feature for parachain validation")
}

fn child_storage(&self, _: &ChildInfo, _: &[u8]) -> Option<Vec<u8>> {
fn child_storage(&mut self, _: &ChildInfo, _: &[u8]) -> Option<Vec<u8>> {
panic!("child_storage: unsupported feature for parachain validation")
}

Expand Down Expand Up @@ -275,11 +275,11 @@ impl sp_externalities::Externalities for ValidationExternalities {
panic!("child_storage_root: unsupported feature for parachain validation")
}

fn next_child_storage_key(&self, _: &ChildInfo, _: &[u8]) -> Option<Vec<u8>> {
fn next_child_storage_key(&mut self, _: &ChildInfo, _: &[u8]) -> Option<Vec<u8>> {
panic!("next_child_storage_key: unsupported feature for parachain validation")
}

fn next_storage_key(&self, _: &[u8]) -> Option<Vec<u8>> {
fn next_storage_key(&mut self, _: &[u8]) -> Option<Vec<u8>> {
panic!("next_storage_key: unsupported feature for parachain validation")
}

Expand Down
13 changes: 13 additions & 0 deletions prdoc/pr_1223.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
title: Optimize storage append operation

doc:
- audience: [Node Dev, Runtime Dev]
description: |
This pull request optimizes the storage append operation in the `OverlayedChanges`.
Before the internal buffer was cloned every time a new transaction was created. Cloning
the internal buffer is now only done when there is no other possibility. This should
improve the performance in situations like when depositing events from batched calls.

crates:
- name: sp-state-machine
bump: major
8 changes: 4 additions & 4 deletions substrate/client/executor/src/integration_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,15 @@ fn storage_should_work(wasm_method: WasmExecutionMethod) {
assert_eq!(output, b"all ok!".to_vec().encode());
}

let expected = TestExternalities::new(sp_core::storage::Storage {
let mut expected = TestExternalities::new(sp_core::storage::Storage {
top: map![
b"input".to_vec() => value,
b"foo".to_vec() => b"bar".to_vec(),
b"baz".to_vec() => b"bar".to_vec()
],
children_default: map![],
});
assert_eq!(ext, expected);
assert!(ext.eq(&mut expected));
}

test_wasm_execution!(clear_prefix_should_work);
Expand All @@ -208,15 +208,15 @@ fn clear_prefix_should_work(wasm_method: WasmExecutionMethod) {
assert_eq!(output, b"all ok!".to_vec().encode());
}

let expected = TestExternalities::new(sp_core::storage::Storage {
let mut expected = TestExternalities::new(sp_core::storage::Storage {
top: map![
b"aaa".to_vec() => b"1".to_vec(),
b"aab".to_vec() => b"2".to_vec(),
b"bbb".to_vec() => b"5".to_vec()
],
children_default: map![],
});
assert_eq!(expected, ext);
assert!(expected.eq(&mut ext));
}

test_wasm_execution!(blake2_256_should_work);
Expand Down
16 changes: 8 additions & 8 deletions substrate/primitives/externalities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,24 @@ pub trait Externalities: ExtensionStore {
fn set_offchain_storage(&mut self, key: &[u8], value: Option<&[u8]>);

/// Read runtime storage.
fn storage(&self, key: &[u8]) -> Option<Vec<u8>>;
fn storage(&mut self, key: &[u8]) -> Option<Vec<u8>>;

/// Get storage value hash.
///
/// This may be optimized for large values.
fn storage_hash(&self, key: &[u8]) -> Option<Vec<u8>>;
fn storage_hash(&mut self, key: &[u8]) -> Option<Vec<u8>>;

/// Get child storage value hash.
///
/// This may be optimized for large values.
///
/// Returns an `Option` that holds the SCALE encoded hash.
fn child_storage_hash(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;
fn child_storage_hash(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;

/// Read child runtime storage.
///
/// Returns an `Option` that holds the SCALE encoded hash.
fn child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;
fn child_storage(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;

/// Set storage entry `key` of current contract being called (effective immediately).
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
Expand All @@ -124,20 +124,20 @@ pub trait Externalities: ExtensionStore {
}

/// Whether a storage entry exists.
fn exists_storage(&self, key: &[u8]) -> bool {
fn exists_storage(&mut self, key: &[u8]) -> bool {
self.storage(key).is_some()
}

/// Whether a child storage entry exists.
fn exists_child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> bool {
fn exists_child_storage(&mut self, child_info: &ChildInfo, key: &[u8]) -> bool {
self.child_storage(child_info, key).is_some()
}

/// Returns the key immediately following the given key, if it exists.
fn next_storage_key(&self, key: &[u8]) -> Option<Vec<u8>>;
fn next_storage_key(&mut self, key: &[u8]) -> Option<Vec<u8>>;

/// Returns the key immediately following the given key, if it exists, in child storage.
fn next_child_storage_key(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;
fn next_child_storage_key(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;

/// Clear an entire child storage.
///
Expand Down
12 changes: 6 additions & 6 deletions substrate/primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl From<MultiRemovalResults> for KillStorageResult {
#[runtime_interface]
pub trait Storage {
/// Returns the data for `key` in the storage or `None` if the key can not be found.
fn get(&self, key: &[u8]) -> Option<bytes::Bytes> {
fn get(&mut self, key: &[u8]) -> Option<bytes::Bytes> {
self.storage(key).map(bytes::Bytes::from)
}

Expand All @@ -190,7 +190,7 @@ pub trait Storage {
/// doesn't exist at all.
/// If `value_out` length is smaller than the returned length, only `value_out` length bytes
/// are copied into `value_out`.
fn read(&self, key: &[u8], value_out: &mut [u8], value_offset: u32) -> Option<u32> {
fn read(&mut self, key: &[u8], value_out: &mut [u8], value_offset: u32) -> Option<u32> {
self.storage(key).map(|value| {
let value_offset = value_offset as usize;
let data = &value[value_offset.min(value.len())..];
Expand All @@ -211,7 +211,7 @@ pub trait Storage {
}

/// Check whether the given `key` exists in storage.
fn exists(&self, key: &[u8]) -> bool {
fn exists(&mut self, key: &[u8]) -> bool {
self.exists_storage(key)
}

Expand Down Expand Up @@ -387,7 +387,7 @@ pub trait DefaultChildStorage {
///
/// Parameter `storage_key` is the unprefixed location of the root of the child trie in the
/// parent trie. Result is `None` if the value for `key` in the child storage can not be found.
fn get(&self, storage_key: &[u8], key: &[u8]) -> Option<Vec<u8>> {
fn get(&mut self, storage_key: &[u8], key: &[u8]) -> Option<Vec<u8>> {
let child_info = ChildInfo::new_default(storage_key);
self.child_storage(&child_info, key).map(|s| s.to_vec())
}
Expand All @@ -400,7 +400,7 @@ pub trait DefaultChildStorage {
/// If `value_out` length is smaller than the returned length, only `value_out` length bytes
/// are copied into `value_out`.
fn read(
&self,
&mut self,
storage_key: &[u8],
key: &[u8],
value_out: &mut [u8],
Expand Down Expand Up @@ -478,7 +478,7 @@ pub trait DefaultChildStorage {
/// Check a child storage key.
///
/// Check whether the given `key` exists in default child defined at `storage_key`.
fn exists(&self, storage_key: &[u8], key: &[u8]) -> bool {
fn exists(&mut self, storage_key: &[u8], key: &[u8]) -> bool {
let child_info = ChildInfo::new_default(storage_key);
self.exists_child_storage(&child_info, key)
}
Expand Down
3 changes: 3 additions & 0 deletions substrate/primitives/state-machine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ sp-externalities = { path = "../externalities", default-features = false }
sp-panic-handler = { path = "../panic-handler", optional = true }
sp-trie = { path = "../trie", default-features = false }
trie-db = { version = "0.29.0", default-features = false }
arbitrary = { version = "1", features = ["derive"], optional = true }

[dev-dependencies]
array-bytes = "6.2.2"
pretty_assertions = "1.2.1"
rand = "0.8.5"
sp-runtime = { path = "../runtime" }
assert_matches = "1.5"
arbitrary = { version = "1", features = ["derive"] }

[features]
default = ["std"]
fuzzing = ["arbitrary"]
std = [
"codec/std",
"hash-db/std",
Expand Down
30 changes: 30 additions & 0 deletions substrate/primitives/state-machine/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "sp-state-machine-fuzz"
version = "0.0.0"
publish = false
license = "Apache-2.0"
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
sp-runtime = { path = "../../runtime" }

[dependencies.sp-state-machine]
path = ".."
features = ["fuzzing"]

# Prevent this from interfering with workspaces
[workspace]
members = ["."]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it interfere with it? Do we not want to run it from the top level?


[profile.release]
debug = 1

[[bin]]
name = "fuzz_append"
path = "fuzz_targets/fuzz_append.rs"
test = false
doc = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![no_main]

use libfuzzer_sys::fuzz_target;
use sp_state_machine::fuzzing::{fuzz_append, FuzzAppendPayload};
use sp_runtime::traits::BlakeTwo256;

fuzz_target!(|data: FuzzAppendPayload| {
fuzz_append::<BlakeTwo256>(data);
});
Loading
Loading