diff --git a/docs/src/developing/clients/jsonrpc-api.md b/docs/src/developing/clients/jsonrpc-api.md index 8cdc6b3bb58d69..7851410da3a781 100644 --- a/docs/src/developing/clients/jsonrpc-api.md +++ b/docs/src/developing/clients/jsonrpc-api.md @@ -387,8 +387,13 @@ Returns identity and transaction information about a confirmed block in the ledg - `` - slot, as u64 integer - `` - (optional) Configuration object containing the following optional fields: - (optional) `encoding: ` - encoding for each returned Transaction, either "json", "jsonParsed", "base58" (_slow_), "base64". If parameter not provided, the default encoding is "json". +<<<<<<< HEAD "jsonParsed" encoding attempts to use program-specific instruction parsers to return more human-readable and explicit data in the `transaction.message.instructions` list. If "jsonParsed" is requested but a parser cannot be found, the instruction falls back to regular JSON encoding (`accounts`, `data`, and `programIdIndex` fields). - (optional) `transactionDetails: ` - level of transaction detail to return, either "full", "signatures", or "none". If parameter not provided, the default detail level is "full". +======= + ["jsonParsed" encoding](jsonrpc-api.md#parsed-responses) attempts to use program-specific instruction parsers to return more human-readable and explicit data in the `transaction.message.instructions` list. If "jsonParsed" is requested but a parser cannot be found, the instruction falls back to regular JSON encoding (`accounts`, `data`, and `programIdIndex` fields). + - (optional) `transactionDetails: ` - level of transaction detail to return, either "full", "accounts", "signatures", or "none". If parameter not provided, the default detail level is "full". If "accounts" are requested, transaction details only include signatures and an annotated list of accounts in each transaction. Transaction metadata is limited to only: fee, err, pre_balances, post_balances, pre_token_balances, and post_token_balances. +>>>>>>> 973a5fb0e (Add new TransactionDetails level (#27601)) - (optional) `rewards: bool` - whether to populate the `rewards` array. If parameter not provided, the default includes rewards. - (optional) [Commitment](jsonrpc-api.md#configuring-state-commitment); "processed" is not supported. If parameter not provided, the default is "finalized". - (optional) `maxSupportedTransactionVersion: ` - set the max transaction version to return in responses. If the requested block contains a transaction with a higher version, an error will be returned. If this parameter is omitted, only legacy transactions will be returned, and a block containing any versioned transaction will prompt the error. diff --git a/transaction-status/src/lib.rs b/transaction-status/src/lib.rs index 3276f972f856b7..fdf103891c6378 100644 --- a/transaction-status/src/lib.rs +++ b/transaction-status/src/lib.rs @@ -73,6 +73,11 @@ pub trait EncodableWithMeta { fn json_encode(&self) -> Self::Encoded; } +trait JsonAccounts { + type Encoded; + fn build_json_accounts(&self) -> Self::Encoded; +} + #[derive(Serialize, Deserialize, Clone, Copy, Debug, Eq, Hash, PartialEq)] #[serde(rename_all = "camelCase")] pub enum TransactionBinaryEncoding { @@ -114,6 +119,7 @@ pub enum TransactionDetails { Full, Signatures, None, + Accounts, } impl Default for TransactionDetails { @@ -409,6 +415,34 @@ impl UiTransactionStatusMeta { loaded_addresses: OptionSerializer::Skip, } } + + fn build_simple(meta: TransactionStatusMeta, show_rewards: bool) -> Self { + Self { + err: meta.status.clone().err(), + status: meta.status, + fee: meta.fee, + pre_balances: meta.pre_balances, + post_balances: meta.post_balances, + inner_instructions: OptionSerializer::Skip, + log_messages: OptionSerializer::Skip, + pre_token_balances: meta + .pre_token_balances + .map(|balance| balance.into_iter().map(Into::into).collect()) + .into(), + post_token_balances: meta + .post_token_balances + .map(|balance| balance.into_iter().map(Into::into).collect()) + .into(), + rewards: if show_rewards { + meta.rewards.into() + } else { + OptionSerializer::Skip + }, + loaded_addresses: OptionSerializer::Skip, + return_data: OptionSerializer::Skip, + compute_units_consumed: OptionSerializer::Skip, + } + } } impl From for UiTransactionStatusMeta { @@ -586,6 +620,20 @@ impl ConfirmedBlock { ), ), TransactionDetails::None => (None, None), + TransactionDetails::Accounts => ( + Some( + self.transactions + .into_iter() + .map(|tx_with_meta| { + tx_with_meta.build_json_accounts( + options.max_supported_transaction_version, + options.show_rewards, + ) + }) + .collect::, _>>()?, + ), + None, + ), }; Ok(UiConfirmedBlock { previous_blockhash: self.previous_blockhash, @@ -709,16 +757,31 @@ impl TransactionWithStatusMeta { Self::Complete(tx_with_meta) => tx_with_meta.account_keys(), } } -} -impl VersionedTransactionWithStatusMeta { - pub fn encode( + fn build_json_accounts( self, - encoding: UiTransactionEncoding, max_supported_transaction_version: Option, show_rewards: bool, ) -> Result { - let version = match ( + match self { + Self::MissingMetadata(ref transaction) => Ok(EncodedTransactionWithStatusMeta { + version: None, + transaction: transaction.build_json_accounts(), + meta: None, + }), + Self::Complete(tx_with_meta) => { + tx_with_meta.build_json_accounts(max_supported_transaction_version, show_rewards) + } + } + } +} + +impl VersionedTransactionWithStatusMeta { + fn validate_version( + &self, + max_supported_transaction_version: Option, + ) -> Result, EncodeError> { + match ( max_supported_transaction_version, self.transaction.version(), ) { @@ -735,7 +798,16 @@ impl VersionedTransactionWithStatusMeta { Err(EncodeError::UnsupportedTransactionVersion(version)) } } - }?; + } + } + + pub fn encode( + self, + encoding: UiTransactionEncoding, + max_supported_transaction_version: Option, + show_rewards: bool, + ) -> Result { + let version = self.validate_version(max_supported_transaction_version)?; Ok(EncodedTransactionWithStatusMeta { transaction: self.transaction.encode_with_meta(encoding, &self.meta), @@ -763,6 +835,40 @@ impl VersionedTransactionWithStatusMeta { Some(&self.meta.loaded_addresses), ) } + + fn build_json_accounts( + self, + max_supported_transaction_version: Option, + show_rewards: bool, + ) -> Result { + let version = self.validate_version(max_supported_transaction_version)?; + + let account_keys = match &self.transaction.message { + VersionedMessage::Legacy(message) => parse_legacy_message_accounts(message), + VersionedMessage::V0(message) => { + let loaded_message = + LoadedMessage::new_borrowed(message, &self.meta.loaded_addresses); + parse_v0_message_accounts(&loaded_message) + } + }; + + Ok(EncodedTransactionWithStatusMeta { + transaction: EncodedTransaction::Accounts(UiAccountsList { + signatures: self + .transaction + .signatures + .iter() + .map(ToString::to_string) + .collect(), + account_keys, + }), + meta: Some(UiTransactionStatusMeta::build_simple( + self.meta, + show_rewards, + )), + version, + }) + } } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -825,6 +931,7 @@ pub enum EncodedTransaction { LegacyBinary(String), // Old way of expressing base-58, retained for RPC backwards compatibility Binary(String, TransactionBinaryEncoding), Json(UiTransaction), + Accounts(UiAccountsList), } impl EncodableWithMeta for VersionedTransaction { @@ -896,10 +1003,20 @@ impl Encodable for Transaction { } } +impl JsonAccounts for Transaction { + type Encoded = EncodedTransaction; + fn build_json_accounts(&self) -> Self::Encoded { + EncodedTransaction::Accounts(UiAccountsList { + signatures: self.signatures.iter().map(ToString::to_string).collect(), + account_keys: parse_legacy_message_accounts(&self.message), + }) + } +} + impl EncodedTransaction { pub fn decode(&self) -> Option { let (blob, encoding) = match self { - Self::Json(_) => return None, + Self::Json(_) | Self::Accounts(_) => return None, Self::LegacyBinary(blob) => (blob, TransactionBinaryEncoding::Base58), Self::Binary(blob, encoding) => (blob, *encoding), }; @@ -1017,6 +1134,13 @@ pub struct UiRawMessage { pub address_table_lookups: Option>, } +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct UiAccountsList { + pub signatures: Vec, + pub account_keys: Vec, +} + /// A duplicate representation of a MessageAddressTableLookup, in raw format, for pretty JSON serialization #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")]