From b0751c417c444c40ac33bf83d45640fb957378fd Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Wed, 23 Oct 2024 17:34:00 -0400 Subject: [PATCH] move `CalculatePow` --- binaries/cuprated/src/rpc/handler.rs | 15 ---------- .../src/rpc/request/blockchain_context.rs | 29 +++++++++++++++++++ .../src/rpc/request/blockchain_manager.rs | 25 ---------------- consensus/context/src/lib.rs | 19 ++++++++++++ consensus/context/src/task.rs | 3 +- 5 files changed, 50 insertions(+), 41 deletions(-) diff --git a/binaries/cuprated/src/rpc/handler.rs b/binaries/cuprated/src/rpc/handler.rs index eb9ffe7..5d49947 100644 --- a/binaries/cuprated/src/rpc/handler.rs +++ b/binaries/cuprated/src/rpc/handler.rs @@ -57,18 +57,6 @@ pub enum BlockchainManagerRequest { /// The height of the next block in the chain. TargetHeight, - /// Calculate proof-of-work for this block. - CalculatePow { - /// The hardfork of the protocol at this block height. - hardfork: HardFork, - /// The height of the block. - height: usize, - /// The block data. - block: Block, - /// The seed hash for the proof-of-work. - seed_hash: [u8; 32], - }, - /// Add auxirilly proof-of-work to a block. /// /// From the RPC `add_aux_pow` usecase's documentation: @@ -130,9 +118,6 @@ pub enum BlockchainManagerResponse { /// Response to [`BlockchainManagerRequest::TargetHeight`] TargetHeight { height: usize }, - /// Response to [`BlockchainManagerRequest::CalculatePow`] - CalculatePow([u8; 32]), - /// Response to [`BlockchainManagerRequest::AddAuxPow`] AddAuxPow(AddAuxPow), diff --git a/binaries/cuprated/src/rpc/request/blockchain_context.rs b/binaries/cuprated/src/rpc/request/blockchain_context.rs index 56052cf..14738a7 100644 --- a/binaries/cuprated/src/rpc/request/blockchain_context.rs +++ b/binaries/cuprated/src/rpc/request/blockchain_context.rs @@ -3,6 +3,8 @@ use std::convert::Infallible; use anyhow::{anyhow, Error}; +use cuprate_helper::cast::u64_to_usize; +use monero_serai::block::Block; use tower::{Service, ServiceExt}; use cuprate_consensus_context::{ @@ -68,3 +70,30 @@ pub(crate) async fn fee_estimate( Ok(fee) } + +/// [`BlockChainContextRequest::CalculatePow`] +pub(crate) async fn calculate_pow( + blockchain_context: &mut BlockChainContextService, + hardfork: HardFork, + height: u64, + block: Box, + seed_hash: [u8; 32], +) -> Result<[u8; 32], Error> { + let BlockChainContextResponse::CalculatePow(hash) = blockchain_context + .ready() + .await + .map_err(|e| anyhow!(e))? + .call(BlockChainContextRequest::CalculatePow { + hardfork, + height: u64_to_usize(height), + block, + seed_hash, + }) + .await + .map_err(|e| anyhow!(e))? + else { + unreachable!(); + }; + + Ok(hash) +} diff --git a/binaries/cuprated/src/rpc/request/blockchain_manager.rs b/binaries/cuprated/src/rpc/request/blockchain_manager.rs index a2c5e6b..4ed0347 100644 --- a/binaries/cuprated/src/rpc/request/blockchain_manager.rs +++ b/binaries/cuprated/src/rpc/request/blockchain_manager.rs @@ -144,31 +144,6 @@ pub(crate) async fn target_height( Ok(usize_to_u64(height)) } -/// [`BlockchainManagerRequest::CalculatePow`] -pub(crate) async fn calculate_pow( - blockchain_manager: &mut BlockchainManagerHandle, - hardfork: HardFork, - height: u64, - block: Block, - seed_hash: [u8; 32], -) -> Result<[u8; 32], Error> { - let BlockchainManagerResponse::CalculatePow(hash) = blockchain_manager - .ready() - .await? - .call(BlockchainManagerRequest::CalculatePow { - hardfork, - height: u64_to_usize(height), - block, - seed_hash, - }) - .await? - else { - unreachable!(); - }; - - Ok(hash) -} - /// [`BlockchainManagerRequest::AddAuxPow`] pub(crate) async fn add_aux_pow( blockchain_manager: &mut BlockchainManagerHandle, diff --git a/consensus/context/src/lib.rs b/consensus/context/src/lib.rs index 82e601d..31641c2 100644 --- a/consensus/context/src/lib.rs +++ b/consensus/context/src/lib.rs @@ -14,6 +14,7 @@ use std::{ }; use futures::{channel::oneshot, FutureExt}; +use monero_serai::block::Block; use tokio::sync::mpsc; use tokio_util::sync::PollSender; use tower::Service; @@ -263,6 +264,21 @@ pub enum BlockChainContextRequest { grace_blocks: u64, }, + /// Calculate proof-of-work for this block. + CalculatePow { + /// The hardfork of the protocol at this block height. + hardfork: HardFork, + /// The height of the block. + height: usize, + /// The block data. + /// + /// This is boxed because [`Block`] causes this enum to be 1200 bytes, + /// where the 2nd variant is only 96 bytes. + block: Box, + /// The seed hash for the proof-of-work. + seed_hash: [u8; 32], + }, + /// Clear the alt chain context caches. ClearAltCache, @@ -360,6 +376,9 @@ pub enum BlockChainContextResponse { /// Response to [`BlockChainContextRequest::FeeEstimate`] FeeEstimate(FeeEstimate), + /// Response to [`BlockChainContextRequest::CalculatePow`] + CalculatePow([u8; 32]), + /// Response to [`BlockChainContextRequest::AltChains`] /// /// If the inner [`Vec::is_empty`], there were no alternate chains. diff --git a/consensus/context/src/task.rs b/consensus/context/src/task.rs index 65cfea9..b075995 100644 --- a/consensus/context/src/task.rs +++ b/consensus/context/src/task.rs @@ -324,7 +324,8 @@ impl ContextTask { } BlockChainContextRequest::HardForkInfo(_) | BlockChainContextRequest::FeeEstimate { .. } - | BlockChainContextRequest::AltChains => { + | BlockChainContextRequest::AltChains + | BlockChainContextRequest::CalculatePow { .. } => { todo!("finish https://github.com/Cuprate/cuprate/pull/297") } })