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

Append overlay refactor proposal #13940

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
03beafd
append variant with initial sizes.
cheme Apr 17, 2023
50cb263
adapt test
cheme Apr 18, 2023
444f54c
append twice code path
cheme Apr 18, 2023
8e6c421
a bit more code path
cheme Apr 18, 2023
d77ac22
rename
cheme Apr 18, 2023
431d5f5
use fields in enum append
cheme Apr 25, 2023
81ec941
simplify logic:
cheme Apr 25, 2023
4a6c7d5
renamings
cheme Apr 25, 2023
fb2b703
non read access logic
cheme Apr 26, 2023
cf6da1a
style
cheme Apr 26, 2023
fd59ad6
Mutable read access and render on read.
cheme Apr 26, 2023
757e475
fix set in same overlay logic
cheme Apr 26, 2023
cc67a05
Fix commit
cheme Apr 26, 2023
509e544
additional assert
cheme Apr 26, 2023
1dc8fa7
clippying
cheme Apr 26, 2023
732e85e
Merge branch 'master' into append
cheme May 16, 2023
9282ed8
initial fuzzing impl
cheme May 16, 2023
f10b873
fuzzer and fixes
cheme May 16, 2023
0311300
fix reference impl
cheme May 16, 2023
ef4d370
fix depth monitoring
cheme May 16, 2023
4a898ab
fix bug
cheme May 16, 2023
f313cff
Restore append on commit
cheme May 16, 2023
68e3ff4
fix similar corner case
cheme May 16, 2023
4a54331
new item to have variance in compact len encoding and break again
cheme May 16, 2023
5b48e97
Decreasing size can actuallly happen
cheme May 17, 2023
70c896d
factor
cheme May 17, 2023
a5f0704
no std
cheme May 17, 2023
d5b0db4
Merge branch 'master' into append
cheme Jul 13, 2023
1f58a09
Merge branch 'master' into append
cheme Jul 31, 2023
4b718c0
missing one
cheme Jul 31, 2023
c5d084e
fmt
cheme Jul 31, 2023
aa496e6
Merge branch 'master' into append
cheme Aug 2, 2023
ec4bbe0
Merge branch 'master' into append
cheme Aug 3, 2023
b85347b
doc
cheme Aug 3, 2023
c780830
slightly enhance append function readability.
cheme Aug 3, 2023
017d2e9
_simple -> _offchain
cheme Aug 3, 2023
ef3abee
slight commit readability changes.
cheme Aug 3, 2023
283d79a
Using enum to make logic explicit when reading.
cheme Aug 3, 2023
f24acbd
from_parent is necessary
cheme Aug 3, 2023
03e520b
Merge branch 'master' into append, make changes for backend transacti…
cheme Aug 28, 2023
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.

8 changes: 4 additions & 4 deletions client/executor/src/integration_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,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 @@ -218,15 +218,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
14 changes: 8 additions & 6 deletions client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,9 +943,10 @@ where
peers: HashSet<Multiaddr>,
) -> Result<(), String> {
let Some(set_id) = self.notification_protocol_ids.get(&protocol) else {
return Err(
format!("Cannot add peers to reserved set of unknown protocol: {}", protocol)
)
return Err(format!(
"Cannot add peers to reserved set of unknown protocol: {}",
protocol
))
};

let peers = self.split_multiaddr_and_peer_id(peers)?;
Expand Down Expand Up @@ -974,9 +975,10 @@ where
peers: Vec<PeerId>,
) -> Result<(), String> {
let Some(set_id) = self.notification_protocol_ids.get(&protocol) else {
return Err(
format!("Cannot remove peers from reserved set of unknown protocol: {}", protocol)
)
return Err(format!(
"Cannot remove peers from reserved set of unknown protocol: {}",
protocol
))
};

for peer_id in peers.into_iter() {
Expand Down
6 changes: 4 additions & 2 deletions client/rpc-spec-v2/src/chain_head/chain_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ where
follow_subscription: String,
operation_id: String,
) -> RpcResult<()> {
let Some(operation) = self.subscriptions.get_operation(&follow_subscription, &operation_id) else {
let Some(operation) = self.subscriptions.get_operation(&follow_subscription, &operation_id)
else {
return Ok(())
};

Expand All @@ -479,7 +480,8 @@ where
follow_subscription: String,
operation_id: String,
) -> RpcResult<()> {
let Some(operation) = self.subscriptions.get_operation(&follow_subscription, &operation_id) else {
let Some(operation) = self.subscriptions.get_operation(&follow_subscription, &operation_id)
else {
return Ok(())
};

Expand Down
4 changes: 1 addition & 3 deletions client/rpc-spec-v2/src/chain_head/chain_head_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ where

let mut ret = Vec::with_capacity(self.operation_max_storage_items);
for _ in 0..self.operation_max_storage_items {
let Some(key) = keys_iter.next() else {
break
};
let Some(key) = keys_iter.next() else { break };

let result = match ty {
IterQueryType::Value => self.query_storage_value(hash, &key, child_key),
Expand Down
8 changes: 2 additions & 6 deletions frame/broker/src/dispatchable_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,8 @@ impl<T: Config> Pallet<T> {
region.begin = r + 1;
contribution.length.saturating_dec();

let Some(mut pool_record) = InstaPoolHistory::<T>::get(r) else {
continue;
};
let Some(total_payout) = pool_record.maybe_payout else {
break;
};
let Some(mut pool_record) = InstaPoolHistory::<T>::get(r) else { continue };
let Some(total_payout) = pool_record.maybe_payout else { break };
let p = total_payout
.saturating_mul(contributed_parts.into())
.checked_div(&pool_record.private_contributions.into())
Expand Down
8 changes: 2 additions & 6 deletions frame/broker/src/tick_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ impl<T: Config> Pallet<T> {
}

pub(crate) fn process_revenue() -> bool {
let Some((until, amount)) = T::Coretime::check_notify_revenue_info() else {
return false;
};
let Some((until, amount)) = T::Coretime::check_notify_revenue_info() else { return false };
let when: Timeslice =
(until / T::TimeslicePeriod::get()).saturating_sub(One::one()).saturated_into();
let mut revenue = T::ConvertBalance::convert_back(amount);
Expand Down Expand Up @@ -289,9 +287,7 @@ impl<T: Config> Pallet<T> {
rc_begin: RelayBlockNumberOf<T>,
core: CoreIndex,
) {
let Some(workplan) = Workplan::<T>::take((timeslice, core)) else {
return;
};
let Some(workplan) = Workplan::<T>::take((timeslice, core)) else { return };
let workload = Workload::<T>::get(core);
let parts_used = workplan.iter().map(|i| i.mask).fold(CoreMask::void(), |a, i| a | i);
let mut workplan = workplan.into_inner();
Expand Down
2 changes: 1 addition & 1 deletion frame/safe-mode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ pub mod pallet {
/// [`EnteredUntil`].
fn on_initialize(current: BlockNumberFor<T>) -> Weight {
let Some(limit) = EnteredUntil::<T>::get() else {
return T::WeightInfo::on_initialize_noop();
return T::WeightInfo::on_initialize_noop()
};

if current > limit {
Expand Down
16 changes: 8 additions & 8 deletions primitives/externalities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,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 @@ -125,20 +125,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 primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,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(|s| bytes::Bytes::from(s.to_vec()))
}

Expand All @@ -180,7 +180,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 @@ -201,7 +201,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 @@ -377,7 +377,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 @@ -390,7 +390,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 @@ -468,7 +468,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
5 changes: 4 additions & 1 deletion primitives/state-machine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
arbitrary = { version = "1", features = ["derive"], optional = true }
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false }
hash-db = { version = "0.16.0", default-features = false }
log = { version = "0.4.17", default-features = false }
Expand All @@ -35,9 +36,11 @@ pretty_assertions = "1.2.1"
rand = "0.8.5"
sp-runtime = { version = "24.0.0", path = "../runtime" }
assert_matches = "1.5"
arbitrary = { version = "1", features = ["derive"] }

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

[package.metadata]
cargo-fuzz = true

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

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

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

[profile.release]
debug = 1

[[bin]]
name = "fuzz_append"
path = "fuzz_targets/fuzz_append.rs"
test = false
doc = false
9 changes: 9 additions & 0 deletions primitives/state-machine/fuzz/fuzz_targets/fuzz_append.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![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