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

automerge
This commit is contained in:
mergify[bot]
2020-04-22 15:17:51 -07:00
committed by GitHub
parent d8aa107fae
commit 33c51ee75d
3 changed files with 73 additions and 32 deletions

View File

@ -13,9 +13,7 @@ use jsonrpc_core::{Error, Metadata, Result};
use jsonrpc_derive::rpc; use jsonrpc_derive::rpc;
use solana_client::rpc_response::*; use solana_client::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_runtime::bank::Bank; use solana_runtime::bank::Bank;
use solana_sdk::{ use solana_sdk::{
clock::{Slot, UnixTimestamp}, clock::{Slot, UnixTimestamp},
@ -381,18 +379,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>> {
@ -543,6 +535,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: &Arc<RwLock<ClusterInfo>>) -> Result<SocketAddr> { fn get_tpu_addr(cluster_info: &Arc<RwLock<ClusterInfo>>) -> Result<SocketAddr> {
@ -801,6 +800,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;
@ -1389,6 +1391,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)
@ -280,22 +281,24 @@ Returns identity and transaction information about a confirmed block in the ledg
The result field will be an object with the following fields: The result field will be an object with the following fields:
* `blockhash: <string>` - the blockhash of this block, as base-58 encoded string * `<null>` - if specified block is not confirmed
* `previousBlockhash: <string>` - the blockhash of this block's parent, as base-58 encoded string * `<object>` - if block is confirmed, an object with the following fields:
* `parentSlot: <u64>` - the slot index of this block's parent * `blockhash: <string>` - the blockhash of this block, as base-58 encoded string
* `transactions: <array>` - an array of JSON objects containing: * `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"
* `transaction: <object|string>` - [Transaction](#transaction-structure) object, either in JSON format or base-58 encoded binary data, depending on encoding parameter * `parentSlot: <u64>` - the slot index of this block's parent
* `meta: <object>` - transaction status metadata object, containing `null` or: * `transactions: <array>` - an array of JSON objects containing:
* `err: <object | null>` - Error if transaction failed, null if transaction succeeded. [TransactionError definitions](https://github.com/solana-labs/solana/blob/master/sdk/src/transaction.rs#L14) * `transaction: <object|string>` - [Transaction](#transaction-structure) object, either in JSON format or base-58 encoded binary data, depending on encoding parameter
* `fee: <u64>` - fee this transaction was charged, as u64 integer * `meta: <object>` - transaction status metadata object, containing `null` or:
* `preBalances: <array>` - array of u64 account balances from before the transaction was processed * `err: <object | null>` - Error if transaction failed, null if transaction succeeded. [TransactionError definitions](https://github.com/solana-labs/solana/blob/master/sdk/src/transaction.rs#L14)
* `postBalances: <array>` - array of u64 account balances after the transaction was processed * `fee: <u64>` - fee this transaction was charged, as u64 integer
* DEPRECATED: `status: <object>` - Transaction status * `preBalances: <array>` - array of u64 account balances from before the transaction was processed
* `"Ok": <null>` - Transaction was successful * `postBalances: <array>` - array of u64 account balances after the transaction was processed
* `"Err": <ERR>` - Transaction failed with TransactionError * DEPRECATED: `status: <object>` - Transaction status
* `rewards: <array>` - an array of JSON objects containing: * `"Ok": <null>` - Transaction was successful
* `pubkey: <string>` - The public key, as base-58 encoded string, of the account that received the reward * `"Err": <ERR>` - Transaction failed with TransactionError
* `lamports: <i64>`- number of reward lamports credited or debited by the account, as a i64 * `rewards: <array>` - an array of JSON objects containing:
* `pubkey: <string>` - The public key, as base-58 encoded string, of the account that received the reward
* `lamports: <i64>`- number of reward lamports credited or debited by the account, as a i64
#### Example: #### Example:
@ -530,6 +533,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

@ -1467,6 +1467,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,
@ -1499,7 +1504,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, None)?; let parent_slot_entries = self
.get_slot_entries(slot_meta.parent_slot, 0, None)
.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 {