sc_network_statement/config.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Configuration of the statement protocol
20
21use std::time;
22
23/// Interval at which we propagate statements;
24pub(crate) const PROPAGATE_TIMEOUT: time::Duration = time::Duration::from_millis(1000);
25
26/// Maximum allowed size for a statement notification.
27pub const MAX_STATEMENT_NOTIFICATION_SIZE: u64 = 1024 * 1024;
28
29/// Minimum wire size of a valid statement: field-count prefix, `AuthenticityProof` and `Expiry`.
30pub const MIN_ENCODED_STATEMENT_SIZE: usize = 108;
31
32/// Most statements a sender can pack into a full [`MAX_STATEMENT_NOTIFICATION_SIZE`]
33/// notification. A batch declaring more is rejected before decoding.
34pub const MAX_STATEMENTS_PER_NOTIFICATION: usize = (MAX_STATEMENT_NOTIFICATION_SIZE as usize -
35 crate::V1_ENVELOPE_OVERHEAD) /
36 MIN_ENCODED_STATEMENT_SIZE;
37
38/// Soft limit on encoded statement chunks held in flight across all peers, shared by initial-sync
39/// and propagation sends. Since admission is checked before adding the next whole notification, it
40/// may be exceeded by less than one maximum-sized notification.
41pub const MAX_SEND_IN_FLIGHT_BYTES: u64 = 16 * MAX_STATEMENT_NOTIFICATION_SIZE;
42
43/// Part of [`MAX_SEND_IN_FLIGHT_BYTES`] withheld from propagation while initial syncs are
44/// pending. Freed budget is otherwise reclaimed synchronously by parked propagations, while
45/// sync bursts only check on a timer and would always find the budget full.
46pub const INITIAL_SYNC_RESERVED_BYTES: u64 = 4 * MAX_STATEMENT_NOTIFICATION_SIZE;
47
48/// Maximum number of statement hashes queued for propagation to one peer.
49/// On overflow the oldest hashes are dropped first.
50pub const MAX_PROPAGATION_OUTBOX_LEN: usize = 64 * 1024;
51
52/// Maximum number of statement validation request we keep at any moment.
53pub const MAX_PENDING_STATEMENTS: usize = 2 * 1024 * 1024;
54
55/// Default maximum statements per second before rate limiting kicks in.
56pub const DEFAULT_STATEMENTS_PER_SECOND: u32 = 50_000;
57
58/// Burst capacity coefficient for the rate limiter.
59pub const STATEMENTS_BURST_COEFFICIENT: u32 = 5;
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64 use codec::Encode;
65 use sp_statement_store::{Proof, Statement};
66
67 #[test]
68 fn min_encoded_statement_size_matches_encoder() {
69 // The smallest statement admission accepts: a proof and an expiry.
70 let min = [
71 Proof::Sr25519 { signature: [0u8; 64], signer: [0u8; 32] },
72 Proof::Ed25519 { signature: [0u8; 64], signer: [0u8; 32] },
73 Proof::Secp256k1Ecdsa { signature: [0u8; 65], signer: [0u8; 33] },
74 ]
75 .into_iter()
76 .map(|proof| {
77 // Stops compiling when a `Proof` variant is added, so the list above stays complete.
78 match proof {
79 Proof::Sr25519 { .. } | Proof::Ed25519 { .. } | Proof::Secp256k1Ecdsa { .. } => (),
80 }
81 let mut statement = Statement::new();
82 statement.set_proof(proof);
83 statement.set_expiry(0);
84 statement.encode().len()
85 })
86 .min()
87 .expect("the proof list is nonempty");
88 assert_eq!(min, MIN_ENCODED_STATEMENT_SIZE);
89 }
90}