mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 16:31:07 +00:00
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:
committed by
Robert Habermeier
parent
c61d03a86f
commit
81f90ed0a4
@@ -21,7 +21,7 @@ use parking_lot::RwLock;
|
||||
|
||||
use kvdb::{KeyValueDB, DBTransaction};
|
||||
|
||||
use client::backend::NewBlockState;
|
||||
use client::backend::{AuxStore, NewBlockState};
|
||||
use client::blockchain::{BlockStatus, Cache as BlockchainCache,
|
||||
HeaderBackend as BlockchainHeaderBackend, Info as BlockchainInfo};
|
||||
use client::{cht, LeafSet};
|
||||
@@ -275,6 +275,31 @@ impl<Block: BlockT> LightStorage<Block> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block> AuxStore for LightStorage<Block>
|
||||
where Block: BlockT,
|
||||
{
|
||||
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<()> {
|
||||
let mut transaction = DBTransaction::new();
|
||||
for (k, v) in insert {
|
||||
transaction.put(columns::AUX, k, v);
|
||||
}
|
||||
for k in delete {
|
||||
transaction.delete(columns::AUX, k);
|
||||
}
|
||||
self.db.write(transaction).map_err(db_err)
|
||||
}
|
||||
|
||||
fn get_aux(&self, key: &[u8]) -> ClientResult<Option<Vec<u8>>> {
|
||||
self.db.get(columns::AUX, key).map(|r| r.map(|v| v.to_vec())).map_err(db_err)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block> LightBlockchainStorage<Block> for LightStorage<Block>
|
||||
where Block: BlockT,
|
||||
{
|
||||
@@ -864,4 +889,28 @@ pub(crate) mod tests {
|
||||
assert_eq!(db.info().unwrap().best_hash, hash0);
|
||||
assert_eq!(db.header(BlockId::Hash::<Block>(hash0)).unwrap().unwrap().hash(), hash0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aux_store_works() {
|
||||
let db = LightStorage::<Block>::new_test();
|
||||
|
||||
// insert aux1 + aux2 using direct store access
|
||||
db.insert_aux(&[(&[1][..], &[101][..]), (&[2][..], &[102][..])], ::std::iter::empty()).unwrap();
|
||||
|
||||
// check aux values
|
||||
assert_eq!(db.get_aux(&[1]).unwrap(), Some(vec![101]));
|
||||
assert_eq!(db.get_aux(&[2]).unwrap(), Some(vec![102]));
|
||||
assert_eq!(db.get_aux(&[3]).unwrap(), None);
|
||||
|
||||
// delete aux1 + insert aux3 using import operation
|
||||
db.import_header(default_header(&Default::default(), 0), None, NewBlockState::Best, vec![
|
||||
(vec![3], Some(vec![103])),
|
||||
(vec![1], None),
|
||||
]).unwrap();
|
||||
|
||||
// check aux values
|
||||
assert_eq!(db.get_aux(&[1]).unwrap(), None);
|
||||
assert_eq!(db.get_aux(&[2]).unwrap(), Some(vec![102]));
|
||||
assert_eq!(db.get_aux(&[3]).unwrap(), Some(vec![103]));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user