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

Rpc: filters performance improvement #20185

Merged
merged 14 commits into from
Oct 14, 2021
Prev Previous commit
Next Next commit
add magic numbers
  • Loading branch information
fanatid committed Oct 13, 2021
commit cc0881db1a8bf7c14210a60cb61fd20109cd53c0
52 changes: 42 additions & 10 deletions client/src/rpc_filter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#![allow(deprecated)]
use {std::borrow::Cow, thiserror::Error};

const MAX_DATA_SIZE: usize = 128;
const MAX_DATA_BASE58_SIZE: usize = 175;
const MAX_DATA_BASE64_SIZE: usize = 172;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum RpcFilterType {
Expand All @@ -18,28 +22,47 @@ impl RpcFilterType {
MemcmpEncoding::Binary => {
use MemcmpEncodedBytes::*;
match &compare.bytes {
Binary(bytes) if bytes.len() > 128 => {
Binary(bytes) if bytes.len() > MAX_DATA_BASE58_SIZE => {
Err(RpcFilterError::Base58DataTooLarge)
}
Base58(bytes) if bytes.len() > 175 => Err(RpcFilterError::DataTooLarge),
Base64(bytes) if bytes.len() > 172 => Err(RpcFilterError::DataTooLarge),
Bytes(bytes) if bytes.len() > 128 => Err(RpcFilterError::DataTooLarge),
Base58(bytes) if bytes.len() > MAX_DATA_BASE58_SIZE => {
Err(RpcFilterError::DataTooLarge)
}
Base64(bytes) if bytes.len() > MAX_DATA_BASE64_SIZE => {
Err(RpcFilterError::DataTooLarge)
}
Bytes(bytes) if bytes.len() > MAX_DATA_SIZE => {
Err(RpcFilterError::DataTooLarge)
}
_ => Ok(()),
}?;
match &compare.bytes {
Binary(bytes) => bs58::decode(&bytes)
.into_vec()
.map(|_| ())
.map_err(RpcFilterError::DecodeError),
Binary(bytes) => {
let bytes = bs58::decode(&bytes)
.into_vec()
.map_err(RpcFilterError::DecodeError)?;
if bytes.len() > MAX_DATA_SIZE {
Err(RpcFilterError::Base58DataTooLarge)
} else {
Ok(())
}
}
Base58(bytes) => {
let bytes = bs58::decode(&bytes).into_vec()?;
if bytes.len() > 128 {
if bytes.len() > MAX_DATA_SIZE {
Err(RpcFilterError::DataTooLarge)
} else {
Ok(())
}
}
Base64(bytes) => {
let bytes = base64::decode(&bytes)?;
if bytes.len() > MAX_DATA_SIZE {
Err(RpcFilterError::DataTooLarge)
} else {
Ok(())
}
}
Base64(bytes) => base64::decode(&bytes).map(|_| ()).map_err(Into::into),
Bytes(_) => Ok(()),
}
}
Expand Down Expand Up @@ -130,6 +153,15 @@ impl Memcmp {
mod tests {
use super::*;

#[test]
fn test_worst_case_encoded_tx_goldens() {
let ff_data = vec![0xffu8; MAX_DATA_SIZE];
let data58 = bs58::encode(&ff_data).into_string();
assert_eq!(data58.len(), MAX_DATA_BASE58_SIZE);
let data64 = base64::encode(&ff_data);
assert_eq!(data64.len(), MAX_DATA_BASE64_SIZE);
}

#[test]
fn test_bytes_match() {
let data = vec![1, 2, 3, 4, 5];
Expand Down