Skip to content

Commit

Permalink
Make get_token_supply() yieldy
Browse files Browse the repository at this point in the history
When this method reaches out to methods on bank that do reads, yield the thread
  • Loading branch information
steveluscher committed Dec 11, 2024
1 parent f45c5f1 commit 6fcf0b2
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ use {
bincode::{config::Options, serialize},
crossbeam_channel::{unbounded, Receiver, Sender},
jsonrpc_core::{
futures::future::{self},
futures::future::{self, OptionFuture},
types::error,
BoxFuture, Error, Metadata, Result,
},
@@ -1972,15 +1972,29 @@ impl JsonRpcRequestProcessor {
Ok(new_response(&bank, balance))
}

pub fn get_token_supply(
pub async fn get_token_supply(
&self,
mint: &Pubkey,
commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<UiTokenAmount>> {
let bank = self.bank(commitment);
let mint_account = bank.get_account(mint).ok_or_else(|| {
Error::invalid_params("Invalid param: could not find account".to_string())
})?;
let mint_account = self
.runtime
.spawn_blocking({
let bank = Arc::clone(&bank);
let mint = Pubkey::clone(mint);
move || {
bank.get_account(&mint)
.ok_or_else(|| {
Error::invalid_params(
"Invalid param: could not find account".to_string(),
)
})
.unwrap()
}
})
.await
.expect("Failed to spawn blocking task");
if !is_known_spl_token_id(mint_account.owner()) {
return Err(Error::invalid_params(
"Invalid param: not a Token mint".to_string(),
@@ -1990,10 +2004,22 @@ impl JsonRpcRequestProcessor {
Error::invalid_params("Invalid param: mint could not be unpacked".to_string())
})?;

let interest_bearing_config = mint
.get_extension::<InterestBearingConfig>()
.map(|x| (*x, bank.clock().unix_timestamp))
.ok();
let interest_bearing_config = OptionFuture::from(
mint.get_extension::<InterestBearingConfig>()
.map(|interest_bearing_config| {
let bank = Arc::clone(&bank);
async move {
let unix_timestamp = self
.runtime
.spawn_blocking(move || bank.clock().unix_timestamp)
.await
.expect("Failed to spawn blocking task");
(*interest_bearing_config, unix_timestamp)
}
})
.ok(),
)
.await;

let supply = token_amount_to_ui_amount_v2(
mint.base.supply,
@@ -3208,7 +3234,7 @@ pub mod rpc_accounts {
meta: Self::Metadata,
mint_str: String,
commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<UiTokenAmount>>;
) -> BoxFuture<Result<RpcResponse<UiTokenAmount>>>;
}

pub struct AccountsDataImpl;
@@ -3282,10 +3308,10 @@ pub mod rpc_accounts {
meta: Self::Metadata,
mint_str: String,
commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<UiTokenAmount>> {
) -> BoxFuture<Result<RpcResponse<UiTokenAmount>>> {
debug!("get_token_supply rpc request received: {:?}", mint_str);
let mint = verify_pubkey(&mint_str)?;
meta.get_token_supply(&mint, commitment)
let mint = verify_pubkey(&mint_str).map_err(Box::pin).unwrap();
Box::pin(async move { meta.get_token_supply(&mint, commitment).await })
}
}
}

0 comments on commit 6fcf0b2

Please sign in to comment.