diff --git a/cli/src/cluster_query.rs b/cli/src/cluster_query.rs index a0b5d83a8bcc9a..5d8f7bafb49d15 100644 --- a/cli/src/cluster_query.rs +++ b/cli/src/cluster_query.rs @@ -2019,16 +2019,17 @@ pub fn process_transaction_history( RpcTransactionConfig { encoding: Some(UiTransactionEncoding::Base64), commitment: Some(CommitmentConfig::confirmed()), + max_supported_transaction_version: None, }, ) { Ok(confirmed_transaction) => { println_transaction( &confirmed_transaction - .transaction + .tx_with_meta .transaction .decode() .expect("Successful decode"), - &confirmed_transaction.transaction.meta, + &confirmed_transaction.tx_with_meta.meta, " ", None, None, diff --git a/cli/src/wallet.rs b/cli/src/wallet.rs index fbab92c5dbff0b..1a5f13826f570a 100644 --- a/cli/src/wallet.rs +++ b/cli/src/wallet.rs @@ -556,11 +556,12 @@ pub fn process_confirm( RpcTransactionConfig { encoding: Some(UiTransactionEncoding::Base64), commitment: Some(CommitmentConfig::confirmed()), + max_supported_transaction_version: None, }, ) { Ok(confirmed_transaction) => { let decoded_transaction = confirmed_transaction - .transaction + .tx_with_meta .transaction .decode() .expect("Successful decode"); @@ -569,7 +570,7 @@ pub fn process_confirm( transaction = Some(CliTransaction { transaction: json_transaction, - meta: confirmed_transaction.transaction.meta, + meta: confirmed_transaction.tx_with_meta.meta, block_time: confirmed_transaction.block_time, slot: Some(confirmed_transaction.slot), decoded_transaction, diff --git a/client-test/tests/client.rs b/client-test/tests/client.rs index f29ea043a3cf79..c0d696f017decb 100644 --- a/client-test/tests/client.rs +++ b/client-test/tests/client.rs @@ -36,7 +36,9 @@ use { }, solana_streamer::socket::SocketAddrSpace, solana_test_validator::TestValidator, - solana_transaction_status::{ConfirmedBlock, TransactionDetails, UiTransactionEncoding}, + solana_transaction_status::{ + BlockEncodingOptions, ConfirmedBlock, TransactionDetails, UiTransactionEncoding, + }, std::{ collections::HashSet, net::{IpAddr, SocketAddr}, @@ -270,6 +272,7 @@ fn test_block_subscription() { encoding: Some(UiTransactionEncoding::Json), transaction_details: Some(TransactionDetails::Signatures), show_rewards: None, + max_supported_transaction_version: None, }), ) .unwrap(); @@ -281,14 +284,17 @@ fn test_block_subscription() { match maybe_actual { Ok(actual) => { let versioned_block = blockstore.get_complete_block(slot, false).unwrap(); - let legacy_block = ConfirmedBlock::from(versioned_block) - .into_legacy_block() + let confirmed_block = ConfirmedBlock::from(versioned_block); + let block = confirmed_block + .encode_with_options( + UiTransactionEncoding::Json, + BlockEncodingOptions { + transaction_details: TransactionDetails::Signatures, + show_rewards: false, + max_supported_transaction_version: None, + }, + ) .unwrap(); - let block = legacy_block.configure( - UiTransactionEncoding::Json, - TransactionDetails::Signatures, - false, - ); assert_eq!(actual.value.slot, slot); assert!(block.eq(&actual.value.block.unwrap())); } diff --git a/client/src/mock_sender.rs b/client/src/mock_sender.rs index aa301bc4265170..0d6a2649bd3d08 100644 --- a/client/src/mock_sender.rs +++ b/client/src/mock_sender.rs @@ -28,7 +28,7 @@ use { pubkey::Pubkey, signature::Signature, sysvar::epoch_schedule::EpochSchedule, - transaction::{self, Transaction, TransactionError}, + transaction::{self, Transaction, TransactionError, TransactionVersion}, }, solana_transaction_status::{ EncodedConfirmedBlock, EncodedConfirmedTransactionWithStatusMeta, EncodedTransaction, @@ -191,7 +191,8 @@ impl RpcSender for MockSender { } "getTransaction" => serde_json::to_value(EncodedConfirmedTransactionWithStatusMeta { slot: 2, - transaction: EncodedTransactionWithStatusMeta { + tx_with_meta: EncodedTransactionWithStatusMeta { + version: Some(TransactionVersion::Legacy), transaction: EncodedTransaction::Json( UiTransaction { signatures: vec!["3AsdoALgZFuq2oUVWrDYhg2pNeaLJKPLf8hU2mQ6U8qJxeJ6hsrPVpMn9ma39DtfYCrDQSvngWRP8NnTpEhezJpE".to_string()], @@ -213,6 +214,7 @@ impl RpcSender for MockSender { accounts: vec![0, 1], data: "3Bxs49DitAvXtoDR".to_string(), }], + address_table_lookups: None, }) }), meta: Some(UiTransactionStatusMeta { @@ -226,6 +228,7 @@ impl RpcSender for MockSender { pre_token_balances: None, post_token_balances: None, rewards: None, + loaded_addresses: None, }), }, block_time: Some(1628633791), @@ -381,6 +384,7 @@ impl RpcSender for MockSender { UiTransactionEncoding::Base58, ), meta: None, + version: Some(TransactionVersion::Legacy), }], rewards: Rewards::new(), block_time: None, diff --git a/client/src/rpc_config.rs b/client/src/rpc_config.rs index edf3dc819877d8..380d180a5af37a 100644 --- a/client/src/rpc_config.rs +++ b/client/src/rpc_config.rs @@ -197,6 +197,7 @@ pub struct RpcBlockSubscribeConfig { pub encoding: Option, pub transaction_details: Option, pub show_rewards: Option, + pub max_supported_transaction_version: Option, } #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] @@ -248,6 +249,7 @@ pub struct RpcBlockConfig { pub rewards: Option, #[serde(flatten)] pub commitment: Option, + pub max_supported_transaction_version: Option, } impl EncodingConfig for RpcBlockConfig { @@ -288,6 +290,7 @@ pub struct RpcTransactionConfig { pub encoding: Option, #[serde(flatten)] pub commitment: Option, + pub max_supported_transaction_version: Option, } impl EncodingConfig for RpcTransactionConfig { diff --git a/client/src/rpc_custom_error.rs b/client/src/rpc_custom_error.rs index 2f1bcd7e1753ce..ae8fc545c55069 100644 --- a/client/src/rpc_custom_error.rs +++ b/client/src/rpc_custom_error.rs @@ -3,6 +3,7 @@ use { crate::rpc_response::RpcSimulateTransactionResult, jsonrpc_core::{Error, ErrorCode}, solana_sdk::clock::Slot, + solana_transaction_status::EncodeError, thiserror::Error, }; @@ -59,7 +60,7 @@ pub enum RpcCustomError { #[error("BlockStatusNotAvailableYet")] BlockStatusNotAvailableYet { slot: Slot }, #[error("UnsupportedTransactionVersion")] - UnsupportedTransactionVersion, + UnsupportedTransactionVersion(u8), } #[derive(Debug, Serialize, Deserialize)] @@ -68,6 +69,16 @@ pub struct NodeUnhealthyErrorData { pub num_slots_behind: Option, } +impl From for RpcCustomError { + fn from(err: EncodeError) -> Self { + match err { + EncodeError::UnsupportedTransactionVersion(version) => { + Self::UnsupportedTransactionVersion(version) + } + } + } +} + impl From for Error { fn from(e: RpcCustomError) -> Self { match e { @@ -172,9 +183,9 @@ impl From for Error { message: format!("Block status not yet available for slot {}", slot), data: None, }, - RpcCustomError::UnsupportedTransactionVersion => Self { + RpcCustomError::UnsupportedTransactionVersion(version) => Self { code: ErrorCode::ServerError(JSON_RPC_SERVER_ERROR_UNSUPPORTED_TRANSACTION_VERSION), - message: "Versioned transactions are not supported".to_string(), + message: format!("Transaction version ({}) is not supported", version), data: None, }, } diff --git a/client/src/rpc_deprecated_config.rs b/client/src/rpc_deprecated_config.rs index 9062513ba55d79..eb4fbaea8c323d 100644 --- a/client/src/rpc_deprecated_config.rs +++ b/client/src/rpc_deprecated_config.rs @@ -71,6 +71,7 @@ impl From for RpcBlockConfig { transaction_details: config.transaction_details, rewards: config.rewards, commitment: config.commitment, + max_supported_transaction_version: None, } } } @@ -98,6 +99,7 @@ impl From for RpcTransactionConfig { Self { encoding: config.encoding, commitment: config.commitment, + max_supported_transaction_version: None, } } } diff --git a/client/src/rpc_response.rs b/client/src/rpc_response.rs index a625072ebb06cd..169b52e4e2f5ea 100644 --- a/client/src/rpc_response.rs +++ b/client/src/rpc_response.rs @@ -432,8 +432,8 @@ pub enum RpcBlockUpdateError { #[error("block store error")] BlockStoreError, - #[error("unsupported transaction version")] - UnsupportedTransactionVersion, + #[error("unsupported transaction version ({0})")] + UnsupportedTransactionVersion(u8), } #[derive(Serialize, Deserialize, Debug)] diff --git a/ledger-tool/src/bigtable.rs b/ledger-tool/src/bigtable.rs index 77144cac938dc3..f9884ec5f1a737 100644 --- a/ledger-tool/src/bigtable.rs +++ b/ledger-tool/src/bigtable.rs @@ -16,7 +16,9 @@ use { }, solana_ledger::{blockstore::Blockstore, blockstore_db::AccessType}, solana_sdk::{clock::Slot, pubkey::Pubkey, signature::Signature}, - solana_transaction_status::{Encodable, LegacyConfirmedBlock, UiTransactionEncoding}, + solana_transaction_status::{ + BlockEncodingOptions, Encodable, EncodeError, LegacyConfirmedBlock, UiTransactionEncoding, + }, std::{ collections::HashSet, path::Path, @@ -72,12 +74,25 @@ async fn block(slot: Slot, output_format: OutputFormat) -> Result<(), Box { + format!( + "Failed to process unsupported transaction version ({}) in block", + version + ) + } + })?; let cli_block = CliBlock { - encoded_confirmed_block: legacy_block.encode(UiTransactionEncoding::Base64), + encoded_confirmed_block: encoded_block.into(), slot, }; println!("{}", output_format.formatted_string(&cli_block)); diff --git a/rpc/src/rpc.rs b/rpc/src/rpc.rs index 613fc71497c13e..e2db2128894288 100644 --- a/rpc/src/rpc.rs +++ b/rpc/src/rpc.rs @@ -69,8 +69,7 @@ use { system_instruction, sysvar::stake_history, transaction::{ - self, DisabledAddressLoader, SanitizedTransaction, TransactionError, - VersionedTransaction, + self, AddressLoader, SanitizedTransaction, TransactionError, VersionedTransaction, }, }, solana_send_transaction_service::{ @@ -80,9 +79,9 @@ use { solana_storage_bigtable::Error as StorageError, solana_streamer::socket::SocketAddrSpace, solana_transaction_status::{ - ConfirmedBlock, ConfirmedTransactionStatusWithSignature, - ConfirmedTransactionWithStatusMeta, Encodable, EncodedConfirmedTransactionWithStatusMeta, - Reward, RewardType, TransactionConfirmationStatus, TransactionStatus, UiConfirmedBlock, + BlockEncodingOptions, ConfirmedBlock, ConfirmedTransactionStatusWithSignature, + ConfirmedTransactionWithStatusMeta, EncodedConfirmedTransactionWithStatusMeta, Reward, + RewardType, TransactionConfirmationStatus, TransactionStatus, UiConfirmedBlock, UiTransactionEncoding, }, solana_vote_program::vote_state::{VoteState, MAX_LOCKOUT_HISTORY}, @@ -986,8 +985,11 @@ impl JsonRpcRequestProcessor { .map(|config| config.convert_to_current()) .unwrap_or_default(); let encoding = config.encoding.unwrap_or(UiTransactionEncoding::Json); - let transaction_details = config.transaction_details.unwrap_or_default(); - let show_rewards = config.rewards.unwrap_or(true); + let encoding_options = BlockEncodingOptions { + transaction_details: config.transaction_details.unwrap_or_default(), + show_rewards: config.rewards.unwrap_or(true), + max_supported_transaction_version: config.max_supported_transaction_version, + }; let commitment = config.commitment.unwrap_or_default(); check_is_at_least_confirmed(commitment)?; @@ -1002,31 +1004,29 @@ impl JsonRpcRequestProcessor { self.check_status_is_complete(slot)?; let result = self.blockstore.get_rooted_block(slot, true); self.check_blockstore_root(&result, slot)?; - let configure_block = |confirmed_block: ConfirmedBlock| { - let legacy_block = confirmed_block - .into_legacy_block() - .ok_or(RpcCustomError::UnsupportedTransactionVersion)?; - let mut confirmed_block = - legacy_block.configure(encoding, transaction_details, show_rewards); + let encode_block = |confirmed_block: ConfirmedBlock| -> Result { + let mut encoded_block = confirmed_block + .encode_with_options(encoding, encoding_options) + .map_err(RpcCustomError::from)?; if slot == 0 { - confirmed_block.block_time = Some(self.genesis_creation_time()); - confirmed_block.block_height = Some(0); + encoded_block.block_time = Some(self.genesis_creation_time()); + encoded_block.block_height = Some(0); } - Ok(confirmed_block) + Ok(encoded_block) }; if result.is_err() { if let Some(bigtable_ledger_storage) = &self.bigtable_ledger_storage { let bigtable_result = bigtable_ledger_storage.get_confirmed_block(slot).await; self.check_bigtable_result(&bigtable_result)?; - return bigtable_result.ok().map(configure_block).transpose(); + return bigtable_result.ok().map(encode_block).transpose(); } } self.check_slot_cleaned_up(&result, slot)?; return result .ok() .map(ConfirmedBlock::from) - .map(configure_block) + .map(encode_block) .transpose(); } else if commitment.is_confirmed() { // Check if block is confirmed @@ -1037,27 +1037,26 @@ impl JsonRpcRequestProcessor { return result .ok() .map(ConfirmedBlock::from) - .map(|confirmed_block| -> Result { - let mut legacy_block = confirmed_block - .into_legacy_block() - .ok_or(RpcCustomError::UnsupportedTransactionVersion)?; - - if legacy_block.block_time.is_none() - || legacy_block.block_height.is_none() + .map(|mut confirmed_block| -> Result { + if confirmed_block.block_time.is_none() + || confirmed_block.block_height.is_none() { let r_bank_forks = self.bank_forks.read().unwrap(); let bank = r_bank_forks.get(slot).cloned(); if let Some(bank) = bank { - if legacy_block.block_time.is_none() { - legacy_block.block_time = Some(bank.clock().unix_timestamp); + if confirmed_block.block_time.is_none() { + confirmed_block.block_time = + Some(bank.clock().unix_timestamp); } - if legacy_block.block_height.is_none() { - legacy_block.block_height = Some(bank.block_height()); + if confirmed_block.block_height.is_none() { + confirmed_block.block_height = Some(bank.block_height()); } } } - Ok(legacy_block.configure(encoding, transaction_details, show_rewards)) + Ok(confirmed_block + .encode_with_options(encoding, encoding_options) + .map_err(RpcCustomError::from)?) }) .transpose(); } @@ -1379,6 +1378,7 @@ impl JsonRpcRequestProcessor { .map(|config| config.convert_to_current()) .unwrap_or_default(); let encoding = config.encoding.unwrap_or(UiTransactionEncoding::Json); + let max_supported_transaction_version = config.max_supported_transaction_version; let commitment = config.commitment.unwrap_or_default(); check_is_at_least_confirmed(commitment)?; @@ -1394,10 +1394,7 @@ impl JsonRpcRequestProcessor { let encode_transaction = |confirmed_tx_with_meta: ConfirmedTransactionWithStatusMeta| -> Result { - let legacy_tx_with_meta = confirmed_tx_with_meta.into_legacy_confirmed_transaction() - .ok_or(RpcCustomError::UnsupportedTransactionVersion)?; - - Ok(legacy_tx_with_meta.encode(encoding)) + Ok(confirmed_tx_with_meta.encode(encoding, max_supported_transaction_version).map_err(RpcCustomError::from)?) }; match confirmed_transaction.unwrap_or(None) { @@ -3473,7 +3470,8 @@ pub mod rpc_full { .preflight_commitment .map(|commitment| CommitmentConfig { commitment }); let preflight_bank = &*meta.bank(preflight_commitment); - let transaction = sanitize_transaction(unsanitized_tx)?; + let finalized_bank = &*meta.bank(Some(CommitmentConfig::finalized())); + let transaction = sanitize_transaction(unsanitized_tx, finalized_bank)?; let signature = *transaction.signature(); let mut last_valid_block_height = preflight_bank @@ -3581,7 +3579,8 @@ pub mod rpc_full { .set_recent_blockhash(bank.last_blockhash()); } - let transaction = sanitize_transaction(unsanitized_tx)?; + let finalized_bank = &*meta.bank(Some(CommitmentConfig::finalized())); + let transaction = sanitize_transaction(unsanitized_tx, finalized_bank)?; if config.sig_verify { verify_transaction(&transaction, &bank.feature_set)?; } @@ -4266,9 +4265,12 @@ where .map(|output| (wire_output, output)) } -fn sanitize_transaction(transaction: VersionedTransaction) -> Result { +fn sanitize_transaction( + transaction: VersionedTransaction, + address_loader: &impl AddressLoader, +) -> Result { let message_hash = transaction.message.hash(); - SanitizedTransaction::try_create(transaction, message_hash, None, &DisabledAddressLoader) + SanitizedTransaction::try_create(transaction, message_hash, None, address_loader) .map_err(|err| Error::invalid_params(format!("invalid transaction: {}", err))) } @@ -4388,12 +4390,12 @@ pub mod tests { fee_calculator::DEFAULT_BURN_PERCENT, hash::{hash, Hash}, instruction::InstructionError, - message::Message, + message::{v0, Message, MessageHeader, VersionedMessage}, nonce, rpc_port, signature::{Keypair, Signer}, system_program, system_transaction, timing::slot_duration_from_slots_per_year, - transaction::{self, Transaction, TransactionError}, + transaction::{self, DisabledAddressLoader, Transaction, TransactionError}, }, solana_transaction_status::{ EncodedConfirmedBlock, EncodedTransaction, EncodedTransactionWithStatusMeta, @@ -6597,9 +6599,16 @@ pub mod tests { assert_eq!(confirmed_block.transactions.len(), 2); assert_eq!(confirmed_block.rewards, vec![]); - for EncodedTransactionWithStatusMeta { transaction, meta } in - confirmed_block.transactions.into_iter() + for EncodedTransactionWithStatusMeta { + transaction, + meta, + version, + } in confirmed_block.transactions.into_iter() { + assert_eq!( + version, None, + "requests which don't set max_supported_transaction_version shouldn't receive a version" + ); if let EncodedTransaction::Json(transaction) = transaction { if transaction.signatures[0] == confirmed_block_signatures[0].to_string() { let meta = meta.unwrap(); @@ -6642,9 +6651,16 @@ pub mod tests { assert_eq!(confirmed_block.transactions.len(), 2); assert_eq!(confirmed_block.rewards, vec![]); - for EncodedTransactionWithStatusMeta { transaction, meta } in - confirmed_block.transactions.into_iter() + for EncodedTransactionWithStatusMeta { + transaction, + meta, + version, + } in confirmed_block.transactions.into_iter() { + assert_eq!( + version, None, + "requests which don't set max_supported_transaction_version shouldn't receive a version" + ); if let EncodedTransaction::LegacyBinary(transaction) = transaction { let decoded_transaction: Transaction = deserialize(&bs58::decode(&transaction).into_vec().unwrap()).unwrap(); @@ -6704,6 +6720,7 @@ pub mod tests { transaction_details: Some(TransactionDetails::Signatures), rewards: Some(false), commitment: None, + max_supported_transaction_version: None, }) ); let res = io.handle_request_sync(&req, meta.clone()); @@ -6725,6 +6742,7 @@ pub mod tests { transaction_details: Some(TransactionDetails::None), rewards: Some(true), commitment: None, + max_supported_transaction_version: None, }) ); let res = io.handle_request_sync(&req, meta); @@ -8082,8 +8100,30 @@ pub mod tests { .to_string(), ); assert_eq!( - sanitize_transaction(unsanitary_versioned_tx).unwrap_err(), + sanitize_transaction(unsanitary_versioned_tx, &DisabledAddressLoader).unwrap_err(), expect58 ); } + + #[test] + fn test_sanitize_unsupported_transaction_version() { + let versioned_tx = VersionedTransaction { + signatures: vec![Signature::default()], + message: VersionedMessage::V0(v0::Message { + header: MessageHeader { + num_required_signatures: 1, + ..MessageHeader::default() + }, + account_keys: vec![Pubkey::new_unique()], + ..v0::Message::default() + }), + }; + + assert_eq!( + sanitize_transaction(versioned_tx, &DisabledAddressLoader).unwrap_err(), + Error::invalid_params( + "invalid transaction: Transaction version is unsupported".to_string(), + ) + ); + } } diff --git a/rpc/src/rpc_pubsub.rs b/rpc/src/rpc_pubsub.rs index e0144017acffa2..db2b9623246192 100644 --- a/rpc/src/rpc_pubsub.rs +++ b/rpc/src/rpc_pubsub.rs @@ -538,6 +538,7 @@ impl RpcSolPubSubInternal for RpcSolPubSubImpl { }, transaction_details: config.transaction_details.unwrap_or_default(), show_rewards: config.show_rewards.unwrap_or_default(), + max_supported_transaction_version: config.max_supported_transaction_version, }; self.subscribe(SubscriptionParams::Block(params)) } diff --git a/rpc/src/rpc_subscription_tracker.rs b/rpc/src/rpc_subscription_tracker.rs index c49ff6fd942a24..ffeffcb7b679ea 100644 --- a/rpc/src/rpc_subscription_tracker.rs +++ b/rpc/src/rpc_subscription_tracker.rs @@ -140,6 +140,7 @@ pub struct BlockSubscriptionParams { pub kind: BlockSubscriptionKind, pub transaction_details: TransactionDetails, pub show_rewards: bool, + pub max_supported_transaction_version: Option, } #[derive(Debug, Clone, PartialEq, Eq, Hash)] diff --git a/rpc/src/rpc_subscriptions.rs b/rpc/src/rpc_subscriptions.rs index 374b5f123cae20..0bdd1686af3693 100644 --- a/rpc/src/rpc_subscriptions.rs +++ b/rpc/src/rpc_subscriptions.rs @@ -40,7 +40,9 @@ use { timing::timestamp, transaction, }, - solana_transaction_status::{ConfirmedBlock, LegacyConfirmedBlock}, + solana_transaction_status::{ + BlockEncodingOptions, ConfirmedBlock, EncodeError, VersionedConfirmedBlock, + }, std::{ cell::RefCell, collections::{HashMap, VecDeque}, @@ -278,39 +280,48 @@ impl RpcNotifier { } fn filter_block_result_txs( - mut block: LegacyConfirmedBlock, + mut block: VersionedConfirmedBlock, last_modified_slot: Slot, params: &BlockSubscriptionParams, -) -> Option { +) -> Result, RpcBlockUpdateError> { block.transactions = match params.kind { BlockSubscriptionKind::All => block.transactions, BlockSubscriptionKind::MentionsAccountOrProgram(pk) => block .transactions .into_iter() - .filter(|tx_with_meta| tx_with_meta.transaction.message.account_keys.contains(&pk)) + .filter(|tx| tx.account_keys().iter().any(|key| key == &pk)) .collect(), }; if block.transactions.is_empty() { if let BlockSubscriptionKind::MentionsAccountOrProgram(_) = params.kind { - return None; + return Ok(None); } } - let block = block.configure( - params.encoding, - params.transaction_details, - params.show_rewards, - ); + let block = ConfirmedBlock::from(block) + .encode_with_options( + params.encoding, + BlockEncodingOptions { + transaction_details: params.transaction_details, + show_rewards: params.show_rewards, + max_supported_transaction_version: params.max_supported_transaction_version, + }, + ) + .map_err(|err| match err { + EncodeError::UnsupportedTransactionVersion(version) => { + RpcBlockUpdateError::UnsupportedTransactionVersion(version) + } + })?; // If last_modified_slot < last_notified_slot, then the last notif was for a fork. // That's the risk clients take when subscribing to non-finalized commitments. // This code lets the logic for dealing with forks live on the client side. - Some(RpcBlockUpdate { + Ok(Some(RpcBlockUpdate { slot: last_modified_slot, block: Some(block), err: None, - }) + })) } fn filter_account_result( @@ -964,19 +975,11 @@ impl RpcSubscriptions { error!("get_complete_block error: {}", e); RpcBlockUpdateError::BlockStoreError }) - .and_then(|versioned_block| { - ConfirmedBlock::from(versioned_block) - .into_legacy_block() - .ok_or( - RpcBlockUpdateError::UnsupportedTransactionVersion, - ) - }); + .and_then(|block| filter_block_result_txs(block, s, params)); match block_update_result { Ok(block_update) => { - if let Some(block_update) = - filter_block_result_txs(block_update, s, params) - { + if let Some(block_update) = block_update { notifier.notify( Response { context: RpcResponseContext { slot: s }, @@ -1385,6 +1388,7 @@ pub(crate) mod tests { encoding: Some(UiTransactionEncoding::Json), transaction_details: Some(TransactionDetails::Signatures), show_rewards: None, + max_supported_transaction_version: None, }; let params = BlockSubscriptionParams { kind: BlockSubscriptionKind::All, @@ -1392,6 +1396,7 @@ pub(crate) mod tests { encoding: config.encoding.unwrap(), transaction_details: config.transaction_details.unwrap(), show_rewards: config.show_rewards.unwrap_or_default(), + max_supported_transaction_version: config.max_supported_transaction_version, }; let sub_id = rpc.block_subscribe(filter, Some(config)).unwrap(); @@ -1421,8 +1426,16 @@ pub(crate) mod tests { let confirmed_block = ConfirmedBlock::from(blockstore.get_complete_block(slot, false).unwrap()); - let legacy_block = confirmed_block.into_legacy_block().unwrap(); - let block = legacy_block.configure(params.encoding, params.transaction_details, false); + let block = confirmed_block + .encode_with_options( + params.encoding, + BlockEncodingOptions { + transaction_details: params.transaction_details, + show_rewards: false, + max_supported_transaction_version: None, + }, + ) + .unwrap(); let expected_resp = RpcBlockUpdate { slot, block: Some(block), @@ -1492,6 +1505,7 @@ pub(crate) mod tests { encoding: Some(UiTransactionEncoding::Json), transaction_details: Some(TransactionDetails::Signatures), show_rewards: None, + max_supported_transaction_version: None, }; let params = BlockSubscriptionParams { kind: BlockSubscriptionKind::MentionsAccountOrProgram(keypair1.pubkey()), @@ -1499,6 +1513,7 @@ pub(crate) mod tests { encoding: config.encoding.unwrap(), transaction_details: config.transaction_details.unwrap(), show_rewards: config.show_rewards.unwrap_or_default(), + max_supported_transaction_version: config.max_supported_transaction_version, }; let sub_id = rpc.block_subscribe(filter, Some(config)).unwrap(); @@ -1526,17 +1541,24 @@ pub(crate) mod tests { let actual_resp = serde_json::from_str::(&actual_resp).unwrap(); // make sure it filtered out the other keypairs - let confirmed_block = + let mut confirmed_block = ConfirmedBlock::from(blockstore.get_complete_block(slot, false).unwrap()); - let mut legacy_block = confirmed_block.into_legacy_block().unwrap(); - legacy_block.transactions.retain(|tx_with_meta| { + confirmed_block.transactions.retain(|tx_with_meta| { tx_with_meta - .transaction - .message - .account_keys - .contains(&keypair1.pubkey()) + .account_keys() + .iter() + .any(|key| key == &keypair1.pubkey()) }); - let block = legacy_block.configure(params.encoding, params.transaction_details, false); + let block = confirmed_block + .encode_with_options( + params.encoding, + BlockEncodingOptions { + transaction_details: params.transaction_details, + show_rewards: false, + max_supported_transaction_version: None, + }, + ) + .unwrap(); let expected_resp = RpcBlockUpdate { slot, block: Some(block), @@ -1594,6 +1616,7 @@ pub(crate) mod tests { encoding: Some(UiTransactionEncoding::Json), transaction_details: Some(TransactionDetails::Signatures), show_rewards: None, + max_supported_transaction_version: None, }; let params = BlockSubscriptionParams { kind: BlockSubscriptionKind::All, @@ -1601,6 +1624,7 @@ pub(crate) mod tests { encoding: config.encoding.unwrap(), transaction_details: config.transaction_details.unwrap(), show_rewards: config.show_rewards.unwrap_or_default(), + max_supported_transaction_version: config.max_supported_transaction_version, }; let sub_id = rpc.block_subscribe(filter, Some(config)).unwrap(); subscriptions @@ -1634,8 +1658,16 @@ pub(crate) mod tests { let confirmed_block = ConfirmedBlock::from(blockstore.get_complete_block(slot, false).unwrap()); - let legacy_block = confirmed_block.into_legacy_block().unwrap(); - let block = legacy_block.configure(params.encoding, params.transaction_details, false); + let block = confirmed_block + .encode_with_options( + params.encoding, + BlockEncodingOptions { + transaction_details: params.transaction_details, + show_rewards: false, + max_supported_transaction_version: None, + }, + ) + .unwrap(); let expected_resp = RpcBlockUpdate { slot, block: Some(block), diff --git a/sdk/program/src/message/sanitized.rs b/sdk/program/src/message/sanitized.rs index 9f28e28a104c6c..f12ee8a9995416 100644 --- a/sdk/program/src/message/sanitized.rs +++ b/sdk/program/src/message/sanitized.rs @@ -24,7 +24,7 @@ pub enum SanitizedMessage { /// Sanitized legacy message Legacy(LegacyMessage), /// Sanitized version #0 message with dynamically loaded addresses - V0(v0::LoadedMessage), + V0(v0::LoadedMessage<'static>), } #[derive(PartialEq, Debug, Error, Eq, Clone)] @@ -347,8 +347,8 @@ mod tests { assert_eq!(legacy_message.num_readonly_accounts(), 2); - let v0_message = SanitizedMessage::V0(v0::LoadedMessage { - message: v0::Message { + let v0_message = SanitizedMessage::V0(v0::LoadedMessage::new( + v0::Message { header: MessageHeader { num_required_signatures: 2, num_readonly_signed_accounts: 1, @@ -357,11 +357,11 @@ mod tests { account_keys: vec![key0, key1, key2, key3], ..v0::Message::default() }, - loaded_addresses: LoadedAddresses { + LoadedAddresses { writable: vec![key4], readonly: vec![key5], }, - }); + )); assert_eq!(v0_message.num_readonly_accounts(), 3); } @@ -414,8 +414,8 @@ mod tests { }) .unwrap(); - let v0_message = SanitizedMessage::V0(v0::LoadedMessage { - message: v0::Message { + let v0_message = SanitizedMessage::V0(v0::LoadedMessage::new( + v0::Message { header: MessageHeader { num_required_signatures: 1, num_readonly_signed_accounts: 0, @@ -424,11 +424,11 @@ mod tests { account_keys: vec![key0, key1], ..v0::Message::default() }, - loaded_addresses: LoadedAddresses { + LoadedAddresses { writable: vec![key2], readonly: vec![program_id], }, - }); + )); for message in vec![legacy_message, v0_message] { assert_eq!( diff --git a/sdk/program/src/message/versions/v0/loaded.rs b/sdk/program/src/message/versions/v0/loaded.rs index 4684f17eb1b043..240d0ccb63a66c 100644 --- a/sdk/program/src/message/versions/v0/loaded.rs +++ b/sdk/program/src/message/versions/v0/loaded.rs @@ -5,19 +5,19 @@ use { pubkey::Pubkey, sysvar, }, - std::{collections::HashSet, ops::Deref}, + std::{borrow::Cow, collections::HashSet, ops::Deref}, }; /// Combination of a version #0 message and its loaded addresses #[derive(Debug, Clone)] -pub struct LoadedMessage { +pub struct LoadedMessage<'a> { /// Message which loaded a collection of lookup table addresses - pub message: v0::Message, + pub message: Cow<'a, v0::Message>, /// Addresses loaded with on-chain address lookup tables - pub loaded_addresses: LoadedAddresses, + pub loaded_addresses: Cow<'a, LoadedAddresses>, } -impl Deref for LoadedMessage { +impl Deref for LoadedMessage<'_> { type Target = v0::Message; fn deref(&self) -> &Self::Target { &self.message @@ -59,7 +59,21 @@ impl LoadedAddresses { } } -impl LoadedMessage { +impl<'a> LoadedMessage<'a> { + pub fn new(message: v0::Message, loaded_addresses: LoadedAddresses) -> Self { + Self { + message: Cow::Owned(message), + loaded_addresses: Cow::Owned(loaded_addresses), + } + } + + pub fn new_borrowed(message: &'a v0::Message, loaded_addresses: &'a LoadedAddresses) -> Self { + Self { + message: Cow::Borrowed(message), + loaded_addresses: Cow::Borrowed(loaded_addresses), + } + } + /// Returns the list of account keys that are loaded for this message. pub fn account_keys(&self) -> AccountKeys { AccountKeys::new(&self.account_keys, Some(&self.loaded_addresses)) @@ -135,7 +149,7 @@ mod tests { itertools::Itertools, }; - fn check_test_loaded_message() -> (LoadedMessage, [Pubkey; 6]) { + fn check_test_loaded_message() -> (LoadedMessage<'static>, [Pubkey; 6]) { let key0 = Pubkey::new_unique(); let key1 = Pubkey::new_unique(); let key2 = Pubkey::new_unique(); @@ -143,8 +157,8 @@ mod tests { let key4 = Pubkey::new_unique(); let key5 = Pubkey::new_unique(); - let message = LoadedMessage { - message: v0::Message { + let message = LoadedMessage::new( + v0::Message { header: MessageHeader { num_required_signatures: 2, num_readonly_signed_accounts: 1, @@ -153,11 +167,11 @@ mod tests { account_keys: vec![key0, key1, key2, key3], ..v0::Message::default() }, - loaded_addresses: LoadedAddresses { + LoadedAddresses { writable: vec![key4], readonly: vec![key5], }, - }; + ); (message, [key0, key1, key2, key3, key4, key5]) } @@ -171,15 +185,17 @@ mod tests { #[test] fn test_has_duplicates_with_dupe_keys() { - let create_message_with_dupe_keys = |mut keys: Vec| LoadedMessage { - message: v0::Message { - account_keys: keys.split_off(2), - ..v0::Message::default() - }, - loaded_addresses: LoadedAddresses { - writable: keys.split_off(2), - readonly: keys, - }, + let create_message_with_dupe_keys = |mut keys: Vec| { + LoadedMessage::new( + v0::Message { + account_keys: keys.split_off(2), + ..v0::Message::default() + }, + LoadedAddresses { + writable: keys.split_off(2), + readonly: keys, + }, + ) }; let key0 = Pubkey::new_unique(); @@ -212,11 +228,11 @@ mod tests { fn test_is_writable() { let mut message = check_test_loaded_message().0; - message.message.account_keys[0] = sysvar::clock::id(); + message.message.to_mut().account_keys[0] = sysvar::clock::id(); assert!(message.is_writable_index(0)); assert!(!message.is_writable(0)); - message.message.account_keys[0] = system_program::id(); + message.message.to_mut().account_keys[0] = system_program::id(); assert!(message.is_writable_index(0)); assert!(!message.is_writable(0)); } @@ -226,8 +242,8 @@ mod tests { let key0 = Pubkey::new_unique(); let key1 = Pubkey::new_unique(); let key2 = Pubkey::new_unique(); - let message = LoadedMessage { - message: v0::Message { + let message = LoadedMessage::new( + v0::Message { header: MessageHeader { num_required_signatures: 1, num_readonly_signed_accounts: 0, @@ -241,11 +257,11 @@ mod tests { }], ..v0::Message::default() }, - loaded_addresses: LoadedAddresses { + LoadedAddresses { writable: vec![key1, key2], readonly: vec![], }, - }; + ); assert!(message.is_writable_index(2)); assert!(!message.is_writable(2)); diff --git a/sdk/program/src/message/versions/v0/mod.rs b/sdk/program/src/message/versions/v0/mod.rs index 3a03e1ea719afc..1c41441e7e2267 100644 --- a/sdk/program/src/message/versions/v0/mod.rs +++ b/sdk/program/src/message/versions/v0/mod.rs @@ -134,6 +134,10 @@ impl Sanitize for Message { } impl Message { + pub fn is_signer(&self, i: usize) -> bool { + i < self.header.num_required_signatures as usize + } + /// Serialize this message with a version #0 prefix using bincode encoding. pub fn serialize(&self) -> Vec { bincode::serialize(&(MESSAGE_VERSION_PREFIX, self)).unwrap() diff --git a/sdk/src/transaction/sanitized.rs b/sdk/src/transaction/sanitized.rs index 8b7cb2994bb2d0..9e3fb7287a0a41 100644 --- a/sdk/src/transaction/sanitized.rs +++ b/sdk/src/transaction/sanitized.rs @@ -65,10 +65,11 @@ impl SanitizedTransaction { let signatures = tx.signatures; let message = match tx.message { VersionedMessage::Legacy(message) => SanitizedMessage::Legacy(message), - VersionedMessage::V0(message) => SanitizedMessage::V0(v0::LoadedMessage { - loaded_addresses: address_loader.load_addresses(&message.address_table_lookups)?, - message, - }), + VersionedMessage::V0(message) => { + let loaded_addresses = + address_loader.load_addresses(&message.address_table_lookups)?; + SanitizedMessage::V0(v0::LoadedMessage::new(message, loaded_addresses)) + } }; let is_simple_vote_tx = is_simple_vote_tx.unwrap_or_else(|| { @@ -139,7 +140,7 @@ impl SanitizedTransaction { match &self.message { SanitizedMessage::V0(sanitized_msg) => VersionedTransaction { signatures, - message: VersionedMessage::V0(sanitized_msg.message.clone()), + message: VersionedMessage::V0(v0::Message::clone(&sanitized_msg.message)), }, SanitizedMessage::Legacy(message) => VersionedTransaction { signatures, @@ -191,7 +192,7 @@ impl SanitizedTransaction { pub fn get_loaded_addresses(&self) -> LoadedAddresses { match &self.message { SanitizedMessage::Legacy(_) => LoadedAddresses::default(), - SanitizedMessage::V0(message) => message.loaded_addresses.clone(), + SanitizedMessage::V0(message) => LoadedAddresses::clone(&message.loaded_addresses), } } diff --git a/sdk/src/transaction/versioned.rs b/sdk/src/transaction/versioned.rs index 2e36f21dd1caca..122de5ede872d2 100644 --- a/sdk/src/transaction/versioned.rs +++ b/sdk/src/transaction/versioned.rs @@ -17,6 +17,13 @@ use { std::cmp::Ordering, }; +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", untagged)] +pub enum TransactionVersion { + Legacy, + Number(u8), +} + // NOTE: Serialization-related changes must be paired with the direct read at sigverify. /// An atomic transaction #[derive(Debug, PartialEq, Default, Eq, Clone, Serialize, Deserialize, AbiExample)] @@ -93,6 +100,14 @@ impl VersionedTransaction { }) } + /// Returns the version of the transaction + pub fn version(&self) -> TransactionVersion { + match self.message { + VersionedMessage::Legacy(_) => TransactionVersion::Legacy, + VersionedMessage::V0(_) => TransactionVersion::Number(0), + } + } + /// Returns a legacy transaction if the transaction message is legacy. pub fn into_legacy_transaction(self) -> Option { match self.message { diff --git a/storage-bigtable/src/lib.rs b/storage-bigtable/src/lib.rs index 08b0f997d4a721..81576cbeabc8c7 100644 --- a/storage-bigtable/src/lib.rs +++ b/storage-bigtable/src/lib.rs @@ -485,7 +485,7 @@ impl LedgerStorage { Ok(None) } Some(tx_with_meta) => { - if tx_with_meta.transaction_signature() != signature { + if tx_with_meta.signature() != signature { warn!( "Transaction info or confirmed block for {} is corrupt", signature diff --git a/transaction-status/src/lib.rs b/transaction-status/src/lib.rs index 52a0e6e3f3dcc1..96ad2920b9b0af 100644 --- a/transaction-status/src/lib.rs +++ b/transaction-status/src/lib.rs @@ -18,7 +18,9 @@ pub mod token_balances; pub use {crate::extract_memos::extract_and_fmt_memos, solana_runtime::bank::RewardType}; use { crate::{ - parse_accounts::{parse_accounts, ParsedAccount}, + parse_accounts::{ + parse_legacy_message_accounts, parse_loaded_message_accounts, ParsedAccount, + }, parse_instruction::{parse, ParsedInstruction}, }, solana_account_decoder::parse_token::UiTokenAmount, @@ -26,18 +28,58 @@ use { clock::{Slot, UnixTimestamp}, commitment_config::CommitmentConfig, instruction::CompiledInstruction, - message::{v0::LoadedAddresses, AccountKeys, Message, MessageHeader}, + message::{ + v0::{self, LoadedAddresses, LoadedMessage, MessageAddressTableLookup}, + AccountKeys, Message, MessageHeader, VersionedMessage, + }, + pubkey::Pubkey, sanitize::Sanitize, signature::Signature, - transaction::{Result, Transaction, TransactionError, VersionedTransaction}, + transaction::{ + Result as TransactionResult, Transaction, TransactionError, TransactionVersion, + VersionedTransaction, + }, }, std::fmt, + thiserror::Error, }; +pub struct BlockEncodingOptions { + pub transaction_details: TransactionDetails, + pub show_rewards: bool, + pub max_supported_transaction_version: Option, +} + +impl Default for BlockEncodingOptions { + fn default() -> Self { + Self { + transaction_details: TransactionDetails::Full, + show_rewards: true, + max_supported_transaction_version: Some(0u8), + } + } +} + +#[derive(Error, Debug, PartialEq, Eq, Clone)] +pub enum EncodeError { + #[error("Encoding does not support transaction version {0}")] + UnsupportedTransactionVersion(u8), +} + /// Represents types that can be encoded into one of several encoding formats pub trait Encodable { type Encoded; - fn encode(self, encoding: UiTransactionEncoding) -> Self::Encoded; + fn encode(&self, encoding: UiTransactionEncoding) -> Self::Encoded; +} + +/// Represents types that can be encoded into one of several encoding formats +pub trait EncodableWithMeta { + type Encoded; + fn encode_with_meta( + &self, + encoding: UiTransactionEncoding, + meta: &TransactionStatusMeta, + ) -> Self::Encoded; } #[derive(Serialize, Deserialize, Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -160,14 +202,13 @@ pub struct UiInnerInstructions { } impl UiInnerInstructions { - fn parse(inner_instructions: InnerInstructions, message: &Message) -> Self { - let account_keys = AccountKeys::new(&message.account_keys, None); + fn parse(inner_instructions: InnerInstructions, account_keys: &AccountKeys) -> Self { Self { index: inner_instructions.index, instructions: inner_instructions .instructions .iter() - .map(|ix| UiInstruction::parse(ix, &account_keys)) + .map(|ix| UiInstruction::parse(ix, account_keys)) .collect(), } } @@ -221,7 +262,7 @@ impl From for UiTransactionTokenBalance { #[derive(Clone, Debug, PartialEq)] pub struct TransactionStatusMeta { - pub status: Result<()>, + pub status: TransactionResult<()>, pub fee: u64, pub pre_balances: Vec, pub post_balances: Vec, @@ -255,7 +296,7 @@ impl Default for TransactionStatusMeta { #[serde(rename_all = "camelCase")] pub struct UiTransactionStatusMeta { pub err: Option, - pub status: Result<()>, // This field is deprecated. See https://github.com/solana-labs/solana/issues/9302 + pub status: TransactionResult<()>, // This field is deprecated. See https://github.com/solana-labs/solana/issues/9302 pub fee: u64, pub pre_balances: Vec, pub post_balances: Vec, @@ -264,10 +305,38 @@ pub struct UiTransactionStatusMeta { pub pre_token_balances: Option>, pub post_token_balances: Option>, pub rewards: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub loaded_addresses: Option, +} + +/// A duplicate representation of LoadedAddresses +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct UiLoadedAddresses { + pub writable: Vec, + pub readonly: Vec, +} + +impl From<&LoadedAddresses> for UiLoadedAddresses { + fn from(loaded_addresses: &LoadedAddresses) -> Self { + Self { + writable: loaded_addresses + .writable + .iter() + .map(ToString::to_string) + .collect(), + readonly: loaded_addresses + .readonly + .iter() + .map(ToString::to_string) + .collect(), + } + } } impl UiTransactionStatusMeta { - fn parse(meta: TransactionStatusMeta, message: &Message) -> Self { + fn parse(meta: TransactionStatusMeta, static_keys: &[Pubkey]) -> Self { + let account_keys = AccountKeys::new(static_keys, Some(&meta.loaded_addresses)); Self { err: meta.status.clone().err(), status: meta.status, @@ -276,7 +345,7 @@ impl UiTransactionStatusMeta { post_balances: meta.post_balances, inner_instructions: meta.inner_instructions.map(|ixs| { ixs.into_iter() - .map(|ix| UiInnerInstructions::parse(ix, message)) + .map(|ix| UiInnerInstructions::parse(ix, &account_keys)) .collect() }), log_messages: meta.log_messages, @@ -287,6 +356,7 @@ impl UiTransactionStatusMeta { .post_token_balances .map(|balance| balance.into_iter().map(Into::into).collect()), rewards: meta.rewards, + loaded_addresses: Some(UiLoadedAddresses::from(&meta.loaded_addresses)), } } } @@ -310,6 +380,7 @@ impl From for UiTransactionStatusMeta { .post_token_balances .map(|balance| balance.into_iter().map(Into::into).collect()), rewards: meta.rewards, + loaded_addresses: Some(UiLoadedAddresses::from(&meta.loaded_addresses)), } } } @@ -326,8 +397,8 @@ pub enum TransactionConfirmationStatus { #[serde(rename_all = "camelCase")] pub struct TransactionStatus { pub slot: Slot, - pub confirmations: Option, // None = rooted - pub status: Result<()>, // legacy field + pub confirmations: Option, // None = rooted + pub status: TransactionResult<()>, // legacy field pub err: Option, pub confirmation_status: Option, } @@ -462,39 +533,21 @@ impl From for ConfirmedBlock { } } -impl Encodable for LegacyConfirmedBlock { - type Encoded = EncodedConfirmedBlock; - fn encode(self, encoding: UiTransactionEncoding) -> Self::Encoded { - Self::Encoded { - previous_blockhash: self.previous_blockhash, - blockhash: self.blockhash, - parent_slot: self.parent_slot, - transactions: self - .transactions - .into_iter() - .map(|tx| tx.encode(encoding)) - .collect(), - rewards: self.rewards, - block_time: self.block_time, - block_height: self.block_height, - } - } -} - -impl LegacyConfirmedBlock { - pub fn configure( +impl ConfirmedBlock { + pub fn encode_with_options( self, encoding: UiTransactionEncoding, - transaction_details: TransactionDetails, - show_rewards: bool, - ) -> UiConfirmedBlock { - let (transactions, signatures) = match transaction_details { + options: BlockEncodingOptions, + ) -> Result { + let (transactions, signatures) = match options.transaction_details { TransactionDetails::Full => ( Some( self.transactions .into_iter() - .map(|tx_with_meta| tx_with_meta.encode(encoding)) - .collect(), + .map(|tx_with_meta| { + tx_with_meta.encode(encoding, options.max_supported_transaction_version) + }) + .collect::, _>>()?, ), None, ), @@ -503,26 +556,26 @@ impl LegacyConfirmedBlock { Some( self.transactions .into_iter() - .map(|tx| tx.transaction.signatures[0].to_string()) + .map(|tx_with_meta| tx_with_meta.signature().to_string()) .collect(), ), ), TransactionDetails::None => (None, None), }; - UiConfirmedBlock { + Ok(UiConfirmedBlock { previous_blockhash: self.previous_blockhash, blockhash: self.blockhash, parent_slot: self.parent_slot, transactions, signatures, - rewards: if show_rewards { + rewards: if options.show_rewards { Some(self.rewards) } else { None }, block_time: self.block_time, block_height: self.block_height, - } + }) } } @@ -568,21 +621,6 @@ pub struct UiConfirmedBlock { pub block_height: Option, } -impl From for UiConfirmedBlock { - fn from(block: EncodedConfirmedBlock) -> Self { - Self { - previous_blockhash: block.previous_blockhash, - blockhash: block.blockhash, - parent_slot: block.parent_slot, - transactions: Some(block.transactions), - signatures: None, - rewards: Some(block.rewards), - block_time: block.block_time, - block_height: block.block_height, - } - } -} - #[derive(Clone, Debug, PartialEq)] #[allow(clippy::large_enum_variant)] pub enum TransactionWithStatusMeta { @@ -604,11 +642,19 @@ pub struct LegacyTransactionWithStatusMeta { } impl TransactionWithStatusMeta { - pub fn transaction_signature(&self) -> &Signature { + pub fn encode( + self, + encoding: UiTransactionEncoding, + max_supported_transaction_version: Option, + ) -> Result { match self { - Self::MissingMetadata(transaction) => &transaction.signatures[0], - Self::Complete(VersionedTransactionWithStatusMeta { transaction, .. }) => { - &transaction.signatures[0] + Self::MissingMetadata(ref transaction) => Ok(EncodedTransactionWithStatusMeta { + version: None, + transaction: transaction.encode(encoding), + meta: None, + }), + Self::Complete(tx_with_meta) => { + tx_with_meta.encode(encoding, max_supported_transaction_version) } } } @@ -622,40 +668,74 @@ impl TransactionWithStatusMeta { }) } TransactionWithStatusMeta::Complete(tx_with_meta) => { - tx_with_meta.into_legacy_transaction_with_meta() + Some(LegacyTransactionWithStatusMeta { + transaction: tx_with_meta.transaction.into_legacy_transaction()?, + meta: Some(tx_with_meta.meta), + }) } } } -} -impl VersionedTransactionWithStatusMeta { pub fn account_keys(&self) -> AccountKeys { - AccountKeys::new( - self.transaction.message.static_account_keys(), - Some(&self.meta.loaded_addresses), - ) + match self { + Self::MissingMetadata(tx) => AccountKeys::new(&tx.message.account_keys, None), + Self::Complete(tx_with_meta) => tx_with_meta.account_keys(), + } } - pub fn into_legacy_transaction_with_meta(self) -> Option { - Some(LegacyTransactionWithStatusMeta { - transaction: self.transaction.into_legacy_transaction()?, - meta: Some(self.meta), - }) + pub fn signature(&self) -> &Signature { + match self { + Self::MissingMetadata(transaction) => &transaction.signatures[0], + Self::Complete(VersionedTransactionWithStatusMeta { transaction, .. }) => { + &transaction.signatures[0] + } + } } } -impl Encodable for LegacyTransactionWithStatusMeta { - type Encoded = EncodedTransactionWithStatusMeta; - fn encode(self, encoding: UiTransactionEncoding) -> Self::Encoded { - Self::Encoded { - transaction: self.transaction.encode(encoding), - meta: self.meta.map(|meta| match encoding { - UiTransactionEncoding::JsonParsed => { - UiTransactionStatusMeta::parse(meta, &self.transaction.message) +impl VersionedTransactionWithStatusMeta { + pub fn encode( + self, + encoding: UiTransactionEncoding, + max_supported_transaction_version: Option, + ) -> Result { + let version = match ( + max_supported_transaction_version, + self.transaction.version(), + ) { + // Set to none because old clients can't handle this field + (None, TransactionVersion::Legacy) => Ok(None), + (None, TransactionVersion::Number(version)) => { + Err(EncodeError::UnsupportedTransactionVersion(version)) + } + (Some(_), TransactionVersion::Legacy) => Ok(Some(TransactionVersion::Legacy)), + (Some(max_version), TransactionVersion::Number(version)) => { + if version <= max_version { + Ok(Some(TransactionVersion::Number(version))) + } else { + Err(EncodeError::UnsupportedTransactionVersion(version)) } - _ => UiTransactionStatusMeta::from(meta), + } + }?; + + Ok(EncodedTransactionWithStatusMeta { + transaction: self.transaction.encode_with_meta(encoding, &self.meta), + meta: Some(match encoding { + UiTransactionEncoding::JsonParsed => UiTransactionStatusMeta::parse( + self.meta, + self.transaction.message.static_account_keys(), + ), + _ => UiTransactionStatusMeta::from(self.meta), }), - } + version, + }) + } + + pub fn account_keys(&self) -> AccountKeys { + AccountKeys::new( + self.transaction.message.static_account_keys(), + Some(&self.meta.loaded_addresses), + ) } } @@ -664,6 +744,8 @@ impl Encodable for LegacyTransactionWithStatusMeta { pub struct EncodedTransactionWithStatusMeta { pub transaction: EncodedTransaction, pub meta: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub version: Option, } #[derive(Debug, Clone, PartialEq)] @@ -686,17 +768,6 @@ pub struct LegacyConfirmedTransactionWithStatusMeta { pub block_time: Option, } -impl Encodable for LegacyConfirmedTransactionWithStatusMeta { - type Encoded = EncodedConfirmedTransactionWithStatusMeta; - fn encode(self, encoding: UiTransactionEncoding) -> Self::Encoded { - Self::Encoded { - slot: self.slot, - transaction: self.tx_with_meta.encode(encoding), - block_time: self.block_time, - } - } -} - impl ConfirmedTransactionWithStatusMeta { /// Downgrades a versioned confirmed transaction into a legacy /// confirmed transaction if it contains a legacy transaction. @@ -709,6 +780,20 @@ impl ConfirmedTransactionWithStatusMeta { slot: self.slot, }) } + + pub fn encode( + self, + encoding: UiTransactionEncoding, + max_supported_transaction_version: Option, + ) -> Result { + Ok(EncodedConfirmedTransactionWithStatusMeta { + slot: self.slot, + tx_with_meta: self + .tx_with_meta + .encode(encoding, max_supported_transaction_version)?, + block_time: self.block_time, + }) + } } #[derive(Debug, PartialEq, Serialize, Deserialize)] @@ -716,7 +801,7 @@ impl ConfirmedTransactionWithStatusMeta { pub struct EncodedConfirmedTransactionWithStatusMeta { pub slot: Slot, #[serde(flatten)] - pub transaction: EncodedTransactionWithStatusMeta, + pub tx_with_meta: EncodedTransactionWithStatusMeta, pub block_time: Option, } @@ -728,9 +813,41 @@ pub enum EncodedTransaction { Json(UiTransaction), } -impl Encodable for &Transaction { +impl EncodableWithMeta for VersionedTransaction { type Encoded = EncodedTransaction; - fn encode(self, encoding: UiTransactionEncoding) -> Self::Encoded { + fn encode_with_meta( + &self, + encoding: UiTransactionEncoding, + meta: &TransactionStatusMeta, + ) -> Self::Encoded { + match encoding { + UiTransactionEncoding::Binary => EncodedTransaction::LegacyBinary( + bs58::encode(bincode::serialize(self).unwrap()).into_string(), + ), + UiTransactionEncoding::Base58 => EncodedTransaction::Binary( + bs58::encode(bincode::serialize(self).unwrap()).into_string(), + encoding, + ), + UiTransactionEncoding::Base64 => EncodedTransaction::Binary( + base64::encode(bincode::serialize(self).unwrap()), + encoding, + ), + UiTransactionEncoding::Json | UiTransactionEncoding::JsonParsed => { + EncodedTransaction::Json(UiTransaction { + signatures: self.signatures.iter().map(ToString::to_string).collect(), + message: match &self.message { + VersionedMessage::Legacy(message) => message.encode(encoding), + VersionedMessage::V0(message) => message.encode_with_meta(encoding, meta), + }, + }) + } + } + } +} + +impl Encodable for Transaction { + type Encoded = EncodedTransaction; + fn encode(&self, encoding: UiTransactionEncoding) -> Self::Encoded { match encoding { UiTransactionEncoding::Binary => EncodedTransaction::LegacyBinary( bs58::encode(bincode::serialize(self).unwrap()).into_string(), @@ -793,19 +910,20 @@ pub enum UiMessage { Raw(UiRawMessage), } -impl Encodable for &Message { +impl Encodable for Message { type Encoded = UiMessage; - fn encode(self, encoding: UiTransactionEncoding) -> Self::Encoded { + fn encode(&self, encoding: UiTransactionEncoding) -> Self::Encoded { if encoding == UiTransactionEncoding::JsonParsed { let account_keys = AccountKeys::new(&self.account_keys, None); UiMessage::Parsed(UiParsedMessage { - account_keys: parse_accounts(self), + account_keys: parse_legacy_message_accounts(self), recent_blockhash: self.recent_blockhash.to_string(), instructions: self .instructions .iter() .map(|instruction| UiInstruction::parse(instruction, &account_keys)) .collect(), + address_table_lookups: None, }) } else { UiMessage::Raw(UiRawMessage { @@ -813,6 +931,43 @@ impl Encodable for &Message { account_keys: self.account_keys.iter().map(ToString::to_string).collect(), recent_blockhash: self.recent_blockhash.to_string(), instructions: self.instructions.iter().map(Into::into).collect(), + address_table_lookups: None, + }) + } + } +} + +impl EncodableWithMeta for v0::Message { + type Encoded = UiMessage; + fn encode_with_meta( + &self, + encoding: UiTransactionEncoding, + meta: &TransactionStatusMeta, + ) -> Self::Encoded { + if encoding == UiTransactionEncoding::JsonParsed { + let account_keys = AccountKeys::new(&self.account_keys, Some(&meta.loaded_addresses)); + let loaded_message = LoadedMessage::new_borrowed(self, &meta.loaded_addresses); + UiMessage::Parsed(UiParsedMessage { + account_keys: parse_loaded_message_accounts(&loaded_message), + recent_blockhash: self.recent_blockhash.to_string(), + instructions: self + .instructions + .iter() + .map(|instruction| UiInstruction::parse(instruction, &account_keys)) + .collect(), + address_table_lookups: Some( + self.address_table_lookups.iter().map(Into::into).collect(), + ), + }) + } else { + UiMessage::Raw(UiRawMessage { + header: self.header, + account_keys: self.account_keys.iter().map(ToString::to_string).collect(), + recent_blockhash: self.recent_blockhash.to_string(), + instructions: self.instructions.iter().map(Into::into).collect(), + address_table_lookups: Some( + self.address_table_lookups.iter().map(Into::into).collect(), + ), }) } } @@ -826,6 +981,27 @@ pub struct UiRawMessage { pub account_keys: Vec, pub recent_blockhash: String, pub instructions: Vec, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub address_table_lookups: Option>, +} + +/// A duplicate representation of a MessageAddressTableLookup, in raw format, for pretty JSON serialization +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct UiAddressTableLookup { + pub account_key: String, + pub writable_indexes: Vec, + pub readonly_indexes: Vec, +} + +impl From<&MessageAddressTableLookup> for UiAddressTableLookup { + fn from(lookup: &MessageAddressTableLookup) -> Self { + Self { + account_key: lookup.account_key.to_string(), + writable_indexes: lookup.writable_indexes.clone(), + readonly_indexes: lookup.readonly_indexes.clone(), + } + } } /// A duplicate representation of a Message, in parsed format, for pretty JSON serialization @@ -835,6 +1011,7 @@ pub struct UiParsedMessage { pub account_keys: Vec, pub recent_blockhash: String, pub instructions: Vec, + pub address_table_lookups: Option>, } // A serialized `Vec` is stored in the `tx-by-addr` table. The row keys are diff --git a/transaction-status/src/parse_accounts.rs b/transaction-status/src/parse_accounts.rs index 35d8d4c9e6ea7b..513dd45c4c9e22 100644 --- a/transaction-status/src/parse_accounts.rs +++ b/transaction-status/src/parse_accounts.rs @@ -1,4 +1,4 @@ -use solana_sdk::message::Message; +use solana_sdk::message::{v0::LoadedMessage, Message}; #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] @@ -8,7 +8,7 @@ pub struct ParsedAccount { pub signer: bool, } -pub fn parse_accounts(message: &Message) -> Vec { +pub fn parse_legacy_message_accounts(message: &Message) -> Vec { let mut accounts: Vec = vec![]; for (i, account_key) in message.account_keys.iter().enumerate() { accounts.push(ParsedAccount { @@ -20,6 +20,18 @@ pub fn parse_accounts(message: &Message) -> Vec { accounts } +pub fn parse_loaded_message_accounts(message: &LoadedMessage) -> Vec { + let mut accounts: Vec = vec![]; + for (i, account_key) in message.account_keys().iter().enumerate() { + accounts.push(ParsedAccount { + pubkey: account_key.to_string(), + writable: message.is_writable(i), + signer: message.is_signer(i), + }); + } + accounts +} + #[cfg(test)] mod test { use {super::*, solana_sdk::message::MessageHeader}; @@ -41,7 +53,7 @@ mod test { }; assert_eq!( - parse_accounts(&message), + parse_legacy_message_accounts(&message), vec![ ParsedAccount { pubkey: pubkey0.to_string(),