casper-node 2.0.3

The Casper blockchain node
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
use std::sync::Weak;

use prometheus::{Counter, IntCounter, IntGauge, Registry};
use tracing::debug;

use super::{outgoing::OutgoingMetrics, MessageKind};
use crate::unregister_metric;

/// Network-type agnostic networking metrics.
#[derive(Debug)]
pub(super) struct Metrics {
    /// How often a request was made by a component to broadcast.
    pub(super) broadcast_requests: IntCounter,
    /// How often a request to send a message directly to a peer was made.
    pub(super) direct_message_requests: IntCounter,
    /// Number of messages still waiting to be sent out (broadcast and direct).
    pub(super) queued_messages: IntGauge,
    /// Number of connected peers.
    pub(super) peers: IntGauge,

    /// Count of outgoing messages that are protocol overhead.
    pub(super) out_count_protocol: IntCounter,
    /// Count of outgoing messages with consensus payload.
    pub(super) out_count_consensus: IntCounter,
    /// Count of outgoing messages with deploy gossiper payload.
    pub(super) out_count_deploy_gossip: IntCounter,
    pub(super) out_count_block_gossip: IntCounter,
    pub(super) out_count_finality_signature_gossip: IntCounter,
    /// Count of outgoing messages with address gossiper payload.
    pub(super) out_count_address_gossip: IntCounter,
    /// Count of outgoing messages with deploy request/response payload.
    pub(super) out_count_deploy_transfer: IntCounter,
    /// Count of outgoing messages with block request/response payload.
    pub(super) out_count_block_transfer: IntCounter,
    /// Count of outgoing messages with trie request/response payload.
    pub(super) out_count_trie_transfer: IntCounter,
    /// Count of outgoing messages with other payload.
    pub(super) out_count_other: IntCounter,

    /// Volume in bytes of outgoing messages that are protocol overhead.
    pub(super) out_bytes_protocol: IntCounter,
    /// Volume in bytes of outgoing messages with consensus payload.
    pub(super) out_bytes_consensus: IntCounter,
    /// Volume in bytes of outgoing messages with deploy gossiper payload.
    pub(super) out_bytes_deploy_gossip: IntCounter,
    pub(super) out_bytes_block_gossip: IntCounter,
    pub(super) out_bytes_finality_signature_gossip: IntCounter,
    /// Volume in bytes of outgoing messages with address gossiper payload.
    pub(super) out_bytes_address_gossip: IntCounter,
    /// Volume in bytes of outgoing messages with deploy request/response payload.
    pub(super) out_bytes_deploy_transfer: IntCounter,
    /// Volume in bytes of outgoing messages with block request/response payload.
    pub(super) out_bytes_block_transfer: IntCounter,
    /// Volume in bytes of outgoing messages with block request/response payload.
    pub(super) out_bytes_trie_transfer: IntCounter,
    /// Volume in bytes of outgoing messages with other payload.
    pub(super) out_bytes_other: IntCounter,

    /// Number of outgoing connections in connecting state.
    pub(super) out_state_connecting: IntGauge,
    /// Number of outgoing connections in waiting state.
    pub(super) out_state_waiting: IntGauge,
    /// Number of outgoing connections in connected state.
    pub(super) out_state_connected: IntGauge,
    /// Number of outgoing connections in blocked state.
    pub(super) out_state_blocked: IntGauge,
    /// Number of outgoing connections in loopback state.
    pub(super) out_state_loopback: IntGauge,

    /// Volume in bytes of incoming messages that are protocol overhead.
    pub(super) in_bytes_protocol: IntCounter,
    /// Volume in bytes of incoming messages with consensus payload.
    pub(super) in_bytes_consensus: IntCounter,
    /// Volume in bytes of incoming messages with deploy gossiper payload.
    pub(super) in_bytes_deploy_gossip: IntCounter,
    pub(super) in_bytes_block_gossip: IntCounter,
    pub(super) in_bytes_finality_signature_gossip: IntCounter,
    /// Volume in bytes of incoming messages with address gossiper payload.
    pub(super) in_bytes_address_gossip: IntCounter,
    /// Volume in bytes of incoming messages with deploy request/response payload.
    pub(super) in_bytes_deploy_transfer: IntCounter,
    /// Volume in bytes of incoming messages with block request/response payload.
    pub(super) in_bytes_block_transfer: IntCounter,
    /// Volume in bytes of incoming messages with block request/response payload.
    pub(super) in_bytes_trie_transfer: IntCounter,
    /// Volume in bytes of incoming messages with other payload.
    pub(super) in_bytes_other: IntCounter,

    /// Count of incoming messages that are protocol overhead.
    pub(super) in_count_protocol: IntCounter,
    /// Count of incoming messages with consensus payload.
    pub(super) in_count_consensus: IntCounter,
    /// Count of incoming messages with deploy gossiper payload.
    pub(super) in_count_deploy_gossip: IntCounter,
    pub(super) in_count_block_gossip: IntCounter,
    pub(super) in_count_finality_signature_gossip: IntCounter,
    /// Count of incoming messages with address gossiper payload.
    pub(super) in_count_address_gossip: IntCounter,
    /// Count of incoming messages with deploy request/response payload.
    pub(super) in_count_deploy_transfer: IntCounter,
    /// Count of incoming messages with block request/response payload.
    pub(super) in_count_block_transfer: IntCounter,
    /// Count of incoming messages with trie request/response payload.
    pub(super) in_count_trie_transfer: IntCounter,
    /// Count of incoming messages with other payload.
    pub(super) in_count_other: IntCounter,

    /// Number of trie requests accepted for processing.
    pub(super) requests_for_trie_accepted: IntCounter,
    /// Number of trie requests finished (successful or unsuccessful).
    pub(super) requests_for_trie_finished: IntCounter,

    /// Total time spent delaying outgoing traffic to non-validators due to limiter, in seconds.
    pub(super) accumulated_outgoing_limiter_delay: Counter,
    /// Total time spent delaying incoming traffic from non-validators due to limiter, in seconds.
    pub(super) accumulated_incoming_limiter_delay: Counter,

    /// Registry instance.
    registry: Registry,
}

impl Metrics {
    /// Creates a new instance of networking metrics.
    pub(super) fn new(registry: &Registry) -> Result<Self, prometheus::Error> {
        let broadcast_requests =
            IntCounter::new("net_broadcast_requests", "number of broadcasting requests")?;
        let direct_message_requests = IntCounter::new(
            "net_direct_message_requests",
            "number of requests to send a message directly to a peer",
        )?;
        let queued_messages = IntGauge::new(
            "net_queued_direct_messages",
            "number of messages waiting to be sent out",
        )?;
        let peers = IntGauge::new("peers", "number of connected peers")?;

        let out_count_protocol = IntCounter::new(
            "net_out_count_protocol",
            "count of outgoing messages that are protocol overhead",
        )?;
        let out_count_consensus = IntCounter::new(
            "net_out_count_consensus",
            "count of outgoing messages with consensus payload",
        )?;
        let out_count_deploy_gossip = IntCounter::new(
            "net_out_count_deploy_gossip",
            "count of outgoing messages with deploy gossiper payload",
        )?;
        let out_count_block_gossip = IntCounter::new(
            "net_out_count_block_gossip",
            "count of outgoing messages with block gossiper payload",
        )?;
        let out_count_finality_signature_gossip = IntCounter::new(
            "net_out_count_finality_signature_gossip",
            "count of outgoing messages with finality signature gossiper payload",
        )?;
        let out_count_address_gossip = IntCounter::new(
            "net_out_count_address_gossip",
            "count of outgoing messages with address gossiper payload",
        )?;
        let out_count_deploy_transfer = IntCounter::new(
            "net_out_count_deploy_transfer",
            "count of outgoing messages with deploy request/response payload",
        )?;
        let out_count_block_transfer = IntCounter::new(
            "net_out_count_block_transfer",
            "count of outgoing messages with block request/response payload",
        )?;
        let out_count_trie_transfer = IntCounter::new(
            "net_out_count_trie_transfer",
            "count of outgoing messages with trie payloads",
        )?;
        let out_count_other = IntCounter::new(
            "net_out_count_other",
            "count of outgoing messages with other payload",
        )?;

        let out_bytes_protocol = IntCounter::new(
            "net_out_bytes_protocol",
            "volume in bytes of outgoing messages that are protocol overhead",
        )?;
        let out_bytes_consensus = IntCounter::new(
            "net_out_bytes_consensus",
            "volume in bytes of outgoing messages with consensus payload",
        )?;
        let out_bytes_deploy_gossip = IntCounter::new(
            "net_out_bytes_deploy_gossip",
            "volume in bytes of outgoing messages with deploy gossiper payload",
        )?;
        let out_bytes_block_gossip = IntCounter::new(
            "net_out_bytes_block_gossip",
            "volume in bytes of outgoing messages with block gossiper payload",
        )?;
        let out_bytes_finality_signature_gossip = IntCounter::new(
            "net_out_bytes_finality_signature_gossip",
            "volume in bytes of outgoing messages with finality signature gossiper payload",
        )?;
        let out_bytes_address_gossip = IntCounter::new(
            "net_out_bytes_address_gossip",
            "volume in bytes of outgoing messages with address gossiper payload",
        )?;
        let out_bytes_deploy_transfer = IntCounter::new(
            "net_out_bytes_deploy_transfer",
            "volume in bytes of outgoing messages with deploy request/response payload",
        )?;
        let out_bytes_block_transfer = IntCounter::new(
            "net_out_bytes_block_transfer",
            "volume in bytes of outgoing messages with block request/response payload",
        )?;
        let out_bytes_trie_transfer = IntCounter::new(
            "net_out_bytes_trie_transfer",
            "volume in bytes of outgoing messages with trie payloads",
        )?;
        let out_bytes_other = IntCounter::new(
            "net_out_bytes_other",
            "volume in bytes of outgoing messages with other payload",
        )?;

        let out_state_connecting = IntGauge::new(
            "out_state_connecting",
            "number of connections in the connecting state",
        )?;
        let out_state_waiting = IntGauge::new(
            "out_state_waiting",
            "number of connections in the waiting state",
        )?;
        let out_state_connected = IntGauge::new(
            "out_state_connected",
            "number of connections in the connected state",
        )?;
        let out_state_blocked = IntGauge::new(
            "out_state_blocked",
            "number of connections in the blocked state",
        )?;
        let out_state_loopback = IntGauge::new(
            "out_state_loopback",
            "number of connections in the loopback state",
        )?;

        let in_count_protocol = IntCounter::new(
            "net_in_count_protocol",
            "count of incoming messages that are protocol overhead",
        )?;
        let in_count_consensus = IntCounter::new(
            "net_in_count_consensus",
            "count of incoming messages with consensus payload",
        )?;
        let in_count_deploy_gossip = IntCounter::new(
            "net_in_count_deploy_gossip",
            "count of incoming messages with deploy gossiper payload",
        )?;
        let in_count_block_gossip = IntCounter::new(
            "net_in_count_block_gossip",
            "count of incoming messages with block gossiper payload",
        )?;
        let in_count_finality_signature_gossip = IntCounter::new(
            "net_in_count_finality_signature_gossip",
            "count of incoming messages with finality signature gossiper payload",
        )?;
        let in_count_address_gossip = IntCounter::new(
            "net_in_count_address_gossip",
            "count of incoming messages with address gossiper payload",
        )?;
        let in_count_deploy_transfer = IntCounter::new(
            "net_in_count_deploy_transfer",
            "count of incoming messages with deploy request/response payload",
        )?;
        let in_count_block_transfer = IntCounter::new(
            "net_in_count_block_transfer",
            "count of incoming messages with block request/response payload",
        )?;
        let in_count_trie_transfer = IntCounter::new(
            "net_in_count_trie_transfer",
            "count of incoming messages with trie payloads",
        )?;
        let in_count_other = IntCounter::new(
            "net_in_count_other",
            "count of incoming messages with other payload",
        )?;

        let in_bytes_protocol = IntCounter::new(
            "net_in_bytes_protocol",
            "volume in bytes of incoming messages that are protocol overhead",
        )?;
        let in_bytes_consensus = IntCounter::new(
            "net_in_bytes_consensus",
            "volume in bytes of incoming messages with consensus payload",
        )?;
        let in_bytes_deploy_gossip = IntCounter::new(
            "net_in_bytes_deploy_gossip",
            "volume in bytes of incoming messages with deploy gossiper payload",
        )?;
        let in_bytes_block_gossip = IntCounter::new(
            "net_in_bytes_block_gossip",
            "volume in bytes of incoming messages with block gossiper payload",
        )?;
        let in_bytes_finality_signature_gossip = IntCounter::new(
            "net_in_bytes_finality_signature_gossip",
            "volume in bytes of incoming messages with finality signature gossiper payload",
        )?;
        let in_bytes_address_gossip = IntCounter::new(
            "net_in_bytes_address_gossip",
            "volume in bytes of incoming messages with address gossiper payload",
        )?;
        let in_bytes_deploy_transfer = IntCounter::new(
            "net_in_bytes_deploy_transfer",
            "volume in bytes of incoming messages with deploy request/response payload",
        )?;
        let in_bytes_block_transfer = IntCounter::new(
            "net_in_bytes_block_transfer",
            "volume in bytes of incoming messages with block request/response payload",
        )?;
        let in_bytes_trie_transfer = IntCounter::new(
            "net_in_bytes_trie_transfer",
            "volume in bytes of incoming messages with trie payloads",
        )?;
        let in_bytes_other = IntCounter::new(
            "net_in_bytes_other",
            "volume in bytes of incoming messages with other payload",
        )?;

        let requests_for_trie_accepted = IntCounter::new(
            "requests_for_trie_accepted",
            "number of trie requests accepted for processing",
        )?;
        let requests_for_trie_finished = IntCounter::new(
            "requests_for_trie_finished",
            "number of trie requests finished, successful or not",
        )?;

        let accumulated_outgoing_limiter_delay = Counter::new(
            "accumulated_outgoing_limiter_delay",
            "seconds spent delaying outgoing traffic to non-validators due to limiter, in seconds",
        )?;
        let accumulated_incoming_limiter_delay = Counter::new(
            "accumulated_incoming_limiter_delay",
            "seconds spent delaying incoming traffic from non-validators due to limiter, in seconds."
        )?;

        registry.register(Box::new(broadcast_requests.clone()))?;
        registry.register(Box::new(direct_message_requests.clone()))?;
        registry.register(Box::new(queued_messages.clone()))?;
        registry.register(Box::new(peers.clone()))?;

        registry.register(Box::new(out_count_protocol.clone()))?;
        registry.register(Box::new(out_count_consensus.clone()))?;
        registry.register(Box::new(out_count_deploy_gossip.clone()))?;
        registry.register(Box::new(out_count_block_gossip.clone()))?;
        registry.register(Box::new(out_count_finality_signature_gossip.clone()))?;
        registry.register(Box::new(out_count_address_gossip.clone()))?;
        registry.register(Box::new(out_count_deploy_transfer.clone()))?;
        registry.register(Box::new(out_count_block_transfer.clone()))?;
        registry.register(Box::new(out_count_trie_transfer.clone()))?;
        registry.register(Box::new(out_count_other.clone()))?;

        registry.register(Box::new(out_bytes_protocol.clone()))?;
        registry.register(Box::new(out_bytes_consensus.clone()))?;
        registry.register(Box::new(out_bytes_deploy_gossip.clone()))?;
        registry.register(Box::new(out_bytes_block_gossip.clone()))?;
        registry.register(Box::new(out_bytes_finality_signature_gossip.clone()))?;
        registry.register(Box::new(out_bytes_address_gossip.clone()))?;
        registry.register(Box::new(out_bytes_deploy_transfer.clone()))?;
        registry.register(Box::new(out_bytes_block_transfer.clone()))?;
        registry.register(Box::new(out_bytes_trie_transfer.clone()))?;
        registry.register(Box::new(out_bytes_other.clone()))?;

        registry.register(Box::new(out_state_connecting.clone()))?;
        registry.register(Box::new(out_state_waiting.clone()))?;
        registry.register(Box::new(out_state_connected.clone()))?;
        registry.register(Box::new(out_state_blocked.clone()))?;
        registry.register(Box::new(out_state_loopback.clone()))?;

        registry.register(Box::new(in_count_protocol.clone()))?;
        registry.register(Box::new(in_count_consensus.clone()))?;
        registry.register(Box::new(in_count_deploy_gossip.clone()))?;
        registry.register(Box::new(in_count_block_gossip.clone()))?;
        registry.register(Box::new(in_count_finality_signature_gossip.clone()))?;
        registry.register(Box::new(in_count_address_gossip.clone()))?;
        registry.register(Box::new(in_count_deploy_transfer.clone()))?;
        registry.register(Box::new(in_count_block_transfer.clone()))?;
        registry.register(Box::new(in_count_trie_transfer.clone()))?;
        registry.register(Box::new(in_count_other.clone()))?;

        registry.register(Box::new(in_bytes_protocol.clone()))?;
        registry.register(Box::new(in_bytes_consensus.clone()))?;
        registry.register(Box::new(in_bytes_deploy_gossip.clone()))?;
        registry.register(Box::new(in_bytes_block_gossip.clone()))?;
        registry.register(Box::new(in_bytes_finality_signature_gossip.clone()))?;
        registry.register(Box::new(in_bytes_address_gossip.clone()))?;
        registry.register(Box::new(in_bytes_deploy_transfer.clone()))?;
        registry.register(Box::new(in_bytes_block_transfer.clone()))?;
        registry.register(Box::new(in_bytes_trie_transfer.clone()))?;
        registry.register(Box::new(in_bytes_other.clone()))?;

        registry.register(Box::new(requests_for_trie_accepted.clone()))?;
        registry.register(Box::new(requests_for_trie_finished.clone()))?;

        registry.register(Box::new(accumulated_outgoing_limiter_delay.clone()))?;
        registry.register(Box::new(accumulated_incoming_limiter_delay.clone()))?;

        Ok(Metrics {
            broadcast_requests,
            direct_message_requests,
            queued_messages,
            peers,
            out_count_protocol,
            out_count_consensus,
            out_count_deploy_gossip,
            out_count_block_gossip,
            out_count_finality_signature_gossip,
            out_count_address_gossip,
            out_count_deploy_transfer,
            out_count_block_transfer,
            out_count_trie_transfer,
            out_count_other,
            out_bytes_protocol,
            out_bytes_consensus,
            out_bytes_deploy_gossip,
            out_bytes_block_gossip,
            out_bytes_finality_signature_gossip,
            out_bytes_address_gossip,
            out_bytes_deploy_transfer,
            out_bytes_block_transfer,
            out_bytes_trie_transfer,
            out_bytes_other,
            out_state_connecting,
            out_state_waiting,
            out_state_connected,
            out_state_blocked,
            out_state_loopback,
            in_count_protocol,
            in_count_consensus,
            in_count_deploy_gossip,
            in_count_block_gossip,
            in_count_finality_signature_gossip,
            in_count_address_gossip,
            in_count_deploy_transfer,
            in_count_block_transfer,
            in_count_trie_transfer,
            in_count_other,
            in_bytes_protocol,
            in_bytes_consensus,
            in_bytes_deploy_gossip,
            in_bytes_block_gossip,
            in_bytes_finality_signature_gossip,
            in_bytes_address_gossip,
            in_bytes_deploy_transfer,
            in_bytes_block_transfer,
            in_bytes_trie_transfer,
            in_bytes_other,
            requests_for_trie_accepted,
            requests_for_trie_finished,
            accumulated_outgoing_limiter_delay,
            accumulated_incoming_limiter_delay,
            registry: registry.clone(),
        })
    }

    /// Records an outgoing payload.
    pub(crate) fn record_payload_out(this: &Weak<Self>, kind: MessageKind, size: u64) {
        if let Some(metrics) = this.upgrade() {
            match kind {
                MessageKind::Protocol => {
                    metrics.out_bytes_protocol.inc_by(size);
                    metrics.out_count_protocol.inc();
                }
                MessageKind::Consensus => {
                    metrics.out_bytes_consensus.inc_by(size);
                    metrics.out_count_consensus.inc();
                }
                MessageKind::TransactionGossip => {
                    metrics.out_bytes_deploy_gossip.inc_by(size);
                    metrics.out_count_deploy_gossip.inc();
                }
                MessageKind::BlockGossip => {
                    metrics.out_bytes_block_gossip.inc_by(size);
                    metrics.out_count_block_gossip.inc();
                }
                MessageKind::FinalitySignatureGossip => {
                    metrics.out_bytes_finality_signature_gossip.inc_by(size);
                    metrics.out_count_finality_signature_gossip.inc();
                }
                MessageKind::AddressGossip => {
                    metrics.out_bytes_address_gossip.inc_by(size);
                    metrics.out_count_address_gossip.inc();
                }
                MessageKind::TransactionTransfer => {
                    metrics.out_bytes_deploy_transfer.inc_by(size);
                    metrics.out_count_deploy_transfer.inc();
                }
                MessageKind::BlockTransfer => {
                    metrics.out_bytes_block_transfer.inc_by(size);
                    metrics.out_count_block_transfer.inc();
                }
                MessageKind::TrieTransfer => {
                    metrics.out_bytes_trie_transfer.inc_by(size);
                    metrics.out_count_trie_transfer.inc();
                }
                MessageKind::Other => {
                    metrics.out_bytes_other.inc_by(size);
                    metrics.out_count_other.inc();
                }
            }
        } else {
            debug!("not recording metrics, component already shut down");
        }
    }

    /// Records an incoming payload.
    pub(crate) fn record_payload_in(this: &Weak<Self>, kind: MessageKind, size: u64) {
        if let Some(metrics) = this.upgrade() {
            match kind {
                MessageKind::Protocol => {
                    metrics.in_bytes_protocol.inc_by(size);
                    metrics.in_count_protocol.inc();
                }
                MessageKind::Consensus => {
                    metrics.in_bytes_consensus.inc_by(size);
                    metrics.in_count_consensus.inc();
                }
                MessageKind::TransactionGossip => {
                    metrics.in_bytes_deploy_gossip.inc_by(size);
                    metrics.in_count_deploy_gossip.inc();
                }
                MessageKind::BlockGossip => {
                    metrics.in_bytes_block_gossip.inc_by(size);
                    metrics.in_count_block_gossip.inc();
                }
                MessageKind::FinalitySignatureGossip => {
                    metrics.in_bytes_finality_signature_gossip.inc_by(size);
                    metrics.in_count_finality_signature_gossip.inc();
                }
                MessageKind::AddressGossip => {
                    metrics.in_bytes_address_gossip.inc_by(size);
                    metrics.in_count_address_gossip.inc();
                }
                MessageKind::TransactionTransfer => {
                    metrics.in_bytes_deploy_transfer.inc_by(size);
                    metrics.in_count_deploy_transfer.inc();
                }
                MessageKind::BlockTransfer => {
                    metrics.in_bytes_block_transfer.inc_by(size);
                    metrics.in_count_block_transfer.inc();
                }
                MessageKind::TrieTransfer => {
                    metrics.in_bytes_trie_transfer.inc_by(size);
                    metrics.in_count_trie_transfer.inc();
                }
                MessageKind::Other => {
                    metrics.in_bytes_other.inc_by(size);
                    metrics.in_count_other.inc();
                }
            }
        } else {
            debug!("not recording metrics, component already shut down");
        }
    }

    /// Creates a set of outgoing metrics that is connected to this set of metrics.
    pub(super) fn create_outgoing_metrics(&self) -> OutgoingMetrics {
        OutgoingMetrics {
            out_state_connecting: self.out_state_connecting.clone(),
            out_state_waiting: self.out_state_waiting.clone(),
            out_state_connected: self.out_state_connected.clone(),
            out_state_blocked: self.out_state_blocked.clone(),
            out_state_loopback: self.out_state_loopback.clone(),
        }
    }

    /// Records that a trie request has been started.
    pub(super) fn record_trie_request_start(this: &Weak<Self>) {
        if let Some(metrics) = this.upgrade() {
            metrics.requests_for_trie_accepted.inc();
        } else {
            debug!("not recording metrics, component already shut down");
        }
    }

    /// Records that a trie request has ended.
    pub(super) fn record_trie_request_end(this: &Weak<Self>) {
        if let Some(metrics) = this.upgrade() {
            metrics.requests_for_trie_finished.inc();
        } else {
            debug!("not recording metrics, component already shut down");
        }
    }
}

impl Drop for Metrics {
    fn drop(&mut self) {
        unregister_metric!(self.registry, self.broadcast_requests);
        unregister_metric!(self.registry, self.direct_message_requests);
        unregister_metric!(self.registry, self.queued_messages);
        unregister_metric!(self.registry, self.peers);

        unregister_metric!(self.registry, self.out_count_protocol);
        unregister_metric!(self.registry, self.out_count_consensus);
        unregister_metric!(self.registry, self.out_count_deploy_gossip);
        unregister_metric!(self.registry, self.out_count_block_gossip);
        unregister_metric!(self.registry, self.out_count_finality_signature_gossip);
        unregister_metric!(self.registry, self.out_count_address_gossip);
        unregister_metric!(self.registry, self.out_count_deploy_transfer);
        unregister_metric!(self.registry, self.out_count_block_transfer);
        unregister_metric!(self.registry, self.out_count_trie_transfer);
        unregister_metric!(self.registry, self.out_count_other);

        unregister_metric!(self.registry, self.out_bytes_protocol);
        unregister_metric!(self.registry, self.out_bytes_consensus);
        unregister_metric!(self.registry, self.out_bytes_deploy_gossip);
        unregister_metric!(self.registry, self.out_bytes_block_gossip);
        unregister_metric!(self.registry, self.out_bytes_finality_signature_gossip);
        unregister_metric!(self.registry, self.out_bytes_address_gossip);
        unregister_metric!(self.registry, self.out_bytes_deploy_transfer);
        unregister_metric!(self.registry, self.out_bytes_block_transfer);
        unregister_metric!(self.registry, self.out_bytes_trie_transfer);
        unregister_metric!(self.registry, self.out_bytes_other);

        unregister_metric!(self.registry, self.out_state_connecting);
        unregister_metric!(self.registry, self.out_state_waiting);
        unregister_metric!(self.registry, self.out_state_connected);
        unregister_metric!(self.registry, self.out_state_blocked);
        unregister_metric!(self.registry, self.out_state_loopback);

        unregister_metric!(self.registry, self.in_count_protocol);
        unregister_metric!(self.registry, self.in_count_consensus);
        unregister_metric!(self.registry, self.in_count_deploy_gossip);
        unregister_metric!(self.registry, self.in_count_block_gossip);
        unregister_metric!(self.registry, self.in_count_finality_signature_gossip);
        unregister_metric!(self.registry, self.in_count_address_gossip);
        unregister_metric!(self.registry, self.in_count_deploy_transfer);
        unregister_metric!(self.registry, self.in_count_block_transfer);
        unregister_metric!(self.registry, self.in_count_trie_transfer);
        unregister_metric!(self.registry, self.in_count_other);

        unregister_metric!(self.registry, self.in_bytes_protocol);
        unregister_metric!(self.registry, self.in_bytes_consensus);
        unregister_metric!(self.registry, self.in_bytes_deploy_gossip);
        unregister_metric!(self.registry, self.in_bytes_block_gossip);
        unregister_metric!(self.registry, self.in_bytes_finality_signature_gossip);
        unregister_metric!(self.registry, self.in_bytes_address_gossip);
        unregister_metric!(self.registry, self.in_bytes_deploy_transfer);
        unregister_metric!(self.registry, self.in_bytes_block_transfer);
        unregister_metric!(self.registry, self.in_bytes_trie_transfer);
        unregister_metric!(self.registry, self.in_bytes_other);

        unregister_metric!(self.registry, self.requests_for_trie_accepted);
        unregister_metric!(self.registry, self.requests_for_trie_finished);

        unregister_metric!(self.registry, self.accumulated_outgoing_limiter_delay);
        unregister_metric!(self.registry, self.accumulated_incoming_limiter_delay);
    }
}