-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(da_block_costs): remove Arc<Mutex<>> on shared_state and expose c…
…hannel (#2278) ## Linked Issues/PRs <!-- List of related issues/PRs --> - follows up #2192 with some changes to clean up the da block costs source. ## Description <!-- List of detailed changes --> - Exposed a `broadcast::Sender` as the shared_state of the DaBlockCostsSource, which is to be subscribed to when a service needs this dependency. ## 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 - [ ] 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? --------- Co-authored-by: Green Baneling <XgreenX9999@gmail.com>
- Loading branch information
Showing
6 changed files
with
135 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pub mod algorithm; | ||
pub mod da_source_adapter; | ||
pub mod da_source_service; |
163 changes: 0 additions & 163 deletions
163
crates/services/gas_price_service/src/v1/da_source_adapter.rs
This file was deleted.
Oops, something went wrong.
98 changes: 98 additions & 0 deletions
98
crates/services/gas_price_service/src/v1/da_source_service.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use crate::v1::da_source_service::service::DaBlockCostsSource; | ||
use std::time::Duration; | ||
|
||
pub mod block_committer_costs; | ||
pub mod dummy_costs; | ||
pub mod service; | ||
|
||
#[derive(Debug, Default, Clone, Eq, Hash, PartialEq)] | ||
pub struct DaBlockCosts { | ||
pub l2_block_range: core::ops::Range<u64>, | ||
pub blob_size_bytes: u32, | ||
pub blob_cost_wei: u128, | ||
} | ||
|
||
#[allow(non_snake_case)] | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::v1::da_source_service::{ | ||
dummy_costs::DummyDaBlockCosts, | ||
service::new_service, | ||
}; | ||
use fuel_core_services::Service; | ||
use std::time::Duration; | ||
use tokio::time::sleep; | ||
|
||
#[tokio::test] | ||
async fn run__when_da_block_cost_source_gives_value_shared_state_is_updated() { | ||
// given | ||
let expected_da_cost = DaBlockCosts { | ||
l2_block_range: 0..10, | ||
blob_size_bytes: 1024 * 128, | ||
blob_cost_wei: 2, | ||
}; | ||
let da_block_costs_source = DummyDaBlockCosts::new(Ok(expected_da_cost.clone())); | ||
let service = new_service(da_block_costs_source, Some(Duration::from_millis(1))); | ||
let mut shared_state = &mut service.shared.subscribe(); | ||
|
||
// when | ||
service.start_and_await().await.unwrap(); | ||
sleep(Duration::from_millis(10)).await; | ||
service.stop_and_await().await.unwrap(); | ||
|
||
// then | ||
let da_block_costs = shared_state.try_recv().unwrap(); | ||
assert_eq!(da_block_costs, expected_da_cost); | ||
} | ||
|
||
#[tokio::test] | ||
async fn run__when_da_block_cost_source_gives_value_shared_state_is_marked_stale() { | ||
// given | ||
let expected_da_cost = DaBlockCosts { | ||
l2_block_range: 0..10, | ||
blob_size_bytes: 1024 * 128, | ||
blob_cost_wei: 1, | ||
}; | ||
let da_block_costs_source = DummyDaBlockCosts::new(Ok(expected_da_cost.clone())); | ||
let service = new_service(da_block_costs_source, Some(Duration::from_millis(8))); | ||
let mut shared_state = &mut service.shared.subscribe(); | ||
|
||
// when | ||
service.start_and_await().await.unwrap(); | ||
sleep(Duration::from_millis(10)).await; | ||
service.stop_and_await().await.unwrap(); | ||
|
||
let actual = shared_state.try_recv().unwrap(); | ||
assert_eq!(actual, expected_da_cost); | ||
|
||
// then | ||
let da_block_costs_res = shared_state.try_recv(); | ||
assert!(da_block_costs_res.is_err()); | ||
assert!(matches!( | ||
da_block_costs_res.err().unwrap(), | ||
tokio::sync::broadcast::error::TryRecvError::Empty | ||
)); | ||
} | ||
|
||
#[tokio::test] | ||
async fn run__when_da_block_cost_source_errors_shared_state_is_not_updated() { | ||
// given | ||
let da_block_costs_source = DummyDaBlockCosts::new(Err(anyhow::anyhow!("boo!"))); | ||
let service = new_service(da_block_costs_source, Some(Duration::from_millis(1))); | ||
let mut shared_state = &mut service.shared.subscribe(); | ||
|
||
// when | ||
service.start_and_await().await.unwrap(); | ||
sleep(Duration::from_millis(10)).await; | ||
service.stop_and_await().await.unwrap(); | ||
|
||
// then | ||
let da_block_costs_res = shared_state.try_recv(); | ||
assert!(da_block_costs_res.is_err()); | ||
assert!(matches!( | ||
da_block_costs_res.err().unwrap(), | ||
tokio::sync::broadcast::error::TryRecvError::Empty | ||
)); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...a_source_adapter/block_committer_costs.rs → ...a_source_service/block_committer_costs.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...e/src/v1/da_source_adapter/dummy_costs.rs → ...e/src/v1/da_source_service/dummy_costs.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.