From 294df3f16373dfcf4c14a6ef8d873ce45fc800fe Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Mon, 14 Oct 2024 20:27:27 -0400 Subject: [PATCH] get_alternate_chains --- binaries/cuprated/src/rpc/json.rs | 26 ++++++++++++++++--- .../cuprated/src/rpc/request/blockchain.rs | 22 +++++++++++++--- storage/blockchain/src/service/read.rs | 6 +++++ types/src/blockchain.rs | 17 ++++++------ types/src/types.rs | 1 - 5 files changed, 57 insertions(+), 15 deletions(-) diff --git a/binaries/cuprated/src/rpc/json.rs b/binaries/cuprated/src/rpc/json.rs index 3bc5bed..989a7d6 100644 --- a/binaries/cuprated/src/rpc/json.rs +++ b/binaries/cuprated/src/rpc/json.rs @@ -41,7 +41,7 @@ use cuprate_rpc_types::{ SetBansRequest, SetBansResponse, SubmitBlockRequest, SubmitBlockResponse, SyncInfoRequest, SyncInfoResponse, }, - misc::{BlockHeader, GetBan, HardforkEntry, HistogramEntry, Status}, + misc::{BlockHeader, ChainInfo, GetBan, HardforkEntry, HistogramEntry, Status}, CORE_RPC_VERSION, }; @@ -617,12 +617,32 @@ async fn get_fee_estimate( /// async fn get_alternate_chains( - state: CupratedRpcHandler, + mut state: CupratedRpcHandler, request: GetAlternateChainsRequest, ) -> Result { + let chains = blockchain::alt_chains(&mut state.blockchain_read) + .await? + .into_iter() + .map(|info| { + let block_hashes = info.block_hashes.into_iter().map(hex::encode).collect(); + let (difficulty, difficulty_top64) = split_u128_into_low_high_bits(info.difficulty); + + ChainInfo { + block_hash: hex::encode(info.block_hash), + block_hashes, + difficulty, + difficulty_top64, + height: info.height, + length: info.length, + main_chain_parent_block: hex::encode(info.main_chain_parent_block), + wide_difficulty: hex::encode(u128::to_ne_bytes(info.difficulty)), + } + }) + .collect(); + Ok(GetAlternateChainsResponse { base: ResponseBase::ok(), - chains: todo!(), + chains, }) } diff --git a/binaries/cuprated/src/rpc/request/blockchain.rs b/binaries/cuprated/src/rpc/request/blockchain.rs index 8f71827..4065423 100644 --- a/binaries/cuprated/src/rpc/request/blockchain.rs +++ b/binaries/cuprated/src/rpc/request/blockchain.rs @@ -9,12 +9,12 @@ use anyhow::Error; use monero_serai::block::Block; use tower::{Service, ServiceExt}; -use cuprate_blockchain::service::BlockchainReadHandle; +use cuprate_blockchain::{service::BlockchainReadHandle, types::AltChainInfo}; use cuprate_helper::cast::{u64_to_usize, usize_to_u64}; use cuprate_types::{ blockchain::{BlockchainReadRequest, BlockchainResponse}, - Chain, CoinbaseTxSum, ExtendedBlockHeader, HardFork, MinerData, OutputHistogramEntry, - OutputHistogramInput, OutputOnChain, + Chain, ChainInfo, CoinbaseTxSum, ExtendedBlockHeader, HardFork, MinerData, + OutputHistogramEntry, OutputHistogramInput, OutputOnChain, }; /// [`BlockchainReadRequest::Block`]. @@ -359,3 +359,19 @@ pub(crate) async fn hard_forks( Ok(hfs) } + +/// [`BlockchainReadRequest::AltChains`] +pub(crate) async fn alt_chains( + blockchain_read: &mut BlockchainReadHandle, +) -> Result, Error> { + let BlockchainResponse::AltChains(vec) = blockchain_read + .ready() + .await? + .call(BlockchainReadRequest::AltChains) + .await? + else { + unreachable!(); + }; + + Ok(vec) +} diff --git a/storage/blockchain/src/service/read.rs b/storage/blockchain/src/service/read.rs index 95d21b3..c04a6f2 100644 --- a/storage/blockchain/src/service/read.rs +++ b/storage/blockchain/src/service/read.rs @@ -122,6 +122,7 @@ fn map_request( R::OutputHistogram(input) => output_histogram(env, input), R::CoinbaseTxSum { height, count } => coinbase_tx_sum(env, height, count), R::HardForks => hard_forks(env), + R::AltChains => alt_chains(env), } /* SOMEDAY: post-request handling, run some code for each request? */ @@ -654,3 +655,8 @@ fn coinbase_tx_sum(env: &ConcreteEnv, height: usize, count: u64) -> ResponseResu fn hard_forks(env: &ConcreteEnv) -> ResponseResult { Ok(BlockchainResponse::HardForks(todo!())) } + +/// [`BlockchainReadRequest::AltChains`] +fn alt_chains(env: &ConcreteEnv) -> ResponseResult { + Ok(BlockchainResponse::AltChains(todo!())) +} diff --git a/types/src/blockchain.rs b/types/src/blockchain.rs index 441fb14..e53619b 100644 --- a/types/src/blockchain.rs +++ b/types/src/blockchain.rs @@ -12,7 +12,7 @@ use monero_serai::block::Block; use crate::{ types::{Chain, ExtendedBlockHeader, OutputOnChain, VerifiedBlockInformation}, - AltBlockInformation, ChainId, CoinbaseTxSum, HardFork, OutputHistogramEntry, + AltBlockInformation, ChainId, ChainInfo, CoinbaseTxSum, HardFork, OutputHistogramEntry, OutputHistogramInput, }; @@ -108,9 +108,7 @@ 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]), @@ -130,12 +128,13 @@ 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 }, + /// TODO HardForks, + + /// TODO + AltChains, } //---------------------------------------------------------------------------------------------------- WriteRequest @@ -290,6 +289,8 @@ pub enum BlockchainResponse { /// - Value = hardfork version HardForks(BTreeMap), + AltChains(Vec), + //------------------------------------------------------ Writes /// A generic Ok response to indicate a request was successfully handled. /// diff --git a/types/src/types.rs b/types/src/types.rs index 669ec5d..a683375 100644 --- a/types/src/types.rs +++ b/types/src/types.rs @@ -240,7 +240,6 @@ pub struct ChainInfo { pub height: u64, pub length: u64, pub main_chain_parent_block: [u8; 32], - pub wide_difficulty: u128, } //---------------------------------------------------------------------------------------------------- Tests