Returning only program id, when there are no filters and dataslice lenghth is 0

This commit is contained in:
godmodegalactus 2024-04-09 14:25:00 +02:00
parent b1d8e4181f
commit de5362d4bb
No known key found for this signature in database
GPG Key ID: 22DA4A30887FDA3C
11 changed files with 61 additions and 25 deletions

View File

@ -1065,6 +1065,7 @@ impl Accounts {
filter: F,
config: &ScanConfig,
byte_limit_for_scan: Option<usize>,
just_get_program_ids: bool,
) -> ScanResult<Vec<TransactionAccount>> {
let sum = AtomicUsize::default();
let config = config.recreate_with_abort();
@ -1077,21 +1078,30 @@ impl Accounts {
*index_key,
|some_account_tuple| {
Self::load_while_filtering(&mut collector, some_account_tuple, |account| {
let use_account = filter(account);
if use_account
&& Self::accumulate_and_check_scan_result_size(
if just_get_program_ids {
Self::accumulate_and_check_scan_result_size(
&sum,
account,
&byte_limit_for_scan,
)
{
// total size of results exceeds size limit, so abort scan
config.abort();
} else {
let use_account = filter(account);
if use_account
&& Self::accumulate_and_check_scan_result_size(
&sum,
account,
&byte_limit_for_scan,
)
{
// total size of results exceeds size limit, so abort scan
config.abort();
}
use_account
}
use_account
});
},
&config,
just_get_program_ids,
)
.map(|_| collector);
Self::maybe_abort_scan(result, &config)

View File

@ -4947,6 +4947,7 @@ impl AccountsDb {
index_key: IndexKey,
mut scan_func: F,
config: &ScanConfig,
just_get_program_ids: bool,
) -> ScanResult<bool>
where
F: FnMut(Option<(&Pubkey, AccountSharedData, Slot)>),
@ -4968,11 +4969,16 @@ impl AccountsDb {
bank_id,
index_key,
|pubkey, (account_info, slot)| {
let account_slot = self
.get_account_accessor(slot, pubkey, &account_info.storage_location())
.get_loaded_account()
.map(|loaded_account| (pubkey, loaded_account.take_account(), slot));
scan_func(account_slot)
if just_get_program_ids {
let dummy_shared_data = AccountSharedData::new(0, 0, key);
scan_func(Some((pubkey, dummy_shared_data, slot)))
} else {
let account_slot = self
.get_account_accessor(slot, pubkey, &account_info.storage_location())
.get_loaded_account()
.map(|loaded_account| (pubkey, loaded_account.take_account(), slot));
scan_func(account_slot)
}
},
config,
)?;

View File

@ -105,7 +105,7 @@ impl Default for ScanConfig {
fn default() -> Self {
Self {
collect_all_unsorted: true,
abort: None
abort: None,
}
}
}

View File

@ -486,6 +486,12 @@ impl JsonRpcRequestProcessor {
min_context_slot,
})?;
let encoding = encoding.unwrap_or(UiAccountEncoding::Binary);
let just_get_program_ids = data_slice_config
.clone()
.map(|x| x.length == 0)
.unwrap_or_default()
&& filters.len() == 0;
optimize_filters(&mut filters);
let keyed_accounts = {
if let Some(owner) = get_spl_token_owner_filter(program_id, &filters) {
@ -493,7 +499,12 @@ impl JsonRpcRequestProcessor {
} else if let Some(mint) = get_spl_token_mint_filter(program_id, &filters) {
self.get_filtered_spl_token_accounts_by_mint(&bank, program_id, &mint, filters)?
} else {
self.get_filtered_program_accounts(&bank, program_id, filters)?
self.get_filtered_program_accounts(
&bank,
program_id,
filters,
just_get_program_ids,
)?
}
};
let accounts = if is_known_spl_token_id(program_id)
@ -1969,7 +1980,7 @@ impl JsonRpcRequestProcessor {
} else {
// Filter on Token Account state
filters.push(RpcFilterType::TokenAccountState);
self.get_filtered_program_accounts(&bank, &token_program_id, filters)?
self.get_filtered_program_accounts(&bank, &token_program_id, filters, false)?
};
let accounts = if encoding == UiAccountEncoding::JsonParsed {
get_parsed_token_accounts(bank.clone(), keyed_accounts.into_iter()).collect()
@ -1993,6 +2004,7 @@ impl JsonRpcRequestProcessor {
bank: &Bank,
program_id: &Pubkey,
mut filters: Vec<RpcFilterType>,
just_get_program_ids: bool,
) -> RpcCustomResult<Vec<(Pubkey, AccountSharedData)>> {
optimize_filters(&mut filters);
let filter_closure = |account: &AccountSharedData| {
@ -2023,6 +2035,7 @@ impl JsonRpcRequestProcessor {
},
&ScanConfig::default(),
bank.byte_limit_for_scans(),
just_get_program_ids,
)
.map_err(|e| RpcCustomError::ScanError {
message: e.to_string(),
@ -2079,12 +2092,13 @@ impl JsonRpcRequestProcessor {
},
&ScanConfig::default(),
bank.byte_limit_for_scans(),
false,
)
.map_err(|e| RpcCustomError::ScanError {
message: e.to_string(),
})?)
} else {
self.get_filtered_program_accounts(bank, program_id, filters)
self.get_filtered_program_accounts(bank, program_id, filters, false)
}
}
@ -2129,12 +2143,13 @@ impl JsonRpcRequestProcessor {
},
&ScanConfig::default(),
bank.byte_limit_for_scans(),
false,
)
.map_err(|e| RpcCustomError::ScanError {
message: e.to_string(),
})?)
} else {
self.get_filtered_program_accounts(bank, program_id, filters)
self.get_filtered_program_accounts(bank, program_id, filters, false)
}
}

View File

@ -6818,6 +6818,7 @@ impl Bank {
filter: F,
config: &ScanConfig,
byte_limit_for_scan: Option<usize>,
just_get_program_ids: bool,
) -> ScanResult<Vec<TransactionAccount>> {
self.rc.accounts.load_by_index_key_with_filter(
&self.ancestors,
@ -6826,6 +6827,7 @@ impl Bank {
filter,
config,
byte_limit_for_scan,
just_get_program_ids,
)
}

View File

@ -4329,6 +4329,7 @@ fn test_get_filtered_indexed_accounts_limit_exceeded() {
|_| true,
&ScanConfig::default(),
Some(limit), // limit here will be exceeded, resulting in aborted scan
false,
)
.is_err());
}
@ -4355,6 +4356,7 @@ fn test_get_filtered_indexed_accounts() {
|_| true,
&ScanConfig::default(),
None,
false,
)
.unwrap();
assert_eq!(indexed_accounts.len(), 1);
@ -4373,6 +4375,7 @@ fn test_get_filtered_indexed_accounts() {
|_| true,
&ScanConfig::default(),
None,
false,
)
.unwrap();
assert_eq!(indexed_accounts.len(), 1);
@ -4383,6 +4386,7 @@ fn test_get_filtered_indexed_accounts() {
|_| true,
&ScanConfig::default(),
None,
false,
)
.unwrap();
assert_eq!(indexed_accounts.len(), 1);
@ -4395,6 +4399,7 @@ fn test_get_filtered_indexed_accounts() {
|account| account.owner() == &program_id,
&ScanConfig::default(),
None,
false,
)
.unwrap();
assert!(indexed_accounts.is_empty());
@ -4404,6 +4409,7 @@ fn test_get_filtered_indexed_accounts() {
|account| account.owner() == &another_program_id,
&ScanConfig::default(),
None,
false,
)
.unwrap();
assert_eq!(indexed_accounts.len(), 1);

View File

@ -43,6 +43,7 @@ pub fn calculate_non_circulating_supply(bank: &Bank) -> ScanResult<NonCirculatin
|account| account.owner() == &stake::program::id(),
config,
None,
false,
)?
} else {
bank.get_program_accounts(&stake::program::id(), config)?

View File

@ -88,8 +88,7 @@ impl FromStr for Hash {
if s.len() > MAX_BASE58_LEN {
return Err(ParseHashError::WrongSize);
}
let bytes = fd_bs58::decode_32(s)
.map_err(|_| ParseHashError::Invalid)?;
let bytes = fd_bs58::decode_32(s).map_err(|_| ParseHashError::Invalid)?;
if bytes.len() != mem::size_of::<Hash>() {
Err(ParseHashError::WrongSize)
} else {

View File

@ -109,8 +109,7 @@ impl FromStr for Hash {
if s.len() > MAX_BASE58_LEN {
return Err(ParseHashError::WrongSize);
}
let bytes = fd_bs58::decode_32(s)
.map_err(|_| ParseHashError::Invalid)?;
let bytes = fd_bs58::decode_32(s).map_err(|_| ParseHashError::Invalid)?;
if bytes.len() != mem::size_of::<Hash>() {
Err(ParseHashError::WrongSize)
} else {

View File

@ -88,8 +88,7 @@ impl FromStr for Hash {
if s.len() > MAX_BASE58_LEN {
return Err(ParseHashError::WrongSize);
}
let bytes = fd_bs58::decode_32(s)
.map_err(|_| ParseHashError::Invalid)?;
let bytes = fd_bs58::decode_32(s).map_err(|_| ParseHashError::Invalid)?;
if bytes.len() != mem::size_of::<Hash>() {
Err(ParseHashError::WrongSize)
} else {

View File

@ -115,8 +115,7 @@ impl FromStr for Pubkey {
if s.len() > MAX_BASE58_LEN {
return Err(ParsePubkeyError::WrongSize);
}
let pubkey_vec = fd_bs58::decode_32(s)
.map_err(|_| ParsePubkeyError::Invalid)?;
let pubkey_vec = fd_bs58::decode_32(s).map_err(|_| ParsePubkeyError::Invalid)?;
if pubkey_vec.len() != mem::size_of::<Pubkey>() {
Err(ParsePubkeyError::WrongSize)
} else {