Skip to content

Commit

Permalink
Update kvdb-* and trie (paritytech#4483)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikVolf authored and gavofyork committed Jan 3, 2020
1 parent bdf6043 commit 5842f6e
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 139 deletions.
88 changes: 35 additions & 53 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ console_log = { version = "0.1.2", optional = true }
js-sys = { version = "0.3.22", optional = true }
wasm-bindgen = { version = "0.2.45", optional = true }
wasm-bindgen-futures = { version = "0.3.22", optional = true }
kvdb-memorydb = { version = "0.1.1", optional = true }
kvdb-memorydb = { version = "0.2.0", optional = true }
rand6 = { package = "rand", version = "0.6", features = ["wasm-bindgen"], optional = true } # Imported just for the `wasm-bindgen` feature

[dev-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ hash-db = { version = "0.15.2" }
hex-literal = { version = "0.2.1" }
sp-inherents = { version = "2.0.0", path = "../primitives/inherents" }
sp-keyring = { version = "2.0.0", path = "../primitives/keyring" }
kvdb = "0.1.1"
kvdb = "0.2.0"
log = { version = "0.4.8" }
parking_lot = { version = "0.9.0" }
sp-core = { version = "2.0.0", path = "../primitives/core" }
Expand All @@ -36,5 +36,5 @@ tracing = "0.1.10"
env_logger = "0.7.0"
tempfile = "3.1.0"
substrate-test-runtime-client = { version = "2.0.0", path = "../test-utils/runtime/client" }
kvdb-memorydb = "0.1.2"
kvdb-memorydb = "0.2.0"
sp-panic-handler = { version = "2.0.0", path = "../primitives/panic-handler" }
4 changes: 2 additions & 2 deletions client/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" }
hex-literal = { version = "0.2.1" }
sp-inherents = { version = "2.0.0", default-features = false, path = "../../primitives/inherents" }
sp-keyring = { version = "2.0.0", path = "../../primitives/keyring" }
kvdb = "0.1.1"
kvdb = "0.2.0"
log = { version = "0.4.8" }
parking_lot = { version = "0.9.0" }
sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" }
Expand All @@ -31,4 +31,4 @@ sp-trie = { version = "2.0.0", path = "../../primitives/trie" }
sp-transaction-pool = { version = "2.0.0", path = "../../primitives/transaction-pool" }

[dev-dependencies]
sp-test-primitives = { version = "2.0.0", path = "../../primitives/test-primitives" }
sp-test-primitives = { version = "2.0.0", path = "../../primitives/test-primitives" }
6 changes: 3 additions & 3 deletions client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ edition = "2018"
[dependencies]
parking_lot = "0.9.0"
log = "0.4.8"
kvdb = "0.1.1"
kvdb-rocksdb = { version = "0.2", optional = true }
kvdb-memorydb = "0.1.2"
kvdb = "0.2.0"
kvdb-rocksdb = { version = "0.3", optional = true }
kvdb-memorydb = "0.2.0"
linked-hash-map = "0.5.2"
hash-db = "0.15.2"
sc-client-api = { version = "2.0.0", path = "../api" }
Expand Down
8 changes: 4 additions & 4 deletions client/db/src/cache/list_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ pub trait StorageTransaction<Block: BlockT, T: CacheItemT> {
#[derive(Debug)]
pub struct DbColumns {
/// Column holding cache meta.
pub meta: Option<u32>,
pub meta: u32,
/// Column holding the mapping of { block number => block hash } for blocks of the best chain.
pub key_lookup: Option<u32>,
pub key_lookup: u32,
/// Column holding the mapping of { block hash => block header }.
pub header: Option<u32>,
pub header: u32,
/// Column holding cache entries.
pub cache: Option<u32>,
pub cache: u32,
}

/// Database-backed list cache storage.
Expand Down
18 changes: 9 additions & 9 deletions client/db/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ impl<T> CacheItemT for T where T: Clone + Decode + Encode + PartialEq {}
pub struct DbCache<Block: BlockT> {
cache_at: HashMap<CacheKeyId, ListCache<Block, Vec<u8>, self::list_storage::DbStorage>>,
db: Arc<dyn KeyValueDB>,
key_lookup_column: Option<u32>,
header_column: Option<u32>,
authorities_column: Option<u32>,
key_lookup_column: u32,
header_column: u32,
authorities_column: u32,
genesis_hash: Block::Hash,
best_finalized_block: ComplexBlockId<Block>,
}
Expand All @@ -88,9 +88,9 @@ impl<Block: BlockT> DbCache<Block> {
/// Create new cache.
pub fn new(
db: Arc<dyn KeyValueDB>,
key_lookup_column: Option<u32>,
header_column: Option<u32>,
authorities_column: Option<u32>,
key_lookup_column: u32,
header_column: u32,
authorities_column: u32,
genesis_hash: Block::Hash,
best_finalized_block: ComplexBlockId<Block>,
) -> Self {
Expand Down Expand Up @@ -150,9 +150,9 @@ fn get_cache_helper<'a, Block: BlockT>(
cache_at: &'a mut HashMap<CacheKeyId, ListCache<Block, Vec<u8>, self::list_storage::DbStorage>>,
name: CacheKeyId,
db: &Arc<dyn KeyValueDB>,
key_lookup: Option<u32>,
header: Option<u32>,
cache: Option<u32>,
key_lookup: u32,
header: u32,
cache: u32,
best_finalized_block: &ComplexBlockId<Block>,
) -> &'a mut ListCache<Block, Vec<u8>, self::list_storage::DbStorage> {
cache_at.entry(name).or_insert_with(|| {
Expand Down
27 changes: 13 additions & 14 deletions client/db/src/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ use codec::{Encode, Decode};
use sp_blockchain;
use std::hash::Hash;


/// Returns the hashes of the children blocks of the block with `parent_hash`.
pub fn read_children<
K: Eq + Hash + Clone + Encode + Decode,
V: Eq + Hash + Clone + Encode + Decode,
>(db: &dyn KeyValueDB, column: Option<u32>, prefix: &[u8], parent_hash: K) -> sp_blockchain::Result<Vec<V>> {
>(db: &dyn KeyValueDB, column: u32, prefix: &[u8], parent_hash: K) -> sp_blockchain::Result<Vec<V>> {
let mut buf = prefix.to_vec();
parent_hash.using_encoded(|s| buf.extend(s));

Expand Down Expand Up @@ -55,7 +54,7 @@ pub fn write_children<
V: Eq + Hash + Clone + Encode + Decode,
>(
tx: &mut DBTransaction,
column: Option<u32>,
column: u32,
prefix: &[u8],
parent_hash: K,
children_hashes: V,
Expand All @@ -70,7 +69,7 @@ pub fn remove_children<
K: Eq + Hash + Clone + Encode + Decode,
>(
tx: &mut DBTransaction,
column: Option<u32>,
column: u32,
prefix: &[u8],
parent_hash: K,
) {
Expand All @@ -87,33 +86,33 @@ mod tests {
#[test]
fn children_write_read_remove() {
const PREFIX: &[u8] = b"children";
let db = ::kvdb_memorydb::create(0);
let db = ::kvdb_memorydb::create(1);

let mut tx = DBTransaction::new();

let mut children1 = Vec::new();
children1.push(1_3);
children1.push(1_5);
write_children(&mut tx, None, PREFIX, 1_1, children1);
write_children(&mut tx, 0, PREFIX, 1_1, children1);

let mut children2 = Vec::new();
children2.push(1_4);
children2.push(1_6);
write_children(&mut tx, None, PREFIX, 1_2, children2);
write_children(&mut tx, 0, PREFIX, 1_2, children2);

db.write(tx.clone()).unwrap();
db.write(tx.clone()).expect("(2) Commiting transaction failed");

let r1: Vec<u32> = read_children(&db, None, PREFIX, 1_1).unwrap();
let r2: Vec<u32> = read_children(&db, None, PREFIX, 1_2).unwrap();
let r1: Vec<u32> = read_children(&db, 0, PREFIX, 1_1).expect("(1) Getting r1 failed");
let r2: Vec<u32> = read_children(&db, 0, PREFIX, 1_2).expect("(1) Getting r2 failed");

assert_eq!(r1, vec![1_3, 1_5]);
assert_eq!(r2, vec![1_4, 1_6]);

remove_children(&mut tx, None, PREFIX, 1_2);
db.write(tx).unwrap();
remove_children(&mut tx, 0, PREFIX, 1_2);
db.write(tx).expect("(2) Commiting transaction failed");

let r1: Vec<u32> = read_children(&db, None, PREFIX, 1_1).unwrap();
let r2: Vec<u32> = read_children(&db, None, PREFIX, 1_2).unwrap();
let r1: Vec<u32> = read_children(&db, 0, PREFIX, 1_1).expect("(2) Getting r1 failed");
let r2: Vec<u32> = read_children(&db, 0, PREFIX, 1_2).expect("(2) Getting r2 failed");

assert_eq!(r1, vec![1_3, 1_5]);
assert_eq!(r2.len(), 0);
Expand Down
22 changes: 11 additions & 11 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,18 @@ pub fn new_client<E, S, Block, RA>(
}

pub(crate) mod columns {
pub const META: Option<u32> = crate::utils::COLUMN_META;
pub const STATE: Option<u32> = Some(1);
pub const STATE_META: Option<u32> = Some(2);
pub const META: u32 = crate::utils::COLUMN_META;
pub const STATE: u32 = 1;
pub const STATE_META: u32 = 2;
/// maps hashes to lookup keys and numbers to canon hashes.
pub const KEY_LOOKUP: Option<u32> = Some(3);
pub const HEADER: Option<u32> = Some(4);
pub const BODY: Option<u32> = Some(5);
pub const JUSTIFICATION: Option<u32> = Some(6);
pub const CHANGES_TRIE: Option<u32> = Some(7);
pub const AUX: Option<u32> = Some(8);
pub const KEY_LOOKUP: u32 = 3;
pub const HEADER: u32 = 4;
pub const BODY: u32 = 5;
pub const JUSTIFICATION: u32 = 6;
pub const CHANGES_TRIE: u32 = 7;
pub const AUX: u32 = 8;
/// Offchain workers local storage
pub const OFFCHAIN: Option<u32> = Some(9);
pub const OFFCHAIN: u32 = 9;
}

struct PendingBlock<Block: BlockT> {
Expand Down Expand Up @@ -633,7 +633,7 @@ struct StorageDb<Block: BlockT> {
impl<Block: BlockT> sp_state_machine::Storage<Blake2Hasher> for StorageDb<Block> {
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)))
self.state_db.get(&key, self)
.map_err(|e| format!("Database backend error: {:?}", e))
}
}
Expand Down
12 changes: 6 additions & 6 deletions client/db/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ use crate::DatabaseSettings;
use log::{trace, warn, debug};

pub(crate) mod columns {
pub const META: Option<u32> = crate::utils::COLUMN_META;
pub const KEY_LOOKUP: Option<u32> = Some(1);
pub const HEADER: Option<u32> = Some(2);
pub const CACHE: Option<u32> = Some(3);
pub const CHT: Option<u32> = Some(4);
pub const AUX: Option<u32> = Some(5);
pub const META: u32 = crate::utils::COLUMN_META;
pub const KEY_LOOKUP: u32 = 1;
pub const HEADER: u32 = 2;
pub const CACHE: u32 = 3;
pub const CHT: u32 = 4;
pub const AUX: u32 = 5;
}

/// Prefix for headers CHT.
Expand Down
38 changes: 19 additions & 19 deletions client/db/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::{DatabaseSettings, DatabaseSettingsSrc};
/// Otherwise RocksDb will fail to open database && check its type.
pub const NUM_COLUMNS: u32 = 10;
/// Meta column. The set of keys in the column is shared by full && light storages.
pub const COLUMN_META: Option<u32> = Some(0);
pub const COLUMN_META: u32 = 0;

/// Keys of entries in COLUMN_META.
pub mod meta_keys {
Expand Down Expand Up @@ -125,7 +125,7 @@ pub fn lookup_key_to_number<N>(key: &[u8]) -> sp_blockchain::Result<N> where
/// Delete number to hash mapping in DB transaction.
pub fn remove_number_to_key_mapping<N: TryInto<u32>>(
transaction: &mut DBTransaction,
key_lookup_col: Option<u32>,
key_lookup_col: u32,
number: N,
) -> sp_blockchain::Result<()> {
transaction.delete(key_lookup_col, number_index_key(number)?.as_ref());
Expand All @@ -135,7 +135,7 @@ pub fn remove_number_to_key_mapping<N: TryInto<u32>>(
/// Remove key mappings.
pub fn remove_key_mappings<N: TryInto<u32>, H: AsRef<[u8]>>(
transaction: &mut DBTransaction,
key_lookup_col: Option<u32>,
key_lookup_col: u32,
number: N,
hash: H,
) -> sp_blockchain::Result<()> {
Expand All @@ -148,7 +148,7 @@ pub fn remove_key_mappings<N: TryInto<u32>, H: AsRef<[u8]>>(
/// block hash at that position.
pub fn insert_number_to_key_mapping<N: TryInto<u32> + Clone, H: AsRef<[u8]>>(
transaction: &mut DBTransaction,
key_lookup_col: Option<u32>,
key_lookup_col: u32,
number: N,
hash: H,
) -> sp_blockchain::Result<()> {
Expand All @@ -163,7 +163,7 @@ pub fn insert_number_to_key_mapping<N: TryInto<u32> + Clone, H: AsRef<[u8]>>(
/// Insert a hash to key mapping in the database.
pub fn insert_hash_to_key_mapping<N: TryInto<u32>, H: AsRef<[u8]> + Clone>(
transaction: &mut DBTransaction,
key_lookup_col: Option<u32>,
key_lookup_col: u32,
number: N,
hash: H,
) -> sp_blockchain::Result<()> {
Expand All @@ -180,7 +180,7 @@ pub fn insert_hash_to_key_mapping<N: TryInto<u32>, H: AsRef<[u8]> + Clone>(
/// looks up lookup key by hash from DB as necessary.
pub fn block_id_to_lookup_key<Block>(
db: &dyn KeyValueDB,
key_lookup_col: Option<u32>,
key_lookup_col: u32,
id: BlockId<Block>
) -> Result<Option<Vec<u8>>, sp_blockchain::Error> where
Block: BlockT,
Expand All @@ -194,7 +194,7 @@ pub fn block_id_to_lookup_key<Block>(
BlockId::Hash(h) => db.get(key_lookup_col, h.as_ref()),
};

res.map(|v| v.map(|v| v.into_vec())).map_err(db_err)
res.map_err(db_err)
}

/// Maps database error to client error
Expand All @@ -205,24 +205,24 @@ pub fn db_err(err: io::Error) -> sp_blockchain::Error {
/// Open RocksDB database.
pub fn open_database(
config: &DatabaseSettings,
col_meta: Option<u32>,
col_meta: u32,
db_type: &str
) -> sp_blockchain::Result<Arc<dyn KeyValueDB>> {
let db: Arc<dyn KeyValueDB> = match &config.source {
#[cfg(feature = "kvdb-rocksdb")]
DatabaseSettingsSrc::Path { path, cache_size } => {
let mut db_config = DatabaseConfig::with_columns(Some(NUM_COLUMNS));
let mut db_config = DatabaseConfig::with_columns(NUM_COLUMNS);

if let Some(cache_size) = cache_size {
let state_col_budget = (*cache_size as f64 * 0.9) as usize;
let other_col_budget = (cache_size - state_col_budget) / (NUM_COLUMNS as usize - 1);

let mut memory_budget = std::collections::HashMap::new();
for i in 0..NUM_COLUMNS {
if Some(i) == crate::columns::STATE {
memory_budget.insert(Some(i), state_col_budget);
if i == crate::columns::STATE {
memory_budget.insert(i, state_col_budget);
} else {
memory_budget.insert(Some(i), other_col_budget);
memory_budget.insert(i, other_col_budget);
}
}

Expand Down Expand Up @@ -261,8 +261,8 @@ pub fn open_database(
/// Read database column entry for the given block.
pub fn read_db<Block>(
db: &dyn KeyValueDB,
col_index: Option<u32>,
col: Option<u32>,
col_index: u32,
col: u32,
id: BlockId<Block>
) -> sp_blockchain::Result<Option<DBValue>>
where
Expand All @@ -277,8 +277,8 @@ pub fn read_db<Block>(
/// Read a header from the database.
pub fn read_header<Block: BlockT>(
db: &dyn KeyValueDB,
col_index: Option<u32>,
col: Option<u32>,
col_index: u32,
col: u32,
id: BlockId<Block>,
) -> sp_blockchain::Result<Option<Block::Header>> {
match read_db(db, col_index, col, id)? {
Expand All @@ -295,16 +295,16 @@ pub fn read_header<Block: BlockT>(
/// Required header from the database.
pub fn require_header<Block: BlockT>(
db: &dyn KeyValueDB,
col_index: Option<u32>,
col: Option<u32>,
col_index: u32,
col: u32,
id: BlockId<Block>,
) -> sp_blockchain::Result<Block::Header> {
read_header(db, col_index, col, id)
.and_then(|header| header.ok_or_else(|| sp_blockchain::Error::UnknownBlock(format!("{}", id))))
}

/// Read meta from the database.
pub fn read_meta<Block>(db: &dyn KeyValueDB, col_meta: Option<u32>, col_header: Option<u32>) -> Result<
pub fn read_meta<Block>(db: &dyn KeyValueDB, col_meta: u32, col_header: u32) -> Result<
Meta<<<Block as BlockT>::Header as HeaderT>::Number, Block::Hash>,
sp_blockchain::Error,
>
Expand Down
Loading

0 comments on commit 5842f6e

Please sign in to comment.