mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-20 13:24:44 +00:00
* Implement freezer database for state vectors * Improve BeaconState safe accessors And fix a bug in the compact committees accessor. * Banish dodgy type bounds back to gRPC * Clean up * Switch to exclusive end points in chunked vec * Cleaning up and start of tests * Randao fix, more tests * Fix unsightly hack * Resolve test FIXMEs * Config file support * More clean-ups, migrator beginnings * Finish migrator, integrate into BeaconChain * Fixups * Fix store tests * Fix BeaconChain tests * Fix LMD GHOST tests * Address review comments, delete 'static bounds * Cargo format * Address review comments * Fix LMD ghost tests * Update to spec v0.9.0 * Update to v0.9.1 * Bump spec tags for v0.9.1 * Formatting, fix CI failures * Resolve accidental KeyPair merge conflict * Document new BeaconState functions * Fix incorrect cache drops in `advance_caches` * Update fork choice for v0.9.1 * Clean up some FIXMEs * Fix a few docs/logs * Update for new builder paradigm, spec changes * Freezer DB integration into BeaconNode * Cleaning up * This works, clean it up * Cleanups * Fix and improve store tests * Refine store test * Delete unused beacon_chain_builder.rs * Fix CLI * Store state at split slot in hot database * Make fork choice lookup fast again * Store freezer DB split slot in the database * Handle potential div by 0 in chunked_vector * Exclude committee caches from freezer DB * Remove FIXME about long-running test
138 lines
3.6 KiB
Rust
138 lines
3.6 KiB
Rust
use super::*;
|
|
use crate::impls::beacon_state::{get_full_state, store_full_state};
|
|
use crate::metrics;
|
|
use db_key::Key;
|
|
use leveldb::database::kv::KV;
|
|
use leveldb::database::Database;
|
|
use leveldb::error::Error as LevelDBError;
|
|
use leveldb::options::{Options, ReadOptions, WriteOptions};
|
|
use std::path::Path;
|
|
|
|
/// A wrapped leveldb database.
|
|
pub struct LevelDB {
|
|
db: Database<BytesKey>,
|
|
}
|
|
|
|
impl LevelDB {
|
|
/// Open a database at `path`, creating a new database if one does not already exist.
|
|
pub fn open(path: &Path) -> Result<Self, Error> {
|
|
let mut options = Options::new();
|
|
|
|
options.create_if_missing = true;
|
|
|
|
let db = Database::open(path, options)?;
|
|
|
|
Ok(Self { db })
|
|
}
|
|
|
|
fn read_options(&self) -> ReadOptions<BytesKey> {
|
|
ReadOptions::new()
|
|
}
|
|
|
|
fn write_options(&self) -> WriteOptions {
|
|
WriteOptions::new()
|
|
}
|
|
|
|
fn get_key_for_col(col: &str, key: &[u8]) -> BytesKey {
|
|
let mut col = col.as_bytes().to_vec();
|
|
col.append(&mut key.to_vec());
|
|
BytesKey { key: col }
|
|
}
|
|
}
|
|
|
|
/// Used for keying leveldb.
|
|
pub struct BytesKey {
|
|
key: Vec<u8>,
|
|
}
|
|
|
|
impl Key for BytesKey {
|
|
fn from_u8(key: &[u8]) -> Self {
|
|
Self { key: key.to_vec() }
|
|
}
|
|
|
|
fn as_slice<T, F: Fn(&[u8]) -> T>(&self, f: F) -> T {
|
|
f(self.key.as_slice())
|
|
}
|
|
}
|
|
|
|
impl Store for LevelDB {
|
|
/// Retrieve some bytes in `column` with `key`.
|
|
fn get_bytes(&self, col: &str, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
|
|
let column_key = Self::get_key_for_col(col, key);
|
|
|
|
metrics::inc_counter(&metrics::DISK_DB_READ_COUNT);
|
|
|
|
let result = self
|
|
.db
|
|
.get(self.read_options(), column_key)
|
|
.map_err(Into::into);
|
|
|
|
if let Ok(Some(bytes)) = &result {
|
|
metrics::inc_counter_by(&metrics::DISK_DB_READ_BYTES, bytes.len() as i64)
|
|
}
|
|
|
|
result
|
|
}
|
|
|
|
/// Store some `value` in `column`, indexed with `key`.
|
|
fn put_bytes(&self, col: &str, key: &[u8], val: &[u8]) -> Result<(), Error> {
|
|
let column_key = Self::get_key_for_col(col, key);
|
|
|
|
metrics::inc_counter(&metrics::DISK_DB_WRITE_COUNT);
|
|
metrics::inc_counter_by(&metrics::DISK_DB_WRITE_BYTES, val.len() as i64);
|
|
|
|
self.db
|
|
.put(self.write_options(), column_key, val)
|
|
.map_err(Into::into)
|
|
}
|
|
|
|
/// Return `true` if `key` exists in `column`.
|
|
fn key_exists(&self, col: &str, key: &[u8]) -> Result<bool, Error> {
|
|
let column_key = Self::get_key_for_col(col, key);
|
|
|
|
metrics::inc_counter(&metrics::DISK_DB_EXISTS_COUNT);
|
|
|
|
self.db
|
|
.get(self.read_options(), column_key)
|
|
.map_err(Into::into)
|
|
.and_then(|val| Ok(val.is_some()))
|
|
}
|
|
|
|
/// Removes `key` from `column`.
|
|
fn key_delete(&self, col: &str, key: &[u8]) -> Result<(), Error> {
|
|
let column_key = Self::get_key_for_col(col, key);
|
|
|
|
metrics::inc_counter(&metrics::DISK_DB_DELETE_COUNT);
|
|
|
|
self.db
|
|
.delete(self.write_options(), column_key)
|
|
.map_err(Into::into)
|
|
}
|
|
|
|
/// Store a state in the store.
|
|
fn put_state<E: EthSpec>(
|
|
&self,
|
|
state_root: &Hash256,
|
|
state: &BeaconState<E>,
|
|
) -> Result<(), Error> {
|
|
store_full_state(self, state_root, state)
|
|
}
|
|
|
|
/// Fetch a state from the store.
|
|
fn get_state<E: EthSpec>(
|
|
&self,
|
|
state_root: &Hash256,
|
|
_: Option<Slot>,
|
|
) -> Result<Option<BeaconState<E>>, Error> {
|
|
get_full_state(self, state_root)
|
|
}
|
|
}
|
|
|
|
impl From<LevelDBError> for Error {
|
|
fn from(e: LevelDBError) -> Error {
|
|
Error::DBError {
|
|
message: format!("{:?}", e),
|
|
}
|
|
}
|
|
}
|