Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
acerone85 committed Oct 1, 2024
1 parent 4c42133 commit 943f60c
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions crates/fuel-core/src/state/historical_rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ pub struct HistoricalRocksDB<Description> {
/// The [`StateRewindPolicy`] used by the historical rocksdb
state_rewind_policy: StateRewindPolicy,
/// The Description of the database.
db: Arc<RocksDb<Historical<Description>>>,
db: RocksDb<Historical<Description>>,
/// Changes that have been committed in memory, but not to rocksdb
in_memory_changes: Mutex<StorageTransaction<Arc<RocksDb<Historical<Description>>>>>,
in_memory_changes: Mutex<StorageTransaction<RocksDb<Historical<Description>>>>,
/// Indicates whether the [`Column::ModificationsHistory`]
/// is being migrated to [`Column::ModificationsHistoryV2`]
// TODO: Change this to an Atomic variable. (Release for Writing, Acquire for reading)
Expand All @@ -114,16 +114,15 @@ where
db: RocksDb<Historical<Description>>,
state_rewind_policy: StateRewindPolicy,
) -> DatabaseResult<Self> {
let db = Arc::new(db);
let db_ref = db.clone();
let in_memory_changes = Mutex::new(StorageTransaction::transaction(
db.create_snapshot(),
ConflictPolicy::Overwrite,
Changes::default(),
));
Ok(Self {
state_rewind_policy,
db,
in_memory_changes: Mutex::new(StorageTransaction::transaction(
db_ref,
ConflictPolicy::Overwrite,
Changes::default(),
)),
in_memory_changes,
modifications_history_migration_in_progress: Arc::new(AtomicBool::new(true)),
})
}
Expand All @@ -133,18 +132,16 @@ where
capacity: Option<usize>,
state_rewind_policy: StateRewindPolicy,
) -> DatabaseResult<Self> {
let db = Arc::new(RocksDb::<Historical<Description>>::default_open(
path, capacity,
)?);
let db_ref = db.clone();
let db = RocksDb::<Historical<Description>>::default_open(path, capacity)?;
let in_memory_changes = Mutex::new(StorageTransaction::transaction(
db.create_snapshot(),
ConflictPolicy::Overwrite,
Changes::default(),
));
Ok(Self {
state_rewind_policy,
db,
in_memory_changes: Mutex::new(StorageTransaction::transaction(
db_ref,
ConflictPolicy::Overwrite,
Changes::default(),
)),
in_memory_changes,
modifications_history_migration_in_progress: Arc::new(AtomicBool::new(true)),
})
}
Expand Down Expand Up @@ -368,7 +365,7 @@ where
}

fn rollback_block_to(&self, height_to_rollback: u64) -> StorageResult<()> {
let mut storage_transaction = (*self.db).read_transaction();
let mut storage_transaction = self.db.read_transaction();

let modifications_history_migration_in_progress = self
.modifications_history_migration_in_progress
Expand Down Expand Up @@ -408,22 +405,26 @@ where
&self,
height: u64,
) -> StorageResult<()> {
let mut in_memory_changes = self.in_memory_changes.lock();

let mut migration_transaction = StorageTransaction::transaction(
&self.db,
ConflictPolicy::Fail,
ConflictPolicy::Overwrite,
Changes::default(),
);

let v1_changes = migration_transaction
.storage_as_mut::<ModificationsHistoryV1<Description>>()
.take(&height)?;
if let Some(v1_changes) = v1_changes {
migration_transaction
.storage_as_mut::<ModificationsHistoryV2<Description>>()
.insert(&height, &v1_changes)?;
let mut lock_guard = self.in_memory_changes.lock().map_err(|poisoned| {
StorageError::Other(anyhow::anyhow!("Lock is poisoned: {poisoned:?}"))
})?;

let storage = (lock_guard.as_mut())
.storage_as_mut::<ModificationsHistoryV2<Description>>();
storage.insert(height, v1_changes);

self.db
.commit_changes(&migration_transaction.into_changes())?;
Ok(())
} else {
Ok(())
Expand Down Expand Up @@ -635,11 +636,8 @@ where
height: Option<Description::Height>,
changes: Changes,
) -> StorageResult<()> {
let mut storage_transaction = StorageTransaction::transaction(
&*self.db,
ConflictPolicy::Overwrite,
changes,
);
let mut storage_transaction =
StorageTransaction::transaction(&self.db, ConflictPolicy::Overwrite, changes);
if let Some(height) = height {
self.store_modifications_history(&mut storage_transaction, &height)?;
}
Expand Down

0 comments on commit 943f60c

Please sign in to comment.