From 5a36ad9fd99d2a269e2c82a538aacf08dcf7eca2 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Sat, 25 Sep 2021 00:34:42 +0300 Subject: [PATCH] Rpc: decode memcmp before filtering accounts --- rpc/src/rpc.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/rpc/src/rpc.rs b/rpc/src/rpc.rs index 7d497e2cd84353..0de36204f1e89f 100644 --- a/rpc/src/rpc.rs +++ b/rpc/src/rpc.rs @@ -1789,8 +1789,9 @@ impl JsonRpcRequestProcessor { &self, bank: &Arc, program_id: &Pubkey, - filters: Vec, + mut filters: Vec, ) -> RpcCustomResult> { + optimize_filters(&mut filters); let filter_closure = |account: &AccountSharedData| { filters.iter().all(|filter_type| match filter_type { RpcFilterType::DataSize(size) => account.data().len() as u64 == *size, @@ -1861,6 +1862,7 @@ impl JsonRpcRequestProcessor { index_key: owner_key.to_string(), }); } + optimize_filters(&mut filters); Ok(bank .get_filtered_indexed_accounts(&IndexKey::SplTokenOwner(*owner_key), |account| { account.owner() == &spl_token_id_v2_0() @@ -1909,6 +1911,7 @@ impl JsonRpcRequestProcessor { index_key: mint_key.to_string(), }); } + optimize_filters(&mut filters); Ok(bank .get_filtered_indexed_accounts(&IndexKey::SplTokenMint(*mint_key), |account| { account.owner() == &spl_token_id_v2_0() @@ -1965,6 +1968,23 @@ impl JsonRpcRequestProcessor { } } +fn optimize_filters(filters: &mut Vec) { + filters.iter_mut().for_each(|filter_type| { + if let RpcFilterType::Memcmp(compare) = filter_type { + use MemcmpEncodedBytes::*; + match &compare.bytes { + Binary(bytes) | Base58(bytes) => { + compare.bytes = Bytes(bs58::decode(bytes).into_vec().unwrap()); + } + Base64(bytes) => { + compare.bytes = Bytes(base64::decode(bytes).unwrap()); + } + _ => {} + } + } + }) +} + fn verify_transaction( transaction: &SanitizedTransaction, feature_set: &Arc,