This repository was archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5k
Rpc: filters performance improvement #20185
Merged
CriesofCarrots
merged 14 commits into
solana-labs:master
from
fanatid:rpc-filter-memcmp
Oct 14, 2021
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e2a62d7
Add Base58,Base64,Bytes to MemcmpEncodedBytes
fanatid 5a36ad9
Rpc: decode memcmp before filtering accounts
fanatid a38a140
Add deprecated attribute
fanatid 5db8808
Add Memcmp::bytes
fanatid 750634e
Fix clippy for deprecated
fanatid 73ce0db
Another clippy fix
fanatid e573789
Merge remote-tracking branch 'upstream/master' into rpc-filter-memcmp
fanatid 02c478f
merge RpcFilterError::DataTooLarge
fanatid 0c3e724
add deprecation for Base58DataTooLarge
fanatid ab99129
change filter data size limit
fanatid 5c7969a
strict data size len for base58
fanatid cc0881d
add magic numbers
fanatid 6010a29
Merge remote-tracking branch 'upstream/master' into rpc-filter-memcmp
fanatid 4864e33
fix tests
fanatid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
use thiserror::Error; | ||
#![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")] | ||
|
@@ -15,15 +20,50 @@ impl RpcFilterType { | |
let encoding = compare.encoding.as_ref().unwrap_or(&MemcmpEncoding::Binary); | ||
match encoding { | ||
MemcmpEncoding::Binary => { | ||
let MemcmpEncodedBytes::Binary(bytes) = &compare.bytes; | ||
|
||
if bytes.len() > 128 { | ||
Err(RpcFilterError::Base58DataTooLarge) | ||
} else { | ||
bs58::decode(&bytes) | ||
.into_vec() | ||
.map(|_| ()) | ||
.map_err(|e| e.into()) | ||
use MemcmpEncodedBytes::*; | ||
match &compare.bytes { | ||
Binary(bytes) if bytes.len() > MAX_DATA_BASE58_SIZE => { | ||
Err(RpcFilterError::Base58DataTooLarge) | ||
} | ||
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 { | ||
Comment on lines
+24
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still find it more confusing than helpful to have two match statements, esp since the four cases have to handled separately. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With added changes I find this bit more confusing too 😞 but once |
||
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() > 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(()) | ||
} | ||
} | ||
Bytes(_) => Ok(()), | ||
} | ||
} | ||
} | ||
|
@@ -34,10 +74,24 @@ impl RpcFilterType { | |
|
||
#[derive(Error, PartialEq, Debug)] | ||
pub enum RpcFilterError { | ||
#[error("bs58 decode error")] | ||
DecodeError(#[from] bs58::decode::Error), | ||
#[error("encoded binary data should be less than 129 bytes")] | ||
DataTooLarge, | ||
#[deprecated( | ||
since = "1.9.0", | ||
note = "Error for MemcmpEncodedBytes::Binary which is deprecated" | ||
)] | ||
#[error("encoded binary (base 58) data should be less than 129 bytes")] | ||
Base58DataTooLarge, | ||
#[deprecated( | ||
since = "1.9.0", | ||
note = "Error for MemcmpEncodedBytes::Binary which is deprecated" | ||
)] | ||
#[error("bs58 decode error")] | ||
DecodeError(bs58::decode::Error), | ||
#[error("base58 decode error")] | ||
Base58DecodeError(#[from] bs58::decode::Error), | ||
#[error("base64 decode error")] | ||
Base64DecodeError(#[from] base64::DecodeError), | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] | ||
|
@@ -49,7 +103,14 @@ pub enum MemcmpEncoding { | |
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase", untagged)] | ||
pub enum MemcmpEncodedBytes { | ||
#[deprecated( | ||
since = "1.9.0", | ||
note = "Please use MemcmpEncodedBytes::Base58 instead" | ||
)] | ||
Binary(String), | ||
Base58(String), | ||
Base64(String), | ||
Bytes(Vec<u8>), | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] | ||
|
@@ -63,14 +124,18 @@ pub struct Memcmp { | |
} | ||
|
||
impl Memcmp { | ||
pub fn bytes_match(&self, data: &[u8]) -> bool { | ||
pub fn bytes(&self) -> Option<Cow<Vec<u8>>> { | ||
use MemcmpEncodedBytes::*; | ||
match &self.bytes { | ||
MemcmpEncodedBytes::Binary(bytes) => { | ||
let bytes = bs58::decode(bytes).into_vec(); | ||
if bytes.is_err() { | ||
return false; | ||
} | ||
let bytes = bytes.unwrap(); | ||
Binary(bytes) | Base58(bytes) => bs58::decode(bytes).into_vec().ok().map(Cow::Owned), | ||
Base64(bytes) => base64::decode(bytes).ok().map(Cow::Owned), | ||
Bytes(bytes) => Some(Cow::Borrowed(bytes)), | ||
} | ||
} | ||
|
||
pub fn bytes_match(&self, data: &[u8]) -> bool { | ||
match self.bytes() { | ||
Some(bytes) => { | ||
if self.offset > data.len() { | ||
return false; | ||
} | ||
|
@@ -79,6 +144,7 @@ impl Memcmp { | |
} | ||
data[self.offset..self.offset + bytes.len()] == bytes[..] | ||
} | ||
None => false, | ||
} | ||
} | ||
} | ||
|
@@ -87,62 +153,71 @@ 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]; | ||
|
||
// Exact match of data succeeds | ||
assert!(Memcmp { | ||
offset: 0, | ||
bytes: MemcmpEncodedBytes::Binary(bs58::encode(vec![1, 2, 3, 4, 5]).into_string()), | ||
bytes: MemcmpEncodedBytes::Base58(bs58::encode(vec![1, 2, 3, 4, 5]).into_string()), | ||
encoding: None, | ||
} | ||
.bytes_match(&data)); | ||
|
||
// Partial match of data succeeds | ||
assert!(Memcmp { | ||
offset: 0, | ||
bytes: MemcmpEncodedBytes::Binary(bs58::encode(vec![1, 2]).into_string()), | ||
bytes: MemcmpEncodedBytes::Base58(bs58::encode(vec![1, 2]).into_string()), | ||
encoding: None, | ||
} | ||
.bytes_match(&data)); | ||
|
||
// Offset partial match of data succeeds | ||
assert!(Memcmp { | ||
offset: 2, | ||
bytes: MemcmpEncodedBytes::Binary(bs58::encode(vec![3, 4]).into_string()), | ||
bytes: MemcmpEncodedBytes::Base58(bs58::encode(vec![3, 4]).into_string()), | ||
encoding: None, | ||
} | ||
.bytes_match(&data)); | ||
|
||
// Incorrect partial match of data fails | ||
assert!(!Memcmp { | ||
offset: 0, | ||
bytes: MemcmpEncodedBytes::Binary(bs58::encode(vec![2]).into_string()), | ||
bytes: MemcmpEncodedBytes::Base58(bs58::encode(vec![2]).into_string()), | ||
encoding: None, | ||
} | ||
.bytes_match(&data)); | ||
|
||
// Bytes overrun data fails | ||
assert!(!Memcmp { | ||
offset: 2, | ||
bytes: MemcmpEncodedBytes::Binary(bs58::encode(vec![3, 4, 5, 6]).into_string()), | ||
bytes: MemcmpEncodedBytes::Base58(bs58::encode(vec![3, 4, 5, 6]).into_string()), | ||
encoding: None, | ||
} | ||
.bytes_match(&data)); | ||
|
||
// Offset outside data fails | ||
assert!(!Memcmp { | ||
offset: 6, | ||
bytes: MemcmpEncodedBytes::Binary(bs58::encode(vec![5]).into_string()), | ||
bytes: MemcmpEncodedBytes::Base58(bs58::encode(vec![5]).into_string()), | ||
encoding: None, | ||
} | ||
.bytes_match(&data)); | ||
|
||
// Invalid base-58 fails | ||
assert!(!Memcmp { | ||
offset: 0, | ||
bytes: MemcmpEncodedBytes::Binary("III".to_string()), | ||
bytes: MemcmpEncodedBytes::Base58("III".to_string()), | ||
encoding: None, | ||
} | ||
.bytes_match(&data)); | ||
|
@@ -157,7 +232,7 @@ mod tests { | |
assert_eq!( | ||
RpcFilterType::Memcmp(Memcmp { | ||
offset: 0, | ||
bytes: MemcmpEncodedBytes::Binary(base58_bytes.to_string()), | ||
bytes: MemcmpEncodedBytes::Base58(base58_bytes.to_string()), | ||
encoding: None, | ||
}) | ||
.verify(), | ||
|
@@ -172,11 +247,11 @@ mod tests { | |
assert_eq!( | ||
RpcFilterType::Memcmp(Memcmp { | ||
offset: 0, | ||
bytes: MemcmpEncodedBytes::Binary(base58_bytes.to_string()), | ||
bytes: MemcmpEncodedBytes::Base58(base58_bytes.to_string()), | ||
encoding: None, | ||
}) | ||
.verify(), | ||
Err(RpcFilterError::Base58DataTooLarge) | ||
Err(RpcFilterError::DataTooLarge) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.