From d7fef15cd696af51c1c1f77b713907a68f4b3278 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Mon, 14 Oct 2024 20:13:21 -0400 Subject: [PATCH] get_version --- binaries/cuprated/src/rpc/json.rs | 25 +++++++++++++++---- .../cuprated/src/rpc/request/blockchain.rs | 20 +++++++++++++-- storage/blockchain/src/service/read.rs | 6 +++++ types/src/blockchain.rs | 22 +++++++++++++--- 4 files changed, 62 insertions(+), 11 deletions(-) diff --git a/binaries/cuprated/src/rpc/json.rs b/binaries/cuprated/src/rpc/json.rs index c97fa932..5bed13c7 100644 --- a/binaries/cuprated/src/rpc/json.rs +++ b/binaries/cuprated/src/rpc/json.rs @@ -14,7 +14,10 @@ use cuprate_constants::{ build::RELEASE, 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_types::{ base::{AccessResponseBase, ResponseBase}, @@ -38,7 +41,7 @@ use cuprate_rpc_types::{ SetBansRequest, SetBansResponse, SubmitBlockRequest, SubmitBlockResponse, SyncInfoRequest, SyncInfoResponse, }, - misc::{BlockHeader, GetBan, HistogramEntry, Status}, + misc::{BlockHeader, GetBan, HardforkEntry, HistogramEntry, Status}, CORE_RPC_VERSION, }; @@ -573,13 +576,25 @@ async fn get_version( mut state: CupratedRpcHandler, request: GetVersionRequest, ) -> Result { + 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 { base: ResponseBase::ok(), version: CORE_RPC_VERSION, release: RELEASE, - current_height: helper::top_height(&mut state).await?.0, - target_height: todo!(), - hard_forks: todo!(), + current_height, + target_height, + hard_forks, }) } diff --git a/binaries/cuprated/src/rpc/request/blockchain.rs b/binaries/cuprated/src/rpc/request/blockchain.rs index 7705320f..8f71827f 100644 --- a/binaries/cuprated/src/rpc/request/blockchain.rs +++ b/binaries/cuprated/src/rpc/request/blockchain.rs @@ -1,7 +1,7 @@ //! Functions for [`BlockchainReadRequest`]. use std::{ - collections::{HashMap, HashSet}, + collections::{BTreeMap, HashMap, HashSet}, ops::Range, }; @@ -13,7 +13,7 @@ use cuprate_blockchain::service::BlockchainReadHandle; use cuprate_helper::cast::{u64_to_usize, usize_to_u64}; use cuprate_types::{ blockchain::{BlockchainReadRequest, BlockchainResponse}, - Chain, CoinbaseTxSum, ExtendedBlockHeader, MinerData, OutputHistogramEntry, + Chain, CoinbaseTxSum, ExtendedBlockHeader, HardFork, MinerData, OutputHistogramEntry, OutputHistogramInput, OutputOnChain, }; @@ -343,3 +343,19 @@ pub(crate) async fn coinbase_tx_sum( Ok(sum) } + +/// [`BlockchainReadRequest::HardForks`] +pub(crate) async fn hard_forks( + blockchain_read: &mut BlockchainReadHandle, +) -> Result, Error> { + let BlockchainResponse::HardForks(hfs) = blockchain_read + .ready() + .await? + .call(BlockchainReadRequest::HardForks) + .await? + else { + unreachable!(); + }; + + Ok(hfs) +} diff --git a/storage/blockchain/src/service/read.rs b/storage/blockchain/src/service/read.rs index a3b82bdb..95d21b3c 100644 --- a/storage/blockchain/src/service/read.rs +++ b/storage/blockchain/src/service/read.rs @@ -121,6 +121,7 @@ fn map_request( R::DatabaseSize => database_size(env), R::OutputHistogram(input) => output_histogram(env, input), 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? */ @@ -648,3 +649,8 @@ fn output_histogram(env: &ConcreteEnv, input: OutputHistogramInput) -> ResponseR fn coinbase_tx_sum(env: &ConcreteEnv, height: usize, count: u64) -> ResponseResult { Ok(BlockchainResponse::CoinbaseTxSum(todo!())) } + +/// [`BlockchainReadRequest::HardForks`] +fn hard_forks(env: &ConcreteEnv) -> ResponseResult { + Ok(BlockchainResponse::HardForks(todo!())) +} diff --git a/types/src/blockchain.rs b/types/src/blockchain.rs index b7436f0a..441fb149 100644 --- a/types/src/blockchain.rs +++ b/types/src/blockchain.rs @@ -4,7 +4,7 @@ //! responses are also tested in Cuprate's blockchain database crate. //---------------------------------------------------------------------------------------------------- Import use std::{ - collections::{HashMap, HashSet}, + collections::{BTreeMap, HashMap, HashSet}, ops::Range, }; @@ -12,7 +12,8 @@ use monero_serai::block::Block; use crate::{ types::{Chain, ExtendedBlockHeader, OutputOnChain, VerifiedBlockInformation}, - AltBlockInformation, ChainId, CoinbaseTxSum, OutputHistogramEntry, OutputHistogramInput, + AltBlockInformation, ChainId, CoinbaseTxSum, HardFork, OutputHistogramEntry, + OutputHistogramInput, }; //---------------------------------------------------------------------------------------------------- ReadRequest @@ -107,7 +108,9 @@ pub enum BlockchainReadRequest { AltBlocksInChain(ChainId), /// Get a [`Block`] by its height. - Block { height: usize }, + Block { + height: usize, + }, /// Get a [`Block`] by its hash. BlockByHash([u8; 32]), @@ -127,7 +130,12 @@ pub enum BlockchainReadRequest { /// `N` last blocks starting at particular height. /// /// TODO: document fields after impl. - CoinbaseTxSum { height: usize, count: u64 }, + CoinbaseTxSum { + height: usize, + count: u64, + }, + + HardForks, } //---------------------------------------------------------------------------------------------------- WriteRequest @@ -276,6 +284,12 @@ pub enum BlockchainResponse { /// Response to [`BlockchainReadRequest::CoinbaseTxSum`]. CoinbaseTxSum(CoinbaseTxSum), + /// Response to [`BlockchainReadRequest::HardForks`]. + /// + /// - Key = height at which the hardfork activated + /// - Value = hardfork version + HardForks(BTreeMap), + //------------------------------------------------------ Writes /// A generic Ok response to indicate a request was successfully handled. ///