Skip to content

Commit

Permalink
fix load from db: avoid trying to insert and checking
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini authored and gostkin committed Sep 29, 2023
1 parent 594c525 commit e491fbb
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions multiverse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ where
let formatted_ir = entry?;
let ir = deps::serde_json::from_slice(&formatted_ir)?;

multiverse.insert(ir)?;
multiverse.insert_in_memory(ir)?;
}

Ok(multiverse)
Expand Down Expand Up @@ -336,11 +336,27 @@ where
)
)]
pub fn insert(&mut self, variant: V) -> Result<(), MultiverseError> {
if !self.db_insert(variant.block_number(), variant.id(), &variant)? {
tracing::debug!(counter = %variant.block_number(), key = ?variant.id(), "half backed insert");
return Ok(());
if !self.db_insert(variant.block_number(), dbg!(variant.id()), &variant)? {
if self.all.contains_key(&EntryRef::new(variant.id().clone())) {
return Ok(());
} else {
tracing::debug!(counter = %variant.block_number(), key = ?variant.id(), "half backed insert");
}
}

self.insert_in_memory(variant)
}

#[tracing::instrument(skip(self, variant)
level = "debug",
err,
fields(
block.id = ?variant.id(),
block.parent_id = ?variant.parent_id(),
block.block_number = %variant.block_number(),
)
)]
fn insert_in_memory(&mut self, variant: V) -> Result<(), MultiverseError> {
let entry_ref = EntryRef::new(variant.id().clone());
let parent = EntryRef::new(variant.parent_id().clone());

Expand Down Expand Up @@ -803,6 +819,28 @@ mod tests {
}
}

#[test]
fn entries_are_loaded_in_main_when_restoring() {
let db = sled::Config::new().temporary(true).open().unwrap();

let blockchain = declare_blockchain! { "Root" };

let mut multiverse = Multiverse::new_with(db.clone(), "temporary", BlockNumber::MIN);

for block in blockchain {
multiverse.insert(block).unwrap();
}

std::mem::drop(multiverse);

let multiverse: Multiverse<K, V> =
Multiverse::load_from(db, "temporary", BlockNumber::MIN).unwrap();

multiverse
.get(&K::new("Root"))
.expect("entries were not restored from db");
}

struct Simulation {
multiverse: Multiverse<K, V>,
selection_rule: BestBlockSelectionRule,
Expand Down

0 comments on commit e491fbb

Please sign in to comment.