penumbra_sdk_app/
server.rs

1//! Facilities related to the Penumbra app's ABCI server.
2
3use {
4    self::{
5        consensus::Consensus, events::EventIndexLayer, info::Info, mempool::Mempool,
6        snapshot::Snapshot,
7    },
8    cnidarium::Storage,
9    penumbra_sdk_tower_trace::trace::request_span,
10    tendermint::v0_37::abci::{
11        ConsensusRequest, ConsensusResponse, MempoolRequest, MempoolResponse,
12    },
13    tower_abci::{v037::Server, BoxError},
14};
15
16pub mod consensus;
17pub mod info;
18pub mod mempool;
19pub mod snapshot;
20
21mod events;
22
23/// Returns a newly instantiated ABCI [`Server`], backed by the provided [`Storage`].
24pub fn new(
25    storage: Storage,
26) -> Server<
27    // These bounds ensure that the server can be bound to a TCP port, or a Unix socket.
28    impl tower_service::Service<
29            ConsensusRequest,
30            Response = ConsensusResponse,
31            Error = BoxError,
32            Future = impl Send + 'static,
33        > + Send
34        + Clone
35        + 'static,
36    impl tower_service::Service<
37            MempoolRequest,
38            Response = MempoolResponse,
39            Error = BoxError,
40            Future = impl Send + 'static,
41        > + Send
42        + Clone
43        + 'static,
44    Info,
45    Snapshot,
46> {
47    let consensus = tower::ServiceBuilder::new()
48        .layer(request_span::layer(|req: &ConsensusRequest| {
49            use penumbra_sdk_tower_trace::v037::RequestExt;
50            req.create_span()
51        }))
52        .layer(EventIndexLayer::index_all())
53        .service(Consensus::new(storage.clone()));
54    let mempool = tower::ServiceBuilder::new()
55        .layer(request_span::layer(|req: &MempoolRequest| {
56            use penumbra_sdk_tower_trace::v037::RequestExt;
57            req.create_span()
58        }))
59        .service(tower_actor::Actor::new(10, |queue: _| {
60            Mempool::new(storage.clone(), queue).run()
61        }));
62    let info = Info::new(storage.clone());
63    let snapshot = Snapshot {};
64
65    tower_abci::v037::Server::builder()
66        .consensus(consensus)
67        .snapshot(snapshot)
68        .mempool(mempool)
69        .info(info.clone())
70        .finish()
71        // Safety: the consensus, snapshot, mempool, and info services have all been provided
72        // to the builder above.
73        .expect("all components of abci have been provided")
74}
75
76#[cfg(test)]
77mod bounds_test {
78    /// Show that a server satisfies the trait bounds needed to listen on a TCP port.
79    #[allow(dead_code, unreachable_code, unused_variables)]
80    async fn servers_can_listen() {
81        let storage: cnidarium::Storage = todo!();
82        let addr: std::net::SocketAddr = todo!();
83        let server = super::new(storage).listen_tcp(addr);
84        drop(server);
85    }
86}