Add getBlockTime rpc api (#7130)
* Add getBlockTime rpc api * Add getBlockTime to docs * Fix duration rounding for common tick/slot durations; add slot duration calculation * Expose slots_per_year * Use genesis values instead of clock defaults to calculate block offset * Add get-block-time cli subcommand * Fix test_rent: decrease magic number usage
This commit is contained in:
@ -11,7 +11,7 @@ use log::*;
|
||||
use serde_json::{json, Value};
|
||||
use solana_sdk::{
|
||||
account::Account,
|
||||
clock::{Slot, DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT},
|
||||
clock::{Slot, UnixTimestamp, DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT},
|
||||
commitment_config::CommitmentConfig,
|
||||
epoch_schedule::EpochSchedule,
|
||||
fee_calculator::FeeCalculator,
|
||||
@ -196,6 +196,32 @@ impl RpcClient {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_block_time(&self, slot: Slot) -> io::Result<UnixTimestamp> {
|
||||
let params = json!(slot);
|
||||
let response = self
|
||||
.client
|
||||
.send(&RpcRequest::GetBlockTime, Some(params), 0, None);
|
||||
|
||||
response
|
||||
.map(|result_json| {
|
||||
if result_json.is_null() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("Block Not Found: slot={}", slot),
|
||||
));
|
||||
}
|
||||
let result = serde_json::from_value(result_json)?;
|
||||
trace!("Response block timestamp {:?} {:?}", slot, result);
|
||||
Ok(result)
|
||||
})
|
||||
.map_err(|err| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("GetBlockTime request failure: {:?}", err),
|
||||
)
|
||||
})?
|
||||
}
|
||||
|
||||
pub fn get_epoch_info(&self) -> io::Result<RpcEpochInfo> {
|
||||
self.get_epoch_info_with_commitment(CommitmentConfig::default())
|
||||
}
|
||||
|
@ -111,6 +111,7 @@ pub enum RpcRequest {
|
||||
ValidatorExit,
|
||||
GetAccountInfo,
|
||||
GetBalance,
|
||||
GetBlockTime,
|
||||
GetClusterNodes,
|
||||
GetEpochInfo,
|
||||
GetEpochSchedule,
|
||||
@ -150,6 +151,7 @@ impl RpcRequest {
|
||||
RpcRequest::ValidatorExit => "validatorExit",
|
||||
RpcRequest::GetAccountInfo => "getAccountInfo",
|
||||
RpcRequest::GetBalance => "getBalance",
|
||||
RpcRequest::GetBlockTime => "getBlockTime",
|
||||
RpcRequest::GetClusterNodes => "getClusterNodes",
|
||||
RpcRequest::GetEpochInfo => "getEpochInfo",
|
||||
RpcRequest::GetEpochSchedule => "getEpochSchedule",
|
||||
|
Reference in New Issue
Block a user