mirror of
https://github.com/hinto-janai/cuprate.git
synced 2025-01-18 16:54:32 +00:00
get_alternate_chains
This commit is contained in:
parent
1e0ab56a5e
commit
294df3f163
5 changed files with 57 additions and 15 deletions
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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!()))
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
///
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue