diff --git a/rpc/src/rpc.rs b/rpc/src/rpc.rs index 559610f802dd4e..c431b8ce37d059 100644 --- a/rpc/src/rpc.rs +++ b/rpc/src/rpc.rs @@ -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, ) -> Result> { 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::() - .map(|x| (*x, bank.clock().unix_timestamp)) - .ok(); + let interest_bearing_config = OptionFuture::from( + mint.get_extension::() + .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, - ) -> Result>; + ) -> BoxFuture>>; } pub struct AccountsDataImpl; @@ -3282,10 +3308,10 @@ pub mod rpc_accounts { meta: Self::Metadata, mint_str: String, commitment: Option, - ) -> Result> { + ) -> BoxFuture>> { 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 }) } } }