Skip to content

Commit

Permalink
Tests for migrations in historical rocks db
Browse files Browse the repository at this point in the history
  • Loading branch information
acerone85 committed Sep 25, 2024
1 parent e02db17 commit ac5bb96
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions crates/fuel-core/src/state/historical_rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,124 @@ mod tests {
assert_eq!(entries.len(), 0);
}

#[test]
fn state_rewind_policy__rewind_range_1__rollback_uses_v2() {
// Given
let rocks_db = RocksDb::<Historical<OnChain>>::default_open_temp(None).unwrap();
let historical_rocks_db = HistoricalRocksDB::new(
rocks_db,
StateRewindPolicy::RewindRange {
size: NonZeroU64::new(1).unwrap(),
},
)
.unwrap();

// when
let mut transaction = historical_rocks_db.read_transaction();
transaction
.storage_as_mut::<ContractsAssets>()
.insert(&key(), &123)
.unwrap();
historical_rocks_db
.commit_changes(Some(1u32.into()), transaction.into_changes())
.unwrap();
let v2_entries = historical_rocks_db
.db
.iter_all::<ModificationsHistoryV2<OnChain>>(None)
.collect::<Vec<_>>();
let v1_entries = historical_rocks_db
.db
.iter_all::<ModificationsHistoryV1<OnChain>>(None)
.collect::<Vec<_>>();
// then

assert_eq!(v2_entries.len(), 1);
assert_eq!(v1_entries.len(), 0);
}

#[test]
fn state_rewind_policy__rewind_range_1__rollback_during_migration_works() {
// Given
let rocks_db = RocksDb::<Historical<OnChain>>::default_open_temp(None).unwrap();
let historical_rocks_db = HistoricalRocksDB::new(
rocks_db,
StateRewindPolicy::RewindRange {
size: NonZeroU64::new(1).unwrap(),
},
)
.unwrap();

let mut transaction = historical_rocks_db.read_transaction();
transaction
.storage_as_mut::<ContractsAssets>()
.insert(&key(), &123)
.unwrap();
historical_rocks_db
.commit_changes(Some(1u32.into()), transaction.into_changes())
.unwrap();

// Migrate the changes from V2 to V1.

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

let v2_changes = migration_transaction
.storage_as_mut::<ModificationsHistoryV2<OnChain>>()
.take(&1u64)
.unwrap()
.unwrap();
migration_transaction
.storage_as_mut::<ModificationsHistoryV1<OnChain>>()
.insert(&1u64, &v2_changes)
.unwrap();

let historical_column_changes = migration_transaction.changes().clone();

StorageTransaction::transaction(
&mut migration_transaction,
ConflictPolicy::Overwrite,
historical_column_changes,
)
.commit()
.unwrap();

historical_rocks_db
.db
.commit_changes(&migration_transaction.into_changes())
.unwrap();

// Check that the history has indeed been written to V1
let v2_entries = historical_rocks_db
.db
.iter_all::<ModificationsHistoryV2<OnChain>>(None)
.collect::<Vec<_>>();
let v1_entries = historical_rocks_db
.db
.iter_all::<ModificationsHistoryV1<OnChain>>(None)
.collect::<Vec<_>>();

assert_eq!(v2_entries.len(), 0);
assert_eq!(v1_entries.len(), 1);

let result = historical_rocks_db.rollback_last_block();

// Then
assert_eq!(result, Ok(1));
let v2_entries = historical_rocks_db
.db
.iter_all::<ModificationsHistoryV2<OnChain>>(None)
.collect::<Vec<_>>();
let v1_entries = historical_rocks_db
.db
.iter_all::<ModificationsHistoryV2<OnChain>>(None)
.collect::<Vec<_>>();
assert_eq!(v2_entries.len(), 0);
assert_eq!(v1_entries.len(), 0);
}

#[test]
fn state_rewind_policy__rewind_range_1__second_rollback_fails() {
// Given
Expand Down

0 comments on commit ac5bb96

Please sign in to comment.