Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Revert serialization changes, and add wrapper to support slimmer meta…
Browse files Browse the repository at this point in the history
… fields
  • Loading branch information
Tyera Eulberg committed Sep 7, 2022
1 parent 91f84cd commit 3bddfa1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 37 deletions.
11 changes: 7 additions & 4 deletions cli-output/src/cli_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use {
},
solana_transaction_status::{
EncodedConfirmedBlock, EncodedTransaction, TransactionConfirmationStatus,
UiTransactionStatusMeta,
UiTransactionStatusMetaWrapper,
},
solana_vote_program::{
authorized_voters::AuthorizedVoters,
Expand Down Expand Up @@ -2421,7 +2421,10 @@ impl fmt::Display for CliBlock {
writeln_transaction(
f,
&transaction_with_meta.transaction.decode().unwrap(),
transaction_with_meta.meta.as_ref(),
transaction_with_meta
.meta
.as_ref()
.map(|meta| meta.unwrap()),
" ",
None,
None,
Expand All @@ -2435,7 +2438,7 @@ impl fmt::Display for CliBlock {
#[serde(rename_all = "camelCase")]
pub struct CliTransaction {
pub transaction: EncodedTransaction,
pub meta: Option<UiTransactionStatusMeta>,
pub meta: Option<UiTransactionStatusMetaWrapper>,
pub block_time: Option<UnixTimestamp>,
#[serde(skip_serializing)]
pub slot: Option<Slot>,
Expand All @@ -2455,7 +2458,7 @@ impl fmt::Display for CliTransaction {
writeln_transaction(
f,
&self.decoded_transaction,
self.meta.as_ref(),
self.meta.as_ref().map(|meta| meta.unwrap()),
&self.prefix,
if !self.sigverify_status.is_empty() {
Some(&self.sigverify_status)
Expand Down
6 changes: 3 additions & 3 deletions rpc-client/src/mock_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use {
EncodedConfirmedBlock, EncodedConfirmedTransactionWithStatusMeta, EncodedTransaction,
EncodedTransactionWithStatusMeta, Rewards, TransactionBinaryEncoding,
TransactionConfirmationStatus, TransactionStatus, UiCompiledInstruction, UiMessage,
UiRawMessage, UiTransaction, UiTransactionStatusMeta,
UiRawMessage, UiTransaction, UiTransactionStatusMeta, UiTransactionStatusMetaWrapper,
},
solana_version::Version,
std::{collections::HashMap, net::SocketAddr, str::FromStr, sync::RwLock},
Expand Down Expand Up @@ -217,7 +217,7 @@ impl RpcSender for MockSender {
address_table_lookups: None,
})
}),
meta: Some(UiTransactionStatusMeta {
meta: Some(UiTransactionStatusMetaWrapper::Full(UiTransactionStatusMeta {
err: None,
status: Ok(()),
fee: 0,
Expand All @@ -231,7 +231,7 @@ impl RpcSender for MockSender {
loaded_addresses: None,
return_data: None,
compute_units_consumed: None,
}),
})),
},
block_time: Some(1628633791),
})?,
Expand Down
93 changes: 63 additions & 30 deletions transaction-status/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,50 @@ impl Default for TransactionStatusMeta {
}
}

/// A wrapper for UiTransactionStatusMeta variants to support different meta field collections
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum UiTransactionStatusMetaWrapper {
Full(UiTransactionStatusMeta),
Simple(UiTransactionStatusMetaSimple),
}

impl From<TransactionStatusMeta> for UiTransactionStatusMetaWrapper {
fn from(meta: TransactionStatusMeta) -> Self {
Self::Full(meta.into())
}
}

impl UiTransactionStatusMetaWrapper {
fn parse(meta: TransactionStatusMeta, static_keys: &[Pubkey]) -> Self {
Self::Full(UiTransactionStatusMeta::parse(meta, static_keys))
}

fn build_simple(meta: TransactionStatusMeta, show_rewards: bool) -> Self {
Self::Simple(UiTransactionStatusMetaSimple {
err: meta.status.clone().err(),
status: meta.status,
fee: meta.fee,
pre_balances: meta.pre_balances,
post_balances: meta.post_balances,
pre_token_balances: meta
.pre_token_balances
.map(|balance| balance.into_iter().map(Into::into).collect()),
post_token_balances: meta
.post_token_balances
.map(|balance| balance.into_iter().map(Into::into).collect()),
rewards: if show_rewards { meta.rewards } else { None },
})
}

pub fn unwrap(&self) -> &UiTransactionStatusMeta {
match self {
Self::Full(meta) => meta,
Self::Simple(_) => panic!("only full UiTransactionStatusMeta objects can be unwrapped"),
}
}
}

/// A duplicate representation of TransactionStatusMeta with `err` field
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -326,22 +370,33 @@ pub struct UiTransactionStatusMeta {
pub fee: u64,
pub pre_balances: Vec<u64>,
pub post_balances: Vec<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub inner_instructions: Option<Vec<UiInnerInstructions>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub log_messages: Option<Vec<String>>,
pub pre_token_balances: Option<Vec<UiTransactionTokenBalance>>,
pub post_token_balances: Option<Vec<UiTransactionTokenBalance>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rewards: Option<Rewards>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub loaded_addresses: Option<UiLoadedAddresses>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub return_data: Option<TransactionReturnData>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub compute_units_consumed: Option<u64>,
}

/// A duplicate representation of UiTransactionStatusMeta without some fields
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UiTransactionStatusMetaSimple {
pub err: Option<TransactionError>,
pub status: TransactionResult<()>, // This field is deprecated. See https://github.com/solana-labs/solana/issues/9302
pub fee: u64,
pub pre_balances: Vec<u64>,
pub post_balances: Vec<u64>,
pub pre_token_balances: Option<Vec<UiTransactionTokenBalance>>,
pub post_token_balances: Option<Vec<UiTransactionTokenBalance>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rewards: Option<Rewards>,
}

/// A duplicate representation of LoadedAddresses
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -394,28 +449,6 @@ impl UiTransactionStatusMeta {
compute_units_consumed: meta.compute_units_consumed,
}
}

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: None,
log_messages: None,
pre_token_balances: meta
.pre_token_balances
.map(|balance| balance.into_iter().map(Into::into).collect()),
post_token_balances: meta
.post_token_balances
.map(|balance| balance.into_iter().map(Into::into).collect()),
rewards: if show_rewards { meta.rewards } else { None },
loaded_addresses: None,
return_data: None,
compute_units_consumed: None,
}
}
}

impl From<TransactionStatusMeta> for UiTransactionStatusMeta {
Expand Down Expand Up @@ -778,11 +811,11 @@ impl VersionedTransactionWithStatusMeta {
Ok(EncodedTransactionWithStatusMeta {
transaction: self.transaction.encode_with_meta(encoding, &self.meta),
meta: Some(match encoding {
UiTransactionEncoding::JsonParsed => UiTransactionStatusMeta::parse(
UiTransactionEncoding::JsonParsed => UiTransactionStatusMetaWrapper::parse(
self.meta,
self.transaction.message.static_account_keys(),
),
_ => UiTransactionStatusMeta::from(self.meta),
_ => UiTransactionStatusMetaWrapper::from(self.meta),
}),
version,
})
Expand Down Expand Up @@ -821,7 +854,7 @@ impl VersionedTransactionWithStatusMeta {
.collect(),
account_keys,
}),
meta: Some(UiTransactionStatusMeta::build_simple(
meta: Some(UiTransactionStatusMetaWrapper::build_simple(
self.meta,
show_rewards,
)),
Expand All @@ -834,7 +867,7 @@ impl VersionedTransactionWithStatusMeta {
#[serde(rename_all = "camelCase")]
pub struct EncodedTransactionWithStatusMeta {
pub transaction: EncodedTransaction,
pub meta: Option<UiTransactionStatusMeta>,
pub meta: Option<UiTransactionStatusMetaWrapper>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<TransactionVersion>,
}
Expand Down

0 comments on commit 3bddfa1

Please sign in to comment.