Rpc: Filter blockstore data by cluster-confirmed root (#9873) (#9880)

automerge
This commit is contained in:
mergify[bot]
2020-05-04 22:37:00 -07:00
committed by GitHub
parent 2acf4d874d
commit 52009788ee

View File

@ -31,7 +31,7 @@ use solana_transaction_status::{
};
use solana_vote_program::vote_state::{VoteState, MAX_LOCKOUT_HISTORY};
use std::{
cmp::max,
cmp::{max, min},
collections::{HashMap, HashSet},
net::{SocketAddr, UdpSocket},
str::FromStr,
@ -406,7 +406,14 @@ impl JsonRpcRequestProcessor {
slot: Slot,
encoding: Option<TransactionEncoding>,
) -> Result<Option<ConfirmedBlock>> {
if self.config.enable_rpc_transaction_history {
if self.config.enable_rpc_transaction_history
&& slot
<= self
.block_commitment_cache
.read()
.unwrap()
.largest_confirmed_root()
{
Ok(self.blockstore.get_confirmed_block(slot, encoding).ok())
} else {
Ok(None)
@ -418,11 +425,13 @@ impl JsonRpcRequestProcessor {
start_slot: Slot,
end_slot: Option<Slot>,
) -> Result<Vec<Slot>> {
let end_slot = if let Some(end_slot) = end_slot {
end_slot
} else {
self.bank(None)?.slot()
};
let end_slot = min(
end_slot.unwrap_or(std::u64::MAX),
self.block_commitment_cache
.read()
.unwrap()
.largest_confirmed_root(),
);
if end_slot < start_slot {
return Ok(vec![]);
}
@ -435,6 +444,13 @@ impl JsonRpcRequestProcessor {
}
pub fn get_block_time(&self, slot: Slot) -> Result<Option<UnixTimestamp>> {
if slot
<= self
.block_commitment_cache
.read()
.unwrap()
.largest_confirmed_root()
{
// This calculation currently assumes that bank.slots_per_year will remain unchanged after
// genesis (ie. that this bank's slot_per_year will be applicable to any rooted slot being
// queried). If these values will be variable in the future, those timing parameters will
@ -451,6 +467,9 @@ impl JsonRpcRequestProcessor {
.get_block_time(slot, slot_duration, stakes)
.ok()
.unwrap_or(None))
} else {
Ok(None)
}
}
pub fn get_signature_confirmation_status(
@ -504,6 +523,13 @@ impl JsonRpcRequestProcessor {
self.blockstore
.get_transaction_status(signature)
.map_err(|_| Error::internal_error())?
.filter(|(slot, _status_meta)| {
slot <= &self
.block_commitment_cache
.read()
.unwrap()
.largest_confirmed_root()
})
.map(|(slot, status_meta)| {
let err = status_meta.status.clone().err();
TransactionStatus {
@ -561,7 +587,15 @@ impl JsonRpcRequestProcessor {
Ok(self
.blockstore
.get_confirmed_transaction(signature, encoding)
.unwrap_or(None))
.unwrap_or(None)
.filter(|confirmed_transaction| {
confirmed_transaction.slot
<= self
.block_commitment_cache
.read()
.unwrap()
.largest_confirmed_root()
}))
} else {
Ok(None)
}
@ -574,6 +608,13 @@ impl JsonRpcRequestProcessor {
end_slot: Slot,
) -> Result<Vec<Signature>> {
if self.config.enable_rpc_transaction_history {
let end_slot = min(
end_slot,
self.block_commitment_cache
.read()
.unwrap()
.largest_confirmed_root(),
);
Ok(self
.blockstore
.get_confirmed_signatures_for_address(pubkey, start_slot, end_slot)
@ -2819,11 +2860,21 @@ pub mod tests {
fn test_get_block_time() {
let bob_pubkey = Pubkey::new_rand();
let base_timestamp = 1576183541;
let RpcHandler { io, meta, bank, .. } = start_rpc_handler_with_tx_and_blockstore(
let RpcHandler {
io,
meta,
bank,
block_commitment_cache,
..
} = start_rpc_handler_with_tx_and_blockstore(
&bob_pubkey,
vec![1, 2, 3, 4, 5, 6, 7],
base_timestamp,
);
block_commitment_cache
.write()
.unwrap()
.set_get_largest_confirmed_root(7);
let slot_duration = slot_duration_from_slots_per_year(bank.slots_per_year());