From 2d319396f3ee577f0355bf0d98752fab65f02bc5 Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Sat, 8 Aug 2020 11:08:03 -0600 Subject: [PATCH] Stringify any account lamports and epochs that can be set to u64::MAX --- account-decoder/src/lib.rs | 24 ++++++++++++++++++- account-decoder/src/parse_nonce.rs | 11 +++++---- account-decoder/src/parse_stake.rs | 37 ++++++++++++++++------------- account-decoder/src/parse_sysvar.rs | 23 +++++++++--------- account-decoder/src/parse_vote.rs | 10 ++++---- 5 files changed, 66 insertions(+), 39 deletions(-) diff --git a/account-decoder/src/lib.rs b/account-decoder/src/lib.rs index 3ddbb551b2e979..ac5cb7a335edef 100644 --- a/account-decoder/src/lib.rs +++ b/account-decoder/src/lib.rs @@ -13,7 +13,7 @@ pub mod parse_vote; pub mod validator_info; use crate::parse_account_data::{parse_account_data, AccountAdditionalData, ParsedAccount}; -use solana_sdk::{account::Account, clock::Epoch, pubkey::Pubkey}; +use solana_sdk::{account::Account, clock::Epoch, fee_calculator::FeeCalculator, pubkey::Pubkey}; use std::str::FromStr; pub type StringAmount = String; @@ -91,3 +91,25 @@ impl UiAccount { }) } } + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] +#[serde(rename_all = "camelCase")] +pub struct UiFeeCalculator { + pub lamports_per_signature: StringAmount, +} + +impl From for UiFeeCalculator { + fn from(fee_calculator: FeeCalculator) -> Self { + Self { + lamports_per_signature: fee_calculator.lamports_per_signature.to_string(), + } + } +} + +impl Default for UiFeeCalculator { + fn default() -> Self { + Self { + lamports_per_signature: "0".to_string(), + } + } +} diff --git a/account-decoder/src/parse_nonce.rs b/account-decoder/src/parse_nonce.rs index 6693350d4f8166..f75766c77a7ebb 100644 --- a/account-decoder/src/parse_nonce.rs +++ b/account-decoder/src/parse_nonce.rs @@ -1,6 +1,5 @@ -use crate::parse_account_data::ParseAccountError; +use crate::{parse_account_data::ParseAccountError, UiFeeCalculator}; use solana_sdk::{ - fee_calculator::FeeCalculator, instruction::InstructionError, nonce::{state::Versions, State}, }; @@ -14,7 +13,7 @@ pub fn parse_nonce(data: &[u8]) -> Result { State::Initialized(data) => Ok(UiNonceState::Initialized(UiNonceData { authority: data.authority.to_string(), blockhash: data.blockhash.to_string(), - fee_calculator: data.fee_calculator, + fee_calculator: data.fee_calculator.into(), })), } } @@ -32,7 +31,7 @@ pub enum UiNonceState { pub struct UiNonceData { pub authority: String, pub blockhash: String, - pub fee_calculator: FeeCalculator, + pub fee_calculator: UiFeeCalculator, } #[cfg(test)] @@ -56,7 +55,9 @@ mod test { UiNonceState::Initialized(UiNonceData { authority: Pubkey::default().to_string(), blockhash: Hash::default().to_string(), - fee_calculator: FeeCalculator::default(), + fee_calculator: UiFeeCalculator { + lamports_per_signature: 0.to_string(), + }, }), ); diff --git a/account-decoder/src/parse_stake.rs b/account-decoder/src/parse_stake.rs index 1efb028c934add..14c87101681759 100644 --- a/account-decoder/src/parse_stake.rs +++ b/account-decoder/src/parse_stake.rs @@ -1,4 +1,7 @@ -use crate::parse_account_data::{ParsableAccount, ParseAccountError}; +use crate::{ + parse_account_data::{ParsableAccount, ParseAccountError}, + StringAmount, +}; use bincode::deserialize; use solana_sdk::clock::{Epoch, UnixTimestamp}; use solana_stake_program::stake_state::{Authorized, Delegation, Lockup, Meta, Stake, StakeState}; @@ -40,7 +43,7 @@ pub struct UiStakeAccount { #[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct UiMeta { - pub rent_exempt_reserve: u64, + pub rent_exempt_reserve: StringAmount, pub authorized: UiAuthorized, pub lockup: UiLockup, } @@ -48,7 +51,7 @@ pub struct UiMeta { impl From for UiMeta { fn from(meta: Meta) -> Self { Self { - rent_exempt_reserve: meta.rent_exempt_reserve, + rent_exempt_reserve: meta.rent_exempt_reserve.to_string(), authorized: meta.authorized.into(), lockup: meta.lockup.into(), } @@ -93,14 +96,14 @@ impl From for UiAuthorized { #[serde(rename_all = "camelCase")] pub struct UiStake { pub delegation: UiDelegation, - pub credits_observed: u64, + pub credits_observed: StringAmount, } impl From for UiStake { fn from(stake: Stake) -> Self { Self { delegation: stake.delegation.into(), - credits_observed: stake.credits_observed, + credits_observed: stake.credits_observed.to_string(), } } } @@ -109,9 +112,9 @@ impl From for UiStake { #[serde(rename_all = "camelCase")] pub struct UiDelegation { pub voter: String, - pub stake: u64, - pub activation_epoch: Epoch, - pub deactivation_epoch: Epoch, + pub stake: StringAmount, + pub activation_epoch: StringAmount, + pub deactivation_epoch: StringAmount, pub warmup_cooldown_rate: f64, } @@ -119,9 +122,9 @@ impl From for UiDelegation { fn from(delegation: Delegation) -> Self { Self { voter: delegation.voter_pubkey.to_string(), - stake: delegation.stake, - activation_epoch: delegation.activation_epoch, - deactivation_epoch: delegation.deactivation_epoch, + stake: delegation.stake.to_string(), + activation_epoch: delegation.activation_epoch.to_string(), + deactivation_epoch: delegation.deactivation_epoch.to_string(), warmup_cooldown_rate: delegation.warmup_cooldown_rate, } } @@ -162,7 +165,7 @@ mod test { parse_stake(&stake_data).unwrap(), StakeAccountType::Initialized(UiStakeAccount { meta: UiMeta { - rent_exempt_reserve: 42, + rent_exempt_reserve: 42.to_string(), authorized: UiAuthorized { staker: pubkey.to_string(), withdrawer: pubkey.to_string(), @@ -195,7 +198,7 @@ mod test { parse_stake(&stake_data).unwrap(), StakeAccountType::Delegated(UiStakeAccount { meta: UiMeta { - rent_exempt_reserve: 42, + rent_exempt_reserve: 42.to_string(), authorized: UiAuthorized { staker: pubkey.to_string(), withdrawer: pubkey.to_string(), @@ -209,12 +212,12 @@ mod test { stake: Some(UiStake { delegation: UiDelegation { voter: voter_pubkey.to_string(), - stake: 20, - activation_epoch: 2, - deactivation_epoch: std::u64::MAX, + stake: 20.to_string(), + activation_epoch: 2.to_string(), + deactivation_epoch: std::u64::MAX.to_string(), warmup_cooldown_rate: 0.25, }, - credits_observed: 10, + credits_observed: 10.to_string(), }) }) ); diff --git a/account-decoder/src/parse_sysvar.rs b/account-decoder/src/parse_sysvar.rs index 1d2783219f667f..9f9436830dd7af 100644 --- a/account-decoder/src/parse_sysvar.rs +++ b/account-decoder/src/parse_sysvar.rs @@ -1,10 +1,12 @@ -use crate::parse_account_data::{ParsableAccount, ParseAccountError}; +use crate::{ + parse_account_data::{ParsableAccount, ParseAccountError}, + StringAmount, UiFeeCalculator, +}; use bincode::deserialize; use bv::BitVec; use solana_sdk::{ clock::{Clock, Epoch, Slot, UnixTimestamp}, epoch_schedule::EpochSchedule, - fee_calculator::FeeCalculator, pubkey::Pubkey, rent::Rent, slot_hashes::SlotHashes, @@ -33,7 +35,7 @@ pub fn parse_sysvar(data: &[u8], pubkey: &Pubkey) -> Result for UiClock { #[derive(Debug, Serialize, Deserialize, PartialEq, Default)] #[serde(rename_all = "camelCase")] pub struct UiFees { - pub fee_calculator: FeeCalculator, + pub fee_calculator: UiFeeCalculator, } impl From for UiFees { fn from(fees: Fees) -> Self { Self { - fee_calculator: fees.fee_calculator, + fee_calculator: fees.fee_calculator.into(), } } } @@ -134,7 +136,7 @@ impl From for UiFees { #[derive(Debug, Serialize, Deserialize, PartialEq, Default)] #[serde(rename_all = "camelCase")] pub struct UiRent { - pub lamports_per_byte_year: u64, + pub lamports_per_byte_year: StringAmount, pub exemption_threshold: f64, pub burn_percent: u8, } @@ -142,7 +144,7 @@ pub struct UiRent { impl From for UiRent { fn from(rent: Rent) -> Self { Self { - lamports_per_byte_year: rent.lamports_per_byte_year, + lamports_per_byte_year: rent.lamports_per_byte_year.to_string(), exemption_threshold: rent.exemption_threshold, burn_percent: rent.burn_percent, } @@ -153,14 +155,12 @@ impl From for UiRent { #[serde(rename_all = "camelCase")] pub struct UiRewards { pub validator_point_value: f64, - pub unused: f64, } impl From for UiRewards { fn from(rewards: Rewards) -> Self { Self { validator_point_value: rewards.validator_point_value, - unused: rewards.unused, } } } @@ -169,7 +169,7 @@ impl From for UiRewards { #[serde(rename_all = "camelCase")] pub struct UiRecentBlockhashesEntry { pub blockhash: String, - pub fee_calculator: FeeCalculator, + pub fee_calculator: UiFeeCalculator, } #[derive(Debug, Serialize, Deserialize, PartialEq)] @@ -212,6 +212,7 @@ pub struct UiStakeHistoryEntry { mod test { use super::*; use solana_sdk::{ + fee_calculator::FeeCalculator, hash::Hash, sysvar::{recent_blockhashes::IterItem, Sysvar}, }; @@ -259,7 +260,7 @@ mod test { .unwrap(), SysvarAccountType::RecentBlockhashes(vec![UiRecentBlockhashesEntry { blockhash: hash.to_string(), - fee_calculator + fee_calculator: fee_calculator.into(), }]), ); diff --git a/account-decoder/src/parse_vote.rs b/account-decoder/src/parse_vote.rs index 70cd691af16be9..2ff9d4dd139f4b 100644 --- a/account-decoder/src/parse_vote.rs +++ b/account-decoder/src/parse_vote.rs @@ -1,4 +1,4 @@ -use crate::parse_account_data::ParseAccountError; +use crate::{parse_account_data::ParseAccountError, StringAmount}; use solana_sdk::{ clock::{Epoch, Slot}, pubkey::Pubkey, @@ -12,8 +12,8 @@ pub fn parse_vote(data: &[u8]) -> Result { .iter() .map(|(epoch, credits, previous_credits)| UiEpochCredits { epoch: *epoch, - credits: *credits, - previous_credits: *previous_credits, + credits: credits.to_string(), + previous_credits: previous_credits.to_string(), }) .collect(); let votes = vote_state @@ -115,8 +115,8 @@ struct UiPriorVoters { #[serde(rename_all = "camelCase")] struct UiEpochCredits { epoch: Epoch, - credits: u64, - previous_credits: u64, + credits: StringAmount, + previous_credits: StringAmount, } #[cfg(test)]