Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve eth pubsub rpc #1298

Merged
Merged
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
822 changes: 424 additions & 398 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions client/rpc-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ targets = ["x86_64-unknown-linux-gnu"]
ethereum = { workspace = true, features = ["with-codec", "with-serde"] }
ethereum-types = { workspace = true }
jsonrpsee = { workspace = true, features = ["server", "macros"] }
rlp = { workspace = true }
rustc-hex = "2.1.0"
serde = { workspace = true }
serde_json = { workspace = true }

# Substrate
sp-core-hashing = { workspace = true, features = ["default"] }

[features]
txpool = []
1 change: 1 addition & 0 deletions client/rpc-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![allow(clippy::explicit_counter_loop)]
#![warn(unused_crate_dependencies)]

pub mod types;
Expand Down
4 changes: 2 additions & 2 deletions client/rpc-core/src/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use serde::{ser::Error, Serialize, Serializer};
use crate::types::{Bytes, Transaction};

/// Block Transactions
#[derive(Debug)]
#[derive(Clone, Debug)]
pub enum BlockTransactions {
/// Only hashes
Hashes(Vec<H256>),
Expand All @@ -45,7 +45,7 @@ impl Serialize for BlockTransactions {
}

/// Block representation
#[derive(Debug, Serialize)]
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Block {
/// Header of the block
Expand Down
2 changes: 1 addition & 1 deletion client/rpc-core/src/types/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use ethereum_types::U256;
use serde::Serialize;

/// `eth_feeHistory` response
#[derive(Debug, Serialize)]
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FeeHistory {
/// Lowest number block of the returned range.
Expand Down
52 changes: 36 additions & 16 deletions client/rpc-core/src/types/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl FilteredParams {
}

/// Build an address-based BloomFilter.
pub fn adresses_bloom_filter(address: &Option<FilterAddress>) -> BloomFilter<'_> {
pub fn address_bloom_filter(address: &Option<FilterAddress>) -> BloomFilter<'_> {
if let Some(address) = address {
return address.into();
}
Expand Down Expand Up @@ -301,7 +301,7 @@ impl FilteredParams {
}

/// Replace None values - aka wildcards - for the log input value in that position.
pub fn replace(&self, log: &Log, topic: FlatTopic) -> Option<Vec<H256>> {
pub fn replace(&self, topics: &[H256], topic: FlatTopic) -> Option<Vec<H256>> {
let mut out: Vec<H256> = Vec::new();
match topic {
VariadicValue::Single(Some(value)) => {
Expand All @@ -312,7 +312,7 @@ impl FilteredParams {
if let Some(v) = v {
out.push(v);
} else {
out.push(log.topics[k]);
out.push(topics[k]);
}
}
}
Expand All @@ -324,6 +324,26 @@ impl FilteredParams {
Some(out)
}

pub fn is_not_filtered(
&self,
block_number: U256,
block_hash: H256,
address: &H160,
topics: &[H256],
) -> bool {
if self.filter.is_some() {
let block_number = block_number.as_u64();
if !self.filter_block_range(block_number)
|| !self.filter_block_hash(block_hash)
|| !self.filter_address(address)
|| !self.filter_topics(topics)
{
return false;
}
}
true
}

pub fn filter_block_range(&self, block_number: u64) -> bool {
let mut out = true;
let filter = self.filter.clone().unwrap();
Expand Down Expand Up @@ -357,16 +377,16 @@ impl FilteredParams {
true
}

pub fn filter_address(&self, log: &Log) -> bool {
pub fn filter_address(&self, address: &H160) -> bool {
if let Some(input_address) = &self.filter.clone().unwrap().address {
match input_address {
VariadicValue::Single(x) => {
if log.address != *x {
if address != x {
return false;
}
}
VariadicValue::Multiple(x) => {
if !x.contains(&log.address) {
if !x.contains(address) {
return false;
}
}
Expand All @@ -378,13 +398,13 @@ impl FilteredParams {
true
}

pub fn filter_topics(&self, log: &Log) -> bool {
pub fn filter_topics(&self, topics: &[H256]) -> bool {
let mut out: bool = true;
for topic in self.flat_topics.clone() {
match topic {
VariadicValue::Single(single) => {
if let Some(single) = single {
if !log.topics.starts_with(&[single]) {
if !topics.starts_with(&[single]) {
out = false;
}
}
Expand All @@ -401,15 +421,15 @@ impl FilteredParams {
new_multi.pop();
}
// We can discard right away any logs with lesser topics than the filter.
if new_multi.len() > log.topics.len() {
if new_multi.len() > topics.len() {
out = false;
break;
}
let replaced: Option<Vec<H256>> =
self.replace(log, VariadicValue::Multiple(new_multi));
self.replace(topics, VariadicValue::Multiple(new_multi));
if let Some(replaced) = replaced {
out = false;
if log.topics.starts_with(&replaced[..]) {
if topics.starts_with(&replaced[..]) {
out = true;
break;
}
Expand All @@ -425,7 +445,7 @@ impl FilteredParams {
}

/// Results of the filter_changes RPC.
#[derive(Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FilterChanges {
/// New logs.
Logs(Vec<Log>),
Expand Down Expand Up @@ -497,7 +517,7 @@ mod tests {
address: Some(VariadicValue::Single(test_address)),
topics: None,
};
let address_bloom = FilteredParams::adresses_bloom_filter(&filter.address);
let address_bloom = FilteredParams::address_bloom_filter(&filter.address);
assert!(FilteredParams::address_in_bloom(
block_bloom(),
&address_bloom
Expand All @@ -514,7 +534,7 @@ mod tests {
address: Some(VariadicValue::Single(test_address)),
topics: None,
};
let address_bloom = FilteredParams::adresses_bloom_filter(&filter.address);
let address_bloom = FilteredParams::address_bloom_filter(&filter.address);
assert!(!FilteredParams::address_in_bloom(
block_bloom(),
&address_bloom
Expand Down Expand Up @@ -635,7 +655,7 @@ mod tests {
} else {
None
};
let address_bloom = FilteredParams::adresses_bloom_filter(&filter.address);
let address_bloom = FilteredParams::address_bloom_filter(&filter.address);
let topics_bloom = FilteredParams::topics_bloom_filter(&topics_input);
let matches = FilteredParams::address_in_bloom(block_bloom(), &address_bloom)
&& FilteredParams::topics_in_bloom(block_bloom(), &topics_bloom);
Expand Down Expand Up @@ -669,7 +689,7 @@ mod tests {
} else {
None
};
let address_bloom = FilteredParams::adresses_bloom_filter(&filter.address);
let address_bloom = FilteredParams::address_bloom_filter(&filter.address);
let topics_bloom = FilteredParams::topics_bloom_filter(&topics_input);
let matches = FilteredParams::address_in_bloom(block_bloom(), &address_bloom)
&& FilteredParams::topics_in_bloom(block_bloom(), &topics_bloom);
Expand Down
Loading
Loading