Skip to content

Commit 02f11ed

Browse files
authored
chore(async_trait): reduce usage of async_trait for small types again (#2634)
## Linked Issues/PRs <!-- List of related issues/PRs --> - none ## Description <!-- List of detailed changes --> this pr description is AI generated, pls take with a grain of salt This pull request includes several changes to remove asynchronous traits and methods, converting them to synchronous ones across various files. The most important changes include modifying the `GasPriceEstimate` trait and its implementations, updating test functions, and adjusting mock expectations. ### Conversion to Synchronous Methods: * [`crates/fuel-core/src/graphql_api/ports.rs`](diffhunk://#diff-49095c45844c0809b8d244f4555fb1eaac1ee48612c103e553ab396d0dfe93c9L287-R289): Removed the `async_trait` attribute from the `GasPriceEstimate` trait and changed `worst_case_gas_price` to a synchronous method. * [`crates/fuel-core/src/service/adapters.rs`](diffhunk://#diff-c710afba67d6633c23d5f7487e423036b27c9f20ec4a8a1541827b366d6e346bL276-R271): Updated the `GasPriceEstimate` implementations for `UniversalGasPriceProvider` and `StaticGasPrice` to synchronous methods. [[1]](diffhunk://#diff-c710afba67d6633c23d5f7487e423036b27c9f20ec4a8a1541827b366d6e346bL276-R271) [[2]](diffhunk://#diff-28c7fdbc1cb25f3e25156f2a0efdd72cf86a6462fe131be225b15b48d6d3f524L184-R185) * [`crates/services/gas_price_service/src/common/gas_price_algorithm.rs`](diffhunk://#diff-38ab61925731d592d431bf2123324895adde7b539dfdc8de04a97f9efcd6c79dL26-R26): Changed the `update` and `worst_case_gas_price` methods to synchronous. [[1]](diffhunk://#diff-38ab61925731d592d431bf2123324895adde7b539dfdc8de04a97f9efcd6c79dL26-R26) [[2]](diffhunk://#diff-38ab61925731d592d431bf2123324895adde7b539dfdc8de04a97f9efcd6c79dL40-R40) ### Updates to Test Functions: * [`crates/fuel-core/src/service/adapters.rs`](diffhunk://#diff-c710afba67d6633c23d5f7487e423036b27c9f20ec4a8a1541827b366d6e346bL103-R103): Adjusted test functions to remove asynchronous calls and runtime blocks. [[1]](diffhunk://#diff-c710afba67d6633c23d5f7487e423036b27c9f20ec4a8a1541827b366d6e346bL103-R103) [[2]](diffhunk://#diff-c710afba67d6633c23d5f7487e423036b27c9f20ec4a8a1541827b366d6e346bL140-R142) [[3]](diffhunk://#diff-c710afba67d6633c23d5f7487e423036b27c9f20ec4a8a1541827b366d6e346bL158-R160) * [`crates/services/sync/src/import/tests.rs`](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cL39-R39): Updated mock expectations to return pinned async blocks for various test functions. [[1]](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cL39-R39) [[2]](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cL89-R89) [[3]](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cL153-R153) [[4]](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cL210-R210) [[5]](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cL268-R268) [[6]](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cL371-R371) [[7]](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cL477-R477) [[8]](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cR515-R525) [[9]](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cL564-R566) [[10]](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cL759-R761) [[11]](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cL805-R807) [[12]](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cL863-R865) [[13]](diffhunk://#diff-06109df4a98a700afefcb2fbc43df2a20e44469d5e95c437f239f865a8e57d4cL943-R945) ### Removal of `async_trait`: * [`crates/fuel-core/src/service/adapters/sync.rs`](diffhunk://#diff-a58d36e1c68bf12145a84b084168b8b751c6178232ae412ed33bfec68723d894L145): Removed `async_trait` from `BlockImporterPort` and `ConsensusPort` implementations. [[1]](diffhunk://#diff-a58d36e1c68bf12145a84b084168b8b751c6178232ae412ed33bfec68723d894L145) [[2]](diffhunk://#diff-a58d36e1c68bf12145a84b084168b8b751c6178232ae412ed33bfec68723d894L163) * [`crates/services/sync/src/import/test_helpers/pressure_block_importer.rs`](diffhunk://#diff-f648b60371e32529d1a465ff14688f3719d3e9fc88a05ccf6997384cb2f80bcaL17): Removed `async_trait` from `BlockImporterPort` implementation. * [`crates/services/sync/src/import/test_helpers/pressure_consensus.rs`](diffhunk://#diff-d377102e8f2624e710b799cb32de729d17cfde1e1a644596e856c9fb0edfceb2L16): Removed `async_trait` from `ConsensusPort` implementation. These changes collectively simplify the codebase by removing unnecessary asynchronous complexity, making the code more straightforward and potentially improving performance. ## Checklist - [ ] Breaking changes are clearly marked as such in the PR description and changelog - [ ] New behavior is reflected in tests - [ ] [The specification](https://github.com/FuelLabs/fuel-specs/) matches the implemented behavior (link update PR if changes are needed)
1 parent 17ed7d3 commit 02f11ed

File tree

13 files changed

+83
-79
lines changed

13 files changed

+83
-79
lines changed

crates/fuel-core/src/graphql_api/ports.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,9 @@ pub trait P2pPort: Send + Sync {
284284
}
285285

286286
/// Trait for defining how to estimate gas price for future blocks
287-
#[async_trait::async_trait]
288287
pub trait GasPriceEstimate: Send + Sync {
289288
/// The worst case scenario for gas price at a given horizon
290-
async fn worst_case_gas_price(&self, height: BlockHeight) -> Option<u64>;
289+
fn worst_case_gas_price(&self, height: BlockHeight) -> Option<u64>;
291290
}
292291

293292
/// Trait for getting VM memory.

crates/fuel-core/src/schema/gas_price.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ impl EstimateGasPriceQuery {
101101
let gas_price_provider = ctx.data_unchecked::<GasPriceProvider>();
102102
let gas_price = gas_price_provider
103103
.worst_case_gas_price(target_block.into())
104-
.await
105104
.ok_or(async_graphql::Error::new(format!(
106105
"Failed to estimate gas price for block, algorithm not yet set: {target_block:?}"
107106
)))?;

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ mod universal_gas_price_provider_tests {
100100
use super::*;
101101
use proptest::proptest;
102102

103-
async fn _worst_case__correctly_calculates_value(
103+
fn _worst_case__correctly_calculates_value(
104104
gas_price: u64,
105105
starting_height: u32,
106106
block_horizon: u32,
@@ -112,10 +112,7 @@ mod universal_gas_price_provider_tests {
112112

113113
// when
114114
let target_height = starting_height.saturating_add(block_horizon);
115-
let estimated = subject
116-
.worst_case_gas_price(target_height.into())
117-
.await
118-
.unwrap();
115+
let estimated = subject.worst_case_gas_price(target_height.into()).unwrap();
119116

120117
// then
121118
let mut actual = gas_price;
@@ -137,13 +134,12 @@ mod universal_gas_price_provider_tests {
137134
block_horizon in 0..10_000u32,
138135
percentage: u16,
139136
) {
140-
let rt = tokio::runtime::Runtime::new().unwrap();
141-
rt.block_on(_worst_case__correctly_calculates_value(
137+
_worst_case__correctly_calculates_value(
142138
gas_price,
143139
starting_height,
144140
block_horizon,
145141
percentage,
146-
));
142+
);
147143
}
148144
}
149145

@@ -155,15 +151,13 @@ mod universal_gas_price_provider_tests {
155151
block_horizon in 0..10_000u32,
156152
percentage: u16
157153
) {
158-
let rt = tokio::runtime::Runtime::new().unwrap();
159-
160154
// given
161155
let subject = UniversalGasPriceProvider::new(starting_height, gas_price, percentage);
162156

163157
// when
164158
let target_height = starting_height.saturating_add(block_horizon);
165159

166-
let _ = rt.block_on(subject.worst_case_gas_price(target_height.into()));
160+
let _ = subject.worst_case_gas_price(target_height.into());
167161

168162
// then
169163
// doesn't panic with an overflow
@@ -273,9 +267,8 @@ impl TxPoolGasPriceProvider for UniversalGasPriceProvider<u32, u64> {
273267
}
274268
}
275269

276-
#[async_trait::async_trait]
277270
impl GasPriceEstimate for UniversalGasPriceProvider<u32, u64> {
278-
async fn worst_case_gas_price(&self, height: BlockHeight) -> Option<u64> {
271+
fn worst_case_gas_price(&self, height: BlockHeight) -> Option<u64> {
279272
let (best_height, best_gas_price) = self.get_height_and_gas_price();
280273
let percentage = self.percentage;
281274

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,8 @@ impl worker::TxPool for TxPoolAdapter {
181181
}
182182
}
183183

184-
#[async_trait::async_trait]
185184
impl GasPriceEstimate for StaticGasPrice {
186-
async fn worst_case_gas_price(&self, _height: BlockHeight) -> Option<u64> {
185+
fn worst_case_gas_price(&self, _height: BlockHeight) -> Option<u64> {
187186
Some(self.gas_price)
188187
}
189188
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ impl PeerReport for P2PAdapterPeerReport {
142142
}
143143
}
144144

145-
#[async_trait::async_trait]
146145
impl BlockImporterPort for BlockImporterAdapter {
147146
fn committed_height_stream(&self) -> BoxStream<BlockHeight> {
148147
use futures::StreamExt;
@@ -160,7 +159,6 @@ impl BlockImporterPort for BlockImporterAdapter {
160159
}
161160
}
162161

163-
#[async_trait::async_trait]
164162
impl ConsensusPort for ConsensusAdapter {
165163
fn check_sealed_header(&self, header: &SealedBlockHeader) -> anyhow::Result<bool> {
166164
Ok(self.block_verifier.verify_consensus(header))

crates/services/gas_price_service/src/common/gas_price_algorithm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ where
2323
Self(Arc::new(parking_lot::RwLock::new(algorithm)))
2424
}
2525

26-
pub async fn update(&mut self, new_algo: A) {
26+
pub fn update(&mut self, new_algo: A) {
2727
let mut write_lock = self.0.write();
2828
*write_lock = new_algo;
2929
}
@@ -37,7 +37,7 @@ where
3737
self.0.read().next_gas_price()
3838
}
3939

40-
pub async fn worst_case_gas_price(&self, block_height: BlockHeight) -> u64 {
40+
pub fn worst_case_gas_price(&self, block_height: BlockHeight) -> u64 {
4141
self.0.read().worst_case_gas_price(block_height)
4242
}
4343
}

crates/services/gas_price_service/src/v0/service.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ where
6262
self.shared_algo.clone()
6363
}
6464

65-
async fn update(&mut self, new_algorithm: AlgorithmV0) {
66-
self.shared_algo.update(new_algorithm).await;
65+
fn update(&mut self, new_algorithm: AlgorithmV0) {
66+
self.shared_algo.update(new_algorithm);
6767
}
6868

6969
fn validate_block_gas_capacity(
@@ -115,7 +115,7 @@ where
115115
}
116116
}
117117

118-
self.update(self.algorithm_updater.algorithm()).await;
118+
self.update(self.algorithm_updater.algorithm());
119119
Ok(())
120120
}
121121
}

crates/services/gas_price_service/src/v1/service.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ where
234234
&self.storage_tx_provider
235235
}
236236

237-
async fn update(&mut self, new_algorithm: AlgorithmV1) {
238-
self.shared_algo.update(new_algorithm).await;
237+
fn update(&mut self, new_algorithm: AlgorithmV1) {
238+
self.shared_algo.update(new_algorithm);
239239
}
240240

241241
fn validate_block_gas_capacity(
@@ -304,7 +304,7 @@ where
304304
AtomicStorage::commit_transaction(storage_tx)?;
305305
let new_algo = self.algorithm_updater.algorithm();
306306
tracing::debug!("Updating gas price: {}", &new_algo.calculate());
307-
self.shared_algo.update(new_algo).await;
307+
self.shared_algo.update(new_algo);
308308
// Clear the buffer after committing changes
309309
self.da_block_costs_buffer.clear();
310310
Ok(())
@@ -321,7 +321,7 @@ where
321321
tx.set_metadata(&metadata).map_err(|err| anyhow!(err))?;
322322
AtomicStorage::commit_transaction(tx)?;
323323
let new_algo = self.algorithm_updater.algorithm();
324-
self.shared_algo.update(new_algo).await;
324+
self.shared_algo.update(new_algo);
325325
}
326326
BlockInfo::Block {
327327
height,

crates/services/sync/src/import/test_helpers/pressure_block_importer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::time::Duration;
1414

1515
pub struct PressureBlockImporter(MockBlockImporterPort, Duration, SharedCounts);
1616

17-
#[async_trait::async_trait]
1817
impl BlockImporterPort for PressureBlockImporter {
1918
fn committed_height_stream(&self) -> BoxStream<BlockHeight> {
2019
self.0.committed_height_stream()
@@ -34,7 +33,8 @@ impl BlockImporterPort for PressureBlockImporter {
3433
impl PressureBlockImporter {
3534
pub fn new(counts: SharedCounts, delays: Duration) -> Self {
3635
let mut mock = MockBlockImporterPort::default();
37-
mock.expect_execute_and_commit().returning(move |_| Ok(()));
36+
mock.expect_execute_and_commit()
37+
.returning(move |_| Box::pin(async move { Ok(()) }));
3838
Self(mock, delays, counts)
3939
}
4040
}

crates/services/sync/src/import/test_helpers/pressure_consensus.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::time::Duration;
1313

1414
pub struct PressureConsensus(MockConsensusPort, Duration, SharedCounts);
1515

16-
#[async_trait::async_trait]
1716
impl ConsensusPort for PressureConsensus {
1817
fn check_sealed_header(&self, header: &SealedBlockHeader) -> anyhow::Result<bool> {
1918
self.0.check_sealed_header(header)
@@ -30,7 +29,8 @@ impl ConsensusPort for PressureConsensus {
3029
impl PressureConsensus {
3130
pub fn new(counts: SharedCounts, delays: Duration) -> Self {
3231
let mut mock = MockConsensusPort::default();
33-
mock.expect_await_da_height().returning(|_| Ok(()));
32+
mock.expect_await_da_height()
33+
.returning(|_| Box::pin(async move { Ok(()) }));
3434
mock.expect_check_sealed_header().returning(|_| Ok(true));
3535
Self(mock, delays, counts)
3636
}

0 commit comments

Comments
 (0)