Skip to content

Commit

Permalink
pr comment
Browse files Browse the repository at this point in the history
  • Loading branch information
segfault-magnet committed Sep 28, 2024
1 parent ad39777 commit 79f8882
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 69 deletions.
8 changes: 3 additions & 5 deletions committer/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use clock::SystemClock;
use eth::{AwsConfig, Eip4844BlobEncoder};
use metrics::{prometheus::Registry, HealthChecker, RegistersMetrics};
use services::{
BlockBundler, BlockBundlerConfig, BlockCommitter, BlockImporterConfig, BlockValidator,
CommitListener, Runner, WalletBalanceTracker,
BlockBundler, BlockBundlerConfig, BlockCommitter, BlockValidator, CommitListener, Runner,
WalletBalanceTracker,
};
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
Expand Down Expand Up @@ -147,9 +147,7 @@ pub fn block_importer(
storage,
fuel,
validator,
BlockImporterConfig {
lookback_window: config.app.bundle.block_height_lookback,
},
config.app.bundle.block_height_lookback,
);

schedule_polling(
Expand Down
2 changes: 1 addition & 1 deletion packages/eth/src/blob_encoding/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Eip4844BlobEncoder;
impl Eip4844BlobEncoder {
#[cfg(feature = "test-helpers")]
pub const FRAGMENT_SIZE: usize =
FIELD_ELEMENTS_PER_BLOB as usize * FIELD_ELEMENT_BYTES as usize;
FIELD_ELEMENTS_PER_BLOB as usize * alloy::eips::eip4844::FIELD_ELEMENT_BYTES as usize;

pub(crate) fn decode(
fragments: impl IntoIterator<Item = Fragment>,
Expand Down
72 changes: 12 additions & 60 deletions packages/services/src/block_importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,14 @@ use tracing::info;

use crate::{validator::Validator, Result, Runner};

#[derive(Debug, Clone, Copy)]
pub struct Config {
pub lookback_window: u32,
}

#[cfg(test)]
impl Default for Config {
fn default() -> Self {
Self {
lookback_window: 1000,
}
}
}

/// The `BlockImporter` is responsible for importing blocks from the Fuel blockchain
/// into local storage. It fetches blocks from the Fuel API, validates them,
/// and stores them if they are not already present.
pub struct BlockImporter<Db, FuelApi, BlockValidator> {
storage: Db,
fuel_api: FuelApi,
block_validator: BlockValidator,
config: Config,
lookback_window: u32,
}

impl<Db, FuelApi, BlockValidator> BlockImporter<Db, FuelApi, BlockValidator> {
Expand All @@ -39,13 +25,13 @@ impl<Db, FuelApi, BlockValidator> BlockImporter<Db, FuelApi, BlockValidator> {
storage: Db,
fuel_api: FuelApi,
block_validator: BlockValidator,
config: Config,
lookback_window: u32,
) -> Self {
Self {
storage,
fuel_api,
block_validator,
config,
lookback_window,
}
}
}
Expand Down Expand Up @@ -166,7 +152,7 @@ where
{
async fn run(&mut self) -> Result<()> {
let chain_height = self.fuel_api.latest_height().await?;
let starting_height = chain_height.saturating_sub(self.config.lookback_window);
let starting_height = chain_height.saturating_sub(self.lookback_window);

for range in self
.storage
Expand Down Expand Up @@ -220,12 +206,7 @@ mod tests {
let fuel_mock = test_utils::mocks::fuel::these_blocks_exist(vec![block.clone()], true);
let block_validator = BlockValidator::new(*secret_key.public_key().hash());

let mut importer = BlockImporter::new(
setup.db(),
fuel_mock,
block_validator,
Config { lookback_window: 0 },
);
let mut importer = BlockImporter::new(setup.db(), fuel_mock, block_validator, 0);

// when
importer.run().await?;
Expand Down Expand Up @@ -258,12 +239,7 @@ mod tests {

let fuel_mock = test_utils::mocks::fuel::these_blocks_exist(vec![block.clone()], true);

let mut importer = BlockImporter::new(
setup.db(),
fuel_mock,
block_validator,
Config { lookback_window: 0 },
);
let mut importer = BlockImporter::new(setup.db(), fuel_mock, block_validator, 0);

// when
let result = importer.run().await;
Expand Down Expand Up @@ -311,8 +287,7 @@ mod tests {
let fuel_mock = test_utils::mocks::fuel::these_blocks_exist(new_blocks.clone(), true);
let block_validator = BlockValidator::new(*secret_key.public_key().hash());

let mut importer =
BlockImporter::new(setup.db(), fuel_mock, block_validator, Config::default());
let mut importer = BlockImporter::new(setup.db(), fuel_mock, block_validator, 1000);

// when
importer.run().await?;
Expand Down Expand Up @@ -353,14 +328,7 @@ mod tests {
let fuel_mock = test_utils::mocks::fuel::these_blocks_exist(new_blocks.clone(), true);
let block_validator = BlockValidator::new(*secret_key.public_key().hash());

let mut importer = BlockImporter::new(
setup.db(),
fuel_mock,
block_validator,
Config {
lookback_window: 5, // Example lookback_window
},
);
let mut importer = BlockImporter::new(setup.db(), fuel_mock, block_validator, 5);

// when
importer.run().await?;
Expand Down Expand Up @@ -399,12 +367,7 @@ mod tests {
let fuel_mock = test_utils::mocks::fuel::these_blocks_exist(fuel_blocks, true);
let block_validator = BlockValidator::new(*secret_key.public_key().hash());

let mut importer = BlockImporter::new(
setup.db(),
fuel_mock,
block_validator,
Config { lookback_window: 0 },
);
let mut importer = BlockImporter::new(setup.db(), fuel_mock, block_validator, 0);

// when
importer.run().await?;
Expand Down Expand Up @@ -435,12 +398,8 @@ mod tests {
let fuel_mock = test_utils::mocks::fuel::these_blocks_exist(blocks_to_import, true);
let block_validator = BlockValidator::new(*secret_key.public_key().hash());

let mut importer = BlockImporter::new(
setup.db(),
fuel_mock,
block_validator,
Config { lookback_window },
);
let mut importer =
BlockImporter::new(setup.db(), fuel_mock, block_validator, lookback_window);

// when
importer.run().await?;
Expand Down Expand Up @@ -512,14 +471,7 @@ mod tests {

let block_validator = BlockValidator::new(*secret_key.public_key().hash());

let mut importer = BlockImporter::new(
setup.db(),
fuel_mock,
block_validator,
Config {
lookback_window: 101,
},
);
let mut importer = BlockImporter::new(setup.db(), fuel_mock, block_validator, 101);

// when
importer.run().await?;
Expand Down
6 changes: 3 additions & 3 deletions packages/services/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use block_bundler::{
BlockBundler, Config as BlockBundlerConfig,
};
pub use block_committer::BlockCommitter;
pub use block_importer::{BlockImporter, Config as BlockImporterConfig};
pub use block_importer::BlockImporter;
pub use commit_listener::CommitListener;
pub use health_reporter::HealthReporter;
pub use state_committer::{Config as StateCommitterConfig, StateCommitter};
Expand Down Expand Up @@ -112,7 +112,7 @@ pub(crate) mod test_utils {
use super::Runner;
use crate::{
block_bundler::bundler::Factory,
block_importer::{self, encode_blocks, Config},
block_importer::{self, encode_blocks},
BlockBundler, BlockBundlerConfig, BlockImporter, BlockValidator, StateCommitter,
StateListener,
};
Expand Down Expand Up @@ -549,7 +549,7 @@ pub(crate) mod test_utils {
let mock = mocks::fuel::these_blocks_exist(fuel_blocks.clone(), false);

(
BlockImporter::new(self.db(), mock, block_validator, Config::default()),
BlockImporter::new(self.db(), mock, block_validator, 1000),
ImportedBlocks {
fuel_blocks,
secret_key,
Expand Down

0 comments on commit 79f8882

Please sign in to comment.