Update kvdb-* and trie (#4483)

This commit is contained in:
Nikolay Volf
2020-01-03 23:46:42 +03:00
committed by Gavin Wood
parent 5cf682cece
commit f0e21eff09
15 changed files with 120 additions and 139 deletions
+4 -4
View File
@@ -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.
+9 -9
View File
@@ -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>,
}
@@ -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 {
@@ -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(|| {
+13 -14
View File
@@ -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));
@@ -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,
@@ -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,
) {
@@ -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);
+11 -11
View File
@@ -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> {
@@ -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))
}
}
+6 -6
View File
@@ -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.
+19 -19
View File
@@ -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 {
@@ -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());
@@ -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<()> {
@@ -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<()> {
@@ -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<()> {
@@ -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,
@@ -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
@@ -205,13 +205,13 @@ 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;
@@ -219,10 +219,10 @@ pub fn open_database(
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);
}
}
@@ -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
@@ -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)? {
@@ -295,8 +295,8 @@ 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)
@@ -304,7 +304,7 @@ pub fn require_header<Block: BlockT>(
}
/// 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,
>