Skip to content

Commit 6593e9e

Browse files
authored
fix(compression_service): prevent syncing from genesis if compression db is empty (#2978)
improving error handling, and refining synchronization logic. Key changes include adding an override for the starting block height, improving synchronization methods, and updating error reporting. ### Enhancements to Configurability: * Added an `override_starting_height` field to `DaCompressionConfig` and `CompressionConfig`, allowing the service to start syncing from a specific block height. (`crates/fuel-core/src/service/config.rs` [[1]](diffhunk://#diff-184f5e785485e92ad26d50d0bbca115a93cd4e76d7d0bafb7052225eb31f0245R366) `crates/services/compression/src/config.rs` [[2]](diffhunk://#diff-5d0b8937548df3be41160e9752c4d383b66f498014e123054aa2ec486b93967aL1-R23) [[3]](diffhunk://#diff-5d0b8937548df3be41160e9752c4d383b66f498014e123054aa2ec486b93967aR37-R41) ### Improvements to Synchronization Logic: * Updated the `sync_previously_produced_blocks` method to support early exit and handle the `override_starting_height` configuration. (`crates/services/compression/src/service.rs` [crates/services/compression/src/service.rsL224-R259](diffhunk://#diff-9fc61045febb6284fa8e01c5c33172787228fe21b8b813c62c8f03d2fec55fe8L224-R259)) * Modified the `await_synced` method to `await_synced_until`, enabling synchronization up to a specified block height. (`crates/fuel-core/src/service.rs` [[1]](diffhunk://#diff-57d6c5d4dadbd1198f75f2070852e4c27a251e756ed40fb6c214b4d78e4a1ecbL232-R242) `crates/services/compression/src/service.rs` [[2]](diffhunk://#diff-9fc61045febb6284fa8e01c5c33172787228fe21b8b813c62c8f03d2fec55fe8L274-R306) ### Error Handling Enhancements: * Updated `get_block` methods in various components to return `anyhow::Result` instead of `Option`, providing more detailed error information. (`crates/fuel-core/src/service/adapters/compression_adapters.rs` [[1]](diffhunk://#diff-9e66cb09255fa99a40c07b24f3002b040d023c09b9469521b2c8ad8736bf33bfL63-R77) `crates/services/compression/src/ports/block_source.rs` [[2]](diffhunk://#diff-b35ece6e136e2fe3793da56f4456d50692646e9d371946e26024ada643151182L76-R76) `crates/services/compression/src/service.rs` [[3]](diffhunk://#diff-9fc61045febb6284fa8e01c5c33172787228fe21b8b813c62c8f03d2fec55fe8L247-R278) ### Test Suite Updates: * Added new test cases to validate the behavior of the `override_starting_height` configuration and ensure proper synchronization logic. (`crates/services/compression/src/service.rs` [[1]](diffhunk://#diff-9fc61045febb6284fa8e01c5c33172787228fe21b8b813c62c8f03d2fec55fe8L607-R665) [[2]](diffhunk://#diff-9fc61045febb6284fa8e01c5c33172787228fe21b8b813c62c8f03d2fec55fe8L638-R739) ### Miscellaneous Changes: * Improved error messages across the codebase to include more context, such as the specific block height or initialization errors. (`crates/services/src/service.rs` [[1]](diffhunk://#diff-daa67623825920672e831440a58a430d60cd713481c0e3b956292030162f569cL405-R405) `crates/services/compression/src/service.rs` [[2]](diffhunk://#diff-9fc61045febb6284fa8e01c5c33172787228fe21b8b813c62c8f03d2fec55fe8L247-R278)
1 parent 6dc7e90 commit 6593e9e

File tree

12 files changed

+344
-53
lines changed

12 files changed

+344
-53
lines changed

bin/fuel-core/src/cli/run.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ use rlimit::{
8282
use std::{
8383
env,
8484
net,
85-
num::NonZeroU64,
85+
num::{
86+
NonZeroU32,
87+
NonZeroU64,
88+
},
8689
path::PathBuf,
8790
str::FromStr,
8891
time::Duration,
@@ -238,6 +241,10 @@ pub struct Command {
238241
#[arg(long = "da-compression", env)]
239242
pub da_compression: Option<humantime::Duration>,
240243

244+
/// The starting height of the compression service.
245+
#[arg(long = "da-compression-starting-height", env)]
246+
pub da_compression_starting_height: Option<NonZeroU32>,
247+
241248
/// A new block is produced instantly when transactions are available.
242249
#[clap(flatten)]
243250
pub poa_trigger: PoATriggerArgs,
@@ -341,6 +348,7 @@ impl Command {
341348
#[cfg(feature = "aws-kms")]
342349
consensus_aws_kms,
343350
da_compression,
351+
da_compression_starting_height,
344352
poa_trigger,
345353
predefined_blocks_path,
346354
coinbase_recipient,
@@ -551,6 +559,7 @@ impl Command {
551559
Some(retention_duration) => DaCompressionMode::Enabled(
552560
fuel_core::service::config::DaCompressionConfig {
553561
retention_duration: retention_duration.into(),
562+
starting_height: da_compression_starting_height,
554563
metrics: metrics.is_enabled(Module::Compression),
555564
},
556565
),

bin/fuel-core/src/cli/run/p2p.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ impl KeypairArg {
237237
return Ok(KeypairArg::InlineSecret(secret))
238238
}
239239
let path = PathBuf::from_str(s);
240+
#[allow(irrefutable_let_patterns)]
240241
if let Ok(pathbuf) = path {
241242
if pathbuf.exists() {
242243
return Ok(KeypairArg::Path(pathbuf))

crates/fuel-core/src/service.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ use fuel_core_storage::{
4040
IsNotFound,
4141
StorageAsMut,
4242
};
43-
use fuel_core_types::blockchain::consensus::Consensus;
43+
use fuel_core_types::{
44+
blockchain::consensus::Consensus,
45+
fuel_types::BlockHeight,
46+
};
4447

4548
use crate::{
4649
combined_database::{
@@ -229,10 +232,14 @@ impl FuelService {
229232
Ok(())
230233
}
231234

232-
/// Wait for the compression service to be in sync with L2 height
233-
pub async fn await_compression_synced(&self) -> anyhow::Result<()> {
235+
/// Waits until the compression service has synced
236+
/// with the given block height
237+
pub async fn await_compression_synced_until(
238+
&self,
239+
block_height: &BlockHeight,
240+
) -> anyhow::Result<()> {
234241
if let Some(sync_observer) = &self.runner.shared.compression {
235-
sync_observer.await_synced().await?;
242+
sync_observer.await_synced_until(block_height).await?;
236243
}
237244
Ok(())
238245
}

crates/fuel-core/src/service/adapters/compression_adapters.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,21 @@ impl block_source::BlockSource for CompressionBlockImporterAdapter {
6060
self.block_importer.events_shared_result()
6161
}
6262

63-
fn get_block(&self, height: BlockAt) -> Option<SharedImportResult> {
63+
fn get_block(&self, height: BlockAt) -> anyhow::Result<SharedImportResult> {
6464
self.import_result_provider_adapter
6565
.result_at_height(height.into())
66-
.ok()
6766
}
6867
}
6968

7069
impl configuration::CompressionConfigProvider
7170
for crate::service::config::DaCompressionConfig
7271
{
7372
fn config(&self) -> config::CompressionConfig {
74-
config::CompressionConfig::new(self.retention_duration, self.metrics)
73+
config::CompressionConfig::new(
74+
self.retention_duration,
75+
self.starting_height,
76+
self.metrics,
77+
)
7578
}
7679
}
7780

crates/fuel-core/src/service/config.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use clap::ValueEnum;
22
use std::{
3-
num::NonZeroU64,
3+
num::{
4+
NonZeroU32,
5+
NonZeroU64,
6+
},
47
path::PathBuf,
58
time::Duration,
69
};
@@ -360,6 +363,7 @@ impl GasPriceConfig {
360363
pub struct DaCompressionConfig {
361364
pub retention_duration: Duration,
362365
pub metrics: bool,
366+
pub starting_height: Option<NonZeroU32>,
363367
}
364368

365369
#[derive(Debug, Clone)]

crates/services/compression/src/config.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
use std::time::Duration;
1+
use std::{
2+
num::NonZeroU32,
3+
time::Duration,
4+
};
25

36
/// Compression configuration
47
#[derive(Debug, Clone, Copy)]
58
pub struct CompressionConfig {
69
temporal_registry_retention: Duration,
10+
starting_height: Option<NonZeroU32>,
711
metrics: bool,
812
}
913

1014
impl CompressionConfig {
1115
/// Create a new compression configuration
12-
pub fn new(temporal_registry_retention: Duration, metrics: bool) -> Self {
16+
pub fn new(
17+
temporal_registry_retention: Duration,
18+
starting_height: Option<NonZeroU32>,
19+
metrics: bool,
20+
) -> Self {
1321
Self {
1422
temporal_registry_retention,
23+
starting_height,
1524
metrics,
1625
}
1726
}
@@ -25,6 +34,11 @@ impl CompressionConfig {
2534
pub fn metrics(&self) -> bool {
2635
self.metrics
2736
}
37+
38+
/// Get the override starting height
39+
pub fn starting_height(&self) -> Option<u32> {
40+
self.starting_height.map(|height| height.get())
41+
}
2842
}
2943

3044
impl From<&CompressionConfig> for fuel_core_compression::Config {

crates/services/compression/src/ports/block_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ pub trait BlockSource: Send + Sync {
7373
/// Should provide a stream of blocks with metadata
7474
fn subscribe(&self) -> BlockStream;
7575
/// Should provide the block at a given height
76-
fn get_block(&self, height: BlockAt) -> Option<BlockWithMetadata>;
76+
fn get_block(&self, height: BlockAt) -> anyhow::Result<BlockWithMetadata>;
7777
}

0 commit comments

Comments
 (0)