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

v2.0: Refactor match and add test cases (backport of #2357) #2359

Merged
merged 1 commit into from
Jul 31, 2024
Merged
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
29 changes: 27 additions & 2 deletions rpc-client-api/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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]
Expand Down