get_alternate_chains

This commit is contained in:
hinto.janai 2024-10-14 20:27:27 -04:00
parent 1e0ab56a5e
commit 294df3f163
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
5 changed files with 57 additions and 15 deletions

View file

@ -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(
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L3033-L3064>
async fn get_alternate_chains(
state: CupratedRpcHandler,
mut state: CupratedRpcHandler,
request: GetAlternateChainsRequest,
) -> Result<GetAlternateChainsResponse, Error> {
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,
})
}

View file

@ -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<Vec<ChainInfo>, Error> {
let BlockchainResponse::AltChains(vec) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::AltChains)
.await?
else {
unreachable!();
};
Ok(vec)
}

View file

@ -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!()))
}

View file

@ -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<usize, HardFork>),
AltChains(Vec<ChainInfo>),
//------------------------------------------------------ Writes
/// A generic Ok response to indicate a request was successfully handled.
///

View file

@ -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