solana-core 4.1.1

Blockchain, Rebuilt for Scale
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
#[cfg(feature = "dev-context-only-utils")]
use qualifier_attr::qualifiers;
use {
    agave_math_utils::welford_stats::WelfordStats,
    solana_clock::Slot,
    std::time::{Duration, Instant},
};

/// Max number of root slots to wait before triggering reporting of stats.
const SLOTS_INTERVAL: Slot = 10;
/// Max amount of seconds to wait before triggering reporting of stats.
const DURATION_INTERVAL: Duration = Duration::from_secs(5);

/// A struct to control when stats should be reported depending on how many slots or time has passed.
#[derive(Debug)]
pub(super) struct Reporting {
    /// The last time when reporting was done.
    time: Instant,
    /// The last slot when reporting was done.
    slot: Slot,
}

impl Reporting {
    fn new(root_slot: Slot) -> Self {
        Self {
            time: Instant::now(),
            slot: root_slot,
        }
    }

    /// Returns `true` if reporting should be done else `false`.
    fn should_report(&self, root_slot: Slot) -> bool {
        root_slot >= self.slot + SLOTS_INTERVAL || self.time.elapsed() > DURATION_INTERVAL
    }
}

/// Stats for the sigverifier.
#[derive(Debug)]
pub(super) struct SigVerifierStats {
    /// Stats for sigverifying votes.
    pub(super) vote_stats: SigVerifyVoteStats,
    /// Stats for sigverifying certs.
    pub(super) cert_stats: SigVerifyCertStats,
    /// Stats on how long [`verify_and_send_batch`] took.
    pub(super) verify_and_send_batch_us: WelfordStats,
    /// Stats on how long [`extract_and_filter_msgs`] took.
    pub(super) extract_filter_msgs_us: WelfordStats,
    /// Number of packets received.
    pub(super) num_pkts: WelfordStats,
    /// Number of discarded packets received from the streamer.
    pub(super) num_discarded_pkts: u64,
    /// Number of times we failed to deserialize a packet.
    pub(super) num_malformed_pkts: u64,
    /// Number of votes discarded due to an invalid rank.
    pub(super) discard_vote_invalid_rank: u64,
    /// Number of votes discarded due to no epoch stakes.
    pub(super) discard_vote_no_epoch_stakes: u64,
    /// Number of outdated votes received.
    pub(super) num_old_votes_received: u64,
    /// Number of outdated certs received.
    pub(super) num_old_certs_received: u64,
    /// Number of already verified certs received.
    pub(super) num_verified_certs_received: u64,
    /// Number of certs received that the node has already generated.
    pub(super) num_generated_certs_received: u64,
    /// Last time the stats were reported.
    pub(super) last_report: Reporting,
}

impl SigVerifierStats {
    pub(super) fn new(root_slot: Slot) -> Self {
        Self {
            vote_stats: SigVerifyVoteStats::default(),
            cert_stats: SigVerifyCertStats::default(),
            extract_filter_msgs_us: WelfordStats::default(),
            num_pkts: WelfordStats::default(),
            discard_vote_invalid_rank: 0,
            num_discarded_pkts: 0,
            num_malformed_pkts: 0,
            discard_vote_no_epoch_stakes: 0,
            num_old_votes_received: 0,
            num_old_certs_received: 0,
            num_verified_certs_received: 0,
            num_generated_certs_received: 0,
            verify_and_send_batch_us: WelfordStats::default(),
            last_report: Reporting::new(root_slot),
        }
    }

    /// Reports stats if they have not been reported in some time.
    ///
    /// Also resets all stats.
    pub(super) fn maybe_report(&mut self, root_slot: Slot) {
        if self.last_report.should_report(root_slot) {
            self.do_report(root_slot);
            *self = SigVerifierStats::new(root_slot);
        }
    }

    /// Reports stats regardless of when they were last reported.
    pub(super) fn do_report(&mut self, root_slot: Slot) {
        let Self {
            vote_stats,
            cert_stats,
            extract_filter_msgs_us,
            num_pkts,
            num_discarded_pkts,
            num_malformed_pkts,
            num_old_votes_received,
            num_old_certs_received,
            num_verified_certs_received,
            num_generated_certs_received,
            discard_vote_invalid_rank,
            discard_vote_no_epoch_stakes,
            verify_and_send_batch_us,
            last_report: _,
        } = self;

        vote_stats.report();
        cert_stats.report();
        datapoint_info!(
            "bls_sig_verifier_stats",
            ("root_slot", root_slot, i64),
            (
                "extract_and_verify_us_count",
                extract_filter_msgs_us.count(),
                i64
            ),
            (
                "extract_and_verify_us_mean",
                extract_filter_msgs_us.mean().unwrap_or(0),
                i64
            ),
            ("discard_vote_invalid_rank", *discard_vote_invalid_rank, i64),
            ("num_discarded_pkts", *num_discarded_pkts, i64),
            ("num_old_votes_received", *num_old_votes_received, i64),
            (
                "num_verified_certs_received",
                *num_verified_certs_received,
                i64
            ),
            (
                "num_generated_certs_received",
                *num_generated_certs_received,
                i64
            ),
            (
                "discard_vote_no_epoch_stakes",
                *discard_vote_no_epoch_stakes,
                i64
            ),
            ("num_malformed_pkts", *num_malformed_pkts, i64),
            ("num_old_certs_received", *num_old_certs_received, i64),
            (
                "verify_and_send_batch_us_max",
                verify_and_send_batch_us.maximum().unwrap_or(0),
                i64
            ),
            (
                "verify_and_send_batch_us_mean",
                verify_and_send_batch_us.mean().unwrap_or(0),
                i64
            ),
            (
                "verify_and_send_batch_us_count",
                verify_and_send_batch_us.count(),
                i64
            ),
            ("num_pkts_max", num_pkts.maximum().unwrap_or(0), i64),
            ("num_pkts_mean", num_pkts.mean().unwrap_or(0), i64),
            ("num_pkts_count", num_pkts.count(), i64),
        );
    }
}

/// Stats from sigverifying certs.
#[derive(Default, Debug)]
pub(super) struct SigVerifyCertStats {
    /// Number of certs [`verify_and_send_certificates`] was requested to verify the signature of.
    pub(super) certs_to_sig_verify: u64,
    /// Number of certs [`verify_and_send_certificates`] successfully verified the signature of.
    pub(super) sig_verified_certs: u64,
    /// Number of certs that were verified unnecessarily because another cert of the same
    /// `CertificateType` was already verified.
    pub(super) unnecessary_certs_verified: u64,
    /// Number of times we are banning a validator that was already banned.
    pub(super) already_banned: u64,

    /// Number of times stake verification failed on a cert.
    pub(super) stake_verification_failed: u64,
    /// Number of times signature verification failed on a cert.
    pub(super) signature_verification_failed: u64,
    /// Number of times the cert was too far in the future and discarded.
    pub(super) too_far_in_future: u64,

    /// Number of votes sent successfully over the channel to consensus pool.
    pub(super) pool_sent: u64,
    /// Number of times the channel to consensus pool was full.
    pub(super) pool_channel_full: u64,

    /// Stats for [`verify_and_send_certificates`].
    pub(super) fn_verify_and_send_certs_stats: WelfordStats,
}

impl SigVerifyCertStats {
    pub(super) fn merge(&mut self, other: Self) {
        let Self {
            certs_to_sig_verify,
            sig_verified_certs,
            unnecessary_certs_verified,
            already_banned,
            stake_verification_failed,
            signature_verification_failed,
            too_far_in_future,
            pool_sent,
            pool_channel_full,
            fn_verify_and_send_certs_stats,
        } = other;
        self.certs_to_sig_verify += certs_to_sig_verify;
        self.sig_verified_certs += sig_verified_certs;
        self.unnecessary_certs_verified += unnecessary_certs_verified;
        self.already_banned += already_banned;
        self.stake_verification_failed += stake_verification_failed;
        self.signature_verification_failed += signature_verification_failed;
        self.too_far_in_future += too_far_in_future;
        self.pool_sent += pool_sent;
        self.pool_channel_full += pool_channel_full;
        self.fn_verify_and_send_certs_stats
            .merge(fn_verify_and_send_certs_stats);
    }

    pub(super) fn report(&self) {
        let Self {
            certs_to_sig_verify,
            sig_verified_certs,
            unnecessary_certs_verified,
            already_banned,
            stake_verification_failed,
            signature_verification_failed,
            too_far_in_future,
            pool_sent,
            pool_channel_full,
            fn_verify_and_send_certs_stats,
        } = self;
        datapoint_info!(
            "bls_cert_sigverify_stats",
            ("certs_to_sig_verify", *certs_to_sig_verify, i64),
            ("sig_verified_certs", *sig_verified_certs, i64),
            (
                "unnecessary_certs_verified",
                *unnecessary_certs_verified,
                i64
            ),
            ("already_banned", *already_banned, i64),
            ("stake_verification_failed", *stake_verification_failed, i64),
            (
                "signature_verification_failed",
                *signature_verification_failed,
                i64
            ),
            ("too_far_in_future", *too_far_in_future, i64),
            ("pool_sent", *pool_sent, i64),
            ("pool_channel_full", *pool_channel_full, i64),
            (
                "fn_verify_and_send_certs_count",
                fn_verify_and_send_certs_stats.count(),
                i64
            ),
            (
                "fn_verify_and_send_certs_mean",
                fn_verify_and_send_certs_stats.mean().unwrap_or(0),
                i64
            ),
        );
    }
}

/// Stats from sigverifying votes.
#[derive(Debug, Default)]
#[cfg_attr(feature = "dev-context-only-utils", qualifiers(pub))]
pub(super) struct SigVerifyVoteStats {
    /// Number of votes [`verify_and_send_votes`] was requested to verify the signature of.
    pub(super) votes_to_sig_verify: u64,
    /// Number of votes [`verify_and_send_votes`] successfully verified the signature of.
    pub(super) sig_verified_votes: u64,

    /// Number of times the cert was too far in the future and discarded.
    pub(super) too_far_in_future: u64,
    /// Number of times we are banning a validator that was already banned.
    pub(super) already_banned: u64,

    /// Number of votes sent successfully over the channel to metrics.
    pub(super) metrics_sent: u64,
    /// Number of times the channel to metrics was full.
    pub(super) metrics_channel_full: u64,
    /// Number of votes sent successfully over the channel to rewards.
    pub(super) rewards_sent: u64,
    /// Number of times the channel to rewards was full.
    pub(super) rewards_channel_full: u64,
    /// Number of votes sent successfully over the channel to consensus pool.
    pub(super) pool_sent: u64,
    /// Number of times the channel to consensus pool was full.
    pub(super) pool_channel_full: u64,
    /// Number of votes sent successfully over the channel to repair.
    pub(super) repair_sent: u64,
    /// Number of times the channel to repair was full.
    pub(super) repair_channel_full: u64,

    /// Stats for [`verify_and_send_votes`].
    pub(super) fn_verify_and_send_votes_stats: WelfordStats,
    /// Stats for [`verify_votes_optimistic`].
    pub(super) fn_verify_votes_optimistic_stats: WelfordStats,
    /// Stats for [`verify_individual_votes`].
    pub(super) fn_verify_individual_votes_stats: WelfordStats,

    /// Stats for number of distinct votes in batches.
    pub(super) distinct_votes_stats: WelfordStats,
}

impl SigVerifyVoteStats {
    pub(super) fn merge(&mut self, other: Self) {
        let Self {
            votes_to_sig_verify,
            sig_verified_votes,
            too_far_in_future,
            already_banned,
            metrics_sent,
            metrics_channel_full,
            rewards_sent,
            rewards_channel_full,
            repair_sent,
            repair_channel_full,
            pool_sent,
            pool_channel_full,
            fn_verify_and_send_votes_stats,
            fn_verify_votes_optimistic_stats,
            fn_verify_individual_votes_stats,
            distinct_votes_stats,
        } = other;
        self.votes_to_sig_verify += votes_to_sig_verify;
        self.sig_verified_votes += sig_verified_votes;
        self.too_far_in_future += too_far_in_future;
        self.already_banned += already_banned;
        self.metrics_sent += metrics_sent;
        self.metrics_channel_full += metrics_channel_full;
        self.rewards_sent += rewards_sent;
        self.rewards_channel_full += rewards_channel_full;
        self.repair_sent += repair_sent;
        self.repair_channel_full += repair_channel_full;
        self.pool_sent += pool_sent;
        self.pool_channel_full += pool_channel_full;
        self.fn_verify_and_send_votes_stats
            .merge(fn_verify_and_send_votes_stats);
        self.fn_verify_votes_optimistic_stats
            .merge(fn_verify_votes_optimistic_stats);
        self.fn_verify_individual_votes_stats
            .merge(fn_verify_individual_votes_stats);
        self.distinct_votes_stats.merge(distinct_votes_stats);
    }

    pub(super) fn report(&self) {
        let Self {
            votes_to_sig_verify,
            sig_verified_votes,
            too_far_in_future,
            already_banned,
            metrics_sent,
            metrics_channel_full,
            rewards_sent,
            rewards_channel_full,
            repair_sent,
            repair_channel_full,
            pool_sent,
            pool_channel_full,
            fn_verify_and_send_votes_stats,
            fn_verify_votes_optimistic_stats,
            fn_verify_individual_votes_stats,
            distinct_votes_stats,
        } = self;
        datapoint_info!(
            "bls_vote_sigverify_stats",
            ("votes_to_sig_verify", *votes_to_sig_verify, i64),
            ("sig_verified_votes", *sig_verified_votes, i64),
            ("too_far_in_future", *too_far_in_future, i64),
            ("already_banned", *already_banned, i64),
            ("metrics_sent", *metrics_sent, i64),
            ("metrics_channel_full", *metrics_channel_full, i64),
            ("rewards_sent", *rewards_sent, i64),
            ("rewards_channel_full", *rewards_channel_full, i64),
            ("repair_sent", *repair_sent, i64),
            ("repair_channel_full", *repair_channel_full, i64),
            ("pool_sent", *pool_sent, i64),
            ("pool_channel_full", *pool_channel_full, i64),
            (
                "fn_verify_and_send_votes_count",
                fn_verify_and_send_votes_stats.count(),
                i64
            ),
            (
                "fn_verify_and_send_votes_mean",
                fn_verify_and_send_votes_stats.mean().unwrap_or(0),
                i64
            ),
            (
                "fn_verify_votes_optimistic_count",
                fn_verify_votes_optimistic_stats.count(),
                i64
            ),
            (
                "fn_verify_votes_optimistic_mean",
                fn_verify_votes_optimistic_stats.mean().unwrap_or(0),
                i64
            ),
            (
                "fn_verify_individual_votes_count",
                fn_verify_individual_votes_stats.count(),
                i64
            ),
            (
                "fn_verify_individual_votes_mean",
                fn_verify_individual_votes_stats.mean().unwrap_or(0),
                i64
            ),
            ("distinct_votes_count", distinct_votes_stats.count(), i64),
            (
                "distinct_votes_mean",
                distinct_votes_stats.mean().unwrap_or(0),
                i64
            ),
        );
    }
}