From d4d40be8706ca89ba2bde24ab2d19335865b3a5e Mon Sep 17 00:00:00 2001 From: Daniel Knopik Date: Sat, 17 Sep 2022 15:58:52 +0200 Subject: [PATCH] storable blobs --- beacon_node/store/src/config.rs | 4 ++++ beacon_node/store/src/hot_cold_store.rs | 30 +++++++++++++++++++++++++ beacon_node/store/src/lib.rs | 2 ++ 3 files changed, 36 insertions(+) diff --git a/beacon_node/store/src/config.rs b/beacon_node/store/src/config.rs index 4268ec2e91..debffdca5e 100644 --- a/beacon_node/store/src/config.rs +++ b/beacon_node/store/src/config.rs @@ -7,6 +7,7 @@ use types::{EthSpec, MinimalEthSpec}; pub const PREV_DEFAULT_SLOTS_PER_RESTORE_POINT: u64 = 2048; pub const DEFAULT_SLOTS_PER_RESTORE_POINT: u64 = 8192; pub const DEFAULT_BLOCK_CACHE_SIZE: usize = 5; +pub const DEFAULT_BLOB_CACHE_SIZE: usize = 5; /// Database configuration parameters. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -17,6 +18,8 @@ pub struct StoreConfig { pub slots_per_restore_point_set_explicitly: bool, /// Maximum number of blocks to store in the in-memory block cache. pub block_cache_size: usize, + /// Maximum number of blobs to store in the in-memory block cache. + pub blob_cache_size: usize, /// Whether to compact the database on initialization. pub compact_on_init: bool, /// Whether to compact the database during database pruning. @@ -41,6 +44,7 @@ impl Default for StoreConfig { slots_per_restore_point: MinimalEthSpec::slots_per_historical_root() as u64, slots_per_restore_point_set_explicitly: false, block_cache_size: DEFAULT_BLOCK_CACHE_SIZE, + blob_cache_size: DEFAULT_BLOB_CACHE_SIZE, compact_on_init: false, compact_on_prune: true, } diff --git a/beacon_node/store/src/hot_cold_store.rs b/beacon_node/store/src/hot_cold_store.rs index c4b4a64a05..8240007b66 100644 --- a/beacon_node/store/src/hot_cold_store.rs +++ b/beacon_node/store/src/hot_cold_store.rs @@ -38,6 +38,7 @@ use std::path::Path; use std::sync::Arc; use std::time::Duration; use types::*; +use types::signed_blobs_sidecar::SignedBlobsSidecar; /// On-disk database that stores finalized states efficiently. /// @@ -59,6 +60,8 @@ pub struct HotColdDB, Cold: ItemStore> { /// /// The hot database also contains all blocks. pub hot_db: Hot, + /// LRU cache of deserialized blobs. Updated whenever a blob is loaded. + blob_cache: Mutex>>, /// LRU cache of deserialized blocks. Updated whenever a block is loaded. block_cache: Mutex>>, /// Chain spec. @@ -128,6 +131,7 @@ impl HotColdDB, MemoryStore> { cold_db: MemoryStore::open(), hot_db: MemoryStore::open(), block_cache: Mutex::new(LruCache::new(config.block_cache_size)), + blob_cache: Mutex::new(LruCache::new(config.blob_cache_size)), config, spec, log, @@ -161,6 +165,7 @@ impl HotColdDB, LevelDB> { cold_db: LevelDB::open(cold_path)?, hot_db: LevelDB::open(hot_path)?, block_cache: Mutex::new(LruCache::new(config.block_cache_size)), + blob_cache: Mutex::new(LruCache::new(config.blob_cache_size)), config, spec, log, @@ -453,6 +458,31 @@ impl, Cold: ItemStore> HotColdDB .key_delete(DBColumn::ExecPayload.into(), block_root.as_bytes()) } + pub fn put_blobs(&self, + block_root: &Hash256, + blobs: SignedBlobsSidecar, + ) -> Result<(), Error> { + self.hot_db.put_bytes(DBColumn::BeaconBlob.into(), block_root.as_bytes(), &blobs.as_ssz_bytes())?; + self.blob_cache.lock().push(*block_root, blobs); + Ok(()) + } + + pub fn get_blobs(&self, + block_root: &Hash256, + ) -> Result>, Error> { + if let Some(blobs) = self.blob_cache.lock().get(block_root) { + Ok(Some(blobs.clone())) + } else { + if let Some(bytes) = self.hot_db.get_bytes(DBColumn::BeaconBlob.into(), block_root.as_bytes())? { + let ret = SignedBlobsSidecar::from_ssz_bytes(&bytes)?; + self.blob_cache.lock().put(*block_root, ret.clone()); + Ok(Some(ret)) + } else { + Ok(None) + } + } + } + pub fn put_state_summary( &self, state_root: &Hash256, diff --git a/beacon_node/store/src/lib.rs b/beacon_node/store/src/lib.rs index 75aeca058b..f7af172f5a 100644 --- a/beacon_node/store/src/lib.rs +++ b/beacon_node/store/src/lib.rs @@ -171,6 +171,8 @@ pub enum DBColumn { BeaconMeta, #[strum(serialize = "blk")] BeaconBlock, + #[strum(serialize = "blo")] + BeaconBlob, /// For full `BeaconState`s in the hot database (finalized or fork-boundary states). #[strum(serialize = "ste")] BeaconState,