mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
authorities_at cache update (#836)
* AuthoritiesAt cache update * fix after merge
This commit is contained in:
committed by
Gav Wood
parent
ceda61f13c
commit
fb058ae235
+1383
File diff suppressed because it is too large
Load Diff
+164
@@ -0,0 +1,164 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! List-cache storage entries.
|
||||
|
||||
use client::error::Result as ClientResult;
|
||||
use runtime_primitives::traits::{Block as BlockT, NumberFor};
|
||||
|
||||
use cache::{CacheItemT, ComplexBlockId};
|
||||
use cache::list_storage::{Storage};
|
||||
|
||||
/// Single list-based cache entry.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(test, derive(PartialEq))]
|
||||
pub struct Entry<Block: BlockT, T> {
|
||||
/// first block, when this value became actual
|
||||
pub valid_from: ComplexBlockId<Block>,
|
||||
/// None means that we do not know the value starting from `valid_from` block
|
||||
pub value: Option<T>,
|
||||
}
|
||||
|
||||
/// Internal representation of the single list-based cache entry. The entry points to the
|
||||
/// previous entry in the cache, allowing us to traverse back in time in list-style.
|
||||
#[derive(Debug, Encode, Decode)]
|
||||
#[cfg_attr(test, derive(Clone, PartialEq))]
|
||||
pub struct StorageEntry<Block: BlockT, T: CacheItemT> {
|
||||
/// None if valid from the beginning
|
||||
pub prev_valid_from: Option<ComplexBlockId<Block>>,
|
||||
/// None means that we do not know the value starting from `valid_from` block
|
||||
pub value: Option<T>,
|
||||
}
|
||||
|
||||
impl<Block: BlockT, T: CacheItemT> Entry<Block, T> {
|
||||
/// Returns Some if the entry should be updated with the new value.
|
||||
pub fn try_update(&self, value: Option<T>) -> Option<StorageEntry<Block, T>> {
|
||||
match self.value == value {
|
||||
true => None,
|
||||
false => Some(StorageEntry {
|
||||
prev_valid_from: Some(self.valid_from.clone()),
|
||||
value,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrapper that calls search_before to get range where the given block fits.
|
||||
pub fn search_best_range_before<S: Storage<Block, T>>(
|
||||
&self,
|
||||
storage: &S,
|
||||
block: NumberFor<Block>,
|
||||
) -> ClientResult<Option<(ComplexBlockId<Block>, Option<ComplexBlockId<Block>>)>> {
|
||||
Ok(self.search_best_before(storage, block, false)?
|
||||
.map(|(entry, next)| (entry.valid_from, next)))
|
||||
}
|
||||
|
||||
/// Searches the list, ending with THIS entry for the best entry preceeding (or at)
|
||||
/// given block number.
|
||||
/// If the entry is found, result is the entry and the block id of next entry (if exists).
|
||||
/// NOTE that this function does not check that the passed block is actually linked to
|
||||
/// the blocks it found.
|
||||
pub fn search_best_before<S: Storage<Block, T>>(
|
||||
&self,
|
||||
storage: &S,
|
||||
block: NumberFor<Block>,
|
||||
require_value: bool,
|
||||
) -> ClientResult<Option<(Entry<Block, T>, Option<ComplexBlockId<Block>>)>> {
|
||||
// we're looking for the best value
|
||||
let mut next = None;
|
||||
let mut current = self.valid_from.clone();
|
||||
if block >= self.valid_from.number {
|
||||
let value = if require_value { self.value.clone() } else { None };
|
||||
return Ok(Some((Entry { valid_from: current, value }, next)));
|
||||
}
|
||||
|
||||
// else - travel back in time
|
||||
loop {
|
||||
let entry = storage.require_entry(¤t)?;
|
||||
if block >= current.number {
|
||||
return Ok(Some((Entry { valid_from: current, value: entry.value }, next)));
|
||||
}
|
||||
|
||||
next = Some(current);
|
||||
current = match entry.prev_valid_from {
|
||||
Some(prev_valid_from) => prev_valid_from,
|
||||
None => return Ok(None),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT, T: CacheItemT> StorageEntry<Block, T> {
|
||||
/// Converts storage entry into an entry, valid from given block.
|
||||
pub fn into_entry(self, valid_from: ComplexBlockId<Block>) -> Entry<Block, T> {
|
||||
Entry {
|
||||
valid_from,
|
||||
value: self.value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use cache::list_cache::tests::test_id;
|
||||
use cache::list_storage::tests::{DummyStorage, FaultyStorage};
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn entry_try_update_works() {
|
||||
// when trying to update with the same None value
|
||||
assert_eq!(Entry::<_, u64> { valid_from: test_id(1), value: None }.try_update(None), None);
|
||||
// when trying to update with the same Some value
|
||||
assert_eq!(Entry { valid_from: test_id(1), value: Some(1) }.try_update(Some(1)), None);
|
||||
// when trying to update with different None value
|
||||
assert_eq!(Entry { valid_from: test_id(1), value: Some(1) }.try_update(None),
|
||||
Some(StorageEntry { prev_valid_from: Some(test_id(1)), value: None }));
|
||||
// when trying to update with different Some value
|
||||
assert_eq!(Entry { valid_from: test_id(1), value: Some(1) }.try_update(Some(2)),
|
||||
Some(StorageEntry { prev_valid_from: Some(test_id(1)), value: Some(2) }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entry_search_best_before_fails() {
|
||||
// when storage returns error
|
||||
assert!(Entry::<_, u64> { valid_from: test_id(100), value: None }.search_best_before(&FaultyStorage, 50, false).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entry_search_best_before_works() {
|
||||
// when block is better than our best block AND value is not required
|
||||
assert_eq!(Entry::<_, u64> { valid_from: test_id(100), value: Some(100) }
|
||||
.search_best_before(&DummyStorage::new(), 150, false).unwrap(),
|
||||
Some((Entry::<_, u64> { valid_from: test_id(100), value: None }, None)));
|
||||
// when block is better than our best block AND value is required
|
||||
assert_eq!(Entry::<_, u64> { valid_from: test_id(100), value: Some(100) }
|
||||
.search_best_before(&DummyStorage::new(), 150, true).unwrap(),
|
||||
Some((Entry::<_, u64> { valid_from: test_id(100), value: Some(100) }, None)));
|
||||
// when block is found between two entries
|
||||
assert_eq!(Entry::<_, u64> { valid_from: test_id(100), value: Some(100) }
|
||||
.search_best_before(&DummyStorage::new()
|
||||
.with_entry(test_id(100), StorageEntry { prev_valid_from: Some(test_id(50)), value: Some(100) })
|
||||
.with_entry(test_id(50), StorageEntry { prev_valid_from: Some(test_id(30)), value: Some(50) }),
|
||||
75, false).unwrap(),
|
||||
Some((Entry::<_, u64> { valid_from: test_id(50), value: Some(50) }, Some(test_id(100)))));
|
||||
// when block is not found
|
||||
assert_eq!(Entry::<_, u64> { valid_from: test_id(100), value: Some(100) }
|
||||
.search_best_before(&DummyStorage::new()
|
||||
.with_entry(test_id(100), StorageEntry { prev_valid_from: Some(test_id(50)), value: Some(100) })
|
||||
.with_entry(test_id(50), StorageEntry { prev_valid_from: None, value: Some(50) }),
|
||||
30, true).unwrap(),
|
||||
None);
|
||||
}
|
||||
}
|
||||
+378
@@ -0,0 +1,378 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! List-cache storage definition and implementation.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use kvdb::{KeyValueDB, DBTransaction};
|
||||
|
||||
use client::error::{Error as ClientError, ErrorKind as ClientErrorKind, Result as ClientResult};
|
||||
use codec::{Encode, Decode};
|
||||
use runtime_primitives::generic::BlockId;
|
||||
use runtime_primitives::traits::{Block as BlockT, NumberFor};
|
||||
use utils::{self, db_err, meta_keys};
|
||||
|
||||
use cache::{CacheItemT, ComplexBlockId};
|
||||
use cache::list_cache::{CommitOperation, Fork};
|
||||
use cache::list_entry::{Entry, StorageEntry};
|
||||
|
||||
/// Single list-cache metadata.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(test, derive(Clone, PartialEq))]
|
||||
pub struct Metadata<Block: BlockT> {
|
||||
/// Block at which best finalized entry is stored.
|
||||
pub finalized: Option<ComplexBlockId<Block>>,
|
||||
/// A set of blocks at which best unfinalized entries are stored.
|
||||
pub unfinalized: Vec<ComplexBlockId<Block>>,
|
||||
}
|
||||
|
||||
/// Readonly list-cache storage trait.
|
||||
pub trait Storage<Block: BlockT, T: CacheItemT> {
|
||||
/// Reads hash of the block at given number.
|
||||
fn read_id(&self, at: NumberFor<Block>) -> ClientResult<Option<Block::Hash>>;
|
||||
|
||||
/// Reads header of the block with given hash.
|
||||
fn read_header(&self, at: &Block::Hash) -> ClientResult<Option<Block::Header>>;
|
||||
|
||||
/// Reads cache metadata: best finalized entry (if some) and the list.
|
||||
fn read_meta(&self) -> ClientResult<Metadata<Block>>;
|
||||
|
||||
/// Reads cache entry from the storage.
|
||||
fn read_entry(&self, at: &ComplexBlockId<Block>) -> ClientResult<Option<StorageEntry<Block, T>>>;
|
||||
|
||||
/// Reads referenced (and thus existing) cache entry from the storage.
|
||||
fn require_entry(&self, at: &ComplexBlockId<Block>) -> ClientResult<StorageEntry<Block, T>> {
|
||||
self.read_entry(at)
|
||||
.and_then(|entry| entry
|
||||
.ok_or_else(|| ClientError::from(
|
||||
ClientErrorKind::Backend(format!("Referenced cache entry at {:?} is not found", at)))))
|
||||
}
|
||||
}
|
||||
|
||||
/// List-cache storage transaction.
|
||||
pub trait StorageTransaction<Block: BlockT, T: CacheItemT> {
|
||||
/// Insert storage entry at given block.
|
||||
fn insert_storage_entry(&mut self, at: &ComplexBlockId<Block>, entry: &StorageEntry<Block, T>);
|
||||
|
||||
/// Delete storage entry at given block.
|
||||
fn remove_storage_entry(&mut self, at: &ComplexBlockId<Block>);
|
||||
|
||||
/// Update metadata of the cache.
|
||||
fn update_meta(
|
||||
&mut self,
|
||||
best_finalized_entry: Option<&Entry<Block, T>>,
|
||||
unfinalized: &[Fork<Block, T>],
|
||||
operation: &CommitOperation<Block, T>,
|
||||
);
|
||||
}
|
||||
|
||||
/// A set of columns used by the DbStorage.
|
||||
#[derive(Debug)]
|
||||
pub struct DbColumns {
|
||||
/// Column holding cache meta.
|
||||
pub meta: Option<u32>,
|
||||
/// Column holding the mapping of { block number => block hash } for blocks of the best chain.
|
||||
pub hash_lookup: Option<u32>,
|
||||
/// Column holding the mapping of { block hash => block header }.
|
||||
pub header: Option<u32>,
|
||||
/// Column holding cache entries.
|
||||
pub cache: Option<u32>,
|
||||
}
|
||||
|
||||
/// Database-backed list cache storage.
|
||||
pub struct DbStorage {
|
||||
name: Vec<u8>,
|
||||
meta_key: Vec<u8>,
|
||||
db: Arc<KeyValueDB>,
|
||||
columns: DbColumns,
|
||||
}
|
||||
|
||||
impl DbStorage {
|
||||
/// Create new database-backed list cache storage.
|
||||
pub fn new(name: Vec<u8>, db: Arc<KeyValueDB>, columns: DbColumns) -> Self {
|
||||
let meta_key = meta::key(&name);
|
||||
DbStorage { name, meta_key, db, columns }
|
||||
}
|
||||
|
||||
/// Get reference to the database.
|
||||
pub fn db(&self) -> &Arc<KeyValueDB> { &self.db }
|
||||
|
||||
/// Get reference to the database columns.
|
||||
pub fn columns(&self) -> &DbColumns { &self.columns }
|
||||
|
||||
/// Encode block id for storing as a key in cache column.
|
||||
/// We append prefix to the actual encoding to allow several caches
|
||||
/// store entries in the same column.
|
||||
pub fn encode_block_id<Block: BlockT>(&self, block: &ComplexBlockId<Block>) -> Vec<u8> {
|
||||
let mut encoded = self.name.clone();
|
||||
encoded.extend(block.hash.as_ref());
|
||||
encoded
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT, T: CacheItemT> Storage<Block, T> for DbStorage {
|
||||
fn read_id(&self, at: NumberFor<Block>) -> ClientResult<Option<Block::Hash>> {
|
||||
utils::read_id::<Block>(&*self.db, self.columns.hash_lookup, BlockId::Number(at))
|
||||
}
|
||||
|
||||
fn read_header(&self, at: &Block::Hash) -> ClientResult<Option<Block::Header>> {
|
||||
utils::read_header::<Block>(&*self.db, self.columns.hash_lookup, self.columns.header, BlockId::Hash(*at))
|
||||
}
|
||||
|
||||
fn read_meta(&self) -> ClientResult<Metadata<Block>> {
|
||||
self.db.get(self.columns.meta, &self.meta_key)
|
||||
.map_err(db_err)
|
||||
.and_then(|meta| match meta {
|
||||
Some(meta) => meta::decode(&*meta),
|
||||
None => Ok(Metadata {
|
||||
finalized: None,
|
||||
unfinalized: Vec::new(),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
fn read_entry(&self, at: &ComplexBlockId<Block>) -> ClientResult<Option<StorageEntry<Block, T>>> {
|
||||
self.db.get(self.columns.cache, &self.encode_block_id(at))
|
||||
.map_err(db_err)
|
||||
.and_then(|entry| match entry {
|
||||
Some(entry) => StorageEntry::<Block, T>::decode(&mut &entry[..])
|
||||
.ok_or_else(|| ClientErrorKind::Backend("Failed to decode cache entry".into()).into())
|
||||
.map(Some),
|
||||
None => Ok(None),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Database-backed list cache storage transaction.
|
||||
pub struct DbStorageTransaction<'a> {
|
||||
storage: &'a DbStorage,
|
||||
tx: &'a mut DBTransaction,
|
||||
}
|
||||
|
||||
impl<'a> DbStorageTransaction<'a> {
|
||||
/// Create new database transaction.
|
||||
pub fn new(storage: &'a DbStorage, tx: &'a mut DBTransaction) -> Self {
|
||||
DbStorageTransaction { storage, tx }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Block: BlockT, T: CacheItemT> StorageTransaction<Block, T> for DbStorageTransaction<'a> {
|
||||
fn insert_storage_entry(&mut self, at: &ComplexBlockId<Block>, entry: &StorageEntry<Block, T>) {
|
||||
self.tx.put(self.storage.columns.cache, &self.storage.encode_block_id(at), &entry.encode());
|
||||
}
|
||||
|
||||
fn remove_storage_entry(&mut self, at: &ComplexBlockId<Block>) {
|
||||
self.tx.delete(self.storage.columns.cache, &self.storage.encode_block_id(at));
|
||||
}
|
||||
|
||||
fn update_meta(
|
||||
&mut self,
|
||||
best_finalized_entry: Option<&Entry<Block, T>>,
|
||||
unfinalized: &[Fork<Block, T>],
|
||||
operation: &CommitOperation<Block, T>,
|
||||
) {
|
||||
self.tx.put(
|
||||
self.storage.columns.meta,
|
||||
&self.storage.meta_key,
|
||||
&meta::encode(best_finalized_entry, unfinalized, operation));
|
||||
}
|
||||
}
|
||||
|
||||
/// Metadata related functions.
|
||||
mod meta {
|
||||
use super::*;
|
||||
|
||||
/// Convert cache name into cache metadata key.
|
||||
pub fn key(name: &[u8]) -> Vec<u8> {
|
||||
let mut key_name = meta_keys::CACHE_META_PREFIX.to_vec();
|
||||
key_name.extend_from_slice(name);
|
||||
key_name
|
||||
}
|
||||
|
||||
/// Encode cache metadata 'applying' commit operation before encoding.
|
||||
pub fn encode<Block: BlockT, T: CacheItemT>(
|
||||
best_finalized_entry: Option<&Entry<Block, T>>,
|
||||
unfinalized: &[Fork<Block, T>],
|
||||
op: &CommitOperation<Block, T>
|
||||
) -> Vec<u8> {
|
||||
let mut finalized = best_finalized_entry.as_ref().map(|entry| &entry.valid_from);
|
||||
let mut unfinalized = unfinalized.iter().map(|fork| &fork.head().valid_from).collect::<Vec<_>>();
|
||||
|
||||
match op {
|
||||
CommitOperation::AppendNewBlock(_, _) => (),
|
||||
CommitOperation::AppendNewEntry(index, ref entry) => {
|
||||
unfinalized[*index] = &entry.valid_from;
|
||||
},
|
||||
CommitOperation::AddNewFork(ref entry) => {
|
||||
unfinalized.push(&entry.valid_from);
|
||||
},
|
||||
CommitOperation::BlockFinalized(_, ref finalizing_entry, ref forks) => {
|
||||
finalized = finalizing_entry.as_ref().map(|entry| &entry.valid_from);
|
||||
for fork_index in forks.iter().rev() {
|
||||
unfinalized.remove(*fork_index);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
(finalized, unfinalized).encode()
|
||||
}
|
||||
|
||||
/// Decode meta information.
|
||||
pub fn decode<Block: BlockT>(encoded: &[u8]) -> ClientResult<Metadata<Block>> {
|
||||
let input = &mut &*encoded;
|
||||
let finalized: Option<ComplexBlockId<Block>> = Decode::decode(input)
|
||||
.ok_or_else(|| ClientError::from(ClientErrorKind::Backend("Error decoding cache meta".into())))?;
|
||||
let unfinalized: Vec<ComplexBlockId<Block>> = Decode::decode(input)
|
||||
.ok_or_else(|| ClientError::from(ClientErrorKind::Backend("Error decoding cache meta".into())))?;
|
||||
|
||||
Ok(Metadata { finalized, unfinalized })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use runtime_primitives::traits::Header as HeaderT;
|
||||
use super::*;
|
||||
|
||||
pub struct FaultyStorage;
|
||||
|
||||
impl<Block: BlockT, T: CacheItemT> Storage<Block, T> for FaultyStorage {
|
||||
fn read_id(&self, _at: NumberFor<Block>) -> ClientResult<Option<Block::Hash>> {
|
||||
Err(ClientErrorKind::Backend("TestError".into()).into())
|
||||
}
|
||||
|
||||
fn read_header(&self, _at: &Block::Hash) -> ClientResult<Option<Block::Header>> {
|
||||
Err(ClientErrorKind::Backend("TestError".into()).into())
|
||||
}
|
||||
|
||||
fn read_meta(&self) -> ClientResult<Metadata<Block>> {
|
||||
Err(ClientErrorKind::Backend("TestError".into()).into())
|
||||
}
|
||||
|
||||
fn read_entry(&self, _at: &ComplexBlockId<Block>) -> ClientResult<Option<StorageEntry<Block, T>>> {
|
||||
Err(ClientErrorKind::Backend("TestError".into()).into())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DummyStorage<Block: BlockT, T: CacheItemT> {
|
||||
meta: Metadata<Block>,
|
||||
ids: HashMap<NumberFor<Block>, Block::Hash>,
|
||||
headers: HashMap<Block::Hash, Block::Header>,
|
||||
entries: HashMap<Block::Hash, StorageEntry<Block, T>>,
|
||||
}
|
||||
|
||||
impl<Block: BlockT, T: CacheItemT> DummyStorage<Block, T> {
|
||||
pub fn new() -> Self {
|
||||
DummyStorage {
|
||||
meta: Metadata {
|
||||
finalized: None,
|
||||
unfinalized: Vec::new(),
|
||||
},
|
||||
ids: HashMap::new(),
|
||||
headers: HashMap::new(),
|
||||
entries: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_meta(mut self, finalized: Option<ComplexBlockId<Block>>, unfinalized: Vec<ComplexBlockId<Block>>) -> Self {
|
||||
self.meta.finalized = finalized;
|
||||
self.meta.unfinalized = unfinalized;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_id(mut self, at: NumberFor<Block>, id: Block::Hash) -> Self {
|
||||
self.ids.insert(at, id);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_header(mut self, header: Block::Header) -> Self {
|
||||
self.headers.insert(header.hash(), header);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_entry(mut self, at: ComplexBlockId<Block>, entry: StorageEntry<Block, T>) -> Self {
|
||||
self.entries.insert(at.hash, entry);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT, T: CacheItemT> Storage<Block, T> for DummyStorage<Block, T> {
|
||||
fn read_id(&self, at: NumberFor<Block>) -> ClientResult<Option<Block::Hash>> {
|
||||
Ok(self.ids.get(&at).cloned())
|
||||
}
|
||||
|
||||
fn read_header(&self, at: &Block::Hash) -> ClientResult<Option<Block::Header>> {
|
||||
Ok(self.headers.get(&at).cloned())
|
||||
}
|
||||
|
||||
fn read_meta(&self) -> ClientResult<Metadata<Block>> {
|
||||
Ok(self.meta.clone())
|
||||
}
|
||||
|
||||
fn read_entry(&self, at: &ComplexBlockId<Block>) -> ClientResult<Option<StorageEntry<Block, T>>> {
|
||||
Ok(self.entries.get(&at.hash).cloned())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DummyTransaction<Block: BlockT> {
|
||||
updated_meta: Option<Metadata<Block>>,
|
||||
inserted_entries: HashSet<Block::Hash>,
|
||||
removed_entries: HashSet<Block::Hash>,
|
||||
}
|
||||
|
||||
impl<Block: BlockT> DummyTransaction<Block> {
|
||||
pub fn new() -> Self {
|
||||
DummyTransaction {
|
||||
updated_meta: None,
|
||||
inserted_entries: HashSet::new(),
|
||||
removed_entries: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inserted_entries(&self) -> &HashSet<Block::Hash> {
|
||||
&self.inserted_entries
|
||||
}
|
||||
|
||||
pub fn removed_entries(&self) -> &HashSet<Block::Hash> {
|
||||
&self.removed_entries
|
||||
}
|
||||
|
||||
pub fn updated_meta(&self) -> &Option<Metadata<Block>> {
|
||||
&self.updated_meta
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT, T: CacheItemT> StorageTransaction<Block, T> for DummyTransaction<Block> {
|
||||
fn insert_storage_entry(&mut self, at: &ComplexBlockId<Block>, _entry: &StorageEntry<Block, T>) {
|
||||
self.inserted_entries.insert(at.hash);
|
||||
}
|
||||
|
||||
fn remove_storage_entry(&mut self, at: &ComplexBlockId<Block>) {
|
||||
self.removed_entries.insert(at.hash);
|
||||
}
|
||||
|
||||
fn update_meta(
|
||||
&mut self,
|
||||
best_finalized_entry: Option<&Entry<Block, T>>,
|
||||
unfinalized: &[Fork<Block, T>],
|
||||
operation: &CommitOperation<Block, T>,
|
||||
) {
|
||||
self.updated_meta = Some(meta::decode(&meta::encode(best_finalized_entry, unfinalized, operation)).unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
+207
@@ -0,0 +1,207 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! DB-backed cache of blockchain data.
|
||||
|
||||
use std::sync::Arc;
|
||||
use parking_lot::RwLock;
|
||||
|
||||
use kvdb::{KeyValueDB, DBTransaction};
|
||||
|
||||
use client::blockchain::Cache as BlockchainCache;
|
||||
use client::error::Result as ClientResult;
|
||||
use codec::{Encode, Decode};
|
||||
use primitives::AuthorityId;
|
||||
use runtime_primitives::generic::BlockId;
|
||||
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor, As};
|
||||
use utils::{self, COLUMN_META};
|
||||
|
||||
use self::list_cache::ListCache;
|
||||
|
||||
mod list_cache;
|
||||
mod list_entry;
|
||||
mod list_storage;
|
||||
|
||||
/// Minimal post-finalization age age of finalized blocks before they'll pruned.
|
||||
const PRUNE_DEPTH: u64 = 1024;
|
||||
|
||||
/// Block identifier that holds both hash and number.
|
||||
#[derive(Clone, Debug, Encode, Decode, PartialEq)]
|
||||
pub struct ComplexBlockId<Block: BlockT> {
|
||||
hash: Block::Hash,
|
||||
number: NumberFor<Block>,
|
||||
}
|
||||
|
||||
impl<Block: BlockT> ComplexBlockId<Block> {
|
||||
/// Create new complex block id.
|
||||
pub fn new(hash: Block::Hash, number: NumberFor<Block>) -> Self {
|
||||
ComplexBlockId { hash, number }
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT> ::std::cmp::PartialOrd for ComplexBlockId<Block> {
|
||||
fn partial_cmp(&self, other: &ComplexBlockId<Block>) -> Option<::std::cmp::Ordering> {
|
||||
self.number.partial_cmp(&other.number)
|
||||
}
|
||||
}
|
||||
|
||||
/// All cache items must implement this trait.
|
||||
pub trait CacheItemT: Clone + Decode + Encode + PartialEq {}
|
||||
|
||||
impl<T> CacheItemT for T where T: Clone + Decode + Encode + PartialEq {}
|
||||
|
||||
/// Database-backed blockchain data cache.
|
||||
pub struct DbCache<Block: BlockT> {
|
||||
authorities_at: ListCache<Block, Vec<AuthorityId>, self::list_storage::DbStorage>,
|
||||
}
|
||||
|
||||
impl<Block: BlockT> DbCache<Block> {
|
||||
/// Create new cache.
|
||||
pub fn new(
|
||||
db: Arc<KeyValueDB>,
|
||||
hash_lookup_column: Option<u32>,
|
||||
header_column: Option<u32>,
|
||||
authorities_column: Option<u32>,
|
||||
best_finalized_block: ComplexBlockId<Block>,
|
||||
) -> Self {
|
||||
DbCache {
|
||||
authorities_at: ListCache::new(
|
||||
self::list_storage::DbStorage::new(b"auth".to_vec(), db,
|
||||
self::list_storage::DbColumns {
|
||||
meta: COLUMN_META,
|
||||
hash_lookup: hash_lookup_column,
|
||||
header: header_column,
|
||||
cache: authorities_column,
|
||||
},
|
||||
),
|
||||
As::sa(PRUNE_DEPTH),
|
||||
best_finalized_block,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Begin cache transaction.
|
||||
pub fn transaction<'a>(&'a mut self, tx: &'a mut DBTransaction) -> DbCacheTransaction<'a, Block> {
|
||||
DbCacheTransaction {
|
||||
cache: self,
|
||||
tx,
|
||||
authorities_at_op: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Run post-commit cache operations.
|
||||
pub fn commit(&mut self, ops: DbCacheTransactionOps<Block>) {
|
||||
if let Some(authorities_at_op) = ops.authorities_at_op {
|
||||
self.authorities_at.on_transaction_commit(authorities_at_op);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Cache operations that are to be committed after database transaction is committed.
|
||||
pub struct DbCacheTransactionOps<Block: BlockT> {
|
||||
authorities_at_op: Option<self::list_cache::CommitOperation<Block, Vec<AuthorityId>>>,
|
||||
}
|
||||
|
||||
/// Database-backed blockchain data cache transaction valid for single block import.
|
||||
pub struct DbCacheTransaction<'a, Block: BlockT> {
|
||||
cache: &'a mut DbCache<Block>,
|
||||
tx: &'a mut DBTransaction,
|
||||
authorities_at_op: Option<self::list_cache::CommitOperation<Block, Vec<AuthorityId>>>,
|
||||
}
|
||||
|
||||
impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> {
|
||||
/// Convert transaction into post-commit operations set.
|
||||
pub fn into_ops(self) -> DbCacheTransactionOps<Block> {
|
||||
DbCacheTransactionOps {
|
||||
authorities_at_op: self.authorities_at_op,
|
||||
}
|
||||
}
|
||||
|
||||
/// When new block is inserted into database.
|
||||
pub fn on_block_insert(
|
||||
mut self,
|
||||
parent: ComplexBlockId<Block>,
|
||||
block: ComplexBlockId<Block>,
|
||||
authorities_at: Option<Vec<AuthorityId>>,
|
||||
is_final: bool,
|
||||
) -> ClientResult<Self> {
|
||||
assert!(self.authorities_at_op.is_none());
|
||||
|
||||
self.authorities_at_op = self.cache.authorities_at.on_block_insert(
|
||||
&mut self::list_storage::DbStorageTransaction::new(
|
||||
self.cache.authorities_at.storage(),
|
||||
&mut self.tx
|
||||
),
|
||||
parent,
|
||||
block,
|
||||
authorities_at,
|
||||
is_final,
|
||||
)?;
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// When previously inserted block is finalized.
|
||||
pub fn on_block_finalize(
|
||||
mut self,
|
||||
parent: ComplexBlockId<Block>,
|
||||
block: ComplexBlockId<Block>
|
||||
) -> ClientResult<Self> {
|
||||
assert!(self.authorities_at_op.is_none());
|
||||
|
||||
self.authorities_at_op = self.cache.authorities_at.on_block_finalize(
|
||||
&mut self::list_storage::DbStorageTransaction::new(
|
||||
self.cache.authorities_at.storage(),
|
||||
&mut self.tx
|
||||
),
|
||||
parent,
|
||||
block,
|
||||
)?;
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Synchronous implementation of database-backed blockchain data cache.
|
||||
pub struct DbCacheSync<Block: BlockT>(pub RwLock<DbCache<Block>>);
|
||||
|
||||
impl<Block: BlockT> BlockchainCache<Block> for DbCacheSync<Block> {
|
||||
fn authorities_at(&self, at: BlockId<Block>) -> Option<Vec<AuthorityId>> {
|
||||
let cache = self.0.read();
|
||||
let storage = cache.authorities_at.storage();
|
||||
let db = storage.db();
|
||||
let columns = storage.columns();
|
||||
let at = match at {
|
||||
BlockId::Hash(hash) => {
|
||||
let header = utils::read_header::<Block>(
|
||||
&**db,
|
||||
columns.hash_lookup,
|
||||
columns.header,
|
||||
BlockId::Hash(hash.clone())).ok()??;
|
||||
ComplexBlockId::new(hash, *header.number())
|
||||
},
|
||||
BlockId::Number(number) => {
|
||||
let hash = utils::read_id::<Block>(
|
||||
&**db,
|
||||
columns.hash_lookup,
|
||||
BlockId::Number(number.clone())).ok()??;
|
||||
ComplexBlockId::new(hash, number)
|
||||
},
|
||||
};
|
||||
|
||||
cache.authorities_at.value_at_block(&at).ok()?
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user