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 9 commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
45e0ee8
Genric over hasher
dvdplm Jul 11, 2018
eb26a69
WIP start adding NodeCodec
dvdplm Jul 11, 2018
acf8284
Add codec to TrieBackend
dvdplm Jul 11, 2018
90707dc
Typechecks
dvdplm Jul 11, 2018
4ccc851
Fix error type
dvdplm Jul 11, 2018
21b7892
Cleanup
dvdplm Jul 11, 2018
6b2fa02
Tests build (and fail)
dvdplm Jul 11, 2018
f331b0c
Fix tests: don't use MemoryDB::default()
dvdplm Jul 11, 2018
f0f2413
Lockfile
dvdplm Jul 11, 2018
87740d7
Address grumbles
dvdplm Jul 12, 2018
a34f614
Teach environmental! about generics
dvdplm Jul 12, 2018
c7503ad
Add Finder artifacts
dvdplm Jul 12, 2018
711da4f
whitespace
dvdplm Jul 12, 2018
a70f4a0
Merge branch 'refactor/environmental-generic-traits' into refactor/su…
dvdplm Jul 12, 2018
b4f210a
Add a toy impl of Hasher and plug it in to Externalities
dvdplm Jul 30, 2018
3b5f60b
Use `uint` and `fixed-hash` from `parity-common`
dvdplm Jul 30, 2018
1aeb495
lock file updates
dvdplm Jul 30, 2018
f7c30f4
Merge branch 'master' into refactor/substrate-state-machine-generic
dvdplm Jul 30, 2018
80c96a1
Make hashes Encodable/Decodable
dvdplm Jul 31, 2018
e3277ac
lock file updates
dvdplm Jul 31, 2018
d38216b
Impl FromIterator for TestExternalities so we can collect() and use map!
dvdplm Aug 1, 2018
5f19c7b
Use rustc-hex from crates
dvdplm Aug 1, 2018
848ce8f
Fix tests in runtime-io
dvdplm Aug 1, 2018
1358bb9
Merge branch 'master' into refactor/substrate-state-machine-generic
dvdplm Aug 1, 2018
9b8f417
lockfile shenanigans
dvdplm Aug 1, 2018
ca01909
Add a BlakeHasher impl
dvdplm Aug 1, 2018
9e42668
Use BlakeHasher in runtime-io
dvdplm Aug 1, 2018
0f77d2b
lockfile updates
dvdplm Aug 2, 2018
402c35c
ws
dvdplm Aug 2, 2018
85b5fe1
Add a Blake2/RLP-flavoured NodeCodec
dvdplm Aug 2, 2018
56649b6
Use Blake-flavoured Hasher and NodeCodec
dvdplm Aug 2, 2018
5e5e61e
lockfile
dvdplm Aug 2, 2018
fc14711
Implement PartialEq and Default for TestExternalities
dvdplm Aug 2, 2018
05d8200
Add note about limitations of environmental!
dvdplm Aug 2, 2018
65c4f64
Make it compile, but this is probably broken
dvdplm Aug 2, 2018
56f9ef6
Derive Debug so tests in executor can work
dvdplm Aug 2, 2018
eac3f25
Make executor use BlakeHasher
dvdplm Aug 2, 2018
f3422c5
ws
dvdplm Aug 3, 2018
aa40b78
WIP make client generic
dvdplm Aug 3, 2018
54a2b66
typechecks
dvdplm Aug 3, 2018
277aadd
cleanup
dvdplm Aug 3, 2018
02a302a
client tests pass
dvdplm Aug 3, 2018
dae069b
Fix client/db
dvdplm Aug 3, 2018
7a93cf7
cleanup
dvdplm Aug 3, 2018
6329abb
Fix network
dvdplm Aug 3, 2018
dc7fc67
Fix rpc
dvdplm Aug 3, 2018
d7c0935
Fix service
dvdplm Aug 3, 2018
a553f31
Make TestExternalities work better au lieu d'un HashMap
dvdplm Aug 3, 2018
9bfe799
Fix tests in council
dvdplm Aug 3, 2018
e4cefb3
Fix tests in contract
dvdplm Aug 3, 2018
6277110
Fix tests in council
dvdplm Aug 3, 2018
adc13a2
Fix democracy
dvdplm Aug 3, 2018
252bbd8
Add comment about odd-looking reexports in tests
dvdplm Aug 3, 2018
c94018c
Don't need to load branch
dvdplm Aug 3, 2018
c49609d
Fix staking
dvdplm Aug 3, 2018
0fb6dc1
Fix session
dvdplm Aug 3, 2018
6ebfc7f
Some polkadot fixes and lockfile
dvdplm Aug 3, 2018
056bffd
Fix executive
dvdplm Aug 3, 2018
ccb61a9
Merge branch 'master' into refactor/substrate-state-machine-generic
dvdplm Aug 3, 2018
f565cf9
fixup lockfile
dvdplm Aug 3, 2018
d11d578
Fix polkadot/api
dvdplm Aug 4, 2018
c7334b2
Fix polkadot/service
dvdplm Aug 4, 2018
b6bde9a
Fix polkadot/runtime tests
dvdplm Aug 4, 2018
08f25d3
Merge branch 'master' into refactor/substrate-state-machine-generic
dvdplm Aug 5, 2018
9b139b2
Fix tests in test-runtime
dvdplm Aug 6, 2018
0c82f95
Test fixes
dvdplm Aug 6, 2018
50b4b12
Fix missing component in the `std` feature
dvdplm Aug 6, 2018
111f9fd
Use PhantomData and Result from core
dvdplm Aug 6, 2018
1276733
Fix paths
dvdplm Aug 6, 2018
334e008
load heapsize on wasm
dvdplm Aug 6, 2018
2faff87
implement `HeapSizeOf` for wasm
dvdplm Aug 6, 2018
751fe9e
Add toy impl of `blake2_256` for no_std
dvdplm Aug 6, 2018
81d7278
lockfile
dvdplm Aug 6, 2018
dd551a6
Merge branch 'master' into refactor/substrate-state-machine-generic
dvdplm Aug 7, 2018
dcd9541
Use kvdb* from parity-common and fix errors
dvdplm Aug 7, 2018
2370a16
rebuilt lockfile
dvdplm Aug 7, 2018
0d4a4fe
Add dummy impl of `on_advance_round` for rhododendron::Context
dvdplm Aug 7, 2018
6367815
Fix build after merge
dvdplm Aug 7, 2018
cf7e338
Add HeapSizeOf bound where needed
dvdplm Aug 8, 2018
af1b17e
Sort out dependencies for no_std
dvdplm Aug 8, 2018
ca89697
Add HeapSizeOf bound where needed
dvdplm Aug 8, 2018
da711ef
use temp branch pending PR merges
dvdplm Aug 8, 2018
3b206c2
Remove unneeded tests
dvdplm Aug 8, 2018
8064c6d
Lock file and wasm artifacts
dvdplm Aug 8, 2018
0066026
lockfile
dvdplm Aug 8, 2018
bfcf5df
Use magic commit for libp2p
dvdplm Aug 8, 2018
35b29ba
Cleanup
dvdplm Aug 9, 2018
04b7065
Implement blake2_256 for no_std
dvdplm Aug 9, 2018
8152a78
Back on parity-common master
dvdplm Aug 9, 2018
cafe669
Merge branch 'master' into refactor/substrate-state-machine-generic
dvdplm Aug 9, 2018
11ea517
missing type params
dvdplm Aug 9, 2018
4d14020
Merge branch 'master' into refactor/substrate-state-machine-generic
gavofyork Aug 9, 2018
19d07db
Update Cargo.lock
gavofyork Aug 9, 2018
60df8c1
whitespace
dvdplm Aug 9, 2018
6833f7b
Merge branch 'refactor/substrate-state-machine-generic' of github.com…
dvdplm Aug 9, 2018
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/target/
**/target/
**/*.rs.bk
*.swp
.wasm-binaries
Expand Down
276 changes: 156 additions & 120 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions substrate/client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ authors = ["Parity Technologies <[email protected]>"]
[dependencies]
parking_lot = "0.4"
log = "0.3"
kvdb = { git = "https://github.com/paritytech/parity.git" }
kvdb-rocksdb = { git = "https://github.com/paritytech/parity.git" }
kvdb = { git = "https://github.com/paritytech/parity-common" }
kvdb-rocksdb = { git = "https://github.com/paritytech/parity-common" }
ethereum-types = "0.3"
hashdb = { git = "https://github.com/paritytech/parity.git" }
patricia-trie = { git = "https://github.com/paritytech/parity.git" }
memorydb = { git = "https://github.com/paritytech/parity.git" }
hashdb = { git = "https://github.com/paritytech/parity-common" }
patricia-trie = { git = "https://github.com/paritytech/parity-common" }
memorydb = { git = "https://github.com/paritytech/parity-common" }
substrate-primitives = { path = "../../../substrate/primitives" }
substrate-runtime-primitives = { path = "../../../substrate/runtime/primitives" }
substrate-client = { path = "../../../substrate/client" }
Expand All @@ -22,4 +22,4 @@ substrate-executor = { path = "../../../substrate/executor" }
substrate-state-db = { path = "../../../substrate/state-db" }

[dev-dependencies]
kvdb-memorydb = { git = "https://github.com/paritytech/parity.git" }
kvdb-memorydb = { git = "https://github.com/paritytech/parity-common" }
2 changes: 1 addition & 1 deletion substrate/keystore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
authors = ["Parity Technologies <[email protected]>"]

[dependencies]
ethcore-crypto = { git = "https://github.com/paritytech/parity.git", default_features = false }
parity-crypto = { git = "https://github.com/paritytech/parity-common.git", default_features = false }
Copy link
Contributor

Choose a reason for hiding this comment

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

You missed renaming an import, see travis test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed, thanks!

ed25519 = { path = "../ed25519" }
error-chain = "0.12"
hex = "0.3"
Expand Down
15 changes: 11 additions & 4 deletions substrate/state-machine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ ethereum-types = "0.3"
hex-literal = "0.1.0"
log = "0.3"
parking_lot = "0.4"
triehash = "0.1"

substrate-primitives = { path = "../primitives", version = "0.1.0" }

hashdb = { git = "https://github.com/paritytech/parity.git" }
memorydb = { git = "https://github.com/paritytech/parity.git" }
patricia-trie = { git = "https://github.com/paritytech/parity.git" }
hashdb = { git = "https://github.com/paritytech/parity-common" }
memorydb = { git = "https://github.com/paritytech/parity-common" }
patricia-trie = { git = "https://github.com/paritytech/parity-common" }
triehash = { git = "https://github.com/paritytech/parity-common" }
rlp = { git = "https://github.com/paritytech/parity-common" }

[dev-dependencies]
# We need `KeccakHasher` from here
keccak-hasher = { git = "https://github.com/paritytech/parity" }
# We need `RlpCodec` from here
patricia-trie-ethereum = { git = "https://github.com/paritytech/parity" }
55 changes: 33 additions & 22 deletions substrate/state-machine/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@
//! State machine backends. These manage the code and storage of contracts.

use std::{error, fmt};
use std::cmp::Ord;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;

use hashdb::Hasher;
use rlp::Encodable;
use trie_backend::{TryIntoTrieBackend, TrieBackend};
use patricia_trie::{TrieDBMut, TrieMut, NodeCodec};

/// A state backend is used to read state data and can have changes committed
/// to it.
///
/// The clone operation (if implemented) should be cheap.
pub trait Backend: TryIntoTrieBackend {
pub trait Backend<H: Hasher, C: NodeCodec<H>>: TryIntoTrieBackend<H, C> {
/// An error type when fetching data is not possible.
type Error: super::Error;

Expand All @@ -41,8 +47,10 @@ pub trait Backend: TryIntoTrieBackend {

/// Calculate the storage root, with given delta over what is already stored in
/// the backend, and produce a "transaction" that can be used to commit.
fn storage_root<I>(&self, delta: I) -> ([u8; 32], Self::Transaction)
where I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>;
fn storage_root<I>(&self, delta: I) -> (H::Out, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
H::Out: Ord + Encodable;

/// Get all key/value pairs into a Vec.
fn pairs(&self) -> Vec<(Vec<u8>, Vec<u8>)>;
Expand All @@ -66,21 +74,25 @@ impl error::Error for Void {
/// In-memory backend. Fully recomputes tries on each commit but useful for
/// tests.
#[derive(Clone, PartialEq, Eq)]
pub struct InMemory {
pub struct InMemory<H, C> {
inner: Arc<HashMap<Vec<u8>, Vec<u8>>>,
_hasher: PhantomData<H>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious: why did you opt for an underscore here?

Copy link
Contributor Author

@dvdplm dvdplm Jul 12, 2018

Choose a reason for hiding this comment

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

No real good reason. I keep going back and forth on how to name my phantoms, so I end up being inconsistent. Sometimes I like marker, sometimes I name it for what it contains, sometimes with a _ prefix to signal to the reader "never mind this, it's there to make the code compile and is not relevant".

I'd like to have a rule to follow here. What do you use?

Copy link
Contributor

Choose a reason for hiding this comment

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

Clippy states that underscore-prefixed names are expected to be in there for compile-passing-purposes only and aren't actually used. Which is the case here, and combined with a proper descriptive name, I think this is totally the appropriate way to do it, @dvdplm :) .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"compile-passing-purposes" – rofl

_codec: PhantomData<C>,
}

impl Default for InMemory {
impl<H, C> Default for InMemory<H, C> {
fn default() -> Self {
InMemory {
inner: Arc::new(Default::default()),
_hasher: PhantomData,
_codec: PhantomData,
}
}
}

impl InMemory {
impl<H: Hasher, C: NodeCodec<H>> InMemory<H, C> {
/// Copy the state, with applied updates
pub fn update(&self, changes: <Self as Backend>::Transaction) -> Self {
pub fn update(&self, changes: <Self as Backend<H, C>>::Transaction) -> Self {
let mut inner: HashMap<_, _> = (&*self.inner).clone();
for (key, val) in changes {
match val {
Expand All @@ -93,15 +105,15 @@ impl InMemory {
}
}

impl From<HashMap<Vec<u8>, Vec<u8>>> for InMemory {
impl<H, C> From<HashMap<Vec<u8>, Vec<u8>>> for InMemory<H, C> {
fn from(inner: HashMap<Vec<u8>, Vec<u8>>) -> Self {
InMemory {
inner: Arc::new(inner),
inner: Arc::new(inner), _hasher: PhantomData, _codec: PhantomData
}
}
}

impl Backend for InMemory {
impl<H: Hasher, C: NodeCodec<H>> Backend<H, C> for InMemory<H, C> {
type Error = Void;
type Transaction = Vec<(Vec<u8>, Option<Vec<u8>>)>;

Expand All @@ -113,17 +125,19 @@ impl Backend for InMemory {
self.inner.keys().filter(|key| key.starts_with(prefix)).map(|k| &**k).for_each(f);
}

fn storage_root<I>(&self, delta: I) -> ([u8; 32], Self::Transaction)
where I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>
fn storage_root<I>(&self, delta: I) -> (H::Out, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
<H as Hasher>::Out: Ord + Encodable,
{
let existing_pairs = self.inner.iter().map(|(k, v)| (k.clone(), Some(v.clone())));

let transaction: Vec<_> = delta.into_iter().collect();
let root = ::triehash::trie_root(existing_pairs.chain(transaction.iter().cloned())
let root = ::triehash::trie_root::<H, _, _, _>(existing_pairs.chain(transaction.iter().cloned())
.collect::<HashMap<_, _>>()
.into_iter()
.filter_map(|(k, maybe_val)| maybe_val.map(|val| (k, val)))
).0;
);

(root, transaction)
}
Expand All @@ -133,16 +147,13 @@ impl Backend for InMemory {
}
}

impl TryIntoTrieBackend for InMemory {
fn try_into_trie_backend(self) -> Option<TrieBackend> {
use ethereum_types::H256 as TrieH256;
impl<H: Hasher, C: NodeCodec<H>> TryIntoTrieBackend<H, C> for InMemory<H, C> {
fn try_into_trie_backend(self) -> Option<TrieBackend<H, C>> {
use memorydb::MemoryDB;
use patricia_trie::{TrieDBMut, TrieMut};

let mut root = TrieH256::default();
let mut mdb = MemoryDB::default();
let mut root = <H as Hasher>::Out::default();
let mut mdb = MemoryDB::new();
{
let mut trie = TrieDBMut::new(&mut mdb, &mut root);
let mut trie = TrieDBMut::<H, C>::new(&mut mdb, &mut root);
for (key, value) in self.inner.iter() {
if let Err(e) = trie.insert(&key, &value) {
warn!(target: "trie", "Failed to write to trie: {}", e);
Expand Down
29 changes: 21 additions & 8 deletions substrate/state-machine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

//! Conrete externalities implementation.

use std::{error, fmt};
use std::{error, fmt, cmp::Ord};
use backend::Backend;
use {Externalities, OverlayedChanges};
use hashdb::Hasher;
use rlp::Encodable;
use patricia_trie::NodeCodec;

/// Errors that can occur when interacting with the externalities.
#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -50,16 +53,22 @@ impl<B: error::Error, E: error::Error> error::Error for Error<B, E> {
}

/// Wraps a read-only backend, call executor, and current overlayed changes.
pub struct Ext<'a, B: 'a + Backend> {
pub struct Ext<'a, H: Hasher, C: NodeCodec<H>, B: 'a + Backend<H, C>> { //TODO: can I get rid of the trait bounds here?
// The overlayed changes to write to.
overlay: &'a mut OverlayedChanges,
// The storage backend to read from.
backend: &'a B,
// The transaction necessary to commit to the backend.
transaction: Option<(B::Transaction, [u8; 32])>,
transaction: Option<(B::Transaction, H::Out)>,
}

impl<'a, B: 'a + Backend> Ext<'a, B> {
impl<'a, H, C, B> Ext<'a, H, C, B>
where
H: Hasher,
C: NodeCodec<H>,
B: 'a + Backend<H, C>,
H::Out: Ord + Encodable
{
/// Create a new `Ext` from overlayed changes and read-only backend
pub fn new(overlay: &'a mut OverlayedChanges, backend: &'a B) -> Self {
Ext {
Expand All @@ -84,7 +93,7 @@ impl<'a, B: 'a + Backend> Ext<'a, B> {
}

#[cfg(test)]
impl<'a, B: 'a + Backend> Ext<'a, B> {
impl<'a, H: Hasher, C: NodeCodec<H>, B: 'a + Backend<H, C>> Ext<'a, H, C, B> {
pub fn storage_pairs(&self) -> Vec<(Vec<u8>, Vec<u8>)> {
use std::collections::HashMap;

Expand All @@ -99,8 +108,12 @@ impl<'a, B: 'a + Backend> Ext<'a, B> {
}
}

impl<'a, B: 'a> Externalities for Ext<'a, B>
where B: Backend
impl<'a, B: 'a, H, C> Externalities<H> for Ext<'a, H, C, B>
where
H: Hasher,
C: NodeCodec<H>,
B: 'a + Backend<H, C>,
H::Out: Ord + Encodable
{
fn storage(&self, key: &[u8]) -> Option<Vec<u8>> {
self.overlay.storage(key).map(|x| x.map(|x| x.to_vec())).unwrap_or_else(||
Expand All @@ -123,7 +136,7 @@ impl<'a, B: 'a> Externalities for Ext<'a, B>
42
}

fn storage_root(&mut self) -> [u8; 32] {
fn storage_root(&mut self) -> H::Out where H::Out: Ord + Encodable {
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this line isn't super long, however I think it reduces readability complexity if we had the where in the second line

if let Some((_, ref root)) = self.transaction {
return root.clone();
}
Expand Down
Loading