AuxStore on light clients (#1251)

* implement AuxStore on light clients

* Update core/client/db/src/light.rs

Co-Authored-By: svyatonik <svyatonik@gmail.com>

* Update core/client/db/src/light.rs

Co-Authored-By: svyatonik <svyatonik@gmail.com>

* Update core/client/db/src/light.rs

Co-Authored-By: svyatonik <svyatonik@gmail.com>
This commit is contained in:
Svyatoslav Nikolsky
2018-12-13 19:01:57 +03:00
committed by Robert Habermeier
parent c61d03a86f
commit 81f90ed0a4
4 changed files with 99 additions and 18 deletions
+25 -9
View File
@@ -338,6 +338,29 @@ impl<Block: BlockT> blockchain::Backend<Block> for Blockchain<Block> {
}
}
impl<Block: BlockT> backend::AuxStore for Blockchain<Block> {
fn insert_aux<
'a,
'b: 'a,
'c: 'a,
I: IntoIterator<Item=&'a(&'c [u8], &'c [u8])>,
D: IntoIterator<Item=&'a &'b [u8]>,
>(&self, insert: I, delete: D) -> error::Result<()> {
let mut storage = self.storage.write();
for (k, v) in insert {
storage.aux.insert(k.to_vec(), v.to_vec());
}
for k in delete {
storage.aux.remove(*k);
}
Ok(())
}
fn get_aux(&self, key: &[u8]) -> error::Result<Option<Vec<u8>>> {
Ok(self.storage.read().aux.get(key).cloned())
}
}
impl<Block: BlockT> light::blockchain::Storage<Block> for Blockchain<Block>
where
Block::Hash: From<[u8; 32]>,
@@ -511,18 +534,11 @@ where
I: IntoIterator<Item=&'a(&'c [u8], &'c [u8])>,
D: IntoIterator<Item=&'a &'b [u8]>,
>(&self, insert: I, delete: D) -> error::Result<()> {
let mut storage = self.blockchain.storage.write();
for (k, v) in insert {
storage.aux.insert(k.to_vec(), v.to_vec());
}
for k in delete {
storage.aux.remove(*k);
}
Ok(())
self.blockchain.insert_aux(insert, delete)
}
fn get_aux(&self, key: &[u8]) -> error::Result<Option<Vec<u8>>> {
Ok(self.blockchain.storage.read().aux.get(key).cloned())
self.blockchain.get_aux(key)
}
}
+6 -6
View File
@@ -27,7 +27,7 @@ use state_machine::{Backend as StateBackend, InMemoryChangesTrieStorage, TrieBac
use runtime_primitives::traits::{Block as BlockT, NumberFor};
use in_mem;
use backend::{Backend as ClientBackend, BlockImportOperation, RemoteBackend, NewBlockState};
use backend::{AuxStore, Backend as ClientBackend, BlockImportOperation, RemoteBackend, NewBlockState};
use blockchain::HeaderBackend as BlockchainHeaderBackend;
use error::{Error as ClientError, ErrorKind as ClientErrorKind, Result as ClientResult};
use light::blockchain::{Blockchain, Storage as BlockchainStorage};
@@ -70,19 +70,19 @@ impl<S, F> Backend<S, F> {
}
}
impl<S, F> ::backend::AuxStore for Backend<S, F> {
impl<S: AuxStore, F> AuxStore for Backend<S, F> {
fn insert_aux<
'a,
'b: 'a,
'c: 'a,
I: IntoIterator<Item=&'a(&'c [u8], &'c [u8])>,
D: IntoIterator<Item=&'a &'b [u8]>,
>(&self, _insert: I, _delete: D) -> ClientResult<()> {
Err(ClientErrorKind::NotAvailableOnLightClient.into())
>(&self, insert: I, delete: D) -> ClientResult<()> {
self.blockchain.storage().insert_aux(insert, delete)
}
fn get_aux(&self, _key: &[u8]) -> ClientResult<Option<Vec<u8>>> {
Err(ClientErrorKind::NotAvailableOnLightClient.into())
fn get_aux(&self, key: &[u8]) -> ClientResult<Option<Vec<u8>>> {
self.blockchain.storage().get_aux(key)
}
}
+18 -2
View File
@@ -25,7 +25,7 @@ use primitives::AuthorityId;
use runtime_primitives::{Justification, generic::BlockId};
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT,NumberFor, Zero};
use backend::NewBlockState;
use backend::{AuxStore, NewBlockState};
use blockchain::{Backend as BlockchainBackend, BlockStatus, Cache as BlockchainCache,
HeaderBackend as BlockchainHeaderBackend, Info as BlockchainInfo};
use cht;
@@ -33,7 +33,7 @@ use error::{ErrorKind as ClientErrorKind, Result as ClientResult};
use light::fetcher::{Fetcher, RemoteHeaderRequest};
/// Light client blockchain storage.
pub trait Storage<Block: BlockT>: BlockchainHeaderBackend<Block> {
pub trait Storage<Block: BlockT>: AuxStore + BlockchainHeaderBackend<Block> {
/// Store new header. Should refuse to revert any finalized blocks.
///
/// Takes new authorities, the leaf state of the new block, and
@@ -207,6 +207,22 @@ pub mod tests {
}
}
impl AuxStore for DummyStorage {
fn insert_aux<
'a,
'b: 'a,
'c: 'a,
I: IntoIterator<Item=&'a(&'c [u8], &'c [u8])>,
D: IntoIterator<Item=&'a &'b [u8]>,
>(&self, _insert: I, _delete: D) -> ClientResult<()> {
Err(ClientErrorKind::Backend("Test error".into()).into())
}
fn get_aux(&self, _key: &[u8]) -> ClientResult<Option<Vec<u8>>> {
Err(ClientErrorKind::Backend("Test error".into()).into())
}
}
impl Storage<Block> for DummyStorage {
fn import_header(
&self,