From a330499528ed9fcedfb54e91c3b13d3c7acd924f Mon Sep 17 00:00:00 2001 From: renancloudwalk <53792026+renancloudwalk@users.noreply.github.com> Date: Sat, 23 Mar 2024 19:00:40 -0300 Subject: [PATCH] Rocks db adaptations (#433) * chore: add rocksdb * chore: use bincode to serialize and deserialize data * chore: create backbone for rocksdb * chore: make adaptability --- src/eth/storage/hybrid/rocks_db.rs | 69 +++++++++++++++++++----------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/src/eth/storage/hybrid/rocks_db.rs b/src/eth/storage/hybrid/rocks_db.rs index e1b5773cd..81103b3ce 100644 --- a/src/eth/storage/hybrid/rocks_db.rs +++ b/src/eth/storage/hybrid/rocks_db.rs @@ -1,6 +1,14 @@ +use core::fmt; +use revm::primitives::Address; use rocksdb::{Options, WriteBatch, DB}; use std::sync::Arc; use std::marker::PhantomData; +use rocksdb::{IteratorMode}; +use serde::{Serialize, Deserialize}; +use serde_json; +use anyhow::Result; + +use super::hybrid_state::AccountInfo; // A generic struct that abstracts over key-value pairs stored in RocksDB. pub struct RocksDb { @@ -8,14 +16,6 @@ pub struct RocksDb { _marker: PhantomData<(K, V)>, } -impl RocksDb { - -} -use rocksdb::{IteratorMode}; -use serde::{Serialize, Deserialize}; -use serde_json; -use anyhow::Result; - impl Deserialize<'de> + std::hash::Hash + Eq, V: Serialize + for<'de> Deserialize<'de>> RocksDb { pub fn new(db_path: &str) -> anyhow::Result { let db = Arc::new(DB::open_default(&db_path)?); @@ -36,38 +36,57 @@ impl Deserialize<'de> + std::hash::Hash + Eq, V: Seriali Ok(()) } - pub fn get(&self, key: &K) -> Result> { - let serialized_key = serde_json::to_vec(key)?; - match self.db.get(serialized_key)? { + pub fn get(&self, key: &K) -> Option { + let serialized_key = match serde_json::to_vec(key) { + Ok(serialized_key) => serialized_key, + Err(_) => return None, + }; + let value_bytes = match self.db.get(&serialized_key){ + Ok(Some(value_bytes)) => Some(value_bytes), + Ok(None) => None, + Err(_) => None, + }; + match value_bytes { Some(value_bytes) => { - let value: V = serde_json::from_slice(&value_bytes)?; - Ok(Some(value)) + match serde_json::from_slice(&value_bytes){ + Ok(value) => Some(value), + Err(_) => None, + } }, - None => Ok(None), + None => None, } } // Mimics the 'insert' functionality of a HashMap - pub fn insert(&self, key: &K, value: &V) -> Result> { - let serialized_key = serde_json::to_vec(key)?; - let prev_value = self.get(key)?; - let serialized_value = serde_json::to_vec(value)?; - self.db.put(&serialized_key, &serialized_value)?; - Ok(prev_value) + pub fn insert(&self, key: K, value: V) -> Option { + let serialized_key = serde_json::to_vec(&key).unwrap(); //XXX this is trully a reason for panic, but maybe we can figure a way to serialize + let prev_value = self.get(&key); + let serialized_value = serde_json::to_vec(&value).unwrap(); + self.db.put(&serialized_key, &serialized_value).unwrap(); + prev_value } // Custom method that combines entry and or_insert_with from a HashMap - pub fn entry_or_insert_with(&self, key: K, default: F) -> Result + pub fn entry_or_insert_with(&self, key: K, default: F) -> V where F: FnOnce() -> V, { - match self.get(&key)? { - Some(value) => Ok(value), + match self.get(&key) { + Some(value) => value, None => { let new_value = default(); - self.insert(&key, &new_value)?; - Ok(new_value) + self.insert(key, new_value); + new_value }, } } + +} + +impl fmt::Debug for RocksDb { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("RocksDb") + .field("db", &"Arc") + .finish() + } }