mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-22 08:01:06 +00:00
statedb: allow longer state pruning history (#11980)
* introduce DbBackedQueue for the state pruning window Signed-off-by: linning <linningde25@gmail.com> * avoid cloning for next_hash Signed-off-by: linning <linningde25@gmail.com> * add tests Signed-off-by: linning <linningde25@gmail.com> * make clippy happy Signed-off-by: linning <linningde25@gmail.com> * impl have_block by checking block number Signed-off-by: linning <linningde25@gmail.com> * refactor Signed-off-by: linning <linningde25@gmail.com> * fix tests & add test for init db-backed queue Signed-off-by: linning <linningde25@gmail.com> * update comment Signed-off-by: linning <linningde25@gmail.com> * add check for have_state_at Signed-off-by: linning <linningde25@gmail.com> * address comment Signed-off-by: linning <linningde25@gmail.com> * renanme unload_blocks to uncached_blocks Signed-off-by: linning <linningde25@gmail.com> * address comment Signed-off-by: linning <linningde25@gmail.com> * fix syncs_state test Signed-off-by: linning <linningde25@gmail.com> * address comment Signed-off-by: linning <linningde25@gmail.com> * revert change to make_test_db to add test cases Signed-off-by: linning <linningde25@gmail.com> * do not prune unavailable block & add tests Signed-off-by: linning <linningde25@gmail.com> * Update client/state-db/src/lib.rs Signed-off-by: linning <linningde25@gmail.com> Co-authored-by: cheme <emericchevalier.pro@gmail.com> * Update client/state-db/src/pruning.rs Signed-off-by: linning <linningde25@gmail.com> Co-authored-by: cheme <emericchevalier.pro@gmail.com> * address comment Signed-off-by: linning <linningde25@gmail.com> Signed-off-by: linning <linningde25@gmail.com> Co-authored-by: cheme <emericchevalier.pro@gmail.com>
This commit is contained in:
@@ -20,10 +20,16 @@
|
||||
|
||||
use crate::{ChangeSet, CommitSet, DBValue, MetaDb, NodeDb};
|
||||
use sp_core::H256;
|
||||
use std::collections::HashMap;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct TestDb(Arc<RwLock<TestDbInner>>);
|
||||
|
||||
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
||||
pub struct TestDb {
|
||||
struct TestDbInner {
|
||||
pub data: HashMap<H256, DBValue>,
|
||||
pub meta: HashMap<Vec<u8>, DBValue>,
|
||||
}
|
||||
@@ -32,7 +38,7 @@ impl MetaDb for TestDb {
|
||||
type Error = ();
|
||||
|
||||
fn get_meta(&self, key: &[u8]) -> Result<Option<DBValue>, ()> {
|
||||
Ok(self.meta.get(key).cloned())
|
||||
Ok(self.0.read().unwrap().meta.get(key).cloned())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,25 +47,29 @@ impl NodeDb for TestDb {
|
||||
type Key = H256;
|
||||
|
||||
fn get(&self, key: &H256) -> Result<Option<DBValue>, ()> {
|
||||
Ok(self.data.get(key).cloned())
|
||||
Ok(self.0.read().unwrap().data.get(key).cloned())
|
||||
}
|
||||
}
|
||||
|
||||
impl TestDb {
|
||||
pub fn commit(&mut self, commit: &CommitSet<H256>) {
|
||||
self.data.extend(commit.data.inserted.iter().cloned());
|
||||
self.meta.extend(commit.meta.inserted.iter().cloned());
|
||||
self.0.write().unwrap().data.extend(commit.data.inserted.iter().cloned());
|
||||
self.0.write().unwrap().meta.extend(commit.meta.inserted.iter().cloned());
|
||||
for k in commit.data.deleted.iter() {
|
||||
self.data.remove(k);
|
||||
self.0.write().unwrap().data.remove(k);
|
||||
}
|
||||
self.meta.extend(commit.meta.inserted.iter().cloned());
|
||||
self.0.write().unwrap().meta.extend(commit.meta.inserted.iter().cloned());
|
||||
for k in commit.meta.deleted.iter() {
|
||||
self.meta.remove(k);
|
||||
self.0.write().unwrap().meta.remove(k);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn data_eq(&self, other: &TestDb) -> bool {
|
||||
self.data == other.data
|
||||
self.0.read().unwrap().data == other.0.read().unwrap().data
|
||||
}
|
||||
|
||||
pub fn meta_len(&self) -> usize {
|
||||
self.0.read().unwrap().meta.len()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,11 +88,11 @@ pub fn make_commit(inserted: &[u64], deleted: &[u64]) -> CommitSet<H256> {
|
||||
}
|
||||
|
||||
pub fn make_db(inserted: &[u64]) -> TestDb {
|
||||
TestDb {
|
||||
TestDb(Arc::new(RwLock::new(TestDbInner {
|
||||
data: inserted
|
||||
.iter()
|
||||
.map(|v| (H256::from_low_u64_be(*v), H256::from_low_u64_be(*v).as_bytes().to_vec()))
|
||||
.collect(),
|
||||
meta: Default::default(),
|
||||
}
|
||||
})))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user