Skip to content

Commit

Permalink
comments, clippy and test
Browse files Browse the repository at this point in the history
  • Loading branch information
fbrv committed Jul 19, 2024
1 parent 259bf8e commit 89aca11
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,40 @@ jobs:
command: fmt
args: --all -- --check

clippy:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install stable toolchain with clippy available
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: clippy

- name: Run cargo clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all -- -D warnings
tests:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Run tests
uses: actions-rs/cargo@v1
with:
command: test
args: --all --verbose
8 changes: 4 additions & 4 deletions src/forward_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ mod test {
async fn test_missing_chain_id() -> Result<()> {
tokio::spawn(async move {
let manager = LookaheadManager::new(
Lookahead{map: DashMap::new().into()},
Lookahead { map: DashMap::new().into() },
LookaheadProvider::None,
);
let mut managers = HashMap::new();
Expand All @@ -190,7 +190,7 @@ mod test {
async fn test_invalid_chain_id() -> Result<()> {
tokio::spawn(async move {
let manager = LookaheadManager::new(
Lookahead{map: DashMap::new().into()},
Lookahead { map: DashMap::new().into() },
LookaheadProvider::None,
);
let mut managers = HashMap::new();
Expand All @@ -214,7 +214,7 @@ mod test {
url: "http://not-a-valid-url".into(),
..Default::default()
});
let manager = LookaheadManager::new(Lookahead{map}, LookaheadProvider::None);
let manager = LookaheadManager::new(Lookahead { map }, LookaheadProvider::None);
let mut managers = HashMap::new();
managers.insert(1u16, manager);
let router = router(SharedState::new(managers).unwrap());
Expand Down Expand Up @@ -244,7 +244,7 @@ mod test {
url: "http://localhost:12004".into(),
..Default::default()
});
let manager = LookaheadManager::new(Lookahead{map}, LookaheadProvider::None);
let manager = LookaheadManager::new(Lookahead { map }, LookaheadProvider::None);
let mut managers = HashMap::new();
managers.insert(1u16, manager);
let router = router(SharedState::new(managers).unwrap());
Expand Down
11 changes: 8 additions & 3 deletions src/lookahead/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ use super::{
};
use crate::config::Config;

/// Manages the state of the lookahead provider.
enum LookaheadProviderManager {
Initialized(LookaheadProvider),
Running,
}

/// Manages the lookahead for preconfer elections.
pub struct LookaheadManager {
lookahead: Lookahead,
provider_manager: Option<LookaheadProviderManager>,
Expand All @@ -27,6 +29,9 @@ impl LookaheadManager {
provider_manager: Some(LookaheadProviderManager::Initialized(lookahead_provider)),
}
}

/// Runs the lookahead provider in a separate thread.
/// It returns an error if the provider is already running.
pub fn run_provider(&mut self) -> Result<()> {
let provider_manager =
self.provider_manager.take().expect("provider manager should never be None");
Expand All @@ -41,21 +46,21 @@ impl LookaheadManager {
_ => bail!("context provider is already running."),
}
}

pub fn get_next_elected_preconfer(&self) -> Option<LookaheadEntry> {
self.lookahead.get_next_elected_preconfer()
}
}

/// BBuilds a map of lookahead managers from the configuration, keyed by the chain-id.
pub fn lookahead_managers_from_config(
config: Config,
beacon_tx: broadcast::Sender<HeadEvent>,
) -> HashMap<u16, LookaheadManager> {
// build managers from relay lookahead providers
let mut map = HashMap::new();
for r_c in config.lookahead_providers_relays {
let lookahead = Lookahead{
map: DashMap::new().into()
};
let lookahead = Lookahead { map: DashMap::new().into() };
let provider = LookaheadProviderOptions {
head_event_receiver: Some(beacon_tx.subscribe()),
relay_provider: Some(RelayLookaheadProvider::new(
Expand Down
3 changes: 2 additions & 1 deletion src/lookahead/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl LookaheadEntry {
}

#[derive(Debug, Clone)]
/// `Lookahead` manages a map of upcoming electors keyed by their election slot.
pub struct Lookahead {
pub map: Arc<DashMap<u64, LookaheadEntry>>,
}
Expand All @@ -45,4 +46,4 @@ impl Lookahead {
pub fn get_next_elected_preconfer(&self) -> Option<LookaheadEntry> {
self.map.iter().min_by_key(|entry| entry.slot()).map(|entry| entry.value().clone())
}
}
}
5 changes: 4 additions & 1 deletion src/lookahead/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,20 @@ impl LookaheadProviderOptions {
}
}

/// `LookaheadProvider` is an enumeration representing the implemented lookahead providers
/// to fetch upcoming lookahead entries from different sources.
pub enum LookaheadProvider {
Relay {
provider: RelayLookaheadProvider,
receiver: Receiver<HeadEvent>,
},
#[allow(dead_code)]
/// used for testing purpose, LookaheadProvider::None does not fetch any lookhead.
/// Used for testing purposes, `LookaheadProvider::None` does not fetch any lookahead.
None,
}

impl LookaheadProvider {
/// Runs the lookahead provider and waits for execution to finish.
pub async fn run(self) {
match self {
LookaheadProvider::Relay { provider, receiver } => provider.run(receiver).await,
Expand Down

0 comments on commit 89aca11

Please sign in to comment.