From 6665bc47e76e1088882f60b05ae40d4fe2bd129d Mon Sep 17 00:00:00 2001 From: Aaryamann Challani <43716372+rymnc@users.noreply.github.com> Date: Tue, 4 Feb 2025 21:24:09 +0530 Subject: [PATCH 1/2] chore(runnable_task): remove async_trait usage (#2660) ## Linked Issues/PRs - none ## Description should improve performance. breaking changes for anyone using fuel-core as a dependency, more specifically the `RunnableTask` trait. maybe cc: @FuelLabs/data-systems ? ## Checklist - [x] Breaking changes are clearly marked as such in the PR description and changelog - [x] New behavior is reflected in tests - [x] [The specification](https://github.com/FuelLabs/fuel-specs/) matches the implemented behavior (link update PR if changes are needed) ### Before requesting review - [x] I have reviewed the code myself - [x] I have created follow-up issues caused by this PR and linked them here ### After merging, notify other teams [Add or remove entries as needed] - [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/) - [ ] [Sway compiler](https://github.com/FuelLabs/sway/) - [ ] [Platform documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+) (for out-of-organization contributors, the person merging the PR will do this) - [ ] Someone else? --- .../fuel-core/src/graphql_api/api_service.rs | 1 - .../src/graphql_api/worker_service.rs | 1 - crates/fuel-core/src/service.rs | 1 - .../adapters/consensus_parameters_provider.rs | 1 - .../consensus_module/poa/src/service.rs | 1 - .../services/consensus_module/poa/src/sync.rs | 3 +-- .../gas_price_service/src/v0/service.rs | 2 -- .../src/v1/da_source_service/service.rs | 1 - .../gas_price_service/src/v1/service.rs | 1 - crates/services/p2p/src/service.rs | 11 +++++++--- crates/services/relayer/src/service.rs | 1 - .../services/shared-sequencer/src/service.rs | 1 - crates/services/src/service.rs | 22 ++++++++----------- crates/services/sync/src/service.rs | 2 -- crates/services/txpool_v2/src/service.rs | 1 - 15 files changed, 18 insertions(+), 32 deletions(-) diff --git a/crates/fuel-core/src/graphql_api/api_service.rs b/crates/fuel-core/src/graphql_api/api_service.rs index aaac2e43648..f8baa21e58e 100644 --- a/crates/fuel-core/src/graphql_api/api_service.rs +++ b/crates/fuel-core/src/graphql_api/api_service.rs @@ -193,7 +193,6 @@ impl RunnableService for GraphqlService { } } -#[async_trait::async_trait] impl RunnableTask for Task { async fn run(&mut self, _: &mut StateWatcher) -> TaskNextAction { match self.server.as_mut().await { diff --git a/crates/fuel-core/src/graphql_api/worker_service.rs b/crates/fuel-core/src/graphql_api/worker_service.rs index e7b9f666eaa..4f8355b6db9 100644 --- a/crates/fuel-core/src/graphql_api/worker_service.rs +++ b/crates/fuel-core/src/graphql_api/worker_service.rs @@ -669,7 +669,6 @@ where Ok(()) } -#[async_trait::async_trait] impl RunnableTask for Task where TxPool: ports::worker::TxPool, diff --git a/crates/fuel-core/src/service.rs b/crates/fuel-core/src/service.rs index 628a3a9d6e4..c335f28da43 100644 --- a/crates/fuel-core/src/service.rs +++ b/crates/fuel-core/src/service.rs @@ -428,7 +428,6 @@ impl RunnableService for Task { } } -#[async_trait::async_trait] impl RunnableTask for Task { #[tracing::instrument(skip_all)] async fn run(&mut self, watcher: &mut StateWatcher) -> TaskNextAction { diff --git a/crates/fuel-core/src/service/adapters/consensus_parameters_provider.rs b/crates/fuel-core/src/service/adapters/consensus_parameters_provider.rs index 5da5b88e10b..1553de47f64 100644 --- a/crates/fuel-core/src/service/adapters/consensus_parameters_provider.rs +++ b/crates/fuel-core/src/service/adapters/consensus_parameters_provider.rs @@ -110,7 +110,6 @@ impl SharedState { } } -#[async_trait::async_trait] impl RunnableTask for Task { async fn run(&mut self, watcher: &mut StateWatcher) -> TaskNextAction { tokio::select! { diff --git a/crates/services/consensus_module/poa/src/service.rs b/crates/services/consensus_module/poa/src/service.rs index abd916d0b92..de7452a0b2a 100644 --- a/crates/services/consensus_module/poa/src/service.rs +++ b/crates/services/consensus_module/poa/src/service.rs @@ -506,7 +506,6 @@ where } } -#[async_trait::async_trait] impl RunnableTask for MainTask where T: TransactionPool, diff --git a/crates/services/consensus_module/poa/src/sync.rs b/crates/services/consensus_module/poa/src/sync.rs index febad5b7c0d..8cf1204a35a 100644 --- a/crates/services/consensus_module/poa/src/sync.rs +++ b/crates/services/consensus_module/poa/src/sync.rs @@ -136,7 +136,6 @@ impl RunnableService for SyncTask { } } -#[async_trait::async_trait] impl RunnableTask for SyncTask { async fn run(&mut self, watcher: &mut StateWatcher) -> TaskNextAction { let tick: BoxFuture = if let Some(timer) = &mut self.timer { @@ -329,7 +328,7 @@ mod tests { } } - /// Helper function that creates a `SyncTask` with a given configuration + /// Helper function that creates a `SyncTask` with a given configuration fn configure_sync_task( min_connected_reserved_peers: usize, connections_stream: impl IntoIterator, diff --git a/crates/services/gas_price_service/src/v0/service.rs b/crates/services/gas_price_service/src/v0/service.rs index beb07f764bf..9227b7b9661 100644 --- a/crates/services/gas_price_service/src/v0/service.rs +++ b/crates/services/gas_price_service/src/v0/service.rs @@ -11,7 +11,6 @@ use crate::{ v0::algorithm::SharedV0Algorithm, }; use anyhow::anyhow; -use async_trait::async_trait; use fuel_core_services::{ RunnableTask, StateWatcher, @@ -137,7 +136,6 @@ where Ok(()) } } -#[async_trait] impl RunnableTask for GasPriceServiceV0 where L2: L2BlockSource, diff --git a/crates/services/gas_price_service/src/v1/da_source_service/service.rs b/crates/services/gas_price_service/src/v1/da_source_service/service.rs index e2e2ff3785c..b43a579eaa5 100644 --- a/crates/services/gas_price_service/src/v1/da_source_service/service.rs +++ b/crates/services/gas_price_service/src/v1/da_source_service/service.rs @@ -181,7 +181,6 @@ where } } -#[async_trait::async_trait] impl RunnableTask for DaSourceService where Source: DaBlockCostsSource, diff --git a/crates/services/gas_price_service/src/v1/service.rs b/crates/services/gas_price_service/src/v1/service.rs index 4353fa9a785..412d36e3e68 100644 --- a/crates/services/gas_price_service/src/v1/service.rs +++ b/crates/services/gas_price_service/src/v1/service.rs @@ -406,7 +406,6 @@ where } } -#[async_trait] impl RunnableTask for GasPriceServiceV1 where L2: L2BlockSource, diff --git a/crates/services/p2p/src/service.rs b/crates/services/p2p/src/service.rs index 87047994c83..bfd5f187e30 100644 --- a/crates/services/p2p/src/service.rs +++ b/crates/services/p2p/src/service.rs @@ -869,7 +869,6 @@ where } // TODO: Add tests https://github.com/FuelLabs/fuel-core/issues/1275 -#[async_trait::async_trait] impl RunnableTask for Task where P: TaskP2PService + 'static, @@ -1019,8 +1018,14 @@ where tracing::error!("Failed to perform peer heartbeat reputation checks: {:?}", e); } } - self.next_check_time += self.heartbeat_check_interval; - TaskNextAction::Continue + + if let Some(next_check_time) = self.next_check_time.checked_add(self.heartbeat_check_interval) { + self.next_check_time = next_check_time; + TaskNextAction::Continue + } else { + tracing::error!("Next check time overflowed"); + TaskNextAction::Stop + } } } } diff --git a/crates/services/relayer/src/service.rs b/crates/services/relayer/src/service.rs index 4b8e56c0154..83fb3cfc4dd 100644 --- a/crates/services/relayer/src/service.rs +++ b/crates/services/relayer/src/service.rs @@ -213,7 +213,6 @@ where } } -#[async_trait] impl RunnableTask for Task where P: Middleware + 'static, diff --git a/crates/services/shared-sequencer/src/service.rs b/crates/services/shared-sequencer/src/service.rs index 7b632f35e5b..49ad5dcea3e 100644 --- a/crates/services/shared-sequencer/src/service.rs +++ b/crates/services/shared-sequencer/src/service.rs @@ -169,7 +169,6 @@ where } } -#[async_trait] impl RunnableTask for Task where S: Signer + 'static, diff --git a/crates/services/src/service.rs b/crates/services/src/service.rs index 6040ca35762..2421f063c8b 100644 --- a/crates/services/src/service.rs +++ b/crates/services/src/service.rs @@ -123,7 +123,6 @@ impl From> for TaskNextAction { /// The trait is implemented by the service task and contains a single iteration of the infinity /// loop. -#[async_trait::async_trait] pub trait RunnableTask: Send { /// This function should contain the main business logic of the service task. It will run until /// the service either returns false, panics or a stop signal is received. @@ -134,10 +133,13 @@ pub trait RunnableTask: Send { /// `State::Started`. So first, the `run` method should return a value, and after, the service /// will stop. If the service should react to the state change earlier, it should handle it in /// the `run` loop on its own. See [`StateWatcher::while_started`]. - async fn run(&mut self, watcher: &mut StateWatcher) -> TaskNextAction; + fn run( + &mut self, + watcher: &mut StateWatcher, + ) -> impl core::future::Future + Send; /// Gracefully shutdowns the task after the end of the execution cycle. - async fn shutdown(self) -> anyhow::Result<()>; + fn shutdown(self) -> impl core::future::Future> + Send; } /// The service runner manages the lifecycle, execution and error handling of a `RunnableService`. @@ -458,7 +460,6 @@ fn panic_to_string(e: Box) -> String { #[cfg(test)] mod tests { use super::*; - use futures::future::BoxFuture; mockall::mock! { Service {} @@ -480,16 +481,11 @@ mod tests { mockall::mock! { Task {} - #[async_trait::async_trait] impl RunnableTask for Task { - fn run<'_self, '_state, 'a>( - &'_self mut self, - state: &'_state mut StateWatcher - ) -> BoxFuture<'a, TaskNextAction> - where - '_self: 'a, - '_state: 'a, - Self: Sync + 'a; + fn run( + &mut self, + state: &mut StateWatcher + ) -> impl core::future::Future + Send; async fn shutdown(self) -> anyhow::Result<()>; } diff --git a/crates/services/sync/src/service.rs b/crates/services/sync/src/service.rs index 5d1f4950943..0e90cd00901 100644 --- a/crates/services/sync/src/service.rs +++ b/crates/services/sync/src/service.rs @@ -112,7 +112,6 @@ where } } -#[async_trait::async_trait] impl RunnableTask for SyncTask where P: PeerToPeerPort + Send + Sync + 'static, @@ -171,7 +170,6 @@ where } } -#[async_trait::async_trait] impl RunnableTask for ImportTask where P: PeerToPeerPort + Send + Sync + 'static, diff --git a/crates/services/txpool_v2/src/service.rs b/crates/services/txpool_v2/src/service.rs index 5ae046c757e..cf8a2ec8eb9 100644 --- a/crates/services/txpool_v2/src/service.rs +++ b/crates/services/txpool_v2/src/service.rs @@ -228,7 +228,6 @@ where } } -#[async_trait::async_trait] impl RunnableTask for Task where View: TxPoolPersistentStorage, From a7e20df567cc523b8a1bde08ec6e776a1dfff348 Mon Sep 17 00:00:00 2001 From: Mitchell Turner Date: Wed, 5 Feb 2025 00:00:29 -0700 Subject: [PATCH 2/2] Expose gas price service test helpers (#2668) ## Linked Issues/PRs Needed for https://github.com/FuelLabs/fuel-core/pull/2538/ ## Description These need to be exposed so we can move the simulation to the fuel-core-utils ## Checklist - [ ] Breaking changes are clearly marked as such in the PR description and changelog --- CHANGELOG.md | 3 +++ crates/services/gas_price_service/src/v1/service.rs | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8b1d3f141d..e106e4502c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added +- [2668](https://github.com/FuelLabs/fuel-core/pull/2668): Expose gas price service test helpers + ## [Version 0.41.5] ### Changed diff --git a/crates/services/gas_price_service/src/v1/service.rs b/crates/services/gas_price_service/src/v1/service.rs index 412d36e3e68..d9fc0c7cf89 100644 --- a/crates/services/gas_price_service/src/v1/service.rs +++ b/crates/services/gas_price_service/src/v1/service.rs @@ -152,12 +152,12 @@ where } } - #[cfg(test)] + #[cfg(any(test, feature = "test-helpers"))] pub fn latest_l2_block(&self) -> &AtomicU32 { &self.latest_l2_block } - #[cfg(test)] + #[cfg(any(test, feature = "test-helpers"))] pub fn initial_recorded_height(&self) -> Option { self.initial_recorded_height } @@ -236,7 +236,7 @@ where self.shared_algo.clone() } - #[cfg(test)] + #[cfg(any(test, feature = "test-helpers"))] pub fn storage_tx_provider(&self) -> &AtomicStorage { &self.storage_tx_provider }