mirror of
https://github.com/Cuprate/cuprate.git
synced 2025-01-11 21:35:22 +00:00
get_block_header_by_height
, use anyhow!()
This commit is contained in:
parent
6c7b5be94e
commit
2d52c26bdc
4 changed files with 72 additions and 32 deletions
|
@ -8,6 +8,7 @@
|
||||||
unused_variables,
|
unused_variables,
|
||||||
clippy::needless_pass_by_value,
|
clippy::needless_pass_by_value,
|
||||||
clippy::unused_async,
|
clippy::unused_async,
|
||||||
|
unreachable_code,
|
||||||
reason = "TODO: remove after v1.0.0"
|
reason = "TODO: remove after v1.0.0"
|
||||||
)]
|
)]
|
||||||
|
|
||||||
|
|
|
@ -23,13 +23,6 @@ use crate::rpc::{bin, json, other};
|
||||||
/// TODO
|
/// TODO
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct CupratedRpcHandler {
|
pub struct CupratedRpcHandler {
|
||||||
/// Should this RPC server be [restricted](RpcHandler::restricted)?
|
|
||||||
//
|
|
||||||
// INVARIANT:
|
|
||||||
// We don't need to include this in `state` and check for
|
|
||||||
// `self.is_restricted()` because `cuprate-rpc-interface` handles that.
|
|
||||||
pub restricted: bool,
|
|
||||||
|
|
||||||
/// State needed for request -> response mapping.
|
/// State needed for request -> response mapping.
|
||||||
pub state: CupratedRpcHandlerState,
|
pub state: CupratedRpcHandlerState,
|
||||||
}
|
}
|
||||||
|
@ -37,6 +30,13 @@ pub struct CupratedRpcHandler {
|
||||||
/// TODO
|
/// TODO
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct CupratedRpcHandlerState {
|
pub struct CupratedRpcHandlerState {
|
||||||
|
/// Should this RPC server be [restricted](RpcHandler::restricted)?
|
||||||
|
//
|
||||||
|
// INVARIANT:
|
||||||
|
// We don't need to include this in `state` and check for
|
||||||
|
// `self.is_restricted()` because `cuprate-rpc-interface` handles that.
|
||||||
|
pub restricted: bool,
|
||||||
|
|
||||||
/// Read handle to the blockchain database.
|
/// Read handle to the blockchain database.
|
||||||
pub blockchain: BlockchainReadHandle,
|
pub blockchain: BlockchainReadHandle,
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ impl CupratedRpcHandler {
|
||||||
|
|
||||||
impl RpcHandler for CupratedRpcHandler {
|
impl RpcHandler for CupratedRpcHandler {
|
||||||
fn restricted(&self) -> bool {
|
fn restricted(&self) -> bool {
|
||||||
self.restricted
|
self.state.restricted
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow::Error;
|
use anyhow::{anyhow, Error};
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use tower::{Service, ServiceExt};
|
use tower::{Service, ServiceExt};
|
||||||
|
|
||||||
|
@ -160,33 +160,32 @@ async fn get_block_header_by_hash(
|
||||||
mut state: CupratedRpcHandlerState,
|
mut state: CupratedRpcHandlerState,
|
||||||
request: GetBlockHeaderByHashRequest,
|
request: GetBlockHeaderByHashRequest,
|
||||||
) -> Result<GetBlockHeaderByHashResponse, Error> {
|
) -> Result<GetBlockHeaderByHashResponse, Error> {
|
||||||
let restricted = todo!();
|
if state.restricted && request.hashes.len() > RESTRICTED_BLOCK_COUNT {
|
||||||
if restricted && request.hashes.len() > RESTRICTED_BLOCK_COUNT {
|
let err = anyhow!("Too many block headers requested in restricted mode");
|
||||||
let message = "Too many block headers requested in restricted mode";
|
return Err(err);
|
||||||
return Err(todo!());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get(
|
async fn get(
|
||||||
state: &mut CupratedRpcHandlerState,
|
state: &mut CupratedRpcHandlerState,
|
||||||
hex: String,
|
hex: String,
|
||||||
fill_pow_hash: bool,
|
fill_pow_hash: bool,
|
||||||
) -> Result<BlockHeader, ()> {
|
) -> Result<BlockHeader, Error> {
|
||||||
let Ok(bytes) = hex::decode(&hex) else {
|
let Ok(bytes) = hex::decode(&hex) else {
|
||||||
let message = format!("Failed to parse hex representation of block hash. Hex = {hex}.");
|
let err = anyhow!("Failed to parse hex representation of block hash. Hex = {hex}.");
|
||||||
return Err(todo!());
|
return Err(err);
|
||||||
};
|
};
|
||||||
|
|
||||||
let Ok(hash) = bytes.try_into() else {
|
let Ok(hash) = bytes.try_into() else {
|
||||||
let message = "TODO";
|
let err = anyhow!("TODO");
|
||||||
return Err(todo!());
|
return Err(err);
|
||||||
};
|
};
|
||||||
|
|
||||||
let ready = state.blockchain.ready().await.expect("TODO");
|
let BlockchainResponse::BlockByHash(block) = state
|
||||||
|
.blockchain
|
||||||
let BlockchainResponse::BlockByHash(block) = ready
|
.ready()
|
||||||
|
.await?
|
||||||
.call(BlockchainReadRequest::BlockByHash(hash))
|
.call(BlockchainReadRequest::BlockByHash(hash))
|
||||||
.await
|
.await?
|
||||||
.expect("TODO")
|
|
||||||
else {
|
else {
|
||||||
unreachable!();
|
unreachable!();
|
||||||
};
|
};
|
||||||
|
@ -196,15 +195,11 @@ async fn get_block_header_by_hash(
|
||||||
Ok(block_header)
|
Ok(block_header)
|
||||||
}
|
}
|
||||||
|
|
||||||
let block_header = get(&mut state, request.hash, request.fill_pow_hash)
|
let block_header = get(&mut state, request.hash, request.fill_pow_hash).await?;
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let block_headers = Vec::with_capacity(request.hashes.len());
|
let mut block_headers = Vec::with_capacity(request.hashes.len());
|
||||||
for hash in request.hashes {
|
for hash in request.hashes {
|
||||||
let hash = get(&mut state, hash, request.fill_pow_hash)
|
let hash = get(&mut state, hash, request.fill_pow_hash).await?;
|
||||||
.await
|
|
||||||
.expect("TODO");
|
|
||||||
block_headers.push(hash);
|
block_headers.push(hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,10 +211,43 @@ async fn get_block_header_by_hash(
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_block_header_by_height(
|
async fn get_block_header_by_height(
|
||||||
state: CupratedRpcHandlerState,
|
mut state: CupratedRpcHandlerState,
|
||||||
request: GetBlockHeaderByHeightRequest,
|
request: GetBlockHeaderByHeightRequest,
|
||||||
) -> Result<GetBlockHeaderByHeightResponse, Error> {
|
) -> Result<GetBlockHeaderByHeightResponse, Error> {
|
||||||
todo!()
|
let BlockchainResponse::ChainHeight(chain_height, _) = state
|
||||||
|
.blockchain
|
||||||
|
.ready()
|
||||||
|
.await?
|
||||||
|
.call(BlockchainReadRequest::ChainHeight)
|
||||||
|
.await?
|
||||||
|
else {
|
||||||
|
unreachable!();
|
||||||
|
};
|
||||||
|
|
||||||
|
let height = chain_height.saturating_sub(1);
|
||||||
|
|
||||||
|
if request.height > usize_to_u64(height) {
|
||||||
|
let err = anyhow!(
|
||||||
|
"Requested block height: {} greater than current top block height: {height}",
|
||||||
|
request.height
|
||||||
|
);
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
let BlockchainResponse::Block(block) = state
|
||||||
|
.blockchain
|
||||||
|
.ready()
|
||||||
|
.await?
|
||||||
|
.call(BlockchainReadRequest::Block(height))
|
||||||
|
.await?
|
||||||
|
else {
|
||||||
|
unreachable!();
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(GetBlockHeaderByHeightResponse {
|
||||||
|
base: AccessResponseBase::ok(),
|
||||||
|
block_header: BlockHeader::from(&block),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_block_headers_range(
|
async fn get_block_headers_range(
|
||||||
|
|
|
@ -3,6 +3,17 @@
|
||||||
#![cfg_attr(any(feature = "proptest"), allow(non_local_definitions))]
|
#![cfg_attr(any(feature = "proptest"), allow(non_local_definitions))]
|
||||||
// Allow some lints when running in debug mode.
|
// Allow some lints when running in debug mode.
|
||||||
#![cfg_attr(debug_assertions, allow(clippy::todo, clippy::multiple_crate_versions))]
|
#![cfg_attr(debug_assertions, allow(clippy::todo, clippy::multiple_crate_versions))]
|
||||||
|
#![allow(
|
||||||
|
unused_imports,
|
||||||
|
unreachable_pub,
|
||||||
|
unused_crate_dependencies,
|
||||||
|
dead_code,
|
||||||
|
unused_variables,
|
||||||
|
clippy::needless_pass_by_value,
|
||||||
|
clippy::unused_async,
|
||||||
|
unreachable_code,
|
||||||
|
reason = "TODO: remove after cuprated RpcHandler impl"
|
||||||
|
)]
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Public API
|
//---------------------------------------------------------------------------------------------------- Public API
|
||||||
// Import private modules, export public types.
|
// Import private modules, export public types.
|
||||||
|
|
Loading…
Reference in a new issue