Initial Commit of Retrospective OTB Verification (#3372)

## Issue Addressed

* #2983 

## Proposed Changes

Basically followed the [instructions laid out here](https://github.com/sigp/lighthouse/issues/2983#issuecomment-1062494947)


Co-authored-by: Paul Hauner <paul@paulhauner.com>
Co-authored-by: ethDreamer <37123614+ethDreamer@users.noreply.github.com>
This commit is contained in:
ethDreamer
2022-07-30 00:22:38 +00:00
parent 6c2d8b2262
commit 034260bd99
12 changed files with 1013 additions and 7 deletions

View File

@@ -208,6 +208,9 @@ pub enum DBColumn {
BeaconRandaoMixes,
#[strum(serialize = "dht")]
DhtEnrs,
/// For Optimistically Imported Merge Transition Blocks
#[strum(serialize = "otb")]
OptimisticTransitionBlock,
}
/// A block from the database, which might have an execution payload or not.

View File

@@ -1,14 +1,17 @@
use super::{Error, ItemStore, KeyValueStore, KeyValueStoreOp};
use crate::{ColumnIter, DBColumn};
use parking_lot::{Mutex, MutexGuard, RwLock};
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::marker::PhantomData;
use types::*;
type DBHashMap = HashMap<Vec<u8>, Vec<u8>>;
type DBKeyMap = HashMap<Vec<u8>, HashSet<Vec<u8>>>;
/// A thread-safe `HashMap` wrapper.
pub struct MemoryStore<E: EthSpec> {
db: RwLock<DBHashMap>,
col_keys: RwLock<DBKeyMap>,
transaction_mutex: Mutex<()>,
_phantom: PhantomData<E>,
}
@@ -18,6 +21,7 @@ impl<E: EthSpec> MemoryStore<E> {
pub fn open() -> Self {
Self {
db: RwLock::new(HashMap::new()),
col_keys: RwLock::new(HashMap::new()),
transaction_mutex: Mutex::new(()),
_phantom: PhantomData,
}
@@ -41,6 +45,11 @@ impl<E: EthSpec> KeyValueStore<E> for MemoryStore<E> {
fn put_bytes(&self, col: &str, key: &[u8], val: &[u8]) -> Result<(), Error> {
let column_key = Self::get_key_for_col(col, key);
self.db.write().insert(column_key, val.to_vec());
self.col_keys
.write()
.entry(col.as_bytes().to_vec())
.or_insert_with(HashSet::new)
.insert(key.to_vec());
Ok(())
}
@@ -63,6 +72,10 @@ impl<E: EthSpec> KeyValueStore<E> for MemoryStore<E> {
fn key_delete(&self, col: &str, key: &[u8]) -> Result<(), Error> {
let column_key = Self::get_key_for_col(col, key);
self.db.write().remove(&column_key);
self.col_keys
.write()
.get_mut(&col.as_bytes().to_vec())
.map(|set| set.remove(key));
Ok(())
}
@@ -81,6 +94,26 @@ impl<E: EthSpec> KeyValueStore<E> for MemoryStore<E> {
Ok(())
}
// pub type ColumnIter<'a> = Box<dyn Iterator<Item = Result<(Hash256, Vec<u8>), Error>> + 'a>;
fn iter_column(&self, column: DBColumn) -> ColumnIter {
let col = column.as_str();
if let Some(keys) = self
.col_keys
.read()
.get(col.as_bytes())
.map(|set| set.iter().cloned().collect::<Vec<_>>())
{
Box::new(keys.into_iter().filter_map(move |key| {
let hash = Hash256::from_slice(&key);
self.get_bytes(col, &key)
.transpose()
.map(|res| res.map(|bytes| (hash, bytes)))
}))
} else {
Box::new(std::iter::empty())
}
}
fn begin_rw_transaction(&self) -> MutexGuard<()> {
self.transaction_mutex.lock()
}