Skip to content

Commit eca57b4

Browse files
committed
Check if the store already has a statement before processing
1 parent f06dab8 commit eca57b4

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const LOG_TARGET: &str = "statement-gossip";
8888

8989
struct Metrics {
9090
propagated_statements: Counter<U64>,
91+
known_statements_received: Counter<U64>,
9192
}
9293

9394
impl Metrics {
@@ -100,6 +101,13 @@ impl Metrics {
100101
)?,
101102
r,
102103
)?,
104+
known_statements_received: register(
105+
Counter::new(
106+
"substrate_sync_known_statement_received",
107+
"Number of statements received via gossiping that were already in the statement store",
108+
)?,
109+
r,
110+
)?,
103111
})
104112
}
105113
}
@@ -388,6 +396,13 @@ where
388396

389397
self.network.report_peer(who, rep::ANY_STATEMENT);
390398

399+
if self.statement_store.has_statement(&hash) {
400+
if let Some(ref metrics) = self.metrics {
401+
metrics.known_statements_received.inc();
402+
}
403+
continue;
404+
}
405+
391406
match self.pending_statements_peers.entry(hash) {
392407
Entry::Vacant(entry) => {
393408
let (completion_sender, completion_receiver) = oneshot::channel();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,10 @@ impl StatementStore for Store {
833833
)
834834
}
835835

836+
fn has_statement(&self, hash: &Hash) -> bool {
837+
self.index.read().entries.contains_key(hash)
838+
}
839+
836840
/// Return the data of all known statements which include all topics and have no `DecryptionKey`
837841
/// field.
838842
fn broadcasts(&self, match_all_topics: &[Topic]) -> Result<Vec<Vec<u8>>> {

substrate/primitives/statement-store/src/store_api.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ pub trait StatementStore: Send + Sync {
7575
/// Get statement by hash.
7676
fn statement(&self, hash: &Hash) -> Result<Option<Statement>>;
7777

78+
/// Check if statement exists in the store
79+
///
80+
/// Fast index check without accessing the DB.
81+
fn has_statement(&self, hash: &Hash) -> bool;
82+
7883
/// Return the data of all known statements which include all topics and have no `DecryptionKey`
7984
/// field.
8085
fn broadcasts(&self, match_all_topics: &[Topic]) -> Result<Vec<Vec<u8>>>;

0 commit comments

Comments
 (0)