Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions prdoc/pr_9965.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
title: Limit the size of the statement for further gossiping
doc:
- audience: Node Dev
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>`."
crates:
- name: sc-network-statement
bump: minor
- name: sc-statement-store
bump: minor
2 changes: 1 addition & 1 deletion substrate/client/network/statement/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(crate) const PROPAGATE_TIMEOUT: time::Duration = time::Duration::from_millis
pub(crate) const MAX_KNOWN_STATEMENTS: usize = 4 * 1024 * 1024;

/// Maximum allowed size for a statement notification.
pub(crate) const MAX_STATEMENT_SIZE: u64 = 256 * 1024;
pub const MAX_STATEMENT_NOTIFICATION_SIZE: u64 = 1024 * 1024;

/// Maximum number of statement validation request we keep at any moment.
pub(crate) const MAX_PENDING_STATEMENTS: usize = 2 * 1024 * 1024;
25 changes: 9 additions & 16 deletions substrate/client/statement-store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,15 @@ targets = ["x86_64-unknown-linux-gnu"]
log = { workspace = true, default-features = true }
parity-db = { workspace = true }
parking_lot = { workspace = true, default-features = true }
prometheus-endpoint.default-features = true
prometheus-endpoint.workspace = true
sc-client-api.default-features = true
sc-client-api.workspace = true
sc-keystore.default-features = true
sc-keystore.workspace = true
sp-api.default-features = true
sp-api.workspace = true
sp-blockchain.default-features = true
sp-blockchain.workspace = true
sp-core.default-features = true
sp-core.workspace = true
sp-runtime.default-features = true
sp-runtime.workspace = true
sp-statement-store.default-features = true
sp-statement-store.workspace = true
prometheus-endpoint = { workspace = true, default-features = true }
sc-client-api = { workspace = true, default-features = true }
sc-keystore = { workspace = true, default-features = true }
sc-network-statement = { workspace = true, default-features = true }
sp-api = { workspace = true, default-features = true }
sp-blockchain = { workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }
sp-runtime = { workspace = true, default-features = true }
sp-statement-store = { workspace = true, default-features = true }
tokio = { features = ["time"], workspace = true, default-features = true }

[dev-dependencies]
Expand Down
39 changes: 39 additions & 0 deletions substrate/client/statement-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ pub const DEFAULT_MAX_TOTAL_STATEMENTS: usize = 4 * 1024 * 1024; // ~4 million
/// The maximum amount of data the statement store can hold, regardless of the number of
/// statements from which the data originates.
pub const DEFAULT_MAX_TOTAL_SIZE: usize = 2 * 1024 * 1024 * 1024; // 2GiB
/// The maximum size of a single statement in bytes.
/// Accounts for the 1-byte vector length prefix when statements are gossiped as `Vec<Statement>`.
pub const MAX_STATEMENT_SIZE: usize =
sc_network_statement::config::MAX_STATEMENT_NOTIFICATION_SIZE as usize - 1;

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

Expand Down Expand Up @@ -861,6 +865,18 @@ impl StatementStore for Store {
/// Submit a statement to the store. Validates the statement and returns validation result.
fn submit(&self, statement: Statement, source: StatementSource) -> SubmitResult {
let hash = statement.hash();
let encoded_size = statement.encoded_size();
if encoded_size > MAX_STATEMENT_SIZE {
log::debug!(
target: LOG_TARGET,
"Statement is too big for propogation: {:?} ({}/{} bytes)",
HexDisplay::from(&hash),
statement.encoded_size(),
MAX_STATEMENT_SIZE
);
return SubmitResult::Ignored
}

match self.index.read().query(&hash) {
IndexQuery::Expired =>
if !source.can_be_resubmitted() {
Expand Down Expand Up @@ -1023,6 +1039,7 @@ mod tests {
Some(a) if a == account(2) => (2, 1000),
Some(a) if a == account(3) => (3, 1000),
Some(a) if a == account(4) => (4, 1000),
Some(a) if a == account(42) => (42, 42 * crate::MAX_STATEMENT_SIZE as u32),
_ => (2, 2000),
};
Ok(ValidStatement{ max_count, max_size })
Expand Down Expand Up @@ -1295,6 +1312,28 @@ mod tests {
assert_eq!(expected_statements, statements);
}

#[test]
fn max_statement_size_for_gossiping() {
let (store, _temp) = test_store();
store.index.write().options.max_total_size = 42 * crate::MAX_STATEMENT_SIZE;

assert_eq!(
store.submit(
statement(42, 1, Some(1), crate::MAX_STATEMENT_SIZE - 500),
StatementSource::Local
),
SubmitResult::New(NetworkPriority::High)
);

assert_eq!(
store.submit(
statement(42, 2, Some(1), 2 * crate::MAX_STATEMENT_SIZE),
StatementSource::Local
),
SubmitResult::Ignored
);
}

#[test]
fn expired_statements_are_purged() {
use super::DEFAULT_PURGE_AFTER_SEC;
Expand Down
Loading