get_block_count, on_get_block_hash, get_block_header_by_hash todo

This commit is contained in:
hinto.janai 2024-09-06 17:58:40 -04:00
parent ae074d6f57
commit 98eb2f642b
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
2 changed files with 140 additions and 23 deletions

View file

@ -3,8 +3,15 @@
//! Will contain the code to initiate the RPC and a request handler. //! Will contain the code to initiate the RPC and a request handler.
mod bin; mod bin;
mod constants;
mod handler; mod handler;
mod json; mod json;
mod other; mod other;
pub use constants::{
DEFAULT_PAYMENT_CREDITS_PER_HASH, DEFAULT_PAYMENT_DIFFICULTY, MAX_RESTRICTED_FAKE_OUTS_COUNT,
MAX_RESTRICTED_GLOBAL_FAKE_OUTS_COUNT, OUTPUT_HISTOGRAM_RECENT_CUTOFF_RESTRICTION,
RESTRICTED_BLOCK_COUNT, RESTRICTED_BLOCK_HEADER_RANGE, RESTRICTED_SPENT_KEY_IMAGES_COUNT,
RESTRICTED_TRANSACTIONS_COUNT,
};
pub use handler::{CupratedRpcHandler, CupratedRpcHandlerState}; pub use handler::{CupratedRpcHandler, CupratedRpcHandlerState};

View file

@ -1,30 +1,41 @@
use std::sync::Arc; use std::sync::Arc;
use tower::ServiceExt; use futures::StreamExt;
use tower::{Service, ServiceExt};
use cuprate_consensus::BlockchainResponse;
use cuprate_helper::cast::{u64_to_usize, usize_to_u64};
use cuprate_rpc_interface::RpcError; use cuprate_rpc_interface::RpcError;
use cuprate_rpc_types::json::{ use cuprate_rpc_types::{
base::{AccessResponseBase, ResponseBase},
json::{
AddAuxPowRequest, AddAuxPowResponse, BannedRequest, BannedResponse, CalcPowRequest, AddAuxPowRequest, AddAuxPowResponse, BannedRequest, BannedResponse, CalcPowRequest,
CalcPowResponse, FlushCacheRequest, FlushCacheResponse, FlushTransactionPoolRequest, CalcPowResponse, FlushCacheRequest, FlushCacheResponse, FlushTransactionPoolRequest,
FlushTransactionPoolResponse, GenerateBlocksRequest, GenerateBlocksResponse, FlushTransactionPoolResponse, GenerateBlocksRequest, GenerateBlocksResponse,
GetAlternateChainsRequest, GetAlternateChainsResponse, GetBansRequest, GetBansResponse, GetAlternateChainsRequest, GetAlternateChainsResponse, GetBansRequest, GetBansResponse,
GetBlockCountRequest, GetBlockCountResponse, GetBlockHeaderByHashRequest, GetBlockCountRequest, GetBlockCountResponse, GetBlockHeaderByHashRequest,
GetBlockHeaderByHashResponse, GetBlockHeaderByHeightRequest, GetBlockHeaderByHeightResponse, GetBlockHeaderByHashResponse, GetBlockHeaderByHeightRequest,
GetBlockHeadersRangeRequest, GetBlockHeadersRangeResponse, GetBlockRequest, GetBlockResponse, GetBlockHeaderByHeightResponse, GetBlockHeadersRangeRequest, GetBlockHeadersRangeResponse,
GetCoinbaseTxSumRequest, GetCoinbaseTxSumResponse, GetConnectionsRequest, GetBlockRequest, GetBlockResponse, GetCoinbaseTxSumRequest, GetCoinbaseTxSumResponse,
GetConnectionsResponse, GetFeeEstimateRequest, GetFeeEstimateResponse, GetInfoRequest, GetConnectionsRequest, GetConnectionsResponse, GetFeeEstimateRequest,
GetInfoResponse, GetLastBlockHeaderRequest, GetLastBlockHeaderResponse, GetMinerDataRequest, GetFeeEstimateResponse, GetInfoRequest, GetInfoResponse, GetLastBlockHeaderRequest,
GetMinerDataResponse, GetOutputHistogramRequest, GetOutputHistogramResponse, GetLastBlockHeaderResponse, GetMinerDataRequest, GetMinerDataResponse,
GetTransactionPoolBacklogRequest, GetTransactionPoolBacklogResponse, GetTxIdsLooseRequest, GetOutputHistogramRequest, GetOutputHistogramResponse, GetTransactionPoolBacklogRequest,
GetTxIdsLooseResponse, GetVersionRequest, GetVersionResponse, HardForkInfoRequest, GetTransactionPoolBacklogResponse, GetTxIdsLooseRequest, GetTxIdsLooseResponse,
HardForkInfoResponse, JsonRpcRequest, JsonRpcResponse, OnGetBlockHashRequest, GetVersionRequest, GetVersionResponse, HardForkInfoRequest, HardForkInfoResponse,
OnGetBlockHashResponse, PruneBlockchainRequest, PruneBlockchainResponse, RelayTxRequest, JsonRpcRequest, JsonRpcResponse, OnGetBlockHashRequest, OnGetBlockHashResponse,
RelayTxResponse, SetBansRequest, SetBansResponse, SubmitBlockRequest, SubmitBlockResponse, PruneBlockchainRequest, PruneBlockchainResponse, RelayTxRequest, RelayTxResponse,
SyncInfoRequest, SyncInfoResponse, SetBansRequest, SetBansResponse, SubmitBlockRequest, SubmitBlockResponse, SyncInfoRequest,
SyncInfoResponse,
},
misc::BlockHeader,
}; };
use cuprate_types::{blockchain::BlockchainReadRequest, Chain};
use crate::rpc::CupratedRpcHandlerState; use crate::rpc::CupratedRpcHandlerState;
use super::RESTRICTED_BLOCK_COUNT;
/// Map a [`JsonRpcRequest`] to the function that will lead to a [`JsonRpcResponse`]. /// Map a [`JsonRpcRequest`] to the function that will lead to a [`JsonRpcResponse`].
pub(super) async fn map_request( pub(super) async fn map_request(
state: CupratedRpcHandlerState, state: CupratedRpcHandlerState,
@ -87,14 +98,38 @@ async fn get_block_count(
state: CupratedRpcHandlerState, state: CupratedRpcHandlerState,
request: GetBlockCountRequest, request: GetBlockCountRequest,
) -> Result<GetBlockCountResponse, RpcError> { ) -> Result<GetBlockCountResponse, RpcError> {
todo!() let BlockchainResponse::ChainHeight(count, hash) = state
.blockchain
.oneshot(BlockchainReadRequest::ChainHeight)
.await?
else {
unreachable!();
};
Ok(GetBlockCountResponse {
base: ResponseBase::ok(),
count: usize_to_u64(count),
})
} }
async fn on_get_block_hash( async fn on_get_block_hash(
state: CupratedRpcHandlerState, state: CupratedRpcHandlerState,
request: OnGetBlockHashRequest, request: OnGetBlockHashRequest,
) -> Result<OnGetBlockHashResponse, RpcError> { ) -> Result<OnGetBlockHashResponse, RpcError> {
todo!() let BlockchainResponse::BlockHash(hash) = state
.blockchain
.oneshot(BlockchainReadRequest::BlockHash(
u64_to_usize(request.block_height[0]),
Chain::Main,
))
.await?
else {
unreachable!();
};
Ok(OnGetBlockHashResponse {
block_hash: hex::encode(hash),
})
} }
async fn submit_block( async fn submit_block(
@ -119,10 +154,85 @@ async fn get_last_block_header(
} }
async fn get_block_header_by_hash( async fn get_block_header_by_hash(
state: CupratedRpcHandlerState, mut state: CupratedRpcHandlerState,
request: GetBlockHeaderByHashRequest, request: GetBlockHeaderByHashRequest,
) -> Result<GetBlockHeaderByHashResponse, RpcError> { ) -> Result<GetBlockHeaderByHashResponse, RpcError> {
todo!() let restricted = todo!();
if restricted && request.hashes.len() > RESTRICTED_BLOCK_COUNT {
let message = "Too many block headers requested in restricted mode";
return Err(todo!());
}
async fn get(
state: &mut CupratedRpcHandlerState,
hex: String,
fill_pow_hash: bool,
) -> Result<BlockHeader, ()> {
let Ok(bytes) = hex::decode(&hex) else {
let message = format!("Failed to parse hex representation of block hash. Hex = {hex}.");
return Err(todo!());
};
let Ok(hash) = bytes.try_into() else {
let message = "TODO";
return Err(todo!());
};
let ready = state.blockchain.ready().await.expect("TODO");
let BlockchainResponse::BlockExtendedHeaderByHash(header) = ready
.call(BlockchainReadRequest::BlockExtendedHeaderByHash(hash))
.await
.expect("TODO")
else {
unreachable!();
};
let block_header = BlockHeader {
block_size: todo!(),
block_weight: todo!(),
cumulative_difficulty_top64: todo!(),
cumulative_difficulty: todo!(),
depth: todo!(),
difficulty_top64: todo!(),
difficulty: todo!(),
hash: todo!(),
height: todo!(),
long_term_weight: todo!(),
major_version: todo!(),
miner_tx_hash: todo!(),
minor_version: todo!(),
nonce: todo!(),
num_txes: todo!(),
orphan_status: todo!(),
pow_hash: todo!(),
prev_hash: todo!(),
reward: todo!(),
timestamp: todo!(),
wide_cumulative_difficulty: todo!(),
wide_difficulty: todo!(),
};
Ok(block_header)
}
let block_header = get(&mut state, request.hash, request.fill_pow_hash)
.await
.unwrap();
let block_headers = Vec::with_capacity(request.hashes.len());
for hash in request.hashes {
let hash = get(&mut state, hash, request.fill_pow_hash)
.await
.expect("TODO");
block_headers.push(hash);
}
Ok(GetBlockHeaderByHashResponse {
base: AccessResponseBase::ok(),
block_header,
block_headers,
})
} }
async fn get_block_header_by_height( async fn get_block_header_by_height(