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

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into offchain-futures
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaka committed Aug 6, 2019
2 parents a5caaf9 + 5342c63 commit 491f028
Show file tree
Hide file tree
Showing 69 changed files with 1,182 additions and 798 deletions.
163 changes: 93 additions & 70 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ state-machine = { package = "substrate-state-machine", path = "../state-machine"
keyring = { package = "substrate-keyring", path = "../keyring", optional = true }
trie = { package = "substrate-trie", path = "../trie", optional = true }
substrate-telemetry = { path = "../telemetry", optional = true }
hash-db = { version = "0.14.0", default-features = false }
hash-db = { version = "0.15.0", default-features = false }
kvdb = { git = "https://github.com/paritytech/parity-common", optional = true, rev="b0317f649ab2c665b7987b8475878fc4d2e1f81d" }
parity-codec = { version = "4.1.1", default-features = false, features = ["derive"] }
primitives = { package = "substrate-primitives", path = "../primitives", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion core/client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ kvdb = { git = "https://github.com/paritytech/parity-common", rev="b0317f649ab2c
kvdb-rocksdb = { git = "https://github.com/paritytech/parity-common", rev="b0317f649ab2c665b7987b8475878fc4d2e1f81d", optional = true }
kvdb-memorydb = { git = "https://github.com/paritytech/parity-common", rev="b0317f649ab2c665b7987b8475878fc4d2e1f81d" }
linked-hash-map = "0.5"
hash-db = { version = "0.14.0" }
hash-db = { version = "0.15.0" }
primitives = { package = "substrate-primitives", path = "../../primitives" }
sr-primitives = { path = "../../sr-primitives" }
client = { package = "substrate-client", path = "../../client" }
Expand Down
89 changes: 51 additions & 38 deletions core/client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use client::blockchain::HeaderBackend;
use client::ExecutionStrategies;
use client::backend::{StorageCollection, ChildStorageCollection};
use parity_codec::{Decode, Encode};
use hash_db::Hasher;
use hash_db::{Hasher, Prefix};
use kvdb::{KeyValueDB, DBTransaction};
use trie::{MemoryDB, PrefixedMemoryDB, prefixed_key};
use parking_lot::{Mutex, RwLock};
Expand Down Expand Up @@ -507,7 +507,7 @@ struct StorageDb<Block: BlockT> {
}

impl<Block: BlockT> state_machine::Storage<Blake2Hasher> for StorageDb<Block> {
fn get(&self, key: &H256, prefix: &[u8]) -> Result<Option<DBValue>, String> {
fn get(&self, key: &H256, prefix: Prefix) -> Result<Option<DBValue>, String> {
let key = prefixed_key::<Blake2Hasher>(key, prefix);
self.state_db.get(&key, self).map(|r| r.map(|v| DBValue::from_slice(&v)))
.map_err(|e| format!("Database backend error: {:?}", e))
Expand Down Expand Up @@ -535,7 +535,7 @@ impl DbGenesisStorage {
}

impl state_machine::Storage<Blake2Hasher> for DbGenesisStorage {
fn get(&self, _key: &H256, _prefix: &[u8]) -> Result<Option<DBValue>, String> {
fn get(&self, _key: &H256, _prefix: Prefix) -> Result<Option<DBValue>, String> {
Ok(None)
}
}
Expand Down Expand Up @@ -675,7 +675,7 @@ impl<Block> state_machine::ChangesTrieStorage<Blake2Hasher, NumberFor<Block>>
where
Block: BlockT<Hash=H256>,
{
fn get(&self, key: &H256, _prefix: &[u8]) -> Result<Option<DBValue>, String> {
fn get(&self, key: &H256, _prefix: Prefix) -> Result<Option<DBValue>, String> {
self.db.get(columns::CHANGES_TRIE, &key[..])
.map_err(|err| format!("{}", err))
}
Expand Down Expand Up @@ -1428,7 +1428,7 @@ where Block: BlockT<Hash=H256> {}

#[cfg(test)]
mod tests {
use hash_db::HashDB;
use hash_db::{HashDB, EMPTY_PREFIX};
use super::*;
use crate::columns;
use client::backend::Backend as BTrait;
Expand Down Expand Up @@ -1654,7 +1654,7 @@ mod tests {

op.reset_storage(storage.iter().cloned().collect(), Default::default()).unwrap();

key = op.db_updates.insert(&[], b"hello");
key = op.db_updates.insert(EMPTY_PREFIX, b"hello");
op.set_block_data(
header,
Some(vec![]),
Expand All @@ -1663,8 +1663,10 @@ mod tests {
).unwrap();

backend.commit_operation(op).unwrap();

assert_eq!(backend.storage.db.get(columns::STATE, key.as_bytes()).unwrap().unwrap(), &b"hello"[..]);
assert_eq!(backend.storage.db.get(
columns::STATE,
&trie::prefixed_key::<Blake2Hasher>(&key, EMPTY_PREFIX)
).unwrap().unwrap(), &b"hello"[..]);
hash
};

Expand All @@ -1688,8 +1690,8 @@ mod tests {
).0.into();
let hash = header.hash();

op.db_updates.insert(&[], b"hello");
op.db_updates.remove(&key, &[]);
op.db_updates.insert(EMPTY_PREFIX, b"hello");
op.db_updates.remove(&key, EMPTY_PREFIX);
op.set_block_data(
header,
Some(vec![]),
Expand All @@ -1698,8 +1700,10 @@ mod tests {
).unwrap();

backend.commit_operation(op).unwrap();

assert_eq!(backend.storage.db.get(columns::STATE, key.as_bytes()).unwrap().unwrap(), &b"hello"[..]);
assert_eq!(backend.storage.db.get(
columns::STATE,
&trie::prefixed_key::<Blake2Hasher>(&key, EMPTY_PREFIX)
).unwrap().unwrap(), &b"hello"[..]);
hash
};

Expand All @@ -1723,7 +1727,7 @@ mod tests {
).0.into();
let hash = header.hash();

op.db_updates.remove(&key, &[]);
op.db_updates.remove(&key, EMPTY_PREFIX);
op.set_block_data(
header,
Some(vec![]),
Expand All @@ -1733,7 +1737,11 @@ mod tests {

backend.commit_operation(op).unwrap();

assert!(backend.storage.db.get(columns::STATE, key.as_bytes()).unwrap().is_some());

assert!(backend.storage.db.get(
columns::STATE,
&trie::prefixed_key::<Blake2Hasher>(&key, EMPTY_PREFIX)
).unwrap().is_some());
hash
};

Expand Down Expand Up @@ -1764,14 +1772,19 @@ mod tests {
).unwrap();

backend.commit_operation(op).unwrap();

assert!(backend.storage.db.get(columns::STATE, key.as_bytes()).unwrap().is_none());
assert!(backend.storage.db.get(
columns::STATE,
&trie::prefixed_key::<Blake2Hasher>(&key, EMPTY_PREFIX)
).unwrap().is_none());
}

backend.finalize_block(BlockId::Number(1), None).unwrap();
backend.finalize_block(BlockId::Number(2), None).unwrap();
backend.finalize_block(BlockId::Number(3), None).unwrap();
assert!(backend.storage.db.get(columns::STATE, key.as_bytes()).unwrap().is_none());
assert!(backend.storage.db.get(
columns::STATE,
&trie::prefixed_key::<Blake2Hasher>(&key, EMPTY_PREFIX)
).unwrap().is_none());
}

#[test]
Expand All @@ -1789,7 +1802,7 @@ mod tests {
assert_eq!(backend.changes_tries_storage.root(&anchor, block), Ok(Some(changes_root)));

for (key, (val, _)) in changes_trie_update.drain() {
assert_eq!(backend.changes_trie_storage().unwrap().get(&key, &[]), Ok(Some(val)));
assert_eq!(backend.changes_trie_storage().unwrap().get(&key, EMPTY_PREFIX), Ok(Some(val)));
}
};

Expand Down Expand Up @@ -1915,34 +1928,34 @@ mod tests {
let mut tx = DBTransaction::new();
backend.changes_tries_storage.prune(&config, &mut tx, Default::default(), 12);
backend.storage.db.write(tx).unwrap();
assert!(backend.changes_tries_storage.get(&root1, &[]).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root2, &[]).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root3, &[]).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root4, &[]).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root5, &[]).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root6, &[]).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root7, &[]).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root8, &[]).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root1, EMPTY_PREFIX).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root2, EMPTY_PREFIX).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root3, EMPTY_PREFIX).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root4, EMPTY_PREFIX).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root5, EMPTY_PREFIX).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root6, EMPTY_PREFIX).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root7, EMPTY_PREFIX).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root8, EMPTY_PREFIX).unwrap().is_some());

// now simulate finalization of block#16, causing prune of tries at #5..#8
let mut tx = DBTransaction::new();
backend.changes_tries_storage.prune(&config, &mut tx, Default::default(), 16);
backend.storage.db.write(tx).unwrap();
assert!(backend.changes_tries_storage.get(&root5, &[]).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root6, &[]).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root7, &[]).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root8, &[]).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root5, EMPTY_PREFIX).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root6, EMPTY_PREFIX).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root7, EMPTY_PREFIX).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root8, EMPTY_PREFIX).unwrap().is_none());

// now "change" pruning mode to archive && simulate finalization of block#20
// => no changes tries are pruned, because we never prune in archive mode
backend.changes_tries_storage.min_blocks_to_keep = None;
let mut tx = DBTransaction::new();
backend.changes_tries_storage.prune(&config, &mut tx, Default::default(), 20);
backend.storage.db.write(tx).unwrap();
assert!(backend.changes_tries_storage.get(&root9, &[]).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root10, &[]).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root11, &[]).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root12, &[]).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root9, EMPTY_PREFIX).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root10, EMPTY_PREFIX).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root11, EMPTY_PREFIX).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root12, EMPTY_PREFIX).unwrap().is_some());
}

#[test]
Expand Down Expand Up @@ -1981,15 +1994,15 @@ mod tests {
let mut tx = DBTransaction::new();
backend.changes_tries_storage.prune(&config, &mut tx, block5, 5);
backend.storage.db.write(tx).unwrap();
assert!(backend.changes_tries_storage.get(&root1, &[]).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root2, &[]).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root1, EMPTY_PREFIX).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root2, EMPTY_PREFIX).unwrap().is_some());

// now simulate finalization of block#6, causing prune of tries at #2
let mut tx = DBTransaction::new();
backend.changes_tries_storage.prune(&config, &mut tx, block6, 6);
backend.storage.db.write(tx).unwrap();
assert!(backend.changes_tries_storage.get(&root2, &[]).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root3, &[]).unwrap().is_some());
assert!(backend.changes_tries_storage.get(&root2, EMPTY_PREFIX).unwrap().is_none());
assert!(backend.changes_tries_storage.get(&root3, EMPTY_PREFIX).unwrap().is_some());
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion core/client/src/cht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ pub fn compute_root<Header, Hasher, I>(
Hasher::Out: Ord,
I: IntoIterator<Item=ClientResult<Option<Header::Hash>>>,
{
Ok(trie::trie_root::<Hasher, _, _, _>(
use trie::TrieConfiguration;
Ok(trie::trie_types::Layout::<Hasher>::trie_root(
build_pairs::<Header, I>(cht_size, cht_num, hashes)?
))
}
Expand Down
37 changes: 6 additions & 31 deletions core/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ use consensus::{
SelectChain, self,
};
use sr_primitives::traits::{
Block as BlockT, Header as HeaderT, Zero, NumberFor, CurrentHeight,
BlockNumberToHash, ApiRef, ProvideRuntimeApi,
SaturatedConversion, One, DigestFor,
Block as BlockT, Header as HeaderT, Zero, NumberFor,
ApiRef, ProvideRuntimeApi, SaturatedConversion, One, DigestFor,
};
use sr_primitives::generic::DigestItem;
use sr_primitives::BuildStorage;
Expand All @@ -58,7 +57,7 @@ use state_machine::{
ChangesTrieRootsStorage, ChangesTrieStorage,
key_changes, key_changes_proof, OverlayedChanges, NeverOffchainExt,
};
use hash_db::Hasher;
use hash_db::{Hasher, Prefix};

use crate::backend::{
self, BlockImportOperation, PrunableStateChangesTrieStorage,
Expand Down Expand Up @@ -615,7 +614,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
}

impl<'a, Block: BlockT> ChangesTrieStorage<Blake2Hasher, NumberFor<Block>> for AccessedRootsRecorder<'a, Block> {
fn get(&self, key: &H256, prefix: &[u8]) -> Result<Option<DBValue>, String> {
fn get(&self, key: &H256, prefix: Prefix) -> Result<Option<DBValue>, String> {
self.storage.get(key, prefix)
}
}
Expand Down Expand Up @@ -1521,30 +1520,6 @@ impl<B, E, Block, RA> consensus::BlockImport<Block> for Client<B, E, Block, RA>
}
}

impl<B, E, Block, RA> CurrentHeight for Client<B, E, Block, RA> where
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher>,
Block: BlockT<Hash=H256>,
{
type BlockNumber = <Block::Header as HeaderT>::Number;
fn current_height(&self) -> Self::BlockNumber {
self.backend.blockchain().info().best_number
}
}

impl<B, E, Block, RA> BlockNumberToHash for Client<B, E, Block, RA> where
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher>,
Block: BlockT<Hash=H256>,
{
type BlockNumber = <Block::Header as HeaderT>::Number;
type Hash = Block::Hash;
fn block_number_to_hash(&self, n: Self::BlockNumber) -> Option<Self::Hash> {
self.block_hash(n).unwrap_or(None)
}
}


impl<B, E, Block, RA> BlockchainEvents<Block> for Client<B, E, Block, RA>
where
E: CallExecutor<Block, Blake2Hasher>,
Expand Down Expand Up @@ -2698,7 +2673,7 @@ pub(crate) mod tests {

let current_balance = ||
client.runtime_api().balance_of(
&BlockId::number(client.current_height()), AccountKeyring::Alice.into()
&BlockId::number(client.info().chain.best_number), AccountKeyring::Alice.into()
).unwrap();

// G -> A1 -> A2
Expand Down Expand Up @@ -2745,7 +2720,7 @@ pub(crate) mod tests {

let current_balance = ||
client.runtime_api().balance_of(
&BlockId::number(client.current_height()), AccountKeyring::Alice.into()
&BlockId::number(client.info().chain.best_number), AccountKeyring::Alice.into()
).unwrap();

// G -> A1
Expand Down
5 changes: 3 additions & 2 deletions core/client/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ mod tests {
state_root: Hash,
txs: Vec<Transfer>
) -> (Vec<u8>, Hash) {
use trie::ordered_trie_root;
use trie::{TrieConfiguration, trie_types::Layout};

let transactions = txs.into_iter().map(|tx| tx.into_signed_tx()).collect::<Vec<_>>();

let extrinsics_root = ordered_trie_root::<Blake2Hasher, _, _>(transactions.iter().map(Encode::encode)).into();
let iter = transactions.iter().map(Encode::encode);
let extrinsics_root = Layout::<Blake2Hasher>::ordered_trie_root(iter).into();

let mut header = Header {
parent_hash,
Expand Down
11 changes: 7 additions & 4 deletions core/client/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use sr_primitives::traits::{Block as BlockT, Header as HeaderT, Zero, NumberFor}
use sr_primitives::{Justification, StorageOverlay, ChildrenStorageOverlay};
use state_machine::backend::{Backend as StateBackend, InMemory};
use state_machine::{self, InMemoryChangesTrieStorage, ChangesTrieAnchorBlockId};
use hash_db::Hasher;
use hash_db::{Hasher, Prefix};
use trie::MemoryDB;
use consensus::well_known_cache_keys::Id as CacheKeyId;

Expand Down Expand Up @@ -532,7 +532,10 @@ where
}
}

/// In-memory backend. Keeps all states and blocks in memory. Useful for testing.
/// In-memory backend. Keeps all states and blocks in memory.
///
/// > **Warning**: Doesn't support all the features necessary for a proper database. Only use this
/// > struct for testing purposes. Do **NOT** use in production.
pub struct Backend<Block, H>
where
Block: BlockT,
Expand Down Expand Up @@ -755,8 +758,8 @@ impl<Block, H> state_machine::ChangesTrieStorage<H, NumberFor<Block>> for Change
Block: BlockT,
H: Hasher,
{
fn get(&self, _key: &H::Out, _prefix: &[u8]) -> Result<Option<state_machine::DBValue>, String> {
Err("Dummy implementation".into())
fn get(&self, key: &H::Out, prefix: Prefix) -> Result<Option<state_machine::DBValue>, String> {
self.0.get(key, prefix)
}
}

Expand Down
3 changes: 1 addition & 2 deletions core/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
//! Creating a [`Client`] is done by calling the `new` method and passing to it a
//! [`Backend`](backend::Backend) and an [`Executor`](CallExecutor).
//!
//! The former is typically provided by the `substrate-client-db` crate, but [`in_mem::Backend`]
//! can be used for testing purposes.
//! The former is typically provided by the `substrate-client-db` crate.
//!
//! The latter typically requires passing one of:
//!
Expand Down
Loading

0 comments on commit 491f028

Please sign in to comment.