Skip to content

Commit

Permalink
Merge pull request #2 from dcSpark/enzo/multiverse-insert-block-twice
Browse files Browse the repository at this point in the history
add test: multiverse_insert_twice
  • Loading branch information
gostkin authored Oct 4, 2023
2 parents ed6381e + e491fbb commit 33822a3
Showing 1 changed file with 55 additions and 3 deletions.
58 changes: 55 additions & 3 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,10 +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");
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 @@ -789,6 +806,41 @@ mod tests {
assert!(discarded.is_empty());
}

#[test]
fn multiverse_insert_twice() {
let mut m: Multiverse<K, V> = Multiverse::temporary().unwrap();

for _ in 0..2 {
let blockchain = declare_blockchain! { "Root" };

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

#[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 33822a3

Please sign in to comment.