1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::task::{Context, Poll};

use futures::future::{self, Ready};
use tower::Service;

use crate::state_machine::{
    coordinator::RoundParameters,
    events::{EventListener, EventSubscriber},
};

/// [`RoundParamsService`]'s request type
pub struct RoundParamsRequest;

/// [`RoundParamsService`]'s response type
pub type RoundParamsResponse = RoundParameters;

/// A service that serves the round parameters for the current round.
pub struct RoundParamsService(EventListener<RoundParameters>);

impl RoundParamsService {
    pub fn new(events: &EventSubscriber) -> Self {
        Self(events.params_listener())
    }
}

impl Service<RoundParamsRequest> for RoundParamsService {
    type Response = RoundParameters;
    type Error = ::std::convert::Infallible;
    type Future = Ready<Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, _req: RoundParamsRequest) -> Self::Future {
        future::ready(Ok(self.0.get_latest().event))
    }
}