Skip to main content

tycho_control/
mempool.rs

1use std::time::Duration;
2
3use serde::{Deserialize, Serialize};
4use tycho_types::cell::HashBytes;
5#[cfg(feature = "server")]
6use {anyhow::Result, bytes::Bytes, futures_util::future::BoxFuture, std::ops::Range};
7
8#[cfg(feature = "server")]
9pub trait MempoolService: Send + Sync + 'static {
10    /// Dumps the current in-memory moderator ban state.
11    fn dump_bans(&self) -> Result<Vec<DumpBansItem>>;
12
13    /// Dumps the in-memory moderator event/toleration cache, not persisted journal rows.
14    fn dump_events(&self, req: DumpEventsRequest) -> Result<String>;
15
16    /// Queues a manual ban and waits for persisted completion while the client stays connected.
17    /// After the node accepts the request, it cannot be cancelled (by timeout, disconnect, etc.).
18    fn manual_ban(&self, req: BanRequest) -> BoxFuture<'static, Result<String>>;
19
20    /// Queues a manual unban and waits for persisted completion while the client stays connected.
21    /// Live visibility can lag because the peer must resolve again after unban.
22    /// After the node accepts the request, it cannot be cancelled (by timeout, disconnect, etc.).
23    fn manual_unban(&self, peer_id: HashBytes) -> BoxFuture<'static, Result<()>>;
24
25    // TODO list banned from known peers in case ban was applied but record was not set
26
27    /// Lists persisted moderator journal records of all types.
28    /// Default order is descending unless `asc=true`.
29    /// Point key with `stored=true` can be used to retrieve full point with a separate call.
30    fn list_events(
31        &self,
32        req: ListEventsRequest,
33    ) -> BoxFuture<'static, Result<Vec<MempoolEventDisplay>>>;
34
35    // TODO async fn get_event_point(key: PointKey) -> boc / parsed
36
37    /// Deletes persisted moderator journal data only; does not mutate in-mem moderator state.
38    /// After the node accepts the request, it cannot be cancelled (by timeout, disconnect, etc.).
39    fn delete_events(&self, millis: Range<u64>) -> BoxFuture<'static, Result<()>>;
40
41    /// Loads a point linked from a stored moderator journal record (if its key has `stored` flag)
42    fn get_event_point(&self, point_key: PointKey) -> BoxFuture<'static, Result<Bytes>>;
43}
44
45#[derive(Debug, Serialize, Deserialize)]
46pub struct DumpBansItem {
47    pub peer_id: HashBytes,
48    pub until_millis: u64,
49    pub created_millis: u64,
50    pub record_seq_no: u32,
51}
52
53#[derive(Debug, Serialize, Deserialize)]
54pub struct DumpEventsRequest {
55    pub peer_id: Option<HashBytes>,
56    pub pretty: bool,
57}
58
59#[derive(Debug, Serialize, Deserialize)]
60pub struct BanRequest {
61    pub peer_id: HashBytes,
62    pub duration: Duration,
63    pub pretty: bool,
64}
65
66#[derive(Debug, Serialize, Deserialize)]
67pub struct ListEventsRequest {
68    pub count: u16,
69    pub page: u32,
70    pub asc: bool,
71    pub with_ids: bool,
72}
73
74#[derive(Debug, Serialize, Deserialize)]
75pub struct MempoolEventDisplay {
76    pub created: u64,
77    pub seq_no: u32,
78    pub peer_id: HashBytes,
79    pub points: usize,
80    pub kind: String,
81    pub message: String,
82    pub point_refs: Option<Vec<StoredPointKeyRef>>,
83}
84
85#[derive(Debug, Serialize, Deserialize)]
86pub struct StoredPointKeyRef {
87    pub stored: bool,
88    pub key: PointKey,
89}
90
91#[derive(Debug, Serialize, Deserialize)]
92pub struct PointKey(pub u32, pub HashBytes);