From c053df143f800258dcc70c64a4996fb84b6d3076 Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Tue, 17 Aug 2021 16:32:58 -0700 Subject: [PATCH] RPC: add option to exclude accounts from get_supply (#19270) --- client/src/rpc_config.rs | 9 +++++ docs/src/developing/clients/jsonrpc-api.md | 6 ++-- rpc/src/rpc.rs | 42 ++++++++++++++++------ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/client/src/rpc_config.rs b/client/src/rpc_config.rs index 9d236794e1..cd9e2fa11d 100644 --- a/client/src/rpc_config.rs +++ b/client/src/rpc_config.rs @@ -116,6 +116,15 @@ pub struct RpcLargestAccountsConfig { pub filter: Option, } +#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct RpcSupplyConfig { + #[serde(flatten)] + pub commitment: Option, + #[serde(default)] + pub exclude_non_circulating_accounts_list: bool, +} + #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct RpcEpochConfig { diff --git a/docs/src/developing/clients/jsonrpc-api.md b/docs/src/developing/clients/jsonrpc-api.md index 088ebbac80..bc1642a9de 100644 --- a/docs/src/developing/clients/jsonrpc-api.md +++ b/docs/src/developing/clients/jsonrpc-api.md @@ -2413,7 +2413,9 @@ Returns information about the current supply. #### Parameters: -- `` - (optional) [Commitment](jsonrpc-api.md#configuring-state-commitment) +- `` - (optional) Configuration object containing the following optional fields: + - (optional) [Commitment](jsonrpc-api.md#configuring-state-commitment) + - (optional) `excludeNonCirculatingAccountsList: ` - exclude non circulating accounts list from response #### Results: @@ -2422,7 +2424,7 @@ The result will be an RpcResponse JSON object with `value` equal to a JSON objec - `total: ` - Total supply in lamports - `circulating: ` - Circulating supply in lamports - `nonCirculating: ` - Non-circulating supply in lamports -- `nonCirculatingAccounts: ` - an array of account addresses of non-circulating accounts, as strings +- `nonCirculatingAccounts: ` - an array of account addresses of non-circulating accounts, as strings. If `excludeNonCirculatingAccountsList` is enabled, the returned array will be empty. #### Example: diff --git a/rpc/src/rpc.rs b/rpc/src/rpc.rs index dacf1dc696..1545d6d65d 100644 --- a/rpc/src/rpc.rs +++ b/rpc/src/rpc.rs @@ -763,25 +763,32 @@ impl JsonRpcRequestProcessor { fn get_supply( &self, - commitment: Option, + config: Option, ) -> RpcCustomResult> { - let bank = self.bank(commitment); + let config = config.unwrap_or_default(); + let bank = self.bank(config.commitment); let non_circulating_supply = calculate_non_circulating_supply(&bank).map_err(|e| RpcCustomError::ScanError { message: e.to_string(), })?; let total_supply = bank.capitalization(); + let non_circulating_accounts = if config.exclude_non_circulating_accounts_list { + vec![] + } else { + non_circulating_supply + .accounts + .iter() + .map(|pubkey| pubkey.to_string()) + .collect() + }; + Ok(new_response( &bank, RpcSupply { total: total_supply, circulating: total_supply - non_circulating_supply.lamports, non_circulating: non_circulating_supply.lamports, - non_circulating_accounts: non_circulating_supply - .accounts - .iter() - .map(|pubkey| pubkey.to_string()) - .collect(), + non_circulating_accounts, }, )) } @@ -2702,7 +2709,7 @@ pub mod rpc_accounts { fn get_supply( &self, meta: Self::Metadata, - commitment: Option, + config: Option, ) -> Result>; #[rpc(meta, name = "getStakeActivation")] @@ -2856,10 +2863,10 @@ pub mod rpc_accounts { fn get_supply( &self, meta: Self::Metadata, - commitment: Option, + config: Option, ) -> Result> { debug!("get_supply rpc request received"); - Ok(meta.get_supply(commitment)?) + Ok(meta.get_supply(config)?) } fn get_stake_activation( @@ -4655,6 +4662,21 @@ pub mod tests { } } + #[test] + fn test_get_supply_exclude_account_list() { + let bob_pubkey = solana_sdk::pubkey::new_rand(); + let RpcHandler { io, meta, .. } = start_rpc_handler_with_tx(&bob_pubkey); + let req = r#"{"jsonrpc":"2.0","id":1,"method":"getSupply","params":[{"excludeNonCirculatingAccountsList":true}]}"#; + let res = io.handle_request_sync(req, meta); + let json: Value = serde_json::from_str(&res.unwrap()).unwrap(); + let supply: RpcSupply = serde_json::from_value(json["result"]["value"].clone()) + .expect("actual response deserialization"); + assert_eq!(supply.non_circulating, 20); + assert!(supply.circulating >= TEST_MINT_LAMPORTS); + assert!(supply.total >= TEST_MINT_LAMPORTS + 20); + assert!(supply.non_circulating_accounts.is_empty()); + } + #[test] fn test_get_largest_accounts() { let bob_pubkey = solana_sdk::pubkey::new_rand();