Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
fuse imports and filter_map prepare_extrinsics_input_inner fold.
  • Loading branch information
cheme committed Sep 10, 2020
commit d1b10c84fdcb6518abbeced6b159b5e746cae753
2 changes: 1 addition & 1 deletion primitives/arithmetic/src/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! Infinite precision unsigned integer for substrate runtime.

use num_traits::Zero;
use sp_std::{cmp::Ordering, ops, prelude::*, cell::RefCell, convert::TryFrom, vec};
use sp_std::{cmp::Ordering, ops, prelude::*, cell::RefCell, convert::TryFrom};

// A sensible value for this would be half of the dword size of the host machine. Since the
// runtime is compiled to 32bit webassembly, using 32 and 64 for single and double respectively
Expand Down
2 changes: 1 addition & 1 deletion primitives/externalities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
sp-storage = { version = "2.0.0-rc6", path = "../storage", default-features = false }
sp-std = { version = "2.0.0-rc5", path = "../std", default-features = false }
sp-std = { version = "2.0.0-rc6", path = "../std", default-features = false }
environmental = { version = "1.1.1", default-features = false }
codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false }

Expand Down
4 changes: 2 additions & 2 deletions primitives/externalities/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
//!
//! It is required that each extension implements the [`Extension`] trait.

use sp_std::collections::btree_map::{BTreeMap, Entry};
use sp_std::{any::{Any, TypeId}, ops::DerefMut, boxed::Box};
use sp_std::{collections::btree_map::{BTreeMap, Entry}, any::{Any, TypeId},
ops::DerefMut, boxed::Box};
use crate::Error;

/// Marker trait for types that should be registered as [`Externalities`](crate::Externalities) extension.
Expand Down
4 changes: 1 addition & 3 deletions primitives/externalities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
//!
//! This crate exposes the main [`Externalities`] trait.

use sp_std::any::{Any, TypeId};
use sp_std::vec::Vec;
use sp_std::boxed::Box;
use sp_std::{any::{Any, TypeId}, vec::Vec, boxed::Box};

use sp_storage::{ChildInfo, TrackedStorageKey};

Expand Down
2 changes: 1 addition & 1 deletion primitives/state-machine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ num-traits = { version = "0.2.8", default-features = false }
rand = { version = "0.7.2", optional = true }
sp-externalities = { version = "0.8.0-rc6", path = "../externalities", default-features = false }
smallvec = "1.4.1"
sp-std = { version = "2.0.0-rc5", default-features = false, path = "../std" }
sp-std = { version = "2.0.0-rc6", default-features = false, path = "../std" }

[dev-dependencies]
hex-literal = "0.3.1"
Expand Down
75 changes: 40 additions & 35 deletions primitives/state-machine/src/changes_trie/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,52 +132,57 @@ fn prepare_extrinsics_input_inner<'a, B, H, Number>(
block: &Number,
overlay: &'a OverlayedChanges,
child_info: Option<ChildInfo>,
mut changes: impl Iterator<Item=(&'a StorageKey, &'a OverlayedValue)>
changes: impl Iterator<Item=(&'a StorageKey, &'a OverlayedValue)>
) -> Result<impl Iterator<Item=InputPair<Number>> + 'a, String>
where
B: Backend<H>,
H: Hasher,
Number: BlockNumber,
{
changes
.try_fold(BTreeMap::new(), |mut map: BTreeMap<&[u8], (ExtrinsicIndex<Number>, Vec<u32>)>, (k, v)| {
.filter_map(|(k, v)| {
Copy link
Member

Choose a reason for hiding this comment

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

Looks much better!

let extrinsics = v.extrinsics();
if !extrinsics.is_empty() {
match map.entry(k) {
Entry::Vacant(entry) => {
// ignore temporary values (values that have null value at the end of operation
// AND are not in storage at the beginning of operation
if let Some(child_info) = child_info.as_ref() {
if !overlay.child_storage(child_info, k).map(|v| v.is_some()).unwrap_or_default() {
if !backend.exists_child_storage(&child_info, k)
.map_err(|e| format!("{}", e))? {
return Ok(map);
}
Some((k, extrinsics))
} else {
None
}
})
.try_fold(BTreeMap::new(), |mut map: BTreeMap<&[u8], (ExtrinsicIndex<Number>, Vec<u32>)>, (k, extrinsics)| {
match map.entry(k) {
Entry::Vacant(entry) => {
// ignore temporary values (values that have null value at the end of operation
// AND are not in storage at the beginning of operation
if let Some(child_info) = child_info.as_ref() {
if !overlay.child_storage(child_info, k).map(|v| v.is_some()).unwrap_or_default() {
if !backend.exists_child_storage(&child_info, k)
.map_err(|e| format!("{}", e))? {
return Ok(map);
}
} else {
if !overlay.storage(k).map(|v| v.is_some()).unwrap_or_default() {
if !backend.exists_storage(k).map_err(|e| format!("{}", e))? {
return Ok(map);
}
}
} else {
if !overlay.storage(k).map(|v| v.is_some()).unwrap_or_default() {
if !backend.exists_storage(k).map_err(|e| format!("{}", e))? {
return Ok(map);
}
};

let extrinsics = extrinsics.into_iter().collect();
entry.insert((ExtrinsicIndex {
block: block.clone(),
key: k.to_vec(),
}, extrinsics));
},
Entry::Occupied(mut entry) => {
// we do not need to check for temporary values here, because entry is Occupied
// AND we are checking it before insertion
let entry_extrinsics = &mut entry.get_mut().1;
entry_extrinsics.extend(
extrinsics.into_iter()
);
entry_extrinsics.sort();
},
}
}
};

let extrinsics = extrinsics.into_iter().collect();
entry.insert((ExtrinsicIndex {
block: block.clone(),
key: k.to_vec(),
}, extrinsics));
},
Entry::Occupied(mut entry) => {
// we do not need to check for temporary values here, because entry is Occupied
// AND we are checking it before insertion
let entry_extrinsics = &mut entry.get_mut().1;
entry_extrinsics.extend(
extrinsics.into_iter()
);
entry_extrinsics.sort();
},
}

Ok(map)
Expand Down
5 changes: 2 additions & 3 deletions primitives/state-machine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub use std_reexport::*;
#[cfg(feature = "std")]
pub use execution::*;


#[cfg(feature = "std")]
pub use log::{debug, warn, trace, error as log_error};

Expand Down Expand Up @@ -102,7 +101,7 @@ macro_rules! log_error {
);
}

/// Default rror type to use with state machine trie backend.
/// Default error type to use with state machine trie backend.
#[cfg(feature = "std")]
pub type DefaultError = String;
/// Error type to use with state machine trie backend.
Expand All @@ -113,7 +112,7 @@ pub struct DefaultError;
#[cfg(not(feature = "std"))]
impl sp_std::fmt::Display for DefaultError {
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
write!(f, "Default Error")
write!(f, "DefaultError")
}
}

Expand Down
2 changes: 1 addition & 1 deletion primitives/state-machine/src/overlayed_changes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Extrinsics {
}
}

/// Merge two extrinsics sets.
/// Extend `self` with `other`.
fn extend(&mut self, other: Self) {
self.0.extend(other.0.into_iter());
}
Expand Down
4 changes: 1 addition & 3 deletions primitives/state-machine/src/trie_backend_essence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#[cfg(feature = "std")]
use std::sync::Arc;
use sp_std::ops::Deref;
use sp_std::{ops::Deref, boxed::Box, vec::Vec};
use crate::{warn, debug};
use hash_db::{self, Hasher, Prefix};
use sp_trie::{Trie, MemoryDB, PrefixedMemoryDB, DBValue,
Expand All @@ -30,8 +30,6 @@ use sp_trie::trie_types::{TrieDB, TrieError, Layout};
use crate::{backend::Consolidate, StorageKey, StorageValue};
use sp_core::storage::ChildInfo;
use codec::Encode;
use sp_std::boxed::Box;
use sp_std::vec::Vec;

#[cfg(not(feature = "std"))]
macro_rules! format {
Expand Down