From b0e6f2b8f71045d3d8377dad01cd76494a022994 Mon Sep 17 00:00:00 2001 From: green Date: Mon, 19 Aug 2024 18:55:06 +0200 Subject: [PATCH 1/5] Fixed the rollback functionality to work with empty gas price database --- crates/fuel-core/src/combined_database.rs | 28 +++++++++------ tests/tests/state_rewind.rs | 42 ++++++++++++++++++++++- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/crates/fuel-core/src/combined_database.rs b/crates/fuel-core/src/combined_database.rs index 2fe51a27464..f639a2e7718 100644 --- a/crates/fuel-core/src/combined_database.rs +++ b/crates/fuel-core/src/combined_database.rs @@ -252,13 +252,15 @@ impl CombinedDatabase { .ok_or(anyhow::anyhow!("off-chain database doesn't have height"))?; let gas_price_chain_height = - self.gas_price().latest_height_from_metadata()?.ok_or( - anyhow::anyhow!("gas-price-chain database doesn't have height"), - )?; + self.gas_price().latest_height_from_metadata()?; + + let gas_price_rollbacked = gas_price_chain_height.is_none() + || gas_price_chain_height.expect("We checked height before") + == target_block_height; if on_chain_height == target_block_height && off_chain_height == target_block_height - && gas_price_chain_height == target_block_height + && gas_price_rollbacked { break; } @@ -277,11 +279,13 @@ impl CombinedDatabase { )); } - if gas_price_chain_height < target_block_height { - return Err(anyhow::anyhow!( - "gas-price-chain database height({gas_price_chain_height}) \ - is less than target height({target_block_height})" - )); + if let Some(gas_price_chain_height) = gas_price_chain_height { + if gas_price_chain_height < target_block_height { + return Err(anyhow::anyhow!( + "gas-price-chain database height({gas_price_chain_height}) \ + is less than target height({target_block_height})" + )); + } } if on_chain_height > target_block_height { @@ -292,8 +296,10 @@ impl CombinedDatabase { self.off_chain().rollback_last_block()?; } - if gas_price_chain_height > target_block_height { - self.gas_price().rollback_last_block()?; + if let Some(gas_price_chain_height) = gas_price_chain_height { + if gas_price_chain_height > target_block_height { + self.gas_price().rollback_last_block()?; + } } } diff --git a/tests/tests/state_rewind.rs b/tests/tests/state_rewind.rs index db507891b27..180338d1f72 100644 --- a/tests/tests/state_rewind.rs +++ b/tests/tests/state_rewind.rs @@ -29,7 +29,10 @@ use rand::{ SeedableRng, }; use std::collections::BTreeSet; -use test_helpers::fuel_core_driver::FuelCoreDriver; +use test_helpers::{ + fuel_core_driver::FuelCoreDriver, + produce_block_with_tx, +}; fn transfer_transaction(min_amount: u64, rng: &mut StdRng) -> Transaction { let mut builder = TransactionBuilder::script(vec![], vec![]); @@ -251,3 +254,40 @@ async fn rollback_chain_to_same_height_1000() -> anyhow::Result<()> { ) .await } + +#[tokio::test(flavor = "multi_thread")] +async fn rollback_to__should_work_with_empty_gas_price_database() -> anyhow::Result<()> { + let mut rng = StdRng::seed_from_u64(1234); + let driver = FuelCoreDriver::spawn_feeless(&[ + "--debug", + "--poa-instant", + "true", + "--state-rewind-duration", + "7d", + ]) + .await?; + + // Given + const TOTAL_BLOCKS: u64 = 500; + for _ in 0..TOTAL_BLOCKS { + produce_block_with_tx(&mut rng, &driver.client).await; + } + let temp_dir = driver.kill().await; + std::fs::remove_dir_all(temp_dir.path().join("gas_price")).unwrap(); + + // When + let args = [ + "_IGNORED_", + "--db-path", + temp_dir.path().to_str().unwrap(), + "--target-block-height", + "1", + ]; + let command = fuel_core_bin::cli::rollback::Command::parse_from(args); + let result = fuel_core_bin::cli::rollback::exec(command).await; + + // Then + let _ = result.expect("Rollback should succeed"); + + Ok(()) +} From bc79c49d6c8929c4da6bcea483823b2557463509 Mon Sep 17 00:00:00 2001 From: green Date: Mon, 19 Aug 2024 18:59:00 +0200 Subject: [PATCH 2/5] Updated CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8234185412..e8d7e23e02e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). #### Breaking - [2051](https://github.com/FuelLabs/fuel-core/pull/2051): Misdocumented `CONSENSUS_KEY` environ variable has been removed, use `CONSENSUS_KEY_SECRET` instead. Also raises MSRV to `1.79.0`. +### Fixed + +- [2105](https://github.com/FuelLabs/fuel-core/pull/2105): Fixed the rollback functionality to work with empty gas price database. ## [Version 0.33.0] From 22b0675e317e009a09a39865671f4c4761ff59ef Mon Sep 17 00:00:00 2001 From: green Date: Mon, 19 Aug 2024 19:26:04 +0200 Subject: [PATCH 3/5] Fix CI --- tests/tests/state_rewind.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests/state_rewind.rs b/tests/tests/state_rewind.rs index 180338d1f72..2770132f56d 100644 --- a/tests/tests/state_rewind.rs +++ b/tests/tests/state_rewind.rs @@ -287,7 +287,7 @@ async fn rollback_to__should_work_with_empty_gas_price_database() -> anyhow::Res let result = fuel_core_bin::cli::rollback::exec(command).await; // Then - let _ = result.expect("Rollback should succeed"); + result.expect("Rollback should succeed"); Ok(()) } From 561db962031cd296368bb706baceb7c58b979ade Mon Sep 17 00:00:00 2001 From: green Date: Tue, 20 Aug 2024 10:34:10 +0200 Subject: [PATCH 4/5] Apply suggestion from the PR --- crates/fuel-core/src/combined_database.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fuel-core/src/combined_database.rs b/crates/fuel-core/src/combined_database.rs index f639a2e7718..9659102f988 100644 --- a/crates/fuel-core/src/combined_database.rs +++ b/crates/fuel-core/src/combined_database.rs @@ -254,13 +254,13 @@ impl CombinedDatabase { let gas_price_chain_height = self.gas_price().latest_height_from_metadata()?; - let gas_price_rollbacked = gas_price_chain_height.is_none() + let gas_price_roll_backed = gas_price_chain_height.is_none() || gas_price_chain_height.expect("We checked height before") == target_block_height; if on_chain_height == target_block_height && off_chain_height == target_block_height - && gas_price_rollbacked + && gas_price_roll_backed { break; } From 546c56ecf126c8f148ee446153a79688ed78ce06 Mon Sep 17 00:00:00 2001 From: green Date: Tue, 20 Aug 2024 12:12:04 +0200 Subject: [PATCH 5/5] Apply suggestion from the PR --- crates/fuel-core/src/combined_database.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fuel-core/src/combined_database.rs b/crates/fuel-core/src/combined_database.rs index 9659102f988..1cc42680bee 100644 --- a/crates/fuel-core/src/combined_database.rs +++ b/crates/fuel-core/src/combined_database.rs @@ -254,13 +254,13 @@ impl CombinedDatabase { let gas_price_chain_height = self.gas_price().latest_height_from_metadata()?; - let gas_price_roll_backed = gas_price_chain_height.is_none() + let gas_price_rolled_back = gas_price_chain_height.is_none() || gas_price_chain_height.expect("We checked height before") == target_block_height; if on_chain_height == target_block_height && off_chain_height == target_block_height - && gas_price_roll_backed + && gas_price_rolled_back { break; }