diff --git a/binaries/cuprated/src/rpc/helper.rs b/binaries/cuprated/src/rpc/helper.rs index f3b7164c..48434a45 100644 --- a/binaries/cuprated/src/rpc/helper.rs +++ b/binaries/cuprated/src/rpc/helper.rs @@ -8,11 +8,15 @@ use anyhow::{anyhow, Error}; use monero_serai::block::Block; +use cuprate_consensus::{BlockChainContext, BlockChainContextService}; use cuprate_helper::{cast::usize_to_u64, map::split_u128_into_low_high_bits}; use cuprate_rpc_types::misc::{BlockHeader, KeyImageSpentStatus}; use cuprate_types::ExtendedBlockHeader; -use crate::{rpc::request::blockchain, rpc::CupratedRpcHandler}; +use crate::{ + rpc::request::{blockchain, blockchain_context}, + rpc::CupratedRpcHandler, +}; pub(super) fn into_block_header( height: u64, diff --git a/binaries/cuprated/src/rpc/json.rs b/binaries/cuprated/src/rpc/json.rs index faccde05..656bc6fa 100644 --- a/binaries/cuprated/src/rpc/json.rs +++ b/binaries/cuprated/src/rpc/json.rs @@ -931,20 +931,31 @@ async fn calc_pow( let block = Block::read(&mut block_blob.as_slice())?; let seed_hash = helper::hex_to_hash(request.seed_hash)?; - // let pow_hash = blockchain_manager::calculate_pow( - // &mut state.blockchain_manager, - // hardfork, - // request.height, - // block, - // seed_hash, - // ) - // .await?; + let block_weight = todo!(); - // let hex = hex::encode(pow_hash); + let median_for_block_reward = blockchain_context::context(&mut state.blockchain_context) + .await? + .unchecked_blockchain_context() + .context_to_verify_block + .median_weight_for_block_reward; - let hex = todo!(); + if cuprate_consensus_rules::blocks::check_block_weight(block_weight, median_for_block_reward) + .is_err() + { + return Err(anyhow!("Block blob size is too big, rejecting block")); + } - Ok(CalcPowResponse { pow_hash: hex }) + let pow_hash = blockchain_context::calculate_pow( + &mut state.blockchain_context, + hardfork, + block, + seed_hash, + ) + .await?; + + let pow_hash = hex::encode(pow_hash); + + Ok(CalcPowResponse { pow_hash }) } /// diff --git a/binaries/cuprated/src/rpc/request/blockchain_context.rs b/binaries/cuprated/src/rpc/request/blockchain_context.rs index 73ae3c0c..f0990afb 100644 --- a/binaries/cuprated/src/rpc/request/blockchain_context.rs +++ b/binaries/cuprated/src/rpc/request/blockchain_context.rs @@ -73,17 +73,22 @@ pub(crate) async fn fee_estimate( pub(crate) async fn calculate_pow( blockchain_context: &mut BlockChainContextService, hardfork: HardFork, - height: u64, - block: Box, + block: Block, seed_hash: [u8; 32], ) -> Result<[u8; 32], Error> { + let Some(height) = block.number() else { + return Err(anyhow!("block is missing height")); + }; + + let block = Box::new(block); + let BlockChainContextResponse::CalculatePow(hash) = blockchain_context .ready() .await .map_err(|e| anyhow!(e))? .call(BlockChainContextRequest::CalculatePow { hardfork, - height: u64_to_usize(height), + height, block, seed_hash, })