Add account-decoder utilities (#10846)

* Fix comment and make less pub

* Add account-decoder crate and use to decode vote and system (nonce) accounts

* Update docs

* Rename RpcAccount struct

* s/Rpc/Display

* Call it jsonParsed and update docs

* Revert "s/Rpc/Display"

This reverts commit 6e7149f503.

* s/Rpc/Ui

* Add tests

* Ui more things

* Comments
This commit is contained in:
Tyera Eulberg
2020-06-30 22:55:11 -06:00
committed by GitHub
parent b89e506cbb
commit d97850f1d9
23 changed files with 508 additions and 95 deletions

View File

@ -8,6 +8,7 @@ use crate::{
use bincode::serialize;
use jsonrpc_core::{Error, Metadata, Result};
use jsonrpc_derive::rpc;
use solana_account_decoder::{UiAccount, UiAccountEncoding};
use solana_client::{
rpc_config::*,
rpc_request::{
@ -205,10 +206,16 @@ impl JsonRpcRequestProcessor {
pub fn get_account_info(
&self,
pubkey: &Pubkey,
commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<Option<RpcAccount>>> {
let bank = self.bank(commitment)?;
new_response(&bank, bank.get_account(pubkey).map(RpcAccount::encode))
config: Option<RpcAccountInfoConfig>,
) -> Result<RpcResponse<Option<UiAccount>>> {
let config = config.unwrap_or_default();
let bank = self.bank(config.commitment)?;
let encoding = config.encoding.unwrap_or(UiAccountEncoding::Binary);
new_response(
&bank,
bank.get_account(pubkey)
.map(|account| UiAccount::encode(account, encoding)),
)
}
pub fn get_minimum_balance_for_rent_exemption(
@ -224,15 +231,17 @@ impl JsonRpcRequestProcessor {
pub fn get_program_accounts(
&self,
program_id: &Pubkey,
commitment: Option<CommitmentConfig>,
config: Option<RpcAccountInfoConfig>,
) -> Result<Vec<RpcKeyedAccount>> {
Ok(self
.bank(commitment)?
let config = config.unwrap_or_default();
let bank = self.bank(config.commitment)?;
let encoding = config.encoding.unwrap_or(UiAccountEncoding::Binary);
Ok(bank
.get_program_accounts(Some(&program_id))
.into_iter()
.map(|(pubkey, account)| RpcKeyedAccount {
pubkey: pubkey.to_string(),
account: RpcAccount::encode(account),
account: UiAccount::encode(account, encoding.clone()),
})
.collect())
}
@ -823,15 +832,15 @@ pub trait RpcSol {
&self,
meta: Self::Metadata,
pubkey_str: String,
commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<Option<RpcAccount>>>;
config: Option<RpcAccountInfoConfig>,
) -> Result<RpcResponse<Option<UiAccount>>>;
#[rpc(meta, name = "getProgramAccounts")]
fn get_program_accounts(
&self,
meta: Self::Metadata,
program_id_str: String,
commitment: Option<CommitmentConfig>,
config: Option<RpcAccountInfoConfig>,
) -> Result<Vec<RpcKeyedAccount>>;
#[rpc(meta, name = "getMinimumBalanceForRentExemption")]
@ -1076,11 +1085,11 @@ impl RpcSol for RpcSolImpl {
&self,
meta: Self::Metadata,
pubkey_str: String,
commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<Option<RpcAccount>>> {
config: Option<RpcAccountInfoConfig>,
) -> Result<RpcResponse<Option<UiAccount>>> {
debug!("get_account_info rpc request received: {:?}", pubkey_str);
let pubkey = verify_pubkey(pubkey_str)?;
meta.get_account_info(&pubkey, commitment)
meta.get_account_info(&pubkey, config)
}
fn get_minimum_balance_for_rent_exemption(
@ -1100,14 +1109,14 @@ impl RpcSol for RpcSolImpl {
&self,
meta: Self::Metadata,
program_id_str: String,
commitment: Option<CommitmentConfig>,
config: Option<RpcAccountInfoConfig>,
) -> Result<Vec<RpcKeyedAccount>> {
debug!(
"get_program_accounts rpc request received: {:?}",
program_id_str
);
let program_id = verify_pubkey(program_id_str)?;
meta.get_program_accounts(&program_id, commitment)
meta.get_program_accounts(&program_id, config)
}
fn get_inflation_governor(

View File

@ -4,9 +4,8 @@ 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_client::rpc_response::{
Response as RpcResponse, RpcAccount, RpcKeyedAccount, RpcSignatureResult,
};
use solana_account_decoder::UiAccount;
use solana_client::rpc_response::{Response as RpcResponse, RpcKeyedAccount, RpcSignatureResult};
#[cfg(test)]
use solana_runtime::bank_forks::BankForks;
use solana_sdk::{
@ -37,7 +36,7 @@ pub trait RpcSolPubSub {
fn account_subscribe(
&self,
meta: Self::Metadata,
subscriber: Subscriber<RpcResponse<RpcAccount>>,
subscriber: Subscriber<RpcResponse<UiAccount>>,
pubkey_str: String,
commitment: Option<CommitmentConfig>,
);
@ -172,7 +171,7 @@ impl RpcSolPubSub for RpcSolPubSubImpl {
fn account_subscribe(
&self,
_meta: Self::Metadata,
subscriber: Subscriber<RpcResponse<RpcAccount>>,
subscriber: Subscriber<RpcResponse<UiAccount>>,
pubkey_str: String,
commitment: Option<CommitmentConfig>,
) {

View File

@ -7,8 +7,9 @@ use jsonrpc_pubsub::{
SubscriptionId,
};
use serde::Serialize;
use solana_account_decoder::{UiAccount, UiAccountEncoding};
use solana_client::rpc_response::{
Response, RpcAccount, RpcKeyedAccount, RpcResponseContext, RpcSignatureResult,
Response, RpcKeyedAccount, RpcResponseContext, RpcSignatureResult,
};
use solana_runtime::{bank::Bank, bank_forks::BankForks, commitment::BlockCommitmentCache};
use solana_sdk::{
@ -90,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<UiAccount>>>>>;
type RpcProgramSubscriptions =
RwLock<HashMap<Pubkey, HashMap<SubscriptionId, SubscriptionData<Response<RpcKeyedAccount>>>>>;
type RpcSignatureSubscriptions = RwLock<
@ -225,12 +226,18 @@ impl RpcNotifier {
fn filter_account_result(
result: Option<(Account, Slot)>,
last_notified_slot: Slot,
) -> (Box<dyn Iterator<Item = RpcAccount>>, Slot) {
) -> (Box<dyn Iterator<Item = UiAccount>>, 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(account))), fork);
return (
Box::new(iter::once(UiAccount::encode(
account,
UiAccountEncoding::Binary,
))),
fork,
);
}
}
(Box::new(iter::empty()), last_notified_slot)
@ -260,7 +267,7 @@ fn filter_program_results(
.into_iter()
.map(|(pubkey, account)| RpcKeyedAccount {
pubkey: pubkey.to_string(),
account: RpcAccount::encode(account),
account: UiAccount::encode(account, UiAccountEncoding::Binary),
}),
),
last_notified_slot,
@ -449,7 +456,7 @@ impl RpcSubscriptions {
pubkey: Pubkey,
commitment: Option<CommitmentConfig>,
sub_id: SubscriptionId,
subscriber: Subscriber<Response<RpcAccount>>,
subscriber: Subscriber<Response<UiAccount>>,
) {
let commitment_level = commitment
.unwrap_or_else(CommitmentConfig::single)