get_version

This commit is contained in:
hinto.janai 2024-10-14 20:13:21 -04:00
parent 1d435cc1f8
commit d7fef15cd6
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
4 changed files with 62 additions and 11 deletions

View file

@ -14,7 +14,10 @@ use cuprate_constants::{
build::RELEASE, build::RELEASE,
rpc::{RESTRICTED_BLOCK_COUNT, RESTRICTED_BLOCK_HEADER_RANGE}, rpc::{RESTRICTED_BLOCK_COUNT, RESTRICTED_BLOCK_HEADER_RANGE},
}; };
use cuprate_helper::{cast::u64_to_usize, map::split_u128_into_low_high_bits}; use cuprate_helper::{
cast::{u64_to_usize, usize_to_u64},
map::split_u128_into_low_high_bits,
};
use cuprate_rpc_interface::RpcHandler; use cuprate_rpc_interface::RpcHandler;
use cuprate_rpc_types::{ use cuprate_rpc_types::{
base::{AccessResponseBase, ResponseBase}, base::{AccessResponseBase, ResponseBase},
@ -38,7 +41,7 @@ use cuprate_rpc_types::{
SetBansRequest, SetBansResponse, SubmitBlockRequest, SubmitBlockResponse, SyncInfoRequest, SetBansRequest, SetBansResponse, SubmitBlockRequest, SubmitBlockResponse, SyncInfoRequest,
SyncInfoResponse, SyncInfoResponse,
}, },
misc::{BlockHeader, GetBan, HistogramEntry, Status}, misc::{BlockHeader, GetBan, HardforkEntry, HistogramEntry, Status},
CORE_RPC_VERSION, CORE_RPC_VERSION,
}; };
@ -573,13 +576,25 @@ async fn get_version(
mut state: CupratedRpcHandler, mut state: CupratedRpcHandler,
request: GetVersionRequest, request: GetVersionRequest,
) -> Result<GetVersionResponse, Error> { ) -> Result<GetVersionResponse, Error> {
let current_height = helper::top_height(&mut state).await?.0;
let target_height = blockchain_manager::target_height(&mut state.blockchain_manager).await?;
let hard_forks = blockchain::hard_forks(&mut state.blockchain_read)
.await?
.into_iter()
.map(|(height, hf)| HardforkEntry {
height: usize_to_u64(height),
hf_version: hf.as_u8(),
})
.collect();
Ok(GetVersionResponse { Ok(GetVersionResponse {
base: ResponseBase::ok(), base: ResponseBase::ok(),
version: CORE_RPC_VERSION, version: CORE_RPC_VERSION,
release: RELEASE, release: RELEASE,
current_height: helper::top_height(&mut state).await?.0, current_height,
target_height: todo!(), target_height,
hard_forks: todo!(), hard_forks,
}) })
} }

View file

@ -1,7 +1,7 @@
//! Functions for [`BlockchainReadRequest`]. //! Functions for [`BlockchainReadRequest`].
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{BTreeMap, HashMap, HashSet},
ops::Range, ops::Range,
}; };
@ -13,7 +13,7 @@ use cuprate_blockchain::service::BlockchainReadHandle;
use cuprate_helper::cast::{u64_to_usize, usize_to_u64}; use cuprate_helper::cast::{u64_to_usize, usize_to_u64};
use cuprate_types::{ use cuprate_types::{
blockchain::{BlockchainReadRequest, BlockchainResponse}, blockchain::{BlockchainReadRequest, BlockchainResponse},
Chain, CoinbaseTxSum, ExtendedBlockHeader, MinerData, OutputHistogramEntry, Chain, CoinbaseTxSum, ExtendedBlockHeader, HardFork, MinerData, OutputHistogramEntry,
OutputHistogramInput, OutputOnChain, OutputHistogramInput, OutputOnChain,
}; };
@ -343,3 +343,19 @@ pub(crate) async fn coinbase_tx_sum(
Ok(sum) Ok(sum)
} }
/// [`BlockchainReadRequest::HardForks`]
pub(crate) async fn hard_forks(
blockchain_read: &mut BlockchainReadHandle,
) -> Result<BTreeMap<usize, HardFork>, Error> {
let BlockchainResponse::HardForks(hfs) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::HardForks)
.await?
else {
unreachable!();
};
Ok(hfs)
}

View file

@ -121,6 +121,7 @@ fn map_request(
R::DatabaseSize => database_size(env), R::DatabaseSize => database_size(env),
R::OutputHistogram(input) => output_histogram(env, input), R::OutputHistogram(input) => output_histogram(env, input),
R::CoinbaseTxSum { height, count } => coinbase_tx_sum(env, height, count), R::CoinbaseTxSum { height, count } => coinbase_tx_sum(env, height, count),
R::HardForks => hard_forks(env),
} }
/* SOMEDAY: post-request handling, run some code for each request? */ /* SOMEDAY: post-request handling, run some code for each request? */
@ -648,3 +649,8 @@ fn output_histogram(env: &ConcreteEnv, input: OutputHistogramInput) -> ResponseR
fn coinbase_tx_sum(env: &ConcreteEnv, height: usize, count: u64) -> ResponseResult { fn coinbase_tx_sum(env: &ConcreteEnv, height: usize, count: u64) -> ResponseResult {
Ok(BlockchainResponse::CoinbaseTxSum(todo!())) Ok(BlockchainResponse::CoinbaseTxSum(todo!()))
} }
/// [`BlockchainReadRequest::HardForks`]
fn hard_forks(env: &ConcreteEnv) -> ResponseResult {
Ok(BlockchainResponse::HardForks(todo!()))
}

View file

@ -4,7 +4,7 @@
//! responses are also tested in Cuprate's blockchain database crate. //! responses are also tested in Cuprate's blockchain database crate.
//---------------------------------------------------------------------------------------------------- Import //---------------------------------------------------------------------------------------------------- Import
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{BTreeMap, HashMap, HashSet},
ops::Range, ops::Range,
}; };
@ -12,7 +12,8 @@ use monero_serai::block::Block;
use crate::{ use crate::{
types::{Chain, ExtendedBlockHeader, OutputOnChain, VerifiedBlockInformation}, types::{Chain, ExtendedBlockHeader, OutputOnChain, VerifiedBlockInformation},
AltBlockInformation, ChainId, CoinbaseTxSum, OutputHistogramEntry, OutputHistogramInput, AltBlockInformation, ChainId, CoinbaseTxSum, HardFork, OutputHistogramEntry,
OutputHistogramInput,
}; };
//---------------------------------------------------------------------------------------------------- ReadRequest //---------------------------------------------------------------------------------------------------- ReadRequest
@ -107,7 +108,9 @@ pub enum BlockchainReadRequest {
AltBlocksInChain(ChainId), AltBlocksInChain(ChainId),
/// Get a [`Block`] by its height. /// Get a [`Block`] by its height.
Block { height: usize }, Block {
height: usize,
},
/// Get a [`Block`] by its hash. /// Get a [`Block`] by its hash.
BlockByHash([u8; 32]), BlockByHash([u8; 32]),
@ -127,7 +130,12 @@ pub enum BlockchainReadRequest {
/// `N` last blocks starting at particular height. /// `N` last blocks starting at particular height.
/// ///
/// TODO: document fields after impl. /// TODO: document fields after impl.
CoinbaseTxSum { height: usize, count: u64 }, CoinbaseTxSum {
height: usize,
count: u64,
},
HardForks,
} }
//---------------------------------------------------------------------------------------------------- WriteRequest //---------------------------------------------------------------------------------------------------- WriteRequest
@ -276,6 +284,12 @@ pub enum BlockchainResponse {
/// Response to [`BlockchainReadRequest::CoinbaseTxSum`]. /// Response to [`BlockchainReadRequest::CoinbaseTxSum`].
CoinbaseTxSum(CoinbaseTxSum), CoinbaseTxSum(CoinbaseTxSum),
/// Response to [`BlockchainReadRequest::HardForks`].
///
/// - Key = height at which the hardfork activated
/// - Value = hardfork version
HardForks(BTreeMap<usize, HardFork>),
//------------------------------------------------------ Writes //------------------------------------------------------ Writes
/// A generic Ok response to indicate a request was successfully handled. /// A generic Ok response to indicate a request was successfully handled.
/// ///