add CumulativeBlockWeightLimit, submit_block

This commit is contained in:
hinto.janai 2024-09-11 20:29:58 -04:00
parent 6dc85c606f
commit 0d0a544e68
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
6 changed files with 62 additions and 3 deletions

View file

@ -206,6 +206,23 @@ pub(super) async fn find_first_unknown(
Ok(resp.map(|(index, height)| (index, usize_to_u64(height))))
}
/// [`BlockchainResponse::CumulativeBlockWeightLimit`]
pub(super) async fn cumulative_block_weight_limit(
state: &mut CupratedRpcHandlerState,
) -> Result<usize, Error> {
let BlockchainResponse::CumulativeBlockWeightLimit(limit) = state
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::CumulativeBlockWeightLimit)
.await?
else {
unreachable!();
};
Ok(limit)
}
// FindBlock([u8; 32]),
// FilterUnknownHashes(HashSet<[u8; 32]>),
// BlockExtendedHeaderInRange(Range<usize>, Chain),

View file

@ -1,6 +1,7 @@
//! TODO
//!
//! <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L63-L77>
//! - <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L63-L77>
//! - <https://github.com/monero-project/monero/blob/a1dc85c5373a30f14aaf7dcfdd95f5a7375d3623/src/cryptonote_core/cryptonote_core.cpp#L63-L72>
pub const MAX_RESTRICTED_FAKE_OUTS_COUNT: usize = 40;
pub const MAX_RESTRICTED_GLOBAL_FAKE_OUTS_COUNT: usize = 5000;
@ -15,3 +16,5 @@ pub const RESTRICTED_BLOCK_HEADER_RANGE: u64 = 1000;
pub const RESTRICTED_TRANSACTIONS_COUNT: usize = 100;
pub const RESTRICTED_SPENT_KEY_IMAGES_COUNT: usize = 5000;
pub const RESTRICTED_BLOCK_COUNT: usize = 1000;
pub const BLOCK_SIZE_SANITY_LEEWAY: usize = 100;

View file

@ -10,6 +10,7 @@ use std::sync::Arc;
use anyhow::{anyhow, Error};
use cuprate_rpc_types::misc::{BlockHeader, KeyImageSpentStatus};
use futures::StreamExt;
use monero_serai::block::Block;
use tower::{Service, ServiceExt};
use cuprate_consensus::BlockchainResponse;

View file

@ -1,7 +1,9 @@
use core::slice::SlicePattern;
use std::sync::Arc;
use anyhow::{anyhow, Error};
use futures::StreamExt;
use monero_serai::block::Block;
use tower::{Service, ServiceExt};
use cuprate_consensus::BlockchainResponse;
@ -44,6 +46,8 @@ use crate::{
version::CUPRATED_VERSION_IS_RELEASE,
};
use super::constants::BLOCK_SIZE_SANITY_LEEWAY;
/// Map a [`JsonRpcRequest`] to the function that will lead to a [`JsonRpcResponse`].
pub(super) async fn map_request(
state: CupratedRpcHandlerState,
@ -127,12 +131,28 @@ async fn on_get_block_hash(
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2209-L2266>
async fn submit_block(
state: CupratedRpcHandlerState,
mut state: CupratedRpcHandlerState,
request: SubmitBlockRequest,
) -> Result<SubmitBlockResponse, Error> {
let [blob] = request.block_blob;
let limit = blockchain::cumulative_block_weight_limit(&mut state).await?;
if blob.len() > limit + BLOCK_SIZE_SANITY_LEEWAY {
return Err(anyhow!("Block size is too big, rejecting block"));
}
let bytes = hex::decode(blob)?;
let block = Block::read(&mut bytes.as_slice())?;
// <https://github.com/monero-project/monero/blob/master/src/cryptonote_core/cryptonote_core.cpp#L1540>
let block_id = todo!("submit block to DB");
todo!("relay to P2P");
todo!("send to txpool");
Ok(SubmitBlockResponse {
base: ResponseBase::ok(),
block_id: todo!(),
block_id,
})
}

View file

@ -104,6 +104,7 @@ fn map_request(
R::KeyImagesSpent(set) => key_images_spent(env, set),
R::CompactChainHistory => compact_chain_history(env),
R::FindFirstUnknown(block_ids) => find_first_unknown(env, &block_ids),
R::CumulativeBlockWeightLimit => cumulative_block_weight_limit(env),
}
/* SOMEDAY: post-request handling, run some code for each request? */
@ -544,3 +545,14 @@ fn find_first_unknown(env: &ConcreteEnv, block_ids: &[BlockHash]) -> ResponseRes
BlockchainResponse::FindFirstUnknown(Some((idx, last_known_height + 1)))
})
}
/// [`BlockchainReadRequest::CumulativeBlockWeightLimit`]
fn cumulative_block_weight_limit(env: &ConcreteEnv) -> ResponseResult {
let env_inner = env.env_inner();
let tx_ro = env_inner.tx_ro()?;
let table_blocks = env_inner.open_db_ro::<BlockInfos>(&tx_ro)?;
let limit = todo!();
Ok(BlockchainResponse::CumulativeBlockWeightLimit(limit))
}

View file

@ -118,6 +118,9 @@ pub enum BlockchainReadRequest {
/// order, or else the returned response is unspecified and meaningless,
/// as this request performs a binary search.
FindFirstUnknown(Vec<[u8; 32]>),
/// TODO
CumulativeBlockWeightLimit,
}
//---------------------------------------------------------------------------------------------------- WriteRequest
@ -239,6 +242,9 @@ pub enum BlockchainResponse {
/// This will be [`None`] if all blocks were known.
FindFirstUnknown(Option<(usize, usize)>),
/// TODO
CumulativeBlockWeightLimit(usize),
//------------------------------------------------------ Writes
/// Response to [`BlockchainWriteRequest::WriteBlock`].
///