ant-node 0.24.0

Pure quantum-proof network node for the Autonomi decentralized network
Documentation
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Cumulative chunk-RPC traffic accounting (V2-834 Part D.1).
//!
//! Client and community chunk serving flowed through saorsa-core's generic
//! wire counters, indistinguishable from DHT messaging. This module keeps
//! process-global relaxed-atomic tables, in the same style as the replication
//! table in [`crate::replication::protocol`], so "serving user downloads" can
//! be separated from everything else in `wire_tx_bytes`.
//!
//! Requests are counted at decode (by request kind) and responses at
//! send-success (by kind × outcome), so the tx figures are bytes confirmed
//! handed to the transport, not bytes merely encoded.

use std::sync::atomic::{AtomicU64, Ordering};

use crate::ant_protocol::{
    ChunkGetOrCloserResponse, ChunkGetResponse, ChunkMessageBody, ChunkPutResponse,
};
use ant_protocol::chunk::{PointerGetResponse, PointerPutResponse};

/// Kind of an inbound chunk message, for the rx table.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChunkRequestKind {
    Get,
    Put,
    Quote,
    MerkleQuote,
    QuoteV2,
    MerkleQuoteV2,
    PointerGet,
    PointerPut,
    /// A non-request variant (responses meant for client subscribers) or an
    /// unknown future variant.
    Other,
    /// Bytes that failed `ChunkMessage::decode`.
    DecodeError,
    /// A get-or-closer request (ADR-0020).
    GetOrCloser,
}

impl ChunkRequestKind {
    const N: usize = 11;

    const fn index(self) -> usize {
        match self {
            Self::Get => 0,
            Self::Put => 1,
            Self::Quote => 2,
            Self::MerkleQuote => 3,
            Self::QuoteV2 => 4,
            Self::MerkleQuoteV2 => 5,
            Self::PointerGet => 6,
            Self::PointerPut => 7,
            Self::Other => 8,
            Self::DecodeError => 9,
            Self::GetOrCloser => 10,
        }
    }
}

/// Kind × outcome of an outbound chunk response, for the tx table.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChunkResponseKey {
    GetSuccess,
    GetNotFound,
    GetError,
    PutSuccess,
    PutAlreadyExists,
    PutPaymentRequired,
    PutError,
    Quote,
    MerkleQuote,
    QuoteV2,
    MerkleQuoteV2,
    PointerGetSuccess,
    PointerGetNotFound,
    PointerGetError,
    PointerPutSuccess,
    PointerPutUnchanged,
    PointerPutStale,
    PointerPutPaymentRequired,
    PointerPutError,
    /// A response variant this table does not itemise (e.g. an `Other`
    /// outcome on a `#[non_exhaustive]` enum).
    Other,
    /// A get-or-closer answer (ADR-0020) carrying the chunk.
    GetOrCloserFound,
    /// A get-or-closer answer carrying closer peers.
    GetOrCloserCloser,
    GetOrCloserError,
}

impl ChunkResponseKey {
    const N: usize = 23;

    const fn index(self) -> usize {
        match self {
            Self::GetSuccess => 0,
            Self::GetNotFound => 1,
            Self::GetError => 2,
            Self::PutSuccess => 3,
            Self::PutAlreadyExists => 4,
            Self::PutPaymentRequired => 5,
            Self::PutError => 6,
            Self::Quote => 7,
            Self::MerkleQuote => 8,
            Self::QuoteV2 => 9,
            Self::MerkleQuoteV2 => 10,
            Self::PointerGetSuccess => 11,
            Self::PointerGetNotFound => 12,
            Self::PointerGetError => 13,
            Self::PointerPutSuccess => 14,
            Self::PointerPutUnchanged => 15,
            Self::PointerPutStale => 16,
            Self::PointerPutPaymentRequired => 17,
            Self::PointerPutError => 18,
            Self::Other => 19,
            Self::GetOrCloserFound => 20,
            Self::GetOrCloserCloser => 21,
            Self::GetOrCloserError => 22,
        }
    }
}

impl ChunkRequestKind {
    /// Classify a decoded inbound message.
    pub fn of(body: &ChunkMessageBody) -> Self {
        match body {
            ChunkMessageBody::GetRequest(_) => Self::Get,
            ChunkMessageBody::PutRequest(_) => Self::Put,
            ChunkMessageBody::QuoteRequest(_) => Self::Quote,
            ChunkMessageBody::MerkleCandidateQuoteRequest(_) => Self::MerkleQuote,
            ChunkMessageBody::QuoteRequestV2(_) => Self::QuoteV2,
            ChunkMessageBody::MerkleCandidateQuoteRequestV2(_) => Self::MerkleQuoteV2,
            ChunkMessageBody::PointerGetRequest(_) => Self::PointerGet,
            ChunkMessageBody::PointerPutRequest(_) => Self::PointerPut,
            ChunkMessageBody::GetOrCloserRequest(_) => Self::GetOrCloser,
            _ => Self::Other,
        }
    }
}

impl ChunkResponseKey {
    /// Classify a get-or-closer response by outcome.
    pub fn of_get_or_closer(response: &ChunkGetOrCloserResponse) -> Self {
        match response {
            ChunkGetOrCloserResponse::Found { .. } => Self::GetOrCloserFound,
            ChunkGetOrCloserResponse::Closer { .. } => Self::GetOrCloserCloser,
            ChunkGetOrCloserResponse::Error(_) => Self::GetOrCloserError,
            _ => Self::Other,
        }
    }

    /// Classify a GET response by outcome.
    pub fn of_get(response: &ChunkGetResponse) -> Self {
        match response {
            ChunkGetResponse::Success { .. } => Self::GetSuccess,
            ChunkGetResponse::NotFound { .. } => Self::GetNotFound,
            ChunkGetResponse::Error(_) => Self::GetError,
            _ => Self::Other,
        }
    }

    /// Classify a pointer GET response by outcome.
    ///
    /// Exhaustive, unlike the chunk classifiers: the pointer response enums are
    /// not `#[non_exhaustive]`, so a new variant is a compile error here rather
    /// than silently becoming `Other`.
    pub fn of_pointer_get(response: &PointerGetResponse) -> Self {
        match response {
            PointerGetResponse::Success { .. } => Self::PointerGetSuccess,
            PointerGetResponse::NotFound { .. } => Self::PointerGetNotFound,
            PointerGetResponse::Error(_) => Self::PointerGetError,
        }
    }

    /// Classify a pointer PUT response by outcome.
    ///
    /// `Unchanged` and `Stale` are itemised separately from `Success` because
    /// they are what a re-submission and a lost race look like, and telling
    /// those apart is the point of counting paid writes at all.
    pub fn of_pointer_put(response: &PointerPutResponse) -> Self {
        match response {
            PointerPutResponse::Success { .. } => Self::PointerPutSuccess,
            PointerPutResponse::Unchanged { .. } => Self::PointerPutUnchanged,
            PointerPutResponse::Stale { .. } => Self::PointerPutStale,
            PointerPutResponse::PaymentRequired { .. } => Self::PointerPutPaymentRequired,
            PointerPutResponse::Error(_) => Self::PointerPutError,
        }
    }

    /// Classify a PUT response by outcome.
    pub fn of_put(response: &ChunkPutResponse) -> Self {
        match response {
            ChunkPutResponse::Success { .. } => Self::PutSuccess,
            ChunkPutResponse::AlreadyExists { .. } => Self::PutAlreadyExists,
            ChunkPutResponse::PaymentRequired { .. } => Self::PutPaymentRequired,
            ChunkPutResponse::Error(_) => Self::PutError,
            _ => Self::Other,
        }
    }
}

static RX_BYTES: [AtomicU64; ChunkRequestKind::N] =
    [const { AtomicU64::new(0) }; ChunkRequestKind::N];
static RX_COUNT: [AtomicU64; ChunkRequestKind::N] =
    [const { AtomicU64::new(0) }; ChunkRequestKind::N];
static TX_BYTES: [AtomicU64; ChunkResponseKey::N] =
    [const { AtomicU64::new(0) }; ChunkResponseKey::N];
static TX_COUNT: [AtomicU64; ChunkResponseKey::N] =
    [const { AtomicU64::new(0) }; ChunkResponseKey::N];
/// Encoded responses whose transport send failed (not in `TX_*`).
static SEND_FAILED_BYTES: AtomicU64 = AtomicU64::new(0);
static SEND_FAILED_COUNT: AtomicU64 = AtomicU64::new(0);

/// Record one inbound chunk message at decode time (wire length).
pub fn record_rx(kind: ChunkRequestKind, bytes: usize) {
    let i = kind.index();
    RX_BYTES[i].fetch_add(bytes as u64, Ordering::Relaxed);
    RX_COUNT[i].fetch_add(1, Ordering::Relaxed);
}

/// Record one chunk response confirmed handed to the transport.
pub fn record_tx(key: ChunkResponseKey, bytes: usize) {
    let i = key.index();
    TX_BYTES[i].fetch_add(bytes as u64, Ordering::Relaxed);
    TX_COUNT[i].fetch_add(1, Ordering::Relaxed);
}

/// Record one encoded chunk response whose send failed.
pub fn record_send_failed(bytes: usize) {
    SEND_FAILED_BYTES.fetch_add(bytes as u64, Ordering::Relaxed);
    SEND_FAILED_COUNT.fetch_add(1, Ordering::Relaxed);
}

/// Emit the cumulative chunk-RPC traffic as INFO summary lines, target
/// `ant_node::storage::traffic`.
///
/// Flat snake-case keys like the replication summary. Four lines sharing the
/// same target and message, distinguished by `group`: rx by request kind
/// (`group = 1`), chunk and quote tx by kind × outcome (`group = 2`), pointer
/// tx by kind × outcome (`group = 3`), and get-or-closer rx and tx
/// (`group = 4`). Pointers and get-or-closer take lines of their own because
/// `group = 2` is already close to `tracing`'s 32-field cap.
pub fn log_chunk_rpc_traffic_summary() {
    use ChunkRequestKind as Q;
    use ChunkResponseKey as R;

    let rb = |k: Q| RX_BYTES[k.index()].load(Ordering::Relaxed);
    let rc = |k: Q| RX_COUNT[k.index()].load(Ordering::Relaxed);
    let tb = |k: R| TX_BYTES[k.index()].load(Ordering::Relaxed);
    let tc = |k: R| TX_COUNT[k.index()].load(Ordering::Relaxed);

    crate::logging::info!(
        target: "ant_node::storage::traffic",
        group = 1,
        get_rx_bytes = rb(Q::Get), get_rx_count = rc(Q::Get),
        put_rx_bytes = rb(Q::Put), put_rx_count = rc(Q::Put),
        quote_rx_bytes = rb(Q::Quote), quote_rx_count = rc(Q::Quote),
        merkle_quote_rx_bytes = rb(Q::MerkleQuote), merkle_quote_rx_count = rc(Q::MerkleQuote),
        quote_v2_rx_bytes = rb(Q::QuoteV2), quote_v2_rx_count = rc(Q::QuoteV2),
        merkle_quote_v2_rx_bytes = rb(Q::MerkleQuoteV2),
        merkle_quote_v2_rx_count = rc(Q::MerkleQuoteV2),
        pointer_get_rx_bytes = rb(Q::PointerGet), pointer_get_rx_count = rc(Q::PointerGet),
        pointer_put_rx_bytes = rb(Q::PointerPut), pointer_put_rx_count = rc(Q::PointerPut),
        other_rx_bytes = rb(Q::Other), other_rx_count = rc(Q::Other),
        decode_error_rx_bytes = rb(Q::DecodeError), decode_error_rx_count = rc(Q::DecodeError),
        "chunk rpc traffic summary (cumulative)"
    );

    crate::logging::info!(
        target: "ant_node::storage::traffic",
        group = 2,
        get_success_tx_bytes = tb(R::GetSuccess), get_success_tx_count = tc(R::GetSuccess),
        get_not_found_tx_bytes = tb(R::GetNotFound), get_not_found_tx_count = tc(R::GetNotFound),
        get_error_tx_bytes = tb(R::GetError), get_error_tx_count = tc(R::GetError),
        put_success_tx_bytes = tb(R::PutSuccess), put_success_tx_count = tc(R::PutSuccess),
        put_already_exists_tx_bytes = tb(R::PutAlreadyExists),
        put_already_exists_tx_count = tc(R::PutAlreadyExists),
        put_payment_required_tx_bytes = tb(R::PutPaymentRequired),
        put_payment_required_tx_count = tc(R::PutPaymentRequired),
        put_error_tx_bytes = tb(R::PutError), put_error_tx_count = tc(R::PutError),
        quote_tx_bytes = tb(R::Quote), quote_tx_count = tc(R::Quote),
        merkle_quote_tx_bytes = tb(R::MerkleQuote), merkle_quote_tx_count = tc(R::MerkleQuote),
        quote_v2_tx_bytes = tb(R::QuoteV2), quote_v2_tx_count = tc(R::QuoteV2),
        merkle_quote_v2_tx_bytes = tb(R::MerkleQuoteV2),
        merkle_quote_v2_tx_count = tc(R::MerkleQuoteV2),
        other_tx_bytes = tb(R::Other), other_tx_count = tc(R::Other),
        send_failed_tx_bytes = SEND_FAILED_BYTES.load(Ordering::Relaxed),
        send_failed_tx_count = SEND_FAILED_COUNT.load(Ordering::Relaxed),
        "chunk rpc traffic summary (cumulative)"
    );

    crate::logging::info!(
        target: "ant_node::storage::traffic",
        group = 3,
        pointer_get_success_tx_bytes = tb(R::PointerGetSuccess),
        pointer_get_success_tx_count = tc(R::PointerGetSuccess),
        pointer_get_not_found_tx_bytes = tb(R::PointerGetNotFound),
        pointer_get_not_found_tx_count = tc(R::PointerGetNotFound),
        pointer_get_error_tx_bytes = tb(R::PointerGetError),
        pointer_get_error_tx_count = tc(R::PointerGetError),
        pointer_put_success_tx_bytes = tb(R::PointerPutSuccess),
        pointer_put_success_tx_count = tc(R::PointerPutSuccess),
        pointer_put_unchanged_tx_bytes = tb(R::PointerPutUnchanged),
        pointer_put_unchanged_tx_count = tc(R::PointerPutUnchanged),
        pointer_put_stale_tx_bytes = tb(R::PointerPutStale),
        pointer_put_stale_tx_count = tc(R::PointerPutStale),
        pointer_put_payment_required_tx_bytes = tb(R::PointerPutPaymentRequired),
        pointer_put_payment_required_tx_count = tc(R::PointerPutPaymentRequired),
        pointer_put_error_tx_bytes = tb(R::PointerPutError),
        pointer_put_error_tx_count = tc(R::PointerPutError),
        "chunk rpc traffic summary (cumulative)"
    );

    crate::logging::info!(
        target: "ant_node::storage::traffic",
        group = 4,
        get_or_closer_rx_bytes = rb(Q::GetOrCloser),
        get_or_closer_rx_count = rc(Q::GetOrCloser),
        get_or_closer_found_tx_bytes = tb(R::GetOrCloserFound),
        get_or_closer_found_tx_count = tc(R::GetOrCloserFound),
        get_or_closer_closer_tx_bytes = tb(R::GetOrCloserCloser),
        get_or_closer_closer_tx_count = tc(R::GetOrCloserCloser),
        get_or_closer_error_tx_bytes = tb(R::GetOrCloserError),
        get_or_closer_error_tx_count = tc(R::GetOrCloserError),
        "chunk rpc traffic summary (cumulative)"
    );
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn indices_are_distinct_and_in_range() {
        let req = [
            ChunkRequestKind::Get,
            ChunkRequestKind::Put,
            ChunkRequestKind::Quote,
            ChunkRequestKind::MerkleQuote,
            ChunkRequestKind::QuoteV2,
            ChunkRequestKind::MerkleQuoteV2,
            ChunkRequestKind::PointerGet,
            ChunkRequestKind::PointerPut,
            ChunkRequestKind::Other,
            ChunkRequestKind::DecodeError,
            ChunkRequestKind::GetOrCloser,
        ];
        let mut seen = std::collections::HashSet::new();
        for k in req {
            assert!(k.index() < ChunkRequestKind::N);
            assert!(seen.insert(k.index()));
        }
        let resp = [
            ChunkResponseKey::GetSuccess,
            ChunkResponseKey::GetNotFound,
            ChunkResponseKey::GetError,
            ChunkResponseKey::PutSuccess,
            ChunkResponseKey::PutAlreadyExists,
            ChunkResponseKey::PutPaymentRequired,
            ChunkResponseKey::PutError,
            ChunkResponseKey::Quote,
            ChunkResponseKey::MerkleQuote,
            ChunkResponseKey::QuoteV2,
            ChunkResponseKey::MerkleQuoteV2,
            ChunkResponseKey::PointerGetSuccess,
            ChunkResponseKey::PointerGetNotFound,
            ChunkResponseKey::PointerGetError,
            ChunkResponseKey::PointerPutSuccess,
            ChunkResponseKey::PointerPutUnchanged,
            ChunkResponseKey::PointerPutStale,
            ChunkResponseKey::PointerPutPaymentRequired,
            ChunkResponseKey::PointerPutError,
            ChunkResponseKey::Other,
            ChunkResponseKey::GetOrCloserFound,
            ChunkResponseKey::GetOrCloserCloser,
            ChunkResponseKey::GetOrCloserError,
        ];
        let mut seen = std::collections::HashSet::new();
        for k in resp {
            assert!(k.index() < ChunkResponseKey::N);
            assert!(seen.insert(k.index()));
        }
    }

    #[test]
    fn records_accumulate() {
        let before_b = RX_BYTES[ChunkRequestKind::Get.index()].load(Ordering::Relaxed);
        let before_c = RX_COUNT[ChunkRequestKind::Get.index()].load(Ordering::Relaxed);
        record_rx(ChunkRequestKind::Get, 40);
        record_rx(ChunkRequestKind::Get, 2);
        assert_eq!(
            RX_BYTES[ChunkRequestKind::Get.index()].load(Ordering::Relaxed),
            before_b + 42
        );
        assert_eq!(
            RX_COUNT[ChunkRequestKind::Get.index()].load(Ordering::Relaxed),
            before_c + 2
        );
        let before_t = TX_BYTES[ChunkResponseKey::GetSuccess.index()].load(Ordering::Relaxed);
        record_tx(ChunkResponseKey::GetSuccess, 4096);
        assert_eq!(
            TX_BYTES[ChunkResponseKey::GetSuccess.index()].load(Ordering::Relaxed),
            before_t + 4096
        );
    }
}