Skip to content

Commit e94e005

Browse files
committed
use bounded vec
Signed-off-by: Alexandru Gheorghe <[email protected]>
1 parent 846a5ac commit e94e005

File tree

5 files changed

+35
-27
lines changed

5 files changed

+35
-27
lines changed

cumulus/zombienet/zombienet-sdk/tests/zombie_ci/statement_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ async fn statement_store() -> Result<(), anyhow::Error> {
7979
let mut subscription = dave_rpc
8080
.subscribe::<Bytes>(
8181
"statement_subscribeStatement",
82-
rpc_params![TopicFilter::MatchAll(vec![topic.to_vec().into()])],
82+
rpc_params![TopicFilter::MatchAll(vec![topic.to_vec().into()].try_into().expect("Single topic"))],
8383
"statement_unsubscribeStatement",
8484
)
8585
.await?;

cumulus/zombienet/zombienet-sdk/tests/zombie_ci/statement_store_bench.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -547,10 +547,11 @@ impl Participant {
547547
.rpc_client
548548
.subscribe::<Bytes>(
549549
"statement_subscribeStatement",
550-
rpc_params![TopicFilter::MatchAll(vec![
551-
topic_public_key().to_vec().into(),
552-
topic_idx(*idx).to_vec().into()
553-
])],
550+
rpc_params![TopicFilter::MatchAll(
551+
vec![topic_public_key().to_vec().into(), topic_idx(*idx).to_vec().into()]
552+
.try_into()
553+
.expect("Two topics")
554+
)],
554555
"statement_unsubscribeStatement",
555556
)
556557
.await?;
@@ -620,10 +621,14 @@ impl Participant {
620621
.rpc_client
621622
.subscribe::<Bytes>(
622623
"statement_subscribeStatement",
623-
rpc_params![TopicFilter::MatchAll(vec![
624+
rpc_params![TopicFilter::MatchAll(
625+
vec![
624626
topic_message().to_vec().into(),
625627
topic_pair(&sender_session_key, &own_session_key).to_vec().into()
626-
])],
628+
]
629+
.try_into()
630+
.expect("Two topics")
631+
)],
627632
"statement_unsubscribeStatement",
628633
)
629634
.await?;
@@ -860,7 +865,9 @@ async fn statement_store_latency_bench() -> Result<(), anyhow::Error> {
860865
let subscription = rpc_client
861866
.subscribe::<Bytes>(
862867
"statement_subscribeStatement",
863-
rpc_params![TopicFilter::MatchAll(vec![topic.to_vec().into()])],
868+
rpc_params![TopicFilter::MatchAll(
869+
vec![topic.to_vec().into()].try_into().expect("Single topic")
870+
)],
864871
"statement_unsubscribeStatement",
865872
)
866873
.await

substrate/client/rpc/src/statement/tests.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,17 @@ async fn subscribe_works() {
7171
let submitted = generate_statements();
7272
let first_topic: Bytes = submitted[0].topic(0).expect("Should have topic").to_vec().into();
7373

74-
let match_all_filter = TopicFilter::MatchAll(vec![first_topic.clone()]);
74+
let match_all_filter =
75+
TopicFilter::MatchAll(vec![first_topic.clone()].try_into().expect("Single topic"));
7576
let submitted_clone = submitted.clone();
76-
let match_any_filter = TopicFilter::MatchAny(vec![
77-
submitted[0].topic(1).expect("Should have topic").to_vec().into(),
78-
submitted[1].topic(1).expect("Should have topic").to_vec().into(),
79-
]);
77+
let match_any_filter = TopicFilter::MatchAny(
78+
vec![
79+
submitted[0].topic(1).expect("Should have topic").to_vec().into(),
80+
submitted[1].topic(1).expect("Should have topic").to_vec().into(),
81+
]
82+
.try_into()
83+
.expect("Two topics"),
84+
);
8085

8186
let subscriptions = subscribe_to_topics(
8287
&api_rpc,
@@ -115,7 +120,7 @@ async fn subscribe_works() {
115120
let mut match_any_with_random = api_rpc
116121
.subscribe_unbounded(
117122
"statement_subscribeStatement",
118-
(TopicFilter::MatchAny(vec![vec![7u8; 32].into()]),),
123+
(TopicFilter::MatchAny(vec![vec![7u8; 32].into()].try_into().expect("Single topic")),),
119124
)
120125
.await
121126
.expect("Failed to subscribe");
@@ -127,7 +132,9 @@ async fn subscribe_works() {
127132
.await;
128133
assert!(res.is_err(), "expected no message for random topic");
129134

130-
let match_all_with_random = TopicFilter::MatchAll(vec![first_topic, vec![7u8; 32].into()]);
135+
let match_all_with_random = TopicFilter::MatchAll(
136+
vec![first_topic, vec![7u8; 32].into()].try_into().expect("Two topics"),
137+
);
131138
let mut match_all_with_random = api_rpc
132139
.subscribe("statement_subscribeStatement", (match_all_with_random,), 100000)
133140
.await

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub type AccountId = [u8; 32];
4242
/// Statement channel.
4343
pub type Channel = [u8; 32];
4444

45-
/// Total number of topic fields allowed.
45+
/// Total number of topic fields allowed in a statement and in `MatchAll` filters.
4646
pub const MAX_TOPICS: usize = 4;
4747
/// `MatchAny` allows to provide a list of topics match against. This is the maximum number of
4848
/// topics allowed.

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
pub use crate::runtime_api::StatementSource;
1919
use crate::{Hash, Statement, Topic, MAX_ANY_TOPICS, MAX_TOPICS};
20-
use sp_core::Bytes;
20+
use sp_core::{bounded_vec::BoundedVec, Bytes, ConstU32};
2121
use std::collections::HashSet;
2222

2323
/// Statement store error.
@@ -43,11 +43,11 @@ pub enum TopicFilter {
4343
/// Matches all topics.
4444
Any,
4545
/// Matches only statements including all of the given topics.
46-
/// Bytes are expected to be a 32-byte topic. Up to `4` topics can be provided.
47-
MatchAll(Vec<Bytes>),
46+
/// Bytes are expected to be a 32-byte topic. Up to [`MAX_TOPICS`] topics can be provided.
47+
MatchAll(BoundedVec<Bytes, ConstU32<{ MAX_TOPICS as u32 }>>),
4848
/// Matches statements including any of the given topics.
49-
/// Bytes are expected to be a 32-byte topic. Up to `128` topics can be provided.
50-
MatchAny(Vec<Bytes>),
49+
/// Bytes are expected to be a 32-byte topic. Up to [`MAX_ANY_TOPICS`] topics can be provided.
50+
MatchAny(BoundedVec<Bytes, ConstU32<{ MAX_ANY_TOPICS as u32 }>>),
5151
}
5252

5353
/// Topic filter for statement subscriptions.
@@ -86,9 +86,6 @@ impl TryInto<CheckedTopicFilter> for TopicFilter {
8686
TopicFilter::Any => Ok(CheckedTopicFilter::Any),
8787
TopicFilter::MatchAll(topics) => {
8888
let mut parsed_topics = HashSet::with_capacity(topics.len());
89-
if topics.len() > MAX_TOPICS {
90-
return Err(Error::Decode("Too many topics in MatchAll filter".into()));
91-
}
9289
for topic in topics {
9390
if topic.0.len() != 32 {
9491
return Err(Error::Decode("Invalid topic format".into()));
@@ -101,9 +98,6 @@ impl TryInto<CheckedTopicFilter> for TopicFilter {
10198
},
10299
TopicFilter::MatchAny(topics) => {
103100
let mut parsed_topics = HashSet::with_capacity(topics.len());
104-
if topics.len() > MAX_ANY_TOPICS {
105-
return Err(Error::Decode("Too many topics in MatchAny filter".into()));
106-
}
107101
for topic in topics {
108102
if topic.0.len() != 32 {
109103
return Err(Error::Decode("Invalid topic format".into()));

0 commit comments

Comments
 (0)