From 808ba06c04dedc4dd1b577a971390c24800dcecf Mon Sep 17 00:00:00 2001 From: Tyera Date: Tue, 30 Jul 2024 15:30:55 -0600 Subject: [PATCH] Refactor match and add test cases (#2357) (cherry picked from commit 9c0a927058c551ee75ecdf99481a65ae7ede54b8) --- rpc-client-api/src/filter.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/rpc-client-api/src/filter.rs b/rpc-client-api/src/filter.rs index bef8d1d16e8e67..4c65f4249e3aed 100644 --- a/rpc-client-api/src/filter.rs +++ b/rpc-client-api/src/filter.rs @@ -121,9 +121,10 @@ impl<'de> Deserialize<'de> for MemcmpEncodedBytes { let memcmp_encoded_bytes = match data.bytes { DataType::Encoded(bytes) => match data.encoding.unwrap_or(RpcMemcmpEncoding::Base58) { - RpcMemcmpEncoding::Base58 => MemcmpEncodedBytes::Base58(bytes), + RpcMemcmpEncoding::Base58 | RpcMemcmpEncoding::Bytes => { + MemcmpEncodedBytes::Base58(bytes) + } RpcMemcmpEncoding::Base64 => MemcmpEncodedBytes::Base64(bytes), - _ => unreachable!(), }, DataType::Raw(bytes) => MemcmpEncodedBytes::Bytes(bytes), }; @@ -334,10 +335,14 @@ mod tests { formatcp!(r#"{{"bytes":"{BASE58_STR}","offset":{OFFSET},"encoding":"base58"}}"#); const BASE64_FILTER: &str = formatcp!(r#"{{"bytes":"{BASE64_STR}","offset":{OFFSET},"encoding":"base64"}}"#); + const MISMATCHED_BASE64_FILTER: &str = + formatcp!(r#"{{"bytes":[0, 1, 2, 3],"offset":{OFFSET},"encoding":"base64"}}"#); const BYTES_FILTER: &str = formatcp!(r#"{{"bytes":[0, 1, 2, 3],"offset":{OFFSET},"encoding":null}}"#); const BYTES_FILTER_WITH_ENCODING: &str = formatcp!(r#"{{"bytes":[0, 1, 2, 3],"offset":{OFFSET},"encoding":"bytes"}}"#); + const MISMATCHED_BYTES_FILTER_WITH_ENCODING: &str = + formatcp!(r#"{{"bytes":"{BASE58_STR}","offset":{OFFSET},"encoding":"bytes"}}"#); #[test] fn test_filter_deserialize() { @@ -393,6 +398,26 @@ mod tests { bytes: MemcmpEncodedBytes::Bytes(BYTES.to_vec()), } ); + + // Mismatched input + let base64_filter: Memcmp = serde_json::from_str(MISMATCHED_BASE64_FILTER).unwrap(); + assert_eq!( + base64_filter, + Memcmp { + offset: OFFSET, + bytes: MemcmpEncodedBytes::Bytes(BYTES.to_vec()), + } + ); + + let bytes_filter: Memcmp = + serde_json::from_str(MISMATCHED_BYTES_FILTER_WITH_ENCODING).unwrap(); + assert_eq!( + bytes_filter, + Memcmp { + offset: OFFSET, + bytes: MemcmpEncodedBytes::Base58(BASE58_STR.to_string()), + } + ); } #[test]