Add getLowestNonpurgedBlock rpc; use blockstore api in getConfirmedBlocks (#9656) (#9664)

automerge
This commit is contained in:
mergify[bot]
2020-04-22 15:13:23 -07:00
committed by GitHub
parent 740c1df045
commit 6c08dc9c9d
3 changed files with 56 additions and 17 deletions

View File

@ -17,9 +17,7 @@ use solana_client::{
rpc_response::*, rpc_response::*,
}; };
use solana_faucet::faucet::request_airdrop_transaction; use solana_faucet::faucet::request_airdrop_transaction;
use solana_ledger::{ use solana_ledger::{bank_forks::BankForks, blockstore::Blockstore};
bank_forks::BankForks, blockstore::Blockstore, rooted_slot_iterator::RootedSlotIterator,
};
use solana_perf::packet::PACKET_DATA_SIZE; use solana_perf::packet::PACKET_DATA_SIZE;
use solana_runtime::bank::Bank; use solana_runtime::bank::Bank;
use solana_sdk::{ use solana_sdk::{
@ -383,18 +381,12 @@ impl JsonRpcRequestProcessor {
if end_slot < start_slot { if end_slot < start_slot {
return Ok(vec![]); return Ok(vec![]);
} }
Ok(self
let start_slot = (start_slot..end_slot).find(|&slot| self.blockstore.is_root(slot)); .blockstore
if let Some(start_slot) = start_slot { .rooted_slot_iterator(start_slot)
let mut slots: Vec<Slot> = RootedSlotIterator::new(start_slot, &self.blockstore) .map_err(|_| Error::internal_error())?
.unwrap() .filter(|&slot| slot <= end_slot)
.map(|(slot, _)| slot) .collect())
.collect();
slots.retain(|&x| x <= end_slot);
Ok(slots)
} else {
Ok(vec![])
}
} }
pub fn get_block_time(&self, slot: Slot) -> Result<Option<UnixTimestamp>> { pub fn get_block_time(&self, slot: Slot) -> Result<Option<UnixTimestamp>> {
@ -547,6 +539,13 @@ impl JsonRpcRequestProcessor {
Ok(vec![]) Ok(vec![])
} }
} }
pub fn get_first_available_block(&self) -> Result<Slot> {
Ok(self
.blockstore
.get_first_available_block()
.unwrap_or_default())
}
} }
fn get_tpu_addr(cluster_info: &ClusterInfo) -> Result<SocketAddr> { fn get_tpu_addr(cluster_info: &ClusterInfo) -> Result<SocketAddr> {
@ -805,6 +804,9 @@ pub trait RpcSol {
start_slot: Slot, start_slot: Slot,
end_slot: Slot, end_slot: Slot,
) -> Result<Vec<String>>; ) -> Result<Vec<String>>;
#[rpc(meta, name = "getFirstAvailableBlock")]
fn get_first_available_block(&self, meta: Self::Metadata) -> Result<Slot>;
} }
pub struct RpcSolImpl; pub struct RpcSolImpl;
@ -1393,6 +1395,13 @@ impl RpcSol for RpcSolImpl {
.collect() .collect()
}) })
} }
fn get_first_available_block(&self, meta: Self::Metadata) -> Result<Slot> {
meta.request_processor
.read()
.unwrap()
.get_first_available_block()
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -27,6 +27,7 @@ To interact with a Solana node inside a JavaScript application, use the [solana-
* [getEpochSchedule](jsonrpc-api.md#getepochschedule) * [getEpochSchedule](jsonrpc-api.md#getepochschedule)
* [getFeeCalculatorForBlockhash](jsonrpc-api.md#getfeecalculatorforblockhash) * [getFeeCalculatorForBlockhash](jsonrpc-api.md#getfeecalculatorforblockhash)
* [getFeeRateGovernor](jsonrpc-api.md#getfeerategovernor) * [getFeeRateGovernor](jsonrpc-api.md#getfeerategovernor)
* [getFirstAvailableBlock](jsonrpc-api.md#getfirstavailableblock)
* [getGenesisHash](jsonrpc-api.md#getgenesishash) * [getGenesisHash](jsonrpc-api.md#getgenesishash)
* [getIdentity](jsonrpc-api.md#getidentity) * [getIdentity](jsonrpc-api.md#getidentity)
* [getInflation](jsonrpc-api.md#getinflation) * [getInflation](jsonrpc-api.md#getinflation)
@ -284,7 +285,7 @@ The result field will be an object with the following fields:
* `<null>` - if specified block is not confirmed * `<null>` - if specified block is not confirmed
* `<object>` - if block is confirmed, an object with the following fields: * `<object>` - if block is confirmed, an object with the following fields:
* `blockhash: <string>` - the blockhash of this block, as base-58 encoded string * `blockhash: <string>` - the blockhash of this block, as base-58 encoded string
* `previousBlockhash: <string>` - the blockhash of this block's parent, as base-58 encoded string * `previousBlockhash: <string>` - the blockhash of this block's parent, as base-58 encoded string; if the parent block is not available due to ledger cleanup, this field will return "11111111111111111111111111111111"
* `parentSlot: <u64>` - the slot index of this block's parent * `parentSlot: <u64>` - the slot index of this block's parent
* `transactions: <array>` - an array of JSON objects containing: * `transactions: <array>` - an array of JSON objects containing:
* `transaction: <object|string>` - [Transaction](#transaction-structure) object, either in JSON format or base-58 encoded binary data, depending on encoding parameter * `transaction: <object|string>` - [Transaction](#transaction-structure) object, either in JSON format or base-58 encoded binary data, depending on encoding parameter
@ -536,6 +537,28 @@ curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1, "m
{"jsonrpc":"2.0","result":{"context":{"slot":54},"value":{"feeRateGovernor":{"burnPercent":50,"maxLamportsPerSignature":100000,"minLamportsPerSignature":5000,"targetLamportsPerSignature":10000,"targetSignaturesPerSlot":20000}}},"id":1} {"jsonrpc":"2.0","result":{"context":{"slot":54},"value":{"feeRateGovernor":{"burnPercent":50,"maxLamportsPerSignature":100000,"minLamportsPerSignature":5000,"targetLamportsPerSignature":10000,"targetSignaturesPerSlot":20000}}},"id":1}
``` ```
### getFirstAvailableBlock
Returns the slot of the lowest confirmed block that has not been purged from the ledger
#### Parameters:
None
#### Results:
* `<u64>` - Slot
#### Example:
```bash
// Request
curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1, "method":"getFirstAvailableBlock"}' http://localhost:8899
// Result
{"jsonrpc":"2.0","result":250000,"id":1}
```
### getGenesisHash ### getGenesisHash
Returns the genesis hash Returns the genesis hash

View File

@ -1501,6 +1501,11 @@ impl Blockstore {
slots slots
} }
pub fn get_first_available_block(&self) -> Result<Slot> {
let mut root_iterator = self.rooted_slot_iterator(0)?;
Ok(root_iterator.next().unwrap_or_default())
}
pub fn get_confirmed_block( pub fn get_confirmed_block(
&self, &self,
slot: Slot, slot: Slot,
@ -1533,7 +1538,9 @@ impl Blockstore {
.iter() .iter()
.cloned() .cloned()
.flat_map(|entry| entry.transactions); .flat_map(|entry| entry.transactions);
let parent_slot_entries = self.get_slot_entries(slot_meta.parent_slot, 0)?; let parent_slot_entries = self
.get_slot_entries(slot_meta.parent_slot, 0)
.unwrap_or_default();
let previous_blockhash = if !parent_slot_entries.is_empty() { let previous_blockhash = if !parent_slot_entries.is_empty() {
get_last_hash(parent_slot_entries.iter()).unwrap() get_last_hash(parent_slot_entries.iter()).unwrap()
} else { } else {