fedimint_client/
api.rs

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::collections::BTreeSet;
use std::result;
use std::string::ToString;

use fedimint_api_client::api::{DynModuleApi, IRawFederationApi, JsonRpcClientError};
use fedimint_core::core::ModuleInstanceId;
use fedimint_core::db::{Database, DatabaseTransaction};
use fedimint_core::task::{MaybeSend, MaybeSync};
use fedimint_core::{apply, async_trait_maybe_send, PeerId};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio::sync::watch;

/// Event log event right before making an api call
///
/// Notably there is no guarantee that a corresponding [`ApiCallDone`]
/// is ever called, or that the api call actually reached the server.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ApiCallStarted {
    method: String,
    peer_id: PeerId,
}

impl Event for ApiCallStarted {
    const MODULE: Option<fedimint_core::core::ModuleKind> = None;

    const KIND: EventKind = EventKind::from_static("api-call-started");

    /// These were deemed heavy volume enough and mostly diagnostics, so they
    /// are not persisted
    const PERSIST: bool = false;
}

/// Event log event right after an api call
///
/// Notably there is no guarantee this event is always created. If the
/// client completed the call, but was abruptly terminated before logging
/// an event, the call might have completed on the server side, but never
/// create this event.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ApiCallDone {
    method: String,
    peer_id: PeerId,
    duration_ms: u64,
    success: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    error_str: Option<String>,
}

impl Event for ApiCallDone {
    const MODULE: Option<fedimint_core::core::ModuleKind> = None;

    const KIND: EventKind = EventKind::from_static("api-call-done");
}

use fedimint_eventlog::{DBTransactionEventLogExt as _, Event, EventKind};

/// Convenience extension trait used for wrapping [`IRawFederationApi`] in
/// a [`ClientRawFederationApi`]
pub trait ClientRawFederationApiExt
where
    Self: Sized,
{
    fn with_client_ext(
        self,
        db: Database,
        log_ordering_wakeup_tx: watch::Sender<()>,
    ) -> ClientRawFederationApi<Self>;
}

impl<T> ClientRawFederationApiExt for T
where
    T: IRawFederationApi + MaybeSend + MaybeSync + 'static,
{
    fn with_client_ext(
        self,
        db: Database,
        log_ordering_wakeup_tx: watch::Sender<()>,
    ) -> ClientRawFederationApi<T> {
        db.ensure_global().expect("Must be given global db");
        ClientRawFederationApi {
            inner: self,
            db,
            log_ordering_wakeup_tx,
        }
    }
}

/// A wrapper over [`IRawFederationApi`] adding client side event logging
///
/// Create using [`ClientRawFederationApiExt::with_client_ext`]
#[derive(Debug)]
pub struct ClientRawFederationApi<I> {
    inner: I,
    db: Database,
    log_ordering_wakeup_tx: watch::Sender<()>,
}

impl<I> ClientRawFederationApi<I> {
    pub async fn log_event<E>(&self, event: E)
    where
        E: Event + Send,
    {
        let mut dbtx = self.db.begin_transaction().await;
        self.log_event_dbtx(&mut dbtx, event).await;
        dbtx.commit_tx().await;
    }

    pub async fn log_event_dbtx<E, Cap>(&self, dbtx: &mut DatabaseTransaction<'_, Cap>, event: E)
    where
        E: Event + Send,
        Cap: Send,
    {
        dbtx.log_event(self.log_ordering_wakeup_tx.clone(), None, event)
            .await;
    }
}

#[apply(async_trait_maybe_send!)]
impl<I> IRawFederationApi for ClientRawFederationApi<I>
where
    I: IRawFederationApi,
{
    fn all_peers(&self) -> &BTreeSet<PeerId> {
        self.inner.all_peers()
    }

    fn self_peer(&self) -> Option<PeerId> {
        self.inner.self_peer()
    }

    fn with_module(&self, id: ModuleInstanceId) -> DynModuleApi {
        self.inner.with_module(id)
    }

    async fn request_raw(
        &self,
        peer_id: PeerId,
        method: &str,
        params: &[Value],
    ) -> result::Result<Value, JsonRpcClientError> {
        self.log_event(ApiCallStarted {
            method: method.to_string(),
            peer_id,
        })
        .await;

        let start = fedimint_core::time::now();
        let res = self.inner.request_raw(peer_id, method, params).await;
        let end = fedimint_core::time::now();

        self.log_event(ApiCallDone {
            method: method.to_string(),
            peer_id,
            duration_ms: end
                .duration_since(start)
                .unwrap_or_default()
                .as_millis()
                .try_into()
                .unwrap_or(u64::MAX),
            success: res.is_ok(),
            error_str: res.as_ref().err().map(ToString::to_string),
        })
        .await;

        res
    }
}