Skip to content

Commit a2c583e

Browse files
paritytech-release-backport-bot[bot]AndreiEresgui1117EgorPopelyaev
authored
[stable2506] Backport #9965 (#10075)
Backport #9965 into `stable2506` from AndreiEres. See the [documentation](https://github.com/paritytech/polkadot-sdk/blob/master/docs/BACKPORT.md) on how to use this bot. <!-- # To be used by other automation, do not modify: original-pr-number: #${pull_number} --> --------- Co-authored-by: Andrei Eres <[email protected]> Co-authored-by: gui1117 <[email protected]> Co-authored-by: Egor_P <[email protected]>
1 parent 28c0963 commit a2c583e

File tree

5 files changed

+59
-17
lines changed

5 files changed

+59
-17
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prdoc/pr_9965.prdoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
title: Limit the size of the statement for further gossiping
2+
doc:
3+
- audience: Node Dev
4+
description: "Limits the size of statements that are further gossiped over the network to prevent skipping oversized messages. The limit is set to match the network protocol's `MAX_STATEMENT_NOTIFICATION_SIZE` (1 MB), accounting for 1-byte vector length overhead because statements are sent as `Vec<Statement>`."
5+
crates:
6+
- name: sc-network-statement
7+
bump: minor
8+
- name: sc-statement-store
9+
bump: minor

substrate/client/network/statement/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub(crate) const PROPAGATE_TIMEOUT: time::Duration = time::Duration::from_millis
2727
pub(crate) const MAX_KNOWN_STATEMENTS: usize = 4 * 1024 * 1024; // * 32 bytes for hash = 128 MB per peer
2828

2929
/// Maximum allowed size for a statement notification.
30-
pub(crate) const MAX_STATEMENT_NOTIFICATION_SIZE: u64 = 1024 * 1024;
30+
pub const MAX_STATEMENT_NOTIFICATION_SIZE: u64 = 1024 * 1024;
3131

3232
/// Maximum number of statement validation request we keep at any moment.
3333
pub(crate) const MAX_PENDING_STATEMENTS: usize = 2 * 1024 * 1024;

substrate/client/statement-store/Cargo.toml

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,15 @@ targets = ["x86_64-unknown-linux-gnu"]
1919
log = { workspace = true, default-features = true }
2020
parity-db = { workspace = true }
2121
parking_lot = { workspace = true, default-features = true }
22-
prometheus-endpoint.default-features = true
23-
prometheus-endpoint.workspace = true
24-
sc-client-api.default-features = true
25-
sc-client-api.workspace = true
26-
sc-keystore.default-features = true
27-
sc-keystore.workspace = true
28-
sp-api.default-features = true
29-
sp-api.workspace = true
30-
sp-blockchain.default-features = true
31-
sp-blockchain.workspace = true
32-
sp-core.default-features = true
33-
sp-core.workspace = true
34-
sp-runtime.default-features = true
35-
sp-runtime.workspace = true
36-
sp-statement-store.default-features = true
37-
sp-statement-store.workspace = true
22+
prometheus-endpoint = { workspace = true, default-features = true }
23+
sc-client-api = { workspace = true, default-features = true }
24+
sc-keystore = { workspace = true, default-features = true }
25+
sc-network-statement = { workspace = true, default-features = true }
26+
sp-api = { workspace = true, default-features = true }
27+
sp-blockchain = { workspace = true, default-features = true }
28+
sp-core = { workspace = true, default-features = true }
29+
sp-runtime = { workspace = true, default-features = true }
30+
sp-statement-store = { workspace = true, default-features = true }
3831
tokio = { features = ["time"], workspace = true, default-features = true }
3932

4033
[dev-dependencies]

substrate/client/statement-store/src/lib.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ pub const DEFAULT_MAX_TOTAL_STATEMENTS: usize = 4 * 1024 * 1024; // ~4 million
8383
/// The maximum amount of data the statement store can hold, regardless of the number of
8484
/// statements from which the data originates.
8585
pub const DEFAULT_MAX_TOTAL_SIZE: usize = 2 * 1024 * 1024 * 1024; // 2GiB
86+
/// The maximum size of a single statement in bytes.
87+
/// Accounts for the 1-byte vector length prefix when statements are gossiped as `Vec<Statement>`.
88+
pub const MAX_STATEMENT_SIZE: usize =
89+
sc_network_statement::config::MAX_STATEMENT_NOTIFICATION_SIZE as usize - 1;
8690

8791
const MAINTENANCE_PERIOD: std::time::Duration = std::time::Duration::from_secs(30);
8892

@@ -889,6 +893,18 @@ impl StatementStore for Store {
889893
/// Submit a statement to the store. Validates the statement and returns validation result.
890894
fn submit(&self, statement: Statement, source: StatementSource) -> SubmitResult {
891895
let hash = statement.hash();
896+
let encoded_size = statement.encoded_size();
897+
if encoded_size > MAX_STATEMENT_SIZE {
898+
log::debug!(
899+
target: LOG_TARGET,
900+
"Statement is too big for propogation: {:?} ({}/{} bytes)",
901+
HexDisplay::from(&hash),
902+
statement.encoded_size(),
903+
MAX_STATEMENT_SIZE
904+
);
905+
return SubmitResult::Ignored
906+
}
907+
892908
match self.index.read().query(&hash) {
893909
IndexQuery::Expired =>
894910
if !source.can_be_resubmitted() {
@@ -1078,6 +1094,7 @@ mod tests {
10781094
Some(a) if a == account(2) => (2, 1000),
10791095
Some(a) if a == account(3) => (3, 1000),
10801096
Some(a) if a == account(4) => (4, 1000),
1097+
Some(a) if a == account(42) => (42, 42 * crate::MAX_STATEMENT_SIZE as u32),
10811098
_ => (2, 2000),
10821099
};
10831100
Ok(ValidStatement{ max_count, max_size })
@@ -1384,6 +1401,28 @@ mod tests {
13841401
assert_eq!(expected_statements, statements);
13851402
}
13861403

1404+
#[test]
1405+
fn max_statement_size_for_gossiping() {
1406+
let (store, _temp) = test_store();
1407+
store.index.write().options.max_total_size = 42 * crate::MAX_STATEMENT_SIZE;
1408+
1409+
assert_eq!(
1410+
store.submit(
1411+
statement(42, 1, Some(1), crate::MAX_STATEMENT_SIZE - 500),
1412+
StatementSource::Local
1413+
),
1414+
SubmitResult::New(NetworkPriority::High)
1415+
);
1416+
1417+
assert_eq!(
1418+
store.submit(
1419+
statement(42, 2, Some(1), 2 * crate::MAX_STATEMENT_SIZE),
1420+
StatementSource::Local
1421+
),
1422+
SubmitResult::Ignored
1423+
);
1424+
}
1425+
13871426
#[test]
13881427
fn expired_statements_are_purged() {
13891428
use super::DEFAULT_PURGE_AFTER_SEC;

0 commit comments

Comments
 (0)