Skip to content

Commit

Permalink
rocksdb stop writing block number in tables
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospb19-cw committed May 28, 2024
1 parent 3dafe22 commit a028093
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 42 deletions.
30 changes: 1 addition & 29 deletions src/eth/storage/rocks/rocks_cf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ where
self.db.put_cf(&cf, serialized_key, serialized_value).unwrap();
}

pub fn prepare_batch_insertion(&self, changes: Vec<(K, V)>, current_block: Option<u64>, batch: &mut WriteBatch) {
pub fn prepare_batch_insertion(&self, changes: Vec<(K, V)>, batch: &mut WriteBatch) {
let cf = self.db.cf_handle(&self.column_family).unwrap();

for (key, value) in changes {
Expand All @@ -126,34 +126,6 @@ where
// Add each serialized key-value pair to the batch
batch.put_cf(&cf, serialized_key, serialized_value);
}

if let Some(current_block) = current_block {
let serialized_block_key = bincode::serialize(&"current_block").unwrap();
let serialized_block_value = bincode::serialize(&current_block).unwrap();
batch.put_cf(&cf, serialized_block_key, serialized_block_value);
}
}

/// inserts data but keep a block as key pointing to the keys inserted in a given block
/// this makes for faster search based on block_number, ergo index
pub fn prepare_batch_insertion_indexed(&self, changes: Vec<(K, V)>, current_block: u64, batch: &mut WriteBatch) {
let cf = self.db.cf_handle(&self.column_family).unwrap();

let mut keys = vec![];

for (key, value) in changes {
let serialized_key = bincode::serialize(&key).unwrap();
let serialized_value = bincode::serialize(&value).unwrap();

keys.push(key);

// Add each serialized key-value pair to the batch
batch.put_cf(&cf, serialized_key, serialized_value);
}

let serialized_block_value = bincode::serialize(&current_block).unwrap();
let serialized_keys = bincode::serialize(&keys).unwrap();
batch.put_cf(&cf, serialized_block_value, serialized_keys);
}

// Deletes an entry from the database by key
Expand Down
10 changes: 4 additions & 6 deletions src/eth/storage/rocks/rocks_permanent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::collections::HashMap;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Arc;

use async_trait::async_trait;
use rocksdb::WriteBatch;
Expand Down Expand Up @@ -157,9 +156,8 @@ impl PermanentStorage for RocksPermanentStorage {
let txs_rocks = self.state.transactions.clone();
let logs_rocks = self.state.logs.clone();

txs_rocks.prepare_batch_insertion_indexed(txs_batch, number.as_u64(), &mut batch);

logs_rocks.prepare_batch_insertion_indexed(logs_batch, number.as_u64(), &mut batch);
txs_rocks.prepare_batch_insertion(txs_batch, &mut batch);
logs_rocks.prepare_batch_insertion(logs_batch, &mut batch);

let hash = *block.hash();

Expand All @@ -171,9 +169,9 @@ impl PermanentStorage for RocksPermanentStorage {
transaction.execution.changes.retain(|_, change| change.bytecode.clone().is_modified());
}
let hash_clone = hash;
blocks_by_number.prepare_batch_insertion(vec![(number.into(), block_without_changes.into())], Some(number.as_u64()), &mut batch);
blocks_by_number.prepare_batch_insertion(vec![(number.into(), block_without_changes.into())], &mut batch);

blocks_by_hash.prepare_batch_insertion_indexed(vec![(hash_clone.into(), number.into())], number.as_u64(), &mut batch);
blocks_by_hash.prepare_batch_insertion(vec![(hash_clone.into(), number.into())], &mut batch);

self.state.update_state_with_execution_changes(&account_changes, number, &mut batch);

Expand Down
14 changes: 7 additions & 7 deletions src/eth/storage/rocks/rocks_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl RocksStorageState {
.collect::<Vec<_>>();

let mut batch = WriteBatch::default();
self_accounts_clone.prepare_batch_insertion(accounts_temp_vec, Some(block_number.into()), &mut batch);
self_accounts_clone.prepare_batch_insertion(accounts_temp_vec, &mut batch);
self_accounts_clone.db.write(batch).unwrap();

info!("Accounts updated up to block number {}", block_number);
Expand Down Expand Up @@ -335,7 +335,7 @@ impl RocksStorageState {
.collect::<Vec<_>>();

let mut batch = WriteBatch::default();
self_account_slots_clone.prepare_batch_insertion(slots_temp_vec, Some(block_number.into()), &mut batch);
self_account_slots_clone.prepare_batch_insertion(slots_temp_vec, &mut batch);
self_account_slots_clone.db.write(batch).unwrap();
}
});
Expand Down Expand Up @@ -380,8 +380,8 @@ impl RocksStorageState {
}
}

accounts.prepare_batch_insertion(account_changes, Some(block_number.into()), batch);
accounts_history.prepare_batch_insertion_indexed(account_history_changes, block_number.into(), batch);
accounts.prepare_batch_insertion(account_changes, batch);
accounts_history.prepare_batch_insertion(account_history_changes, batch);

let mut slot_changes = Vec::new();
let mut slot_history_changes = Vec::new();
Expand All @@ -396,8 +396,8 @@ impl RocksStorageState {
}
}
}
account_slots.prepare_batch_insertion(slot_changes, Some(block_number.into()), batch);
account_slots_history.prepare_batch_insertion_indexed(slot_history_changes, block_number.into(), batch);
account_slots.prepare_batch_insertion(slot_changes, batch);
account_slots_history.prepare_batch_insertion(slot_history_changes, batch);
}

pub fn read_transaction(&self, tx_hash: &Hash) -> anyhow::Result<Option<TransactionMined>> {
Expand Down Expand Up @@ -546,7 +546,7 @@ impl RocksStorageState {
.collect_vec();

let mut batch = WriteBatch::default();
self.account_slots.prepare_batch_insertion(slot_batch, Some(block_number.into()), &mut batch);
self.account_slots.prepare_batch_insertion(slots, &mut batch);
self.account_slots.db.write(batch).unwrap();
}

Expand Down

0 comments on commit a028093

Please sign in to comment.