Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 335007c

Browse files
authored
State-db refactoring (#12239)
* Prune discarded blocks immediately * state-db refactoring part 1 * Some renames * Get rid of pending state * Revert "Prune discarded blocks immediately" This reverts commit 790f540. * Cleanup * Make clippy happy * Minor changes
1 parent a1c1286 commit 335007c

File tree

4 files changed

+181
-557
lines changed

4 files changed

+181
-557
lines changed

client/db/src/lib.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1994,15 +1994,15 @@ impl<Block: BlockT> sc_client_api::backend::Backend<Block> for Backend<Block> {
19941994
let usage = operation.old_state.usage_info();
19951995
self.state_usage.merge_sm(usage);
19961996

1997-
match self.try_commit_operation(operation) {
1998-
Ok(_) => {
1999-
self.storage.state_db.apply_pending();
2000-
Ok(())
2001-
},
2002-
e @ Err(_) => {
2003-
self.storage.state_db.revert_pending();
2004-
e
2005-
},
1997+
if let Err(e) = self.try_commit_operation(operation) {
1998+
let state_meta_db = StateMetaDb(self.storage.db.clone());
1999+
self.storage
2000+
.state_db
2001+
.reset(state_meta_db)
2002+
.map_err(sp_blockchain::Error::from_state_db)?;
2003+
Err(e)
2004+
} else {
2005+
Ok(())
20062006
}
20072007
}
20082008

client/state-db/src/lib.rs

+13-44
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ pub struct StateDbSync<BlockHash: Hash, Key: Hash, D: MetaDb> {
291291
non_canonical: NonCanonicalOverlay<BlockHash, Key>,
292292
pruning: Option<RefWindow<BlockHash, Key, D>>,
293293
pinned: HashMap<BlockHash, u32>,
294+
ref_counting: bool,
294295
}
295296

296297
impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf, D: MetaDb>
@@ -311,7 +312,7 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf, D: MetaDb>
311312
PruningMode::ArchiveAll | PruningMode::ArchiveCanonical => None,
312313
};
313314

314-
Ok(StateDbSync { mode, non_canonical, pruning, pinned: Default::default() })
315+
Ok(StateDbSync { mode, non_canonical, pruning, pinned: Default::default(), ref_counting })
315316
}
316317

317318
fn insert_block(
@@ -372,9 +373,9 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf, D: MetaDb>
372373
match self.pruning.as_ref() {
373374
None => IsPruned::NotPruned,
374375
Some(pruning) => match pruning.have_block(hash, number) {
375-
HaveBlock::NotHave => IsPruned::Pruned,
376-
HaveBlock::Have => IsPruned::NotPruned,
377-
HaveBlock::MayHave => IsPruned::MaybePruned,
376+
HaveBlock::No => IsPruned::Pruned,
377+
HaveBlock::Yes => IsPruned::NotPruned,
378+
HaveBlock::Maybe => IsPruned::MaybePruned,
378379
},
379380
}
380381
}
@@ -444,9 +445,9 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf, D: MetaDb>
444445
let have_block = self.non_canonical.have_block(hash) ||
445446
self.pruning.as_ref().map_or(false, |pruning| {
446447
match pruning.have_block(hash, number) {
447-
HaveBlock::NotHave => false,
448-
HaveBlock::Have => true,
449-
HaveBlock::MayHave => hint(),
448+
HaveBlock::No => false,
449+
HaveBlock::Yes => true,
450+
HaveBlock::Maybe => hint(),
450451
}
451452
});
452453
if have_block {
@@ -496,30 +497,6 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf, D: MetaDb>
496497
db.get(key.as_ref()).map_err(Error::Db)
497498
}
498499

499-
fn apply_pending(&mut self) {
500-
self.non_canonical.apply_pending();
501-
if let Some(pruning) = &mut self.pruning {
502-
pruning.apply_pending();
503-
}
504-
let next_hash = self.pruning.as_mut().map(|p| p.next_hash());
505-
trace!(
506-
target: "forks",
507-
"First available: {:?} ({}), Last canon: {:?} ({}), Best forks: {:?}",
508-
next_hash,
509-
self.pruning.as_ref().map(|p| p.pending()).unwrap_or(0),
510-
self.non_canonical.last_canonicalized_hash(),
511-
self.non_canonical.last_canonicalized_block_number().unwrap_or(0),
512-
self.non_canonical.top_level(),
513-
);
514-
}
515-
516-
fn revert_pending(&mut self) {
517-
if let Some(pruning) = &mut self.pruning {
518-
pruning.revert_pending();
519-
}
520-
self.non_canonical.revert_pending();
521-
}
522-
523500
fn memory_info(&self) -> StateDbMemoryInfo {
524501
StateDbMemoryInfo {
525502
non_canonical: MemorySize::from_bytes(malloc_size(&self.non_canonical)),
@@ -654,14 +631,11 @@ impl<BlockHash: Hash + MallocSizeOf, Key: Hash + MallocSizeOf, D: MetaDb>
654631
return self.db.read().is_pruned(hash, number)
655632
}
656633

657-
/// Apply all pending changes
658-
pub fn apply_pending(&self) {
659-
self.db.write().apply_pending();
660-
}
661-
662-
/// Revert all pending changes
663-
pub fn revert_pending(&self) {
664-
self.db.write().revert_pending();
634+
/// Reset in-memory changes to the last disk-backed state.
635+
pub fn reset(&self, db: D) -> Result<(), Error<D::Error>> {
636+
let mut state_db = self.db.write();
637+
*state_db = StateDbSync::new(state_db.mode.clone(), state_db.ref_counting, db)?;
638+
Ok(())
665639
}
666640

667641
/// Returns the current memory statistics of this instance.
@@ -766,9 +740,7 @@ mod tests {
766740
)
767741
.unwrap(),
768742
);
769-
state_db.apply_pending();
770743
db.commit(&state_db.canonicalize_block(&H256::from_low_u64_be(1)).unwrap());
771-
state_db.apply_pending();
772744
db.commit(
773745
&state_db
774746
.insert_block(
@@ -779,11 +751,8 @@ mod tests {
779751
)
780752
.unwrap(),
781753
);
782-
state_db.apply_pending();
783754
db.commit(&state_db.canonicalize_block(&H256::from_low_u64_be(21)).unwrap());
784-
state_db.apply_pending();
785755
db.commit(&state_db.canonicalize_block(&H256::from_low_u64_be(3)).unwrap());
786-
state_db.apply_pending();
787756

788757
(db, state_db)
789758
}

0 commit comments

Comments
 (0)