Skip to content

Commit

Permalink
Rename RpcAccount struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyera Eulberg committed Jun 30, 2020
1 parent ce55a76 commit 592f882
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 34 deletions.
18 changes: 9 additions & 9 deletions account-decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ use std::str::FromStr;
/// A duplicate representation of a Message for pretty JSON serialization
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct RpcAccount {
pub struct EncodedAccount {
pub lamports: u64,
pub data: EncodedAccount,
pub data: EncodedAccountData,
pub owner: String,
pub executable: bool,
pub rent_epoch: Epoch,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", untagged)]
pub enum EncodedAccount {
pub enum EncodedAccountData {
Binary(String),
Json(Value),
}

impl From<Vec<u8>> for EncodedAccount {
impl From<Vec<u8>> for EncodedAccountData {
fn from(data: Vec<u8>) -> Self {
Self::Binary(bs58::encode(data).into_string())
}
Expand All @@ -43,19 +43,19 @@ pub enum AccountEncoding {
Json,
}

impl RpcAccount {
impl EncodedAccount {
pub fn encode(account: Account, encoding: AccountEncoding) -> Self {
let data = match encoding {
AccountEncoding::Binary => account.data.into(),
AccountEncoding::Json => {
if let Ok(parsed_data) = parse_account_data(&account.owner, &account.data) {
EncodedAccount::Json(parsed_data)
EncodedAccountData::Json(parsed_data)
} else {
account.data.into()
}
}
};
RpcAccount {
EncodedAccount {
lamports: account.lamports,
data,
owner: account.owner.to_string(),
Expand All @@ -66,8 +66,8 @@ impl RpcAccount {

pub fn decode(&self) -> Option<Account> {
let data = match &self.data {
EncodedAccount::Json(_) => None,
EncodedAccount::Binary(blob) => bs58::decode(blob).into_vec().ok(),
EncodedAccountData::Json(_) => None,
EncodedAccountData::Binary(blob) => bs58::decode(blob).into_vec().ok(),
}?;
Some(Account {
lamports: self.lamports,
Expand Down
4 changes: 2 additions & 2 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
use log::*;
use num_traits::FromPrimitive;
use serde_json::{self, json, Value};
use solana_account_decoder::{AccountEncoding, RpcAccount};
use solana_account_decoder::{AccountEncoding, EncodedAccount};
use solana_budget_program::budget_instruction::{self, BudgetError};
use solana_clap_utils::{
commitment::commitment_arg_with_default, input_parsers::*, input_validators::*,
Expand Down Expand Up @@ -1226,7 +1226,7 @@ fn process_show_account(
let cli_account = CliAccount {
keyed_account: RpcKeyedAccount {
pubkey: account_pubkey.to_string(),
account: RpcAccount::encode(account, AccountEncoding::Binary),
account: EncodedAccount::encode(account, AccountEncoding::Binary),
},
use_lamports_unit,
};
Expand Down
4 changes: 2 additions & 2 deletions cli/src/offline/blockhash_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ mod tests {
use crate::{nonce::nonce_arg, offline::blockhash_query::BlockhashQuery};
use clap::App;
use serde_json::{self, json, Value};
use solana_account_decoder::{AccountEncoding, RpcAccount};
use solana_account_decoder::{AccountEncoding, EncodedAccount};
use solana_client::{
rpc_request::RpcRequest,
rpc_response::{Response, RpcFeeCalculator, RpcResponseContext},
Expand Down Expand Up @@ -350,7 +350,7 @@ mod tests {
)
.unwrap();
let nonce_pubkey = Pubkey::new(&[4u8; 32]);
let rpc_nonce_account = RpcAccount::encode(nonce_account, AccountEncoding::Binary);
let rpc_nonce_account = EncodedAccount::encode(nonce_account, AccountEncoding::Binary);
let get_account_response = json!(Response {
context: RpcResponseContext { slot: 1 },
value: json!(Some(rpc_nonce_account)),
Expand Down
4 changes: 2 additions & 2 deletions client/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bincode::serialize;
use indicatif::{ProgressBar, ProgressStyle};
use log::*;
use serde_json::{json, Value};
use solana_account_decoder::RpcAccount;
use solana_account_decoder::EncodedAccount;
use solana_sdk::{
account::Account,
clock::{
Expand Down Expand Up @@ -441,7 +441,7 @@ impl RpcClient {
let Response {
context,
value: rpc_account,
} = serde_json::from_value::<Response<Option<RpcAccount>>>(result_json)?;
} = serde_json::from_value::<Response<Option<EncodedAccount>>>(result_json)?;
trace!("Response account {:?} {:?}", pubkey, rpc_account);
let account = rpc_account.and_then(|rpc_account| rpc_account.decode());
Ok(Response {
Expand Down
4 changes: 2 additions & 2 deletions client/src/rpc_response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::client_error;
use solana_account_decoder::RpcAccount;
use solana_account_decoder::EncodedAccount;
use solana_sdk::{
clock::{Epoch, Slot},
fee_calculator::{FeeCalculator, FeeRateGovernor},
Expand Down Expand Up @@ -90,7 +90,7 @@ pub struct RpcInflationRate {
#[serde(rename_all = "camelCase")]
pub struct RpcKeyedAccount {
pub pubkey: String,
pub account: RpcAccount,
pub account: EncodedAccount,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down
12 changes: 6 additions & 6 deletions core/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
use bincode::serialize;
use jsonrpc_core::{Error, Metadata, Result};
use jsonrpc_derive::rpc;
use solana_account_decoder::{AccountEncoding, RpcAccount};
use solana_account_decoder::{AccountEncoding, EncodedAccount};
use solana_client::{
rpc_config::*,
rpc_request::{
Expand Down Expand Up @@ -207,14 +207,14 @@ impl JsonRpcRequestProcessor {
&self,
pubkey: &Pubkey,
config: Option<RpcAccountInfoConfig>,
) -> Result<RpcResponse<Option<RpcAccount>>> {
) -> Result<RpcResponse<Option<EncodedAccount>>> {
let config = config.unwrap_or_default();
let bank = self.bank(config.commitment)?;
let encoding = config.encoding.unwrap_or(AccountEncoding::Binary);
new_response(
&bank,
bank.get_account(pubkey)
.map(|account| RpcAccount::encode(account, encoding)),
.map(|account| EncodedAccount::encode(account, encoding)),
)
}

Expand All @@ -241,7 +241,7 @@ impl JsonRpcRequestProcessor {
.into_iter()
.map(|(pubkey, account)| RpcKeyedAccount {
pubkey: pubkey.to_string(),
account: RpcAccount::encode(account, encoding.clone()),
account: EncodedAccount::encode(account, encoding.clone()),
})
.collect())
}
Expand Down Expand Up @@ -833,7 +833,7 @@ pub trait RpcSol {
meta: Self::Metadata,
pubkey_str: String,
config: Option<RpcAccountInfoConfig>,
) -> Result<RpcResponse<Option<RpcAccount>>>;
) -> Result<RpcResponse<Option<EncodedAccount>>>;

#[rpc(meta, name = "getProgramAccounts")]
fn get_program_accounts(
Expand Down Expand Up @@ -1086,7 +1086,7 @@ impl RpcSol for RpcSolImpl {
meta: Self::Metadata,
pubkey_str: String,
config: Option<RpcAccountInfoConfig>,
) -> Result<RpcResponse<Option<RpcAccount>>> {
) -> Result<RpcResponse<Option<EncodedAccount>>> {
debug!("get_account_info rpc request received: {:?}", pubkey_str);
let pubkey = verify_pubkey(pubkey_str)?;
meta.get_account_info(&pubkey, config)
Expand Down
6 changes: 3 additions & 3 deletions core/src/rpc_pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::rpc_subscriptions::{RpcSubscriptions, RpcVote, SlotInfo};
use jsonrpc_core::{Error, ErrorCode, Result};
use jsonrpc_derive::rpc;
use jsonrpc_pubsub::{typed::Subscriber, Session, SubscriptionId};
use solana_account_decoder::RpcAccount;
use solana_account_decoder::EncodedAccount;
use solana_client::rpc_response::{Response as RpcResponse, RpcKeyedAccount, RpcSignatureResult};
#[cfg(test)]
use solana_runtime::bank_forks::BankForks;
Expand Down Expand Up @@ -36,7 +36,7 @@ pub trait RpcSolPubSub {
fn account_subscribe(
&self,
meta: Self::Metadata,
subscriber: Subscriber<RpcResponse<RpcAccount>>,
subscriber: Subscriber<RpcResponse<EncodedAccount>>,
pubkey_str: String,
commitment: Option<CommitmentConfig>,
);
Expand Down Expand Up @@ -171,7 +171,7 @@ impl RpcSolPubSub for RpcSolPubSubImpl {
fn account_subscribe(
&self,
_meta: Self::Metadata,
subscriber: Subscriber<RpcResponse<RpcAccount>>,
subscriber: Subscriber<RpcResponse<EncodedAccount>>,
pubkey_str: String,
commitment: Option<CommitmentConfig>,
) {
Expand Down
12 changes: 6 additions & 6 deletions core/src/rpc_subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use jsonrpc_pubsub::{
SubscriptionId,
};
use serde::Serialize;
use solana_account_decoder::{AccountEncoding, RpcAccount};
use solana_account_decoder::{AccountEncoding, EncodedAccount};
use solana_client::rpc_response::{
Response, RpcKeyedAccount, RpcResponseContext, RpcSignatureResult,
};
Expand Down Expand Up @@ -91,7 +91,7 @@ struct SubscriptionData<S> {
last_notified_slot: RwLock<Slot>,
}
type RpcAccountSubscriptions =
RwLock<HashMap<Pubkey, HashMap<SubscriptionId, SubscriptionData<Response<RpcAccount>>>>>;
RwLock<HashMap<Pubkey, HashMap<SubscriptionId, SubscriptionData<Response<EncodedAccount>>>>>;
type RpcProgramSubscriptions =
RwLock<HashMap<Pubkey, HashMap<SubscriptionId, SubscriptionData<Response<RpcKeyedAccount>>>>>;
type RpcSignatureSubscriptions = RwLock<
Expand Down Expand Up @@ -226,13 +226,13 @@ impl RpcNotifier {
fn filter_account_result(
result: Option<(Account, Slot)>,
last_notified_slot: Slot,
) -> (Box<dyn Iterator<Item = RpcAccount>>, Slot) {
) -> (Box<dyn Iterator<Item = EncodedAccount>>, Slot) {
if let Some((account, fork)) = result {
// If fork < last_notified_slot this means that we last notified for a fork
// and should notify that the account state has been reverted.
if fork != last_notified_slot {
return (
Box::new(iter::once(RpcAccount::encode(
Box::new(iter::once(EncodedAccount::encode(
account,
AccountEncoding::Binary,
))),
Expand Down Expand Up @@ -267,7 +267,7 @@ fn filter_program_results(
.into_iter()
.map(|(pubkey, account)| RpcKeyedAccount {
pubkey: pubkey.to_string(),
account: RpcAccount::encode(account, AccountEncoding::Binary),
account: EncodedAccount::encode(account, AccountEncoding::Binary),
}),
),
last_notified_slot,
Expand Down Expand Up @@ -456,7 +456,7 @@ impl RpcSubscriptions {
pubkey: Pubkey,
commitment: Option<CommitmentConfig>,
sub_id: SubscriptionId,
subscriber: Subscriber<Response<RpcAccount>>,
subscriber: Subscriber<Response<EncodedAccount>>,
) {
let commitment_level = commitment
.unwrap_or_else(CommitmentConfig::single)
Expand Down
4 changes: 2 additions & 2 deletions core/tests/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use jsonrpc_core_client::transports::ws;
use log::*;
use reqwest::{self, header::CONTENT_TYPE};
use serde_json::{json, Value};
use solana_account_decoder::RpcAccount;
use solana_account_decoder::EncodedAccount;
use solana_client::{
rpc_client::{get_rpc_request_str, RpcClient},
rpc_response::{Response, RpcSignatureResult},
Expand Down Expand Up @@ -173,7 +173,7 @@ fn test_rpc_subscriptions() {
// Track when subscriptions are ready
let (ready_sender, ready_receiver) = channel::<()>();
// Track account notifications are received
let (account_sender, account_receiver) = channel::<Response<RpcAccount>>();
let (account_sender, account_receiver) = channel::<Response<EncodedAccount>>();
// Track when status notifications are received
let (status_sender, status_receiver) = channel::<(String, Response<RpcSignatureResult>)>();

Expand Down

0 comments on commit 592f882

Please sign in to comment.