* Clean up rpc module
* Simplify getting bank
(cherry picked from commit 62b873b054
)
Co-authored-by: Greg Fitzgerald <greg@solana.com>
This commit is contained in:
143
core/src/rpc.rs
143
core/src/rpc.rs
@ -85,21 +85,23 @@ impl JsonRpcRequestProcessor {
|
|||||||
debug!("RPC commitment_config: {:?}", commitment);
|
debug!("RPC commitment_config: {:?}", commitment);
|
||||||
let r_bank_forks = self.bank_forks.read().unwrap();
|
let r_bank_forks = self.bank_forks.read().unwrap();
|
||||||
|
|
||||||
match commitment {
|
let commitment_level = match commitment {
|
||||||
Some(commitment_config) if commitment_config.commitment == CommitmentLevel::Recent => {
|
None => CommitmentLevel::Max,
|
||||||
|
Some(config) => config.commitment,
|
||||||
|
};
|
||||||
|
|
||||||
|
match commitment_level {
|
||||||
|
CommitmentLevel::Recent => {
|
||||||
let bank = r_bank_forks.working_bank();
|
let bank = r_bank_forks.working_bank();
|
||||||
debug!("RPC using working_bank: {:?}", bank.slot());
|
debug!("RPC using working_bank: {:?}", bank.slot());
|
||||||
Ok(bank)
|
Ok(bank)
|
||||||
}
|
}
|
||||||
Some(commitment_config) if commitment_config.commitment == CommitmentLevel::Root => {
|
CommitmentLevel::Root => {
|
||||||
let slot = r_bank_forks.root();
|
let slot = r_bank_forks.root();
|
||||||
debug!("RPC using node root: {:?}", slot);
|
debug!("RPC using node root: {:?}", slot);
|
||||||
Ok(r_bank_forks.get(slot).cloned().unwrap())
|
Ok(r_bank_forks.get(slot).cloned().unwrap())
|
||||||
}
|
}
|
||||||
Some(commitment_config)
|
CommitmentLevel::Single | CommitmentLevel::SingleGossip => {
|
||||||
if commitment_config.commitment == CommitmentLevel::Single
|
|
||||||
|| commitment_config.commitment == CommitmentLevel::SingleGossip =>
|
|
||||||
{
|
|
||||||
let slot = self
|
let slot = self
|
||||||
.block_commitment_cache
|
.block_commitment_cache
|
||||||
.read()
|
.read()
|
||||||
@ -108,7 +110,7 @@ impl JsonRpcRequestProcessor {
|
|||||||
debug!("RPC using confirmed slot: {:?}", slot);
|
debug!("RPC using confirmed slot: {:?}", slot);
|
||||||
Ok(r_bank_forks.get(slot).cloned().unwrap())
|
Ok(r_bank_forks.get(slot).cloned().unwrap())
|
||||||
}
|
}
|
||||||
_ => {
|
CommitmentLevel::Max => {
|
||||||
let cluster_root = self
|
let cluster_root = self
|
||||||
.block_commitment_cache
|
.block_commitment_cache
|
||||||
.read()
|
.read()
|
||||||
@ -152,11 +154,11 @@ impl JsonRpcRequestProcessor {
|
|||||||
|
|
||||||
pub fn get_account_info(
|
pub fn get_account_info(
|
||||||
&self,
|
&self,
|
||||||
pubkey: Result<Pubkey>,
|
pubkey: &Pubkey,
|
||||||
commitment: Option<CommitmentConfig>,
|
commitment: Option<CommitmentConfig>,
|
||||||
) -> Result<RpcResponse<Option<RpcAccount>>> {
|
) -> Result<RpcResponse<Option<RpcAccount>>> {
|
||||||
let bank = &*self.bank(commitment)?;
|
let bank = self.bank(commitment)?;
|
||||||
pubkey.and_then(|key| new_response(bank, bank.get_account(&key).map(RpcAccount::encode)))
|
new_response(&bank, bank.get_account(pubkey).map(RpcAccount::encode))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_minimum_balance_for_rent_exemption(
|
pub fn get_minimum_balance_for_rent_exemption(
|
||||||
@ -211,26 +213,27 @@ impl JsonRpcRequestProcessor {
|
|||||||
pub fn get_epoch_schedule(&self) -> Result<EpochSchedule> {
|
pub fn get_epoch_schedule(&self) -> Result<EpochSchedule> {
|
||||||
// Since epoch schedule data comes from the genesis config, any commitment level should be
|
// Since epoch schedule data comes from the genesis config, any commitment level should be
|
||||||
// fine
|
// fine
|
||||||
Ok(*self.bank(Some(CommitmentConfig::root()))?.epoch_schedule())
|
let bank = self.bank(Some(CommitmentConfig::root()))?;
|
||||||
|
Ok(*bank.epoch_schedule())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_balance(
|
pub fn get_balance(
|
||||||
&self,
|
&self,
|
||||||
pubkey: Result<Pubkey>,
|
pubkey: &Pubkey,
|
||||||
commitment: Option<CommitmentConfig>,
|
commitment: Option<CommitmentConfig>,
|
||||||
) -> Result<RpcResponse<u64>> {
|
) -> Result<RpcResponse<u64>> {
|
||||||
let bank = &*self.bank(commitment)?;
|
let bank = self.bank(commitment)?;
|
||||||
pubkey.and_then(|key| new_response(bank, bank.get_balance(&key)))
|
new_response(&bank, bank.get_balance(pubkey))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_recent_blockhash(
|
fn get_recent_blockhash(
|
||||||
&self,
|
&self,
|
||||||
commitment: Option<CommitmentConfig>,
|
commitment: Option<CommitmentConfig>,
|
||||||
) -> Result<RpcResponse<RpcBlockhashFeeCalculator>> {
|
) -> Result<RpcResponse<RpcBlockhashFeeCalculator>> {
|
||||||
let bank = &*self.bank(commitment)?;
|
let bank = self.bank(commitment)?;
|
||||||
let (blockhash, fee_calculator) = bank.confirmed_last_blockhash();
|
let (blockhash, fee_calculator) = bank.confirmed_last_blockhash();
|
||||||
new_response(
|
new_response(
|
||||||
bank,
|
&bank,
|
||||||
RpcBlockhashFeeCalculator {
|
RpcBlockhashFeeCalculator {
|
||||||
blockhash: blockhash.to_string(),
|
blockhash: blockhash.to_string(),
|
||||||
fee_calculator,
|
fee_calculator,
|
||||||
@ -239,13 +242,13 @@ impl JsonRpcRequestProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_fees(&self, commitment: Option<CommitmentConfig>) -> Result<RpcResponse<RpcFees>> {
|
fn get_fees(&self, commitment: Option<CommitmentConfig>) -> Result<RpcResponse<RpcFees>> {
|
||||||
let bank = &*self.bank(commitment)?;
|
let bank = self.bank(commitment)?;
|
||||||
let (blockhash, fee_calculator) = bank.confirmed_last_blockhash();
|
let (blockhash, fee_calculator) = bank.confirmed_last_blockhash();
|
||||||
let last_valid_slot = bank
|
let last_valid_slot = bank
|
||||||
.get_blockhash_last_valid_slot(&blockhash)
|
.get_blockhash_last_valid_slot(&blockhash)
|
||||||
.expect("bank blockhash queue should contain blockhash");
|
.expect("bank blockhash queue should contain blockhash");
|
||||||
new_response(
|
new_response(
|
||||||
bank,
|
&bank,
|
||||||
RpcFees {
|
RpcFees {
|
||||||
blockhash: blockhash.to_string(),
|
blockhash: blockhash.to_string(),
|
||||||
fee_calculator,
|
fee_calculator,
|
||||||
@ -259,19 +262,19 @@ impl JsonRpcRequestProcessor {
|
|||||||
blockhash: &Hash,
|
blockhash: &Hash,
|
||||||
commitment: Option<CommitmentConfig>,
|
commitment: Option<CommitmentConfig>,
|
||||||
) -> Result<RpcResponse<Option<RpcFeeCalculator>>> {
|
) -> Result<RpcResponse<Option<RpcFeeCalculator>>> {
|
||||||
let bank = &*self.bank(commitment)?;
|
let bank = self.bank(commitment)?;
|
||||||
let fee_calculator = bank.get_fee_calculator(blockhash);
|
let fee_calculator = bank.get_fee_calculator(blockhash);
|
||||||
new_response(
|
new_response(
|
||||||
bank,
|
&bank,
|
||||||
fee_calculator.map(|fee_calculator| RpcFeeCalculator { fee_calculator }),
|
fee_calculator.map(|fee_calculator| RpcFeeCalculator { fee_calculator }),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_fee_rate_governor(&self) -> Result<RpcResponse<RpcFeeRateGovernor>> {
|
fn get_fee_rate_governor(&self) -> Result<RpcResponse<RpcFeeRateGovernor>> {
|
||||||
let bank = &*self.bank(None)?;
|
let bank = self.bank(None)?;
|
||||||
let fee_rate_governor = bank.get_fee_rate_governor();
|
let fee_rate_governor = bank.get_fee_rate_governor();
|
||||||
new_response(
|
new_response(
|
||||||
bank,
|
&bank,
|
||||||
RpcFeeRateGovernor {
|
RpcFeeRateGovernor {
|
||||||
fee_rate_governor: fee_rate_governor.clone(),
|
fee_rate_governor: fee_rate_governor.clone(),
|
||||||
},
|
},
|
||||||
@ -280,19 +283,14 @@ impl JsonRpcRequestProcessor {
|
|||||||
|
|
||||||
pub fn confirm_transaction(
|
pub fn confirm_transaction(
|
||||||
&self,
|
&self,
|
||||||
signature: Result<Signature>,
|
signature: &Signature,
|
||||||
commitment: Option<CommitmentConfig>,
|
commitment: Option<CommitmentConfig>,
|
||||||
) -> Result<RpcResponse<bool>> {
|
) -> Result<RpcResponse<bool>> {
|
||||||
let bank = &*self.bank(commitment)?;
|
let bank = self.bank(commitment)?;
|
||||||
match signature {
|
let status = bank.get_signature_status(signature);
|
||||||
Err(e) => Err(e),
|
match status {
|
||||||
Ok(sig) => {
|
Some(status) => new_response(&bank, status.is_ok()),
|
||||||
let status = bank.get_signature_status(&sig);
|
None => new_response(&bank, false),
|
||||||
match status {
|
|
||||||
Some(status) => new_response(bank, status.is_ok()),
|
|
||||||
None => new_response(bank, false),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -557,17 +555,15 @@ impl JsonRpcRequestProcessor {
|
|||||||
signature: Signature,
|
signature: Signature,
|
||||||
commitment: Option<CommitmentConfig>,
|
commitment: Option<CommitmentConfig>,
|
||||||
) -> Option<RpcSignatureConfirmation> {
|
) -> Option<RpcSignatureConfirmation> {
|
||||||
self.get_transaction_status(signature, &self.bank(commitment).ok()?)
|
let bank = self.bank(commitment).ok()?;
|
||||||
.map(
|
let transaction_status = self.get_transaction_status(signature, &bank)?;
|
||||||
|TransactionStatus {
|
let confirmations = transaction_status
|
||||||
status,
|
.confirmations
|
||||||
confirmations,
|
.unwrap_or(MAX_LOCKOUT_HISTORY + 1);
|
||||||
..
|
Some(RpcSignatureConfirmation {
|
||||||
}| RpcSignatureConfirmation {
|
confirmations,
|
||||||
confirmations: confirmations.unwrap_or(MAX_LOCKOUT_HISTORY + 1),
|
status: transaction_status.status,
|
||||||
status,
|
})
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_signature_status(
|
pub fn get_signature_status(
|
||||||
@ -575,10 +571,9 @@ impl JsonRpcRequestProcessor {
|
|||||||
signature: Signature,
|
signature: Signature,
|
||||||
commitment: Option<CommitmentConfig>,
|
commitment: Option<CommitmentConfig>,
|
||||||
) -> Option<transaction::Result<()>> {
|
) -> Option<transaction::Result<()>> {
|
||||||
self.bank(commitment)
|
let bank = self.bank(commitment).ok()?;
|
||||||
.ok()?
|
let (_, status) = bank.get_signature_status_slot(&signature)?;
|
||||||
.get_signature_status_slot(&signature)
|
Some(status)
|
||||||
.map(|(_, status)| status)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_signature_statuses(
|
pub fn get_signature_statuses(
|
||||||
@ -632,27 +627,25 @@ impl JsonRpcRequestProcessor {
|
|||||||
signature: Signature,
|
signature: Signature,
|
||||||
bank: &Arc<Bank>,
|
bank: &Arc<Bank>,
|
||||||
) -> Option<TransactionStatus> {
|
) -> Option<TransactionStatus> {
|
||||||
bank.get_signature_status_slot(&signature)
|
let (slot, status) = bank.get_signature_status_slot(&signature)?;
|
||||||
.map(|(slot, status)| {
|
let r_block_commitment_cache = self.block_commitment_cache.read().unwrap();
|
||||||
let r_block_commitment_cache = self.block_commitment_cache.read().unwrap();
|
|
||||||
|
|
||||||
let confirmations = if r_block_commitment_cache.root() >= slot
|
let confirmations = if r_block_commitment_cache.root() >= slot
|
||||||
&& r_block_commitment_cache.is_confirmed_rooted(slot)
|
&& r_block_commitment_cache.is_confirmed_rooted(slot)
|
||||||
{
|
{
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
r_block_commitment_cache
|
r_block_commitment_cache
|
||||||
.get_confirmation_count(slot)
|
.get_confirmation_count(slot)
|
||||||
.or(Some(0))
|
.or(Some(0))
|
||||||
};
|
};
|
||||||
let err = status.clone().err();
|
let err = status.clone().err();
|
||||||
TransactionStatus {
|
Some(TransactionStatus {
|
||||||
slot,
|
slot,
|
||||||
status,
|
status,
|
||||||
confirmations,
|
confirmations,
|
||||||
err,
|
err,
|
||||||
}
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_confirmed_transaction(
|
pub fn get_confirmed_transaction(
|
||||||
@ -1021,8 +1014,8 @@ impl RpcSol for RpcSolImpl {
|
|||||||
commitment: Option<CommitmentConfig>,
|
commitment: Option<CommitmentConfig>,
|
||||||
) -> Result<RpcResponse<bool>> {
|
) -> Result<RpcResponse<bool>> {
|
||||||
debug!("confirm_transaction rpc request received: {:?}", id);
|
debug!("confirm_transaction rpc request received: {:?}", id);
|
||||||
let signature = verify_signature(&id);
|
let signature = verify_signature(&id)?;
|
||||||
meta.confirm_transaction(signature, commitment)
|
meta.confirm_transaction(&signature, commitment)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_account_info(
|
fn get_account_info(
|
||||||
@ -1032,8 +1025,8 @@ impl RpcSol for RpcSolImpl {
|
|||||||
commitment: Option<CommitmentConfig>,
|
commitment: Option<CommitmentConfig>,
|
||||||
) -> Result<RpcResponse<Option<RpcAccount>>> {
|
) -> Result<RpcResponse<Option<RpcAccount>>> {
|
||||||
debug!("get_account_info rpc request received: {:?}", pubkey_str);
|
debug!("get_account_info rpc request received: {:?}", pubkey_str);
|
||||||
let pubkey = verify_pubkey(pubkey_str);
|
let pubkey = verify_pubkey(pubkey_str)?;
|
||||||
meta.get_account_info(pubkey, commitment)
|
meta.get_account_info(&pubkey, commitment)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_minimum_balance_for_rent_exemption(
|
fn get_minimum_balance_for_rent_exemption(
|
||||||
@ -1093,8 +1086,8 @@ impl RpcSol for RpcSolImpl {
|
|||||||
commitment: Option<CommitmentConfig>,
|
commitment: Option<CommitmentConfig>,
|
||||||
) -> Result<RpcResponse<u64>> {
|
) -> Result<RpcResponse<u64>> {
|
||||||
debug!("get_balance rpc request received: {:?}", pubkey_str);
|
debug!("get_balance rpc request received: {:?}", pubkey_str);
|
||||||
let pubkey = verify_pubkey(pubkey_str);
|
let pubkey = verify_pubkey(pubkey_str)?;
|
||||||
meta.get_balance(pubkey, commitment)
|
meta.get_balance(&pubkey, commitment)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_cluster_nodes(&self, meta: Self::Metadata) -> Result<Vec<RpcContactInfo>> {
|
fn get_cluster_nodes(&self, meta: Self::Metadata) -> Result<Vec<RpcContactInfo>> {
|
||||||
|
@ -402,7 +402,7 @@ mod tests {
|
|||||||
10_000,
|
10_000,
|
||||||
rpc_service
|
rpc_service
|
||||||
.request_processor
|
.request_processor
|
||||||
.get_balance(Ok(mint_keypair.pubkey()), None)
|
.get_balance(&mint_keypair.pubkey(), None)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.value
|
.value
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user