mirror of
https://github.com/hinto-janai/cuprate.git
synced 2025-02-28 05:40:17 +00:00
get_alternate_chains
This commit is contained in:
parent
1e0ab56a5e
commit
294df3f163
5 changed files with 57 additions and 15 deletions
binaries/cuprated/src/rpc
storage/blockchain/src/service
types/src
|
@ -41,7 +41,7 @@ use cuprate_rpc_types::{
|
||||||
SetBansRequest, SetBansResponse, SubmitBlockRequest, SubmitBlockResponse, SyncInfoRequest,
|
SetBansRequest, SetBansResponse, SubmitBlockRequest, SubmitBlockResponse, SyncInfoRequest,
|
||||||
SyncInfoResponse,
|
SyncInfoResponse,
|
||||||
},
|
},
|
||||||
misc::{BlockHeader, GetBan, HardforkEntry, HistogramEntry, Status},
|
misc::{BlockHeader, ChainInfo, GetBan, HardforkEntry, HistogramEntry, Status},
|
||||||
CORE_RPC_VERSION,
|
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>
|
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L3033-L3064>
|
||||||
async fn get_alternate_chains(
|
async fn get_alternate_chains(
|
||||||
state: CupratedRpcHandler,
|
mut state: CupratedRpcHandler,
|
||||||
request: GetAlternateChainsRequest,
|
request: GetAlternateChainsRequest,
|
||||||
) -> Result<GetAlternateChainsResponse, Error> {
|
) -> 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 {
|
Ok(GetAlternateChainsResponse {
|
||||||
base: ResponseBase::ok(),
|
base: ResponseBase::ok(),
|
||||||
chains: todo!(),
|
chains,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,12 +9,12 @@ use anyhow::Error;
|
||||||
use monero_serai::block::Block;
|
use monero_serai::block::Block;
|
||||||
use tower::{Service, ServiceExt};
|
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_helper::cast::{u64_to_usize, usize_to_u64};
|
||||||
use cuprate_types::{
|
use cuprate_types::{
|
||||||
blockchain::{BlockchainReadRequest, BlockchainResponse},
|
blockchain::{BlockchainReadRequest, BlockchainResponse},
|
||||||
Chain, CoinbaseTxSum, ExtendedBlockHeader, HardFork, MinerData, OutputHistogramEntry,
|
Chain, ChainInfo, CoinbaseTxSum, ExtendedBlockHeader, HardFork, MinerData,
|
||||||
OutputHistogramInput, OutputOnChain,
|
OutputHistogramEntry, OutputHistogramInput, OutputOnChain,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// [`BlockchainReadRequest::Block`].
|
/// [`BlockchainReadRequest::Block`].
|
||||||
|
@ -359,3 +359,19 @@ pub(crate) async fn hard_forks(
|
||||||
|
|
||||||
Ok(hfs)
|
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::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),
|
R::HardForks => hard_forks(env),
|
||||||
|
R::AltChains => alt_chains(env),
|
||||||
}
|
}
|
||||||
|
|
||||||
/* SOMEDAY: post-request handling, run some code for each request? */
|
/* 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 {
|
fn hard_forks(env: &ConcreteEnv) -> ResponseResult {
|
||||||
Ok(BlockchainResponse::HardForks(todo!()))
|
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::{
|
use crate::{
|
||||||
types::{Chain, ExtendedBlockHeader, OutputOnChain, VerifiedBlockInformation},
|
types::{Chain, ExtendedBlockHeader, OutputOnChain, VerifiedBlockInformation},
|
||||||
AltBlockInformation, ChainId, CoinbaseTxSum, HardFork, OutputHistogramEntry,
|
AltBlockInformation, ChainId, ChainInfo, CoinbaseTxSum, HardFork, OutputHistogramEntry,
|
||||||
OutputHistogramInput,
|
OutputHistogramInput,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -108,9 +108,7 @@ pub enum BlockchainReadRequest {
|
||||||
AltBlocksInChain(ChainId),
|
AltBlocksInChain(ChainId),
|
||||||
|
|
||||||
/// Get a [`Block`] by its height.
|
/// Get a [`Block`] by its height.
|
||||||
Block {
|
Block { height: usize },
|
||||||
height: usize,
|
|
||||||
},
|
|
||||||
|
|
||||||
/// Get a [`Block`] by its hash.
|
/// Get a [`Block`] by its hash.
|
||||||
BlockByHash([u8; 32]),
|
BlockByHash([u8; 32]),
|
||||||
|
@ -130,12 +128,13 @@ 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 {
|
CoinbaseTxSum { height: usize, count: u64 },
|
||||||
height: usize,
|
|
||||||
count: u64,
|
|
||||||
},
|
|
||||||
|
|
||||||
|
/// TODO
|
||||||
HardForks,
|
HardForks,
|
||||||
|
|
||||||
|
/// TODO
|
||||||
|
AltChains,
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- WriteRequest
|
//---------------------------------------------------------------------------------------------------- WriteRequest
|
||||||
|
@ -290,6 +289,8 @@ pub enum BlockchainResponse {
|
||||||
/// - Value = hardfork version
|
/// - Value = hardfork version
|
||||||
HardForks(BTreeMap<usize, HardFork>),
|
HardForks(BTreeMap<usize, HardFork>),
|
||||||
|
|
||||||
|
AltChains(Vec<ChainInfo>),
|
||||||
|
|
||||||
//------------------------------------------------------ Writes
|
//------------------------------------------------------ Writes
|
||||||
/// A generic Ok response to indicate a request was successfully handled.
|
/// A generic Ok response to indicate a request was successfully handled.
|
||||||
///
|
///
|
||||||
|
|
|
@ -240,7 +240,6 @@ pub struct ChainInfo {
|
||||||
pub height: u64,
|
pub height: u64,
|
||||||
pub length: u64,
|
pub length: u64,
|
||||||
pub main_chain_parent_block: [u8; 32],
|
pub main_chain_parent_block: [u8; 32],
|
||||||
pub wide_difficulty: u128,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Tests
|
//---------------------------------------------------------------------------------------------------- Tests
|
||||||
|
|
Loading…
Reference in a new issue