Skip to content

Commit

Permalink
Rpc: getProgramAccounts, restruct base58 account data size (solana-la…
Browse files Browse the repository at this point in the history
…bs#18852)

* Refactor account encoding to povide helper w/out querying Bank

* Use new method in get_program_accounts to properly return length err
  • Loading branch information
CriesofCarrots authored Jul 23, 2021
1 parent 611af87 commit 3eecb6f
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,19 +384,14 @@ impl JsonRpcRequestProcessor {
if program_id == &spl_token_id_v2_0() && encoding == UiAccountEncoding::JsonParsed {
get_parsed_token_accounts(bank.clone(), keyed_accounts.into_iter()).collect()
} else {
keyed_accounts
.into_iter()
.map(|(pubkey, account)| RpcKeyedAccount {
let mut encoded_accounts = vec![];
for (pubkey, account) in keyed_accounts {
encoded_accounts.push(RpcKeyedAccount {
pubkey: pubkey.to_string(),
account: UiAccount::encode(
&pubkey,
&account,
encoding,
None,
data_slice_config,
),
})
.collect()
account: encode_account(&account, &pubkey, encoding, data_slice_config)?,
});
}
encoded_accounts
};
Ok(result).map(|result| match with_context {
true => OptionalContext::Context(new_response(&bank, result)),
Expand Down Expand Up @@ -1995,26 +1990,41 @@ fn get_encoded_account(
encoding: UiAccountEncoding,
data_slice: Option<UiDataSliceConfig>,
) -> Result<Option<UiAccount>> {
let mut response = None;
if let Some(account) = bank.get_account(pubkey) {
if account.owner() == &spl_token_id_v2_0() && encoding == UiAccountEncoding::JsonParsed {
response = Some(get_parsed_token_account(bank.clone(), pubkey, account));
} else if (encoding == UiAccountEncoding::Binary || encoding == UiAccountEncoding::Base58)
&& account.data().len() > 128
{
let message = "Encoded binary (base 58) data should be less than 128 bytes, please use Base64 encoding.".to_string();
return Err(error::Error {
code: error::ErrorCode::InvalidRequest,
message,
data: None,
});
} else {
response = Some(UiAccount::encode(
pubkey, &account, encoding, None, data_slice,
));
match bank.get_account(pubkey) {
Some(account) => {
let response = if account.owner() == &spl_token_id_v2_0()
&& encoding == UiAccountEncoding::JsonParsed
{
get_parsed_token_account(bank.clone(), pubkey, account)
} else {
encode_account(&account, pubkey, encoding, data_slice)?
};
Ok(Some(response))
}
None => Ok(None),
}
}

fn encode_account<T: ReadableAccount>(
account: &T,
pubkey: &Pubkey,
encoding: UiAccountEncoding,
data_slice: Option<UiDataSliceConfig>,
) -> Result<UiAccount> {
if (encoding == UiAccountEncoding::Binary || encoding == UiAccountEncoding::Base58)
&& account.data().len() > 128
{
let message = "Encoded binary (base 58) data should be less than 128 bytes, please use Base64 encoding.".to_string();
Err(error::Error {
code: error::ErrorCode::InvalidRequest,
message,
data: None,
})
} else {
Ok(UiAccount::encode(
pubkey, account, encoding, None, data_slice,
))
}
Ok(response)
}

fn get_spl_token_owner_filter(program_id: &Pubkey, filters: &[RpcFilterType]) -> Option<Pubkey> {
Expand Down

0 comments on commit 3eecb6f

Please sign in to comment.