mirror of
https://github.com/hinto-janai/cuprate.git
synced 2024-12-23 03:59:37 +00:00
move CalculatePow
This commit is contained in:
parent
afab072816
commit
b0751c417c
5 changed files with 50 additions and 41 deletions
|
@ -57,18 +57,6 @@ pub enum BlockchainManagerRequest {
|
||||||
/// The height of the next block in the chain.
|
/// The height of the next block in the chain.
|
||||||
TargetHeight,
|
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.
|
/// Add auxirilly proof-of-work to a block.
|
||||||
///
|
///
|
||||||
/// From the RPC `add_aux_pow` usecase's documentation:
|
/// From the RPC `add_aux_pow` usecase's documentation:
|
||||||
|
@ -130,9 +118,6 @@ pub enum BlockchainManagerResponse {
|
||||||
/// Response to [`BlockchainManagerRequest::TargetHeight`]
|
/// Response to [`BlockchainManagerRequest::TargetHeight`]
|
||||||
TargetHeight { height: usize },
|
TargetHeight { height: usize },
|
||||||
|
|
||||||
/// Response to [`BlockchainManagerRequest::CalculatePow`]
|
|
||||||
CalculatePow([u8; 32]),
|
|
||||||
|
|
||||||
/// Response to [`BlockchainManagerRequest::AddAuxPow`]
|
/// Response to [`BlockchainManagerRequest::AddAuxPow`]
|
||||||
AddAuxPow(AddAuxPow),
|
AddAuxPow(AddAuxPow),
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
|
|
||||||
use anyhow::{anyhow, Error};
|
use anyhow::{anyhow, Error};
|
||||||
|
use cuprate_helper::cast::u64_to_usize;
|
||||||
|
use monero_serai::block::Block;
|
||||||
use tower::{Service, ServiceExt};
|
use tower::{Service, ServiceExt};
|
||||||
|
|
||||||
use cuprate_consensus_context::{
|
use cuprate_consensus_context::{
|
||||||
|
@ -68,3 +70,30 @@ pub(crate) async fn fee_estimate(
|
||||||
|
|
||||||
Ok(fee)
|
Ok(fee)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// [`BlockChainContextRequest::CalculatePow`]
|
||||||
|
pub(crate) async fn calculate_pow(
|
||||||
|
blockchain_context: &mut BlockChainContextService,
|
||||||
|
hardfork: HardFork,
|
||||||
|
height: u64,
|
||||||
|
block: Box<Block>,
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -144,31 +144,6 @@ pub(crate) async fn target_height(
|
||||||
Ok(usize_to_u64(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`]
|
/// [`BlockchainManagerRequest::AddAuxPow`]
|
||||||
pub(crate) async fn add_aux_pow(
|
pub(crate) async fn add_aux_pow(
|
||||||
blockchain_manager: &mut BlockchainManagerHandle,
|
blockchain_manager: &mut BlockchainManagerHandle,
|
||||||
|
|
|
@ -14,6 +14,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use futures::{channel::oneshot, FutureExt};
|
use futures::{channel::oneshot, FutureExt};
|
||||||
|
use monero_serai::block::Block;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tokio_util::sync::PollSender;
|
use tokio_util::sync::PollSender;
|
||||||
use tower::Service;
|
use tower::Service;
|
||||||
|
@ -263,6 +264,21 @@ pub enum BlockChainContextRequest {
|
||||||
grace_blocks: u64,
|
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<Block>,
|
||||||
|
/// The seed hash for the proof-of-work.
|
||||||
|
seed_hash: [u8; 32],
|
||||||
|
},
|
||||||
|
|
||||||
/// Clear the alt chain context caches.
|
/// Clear the alt chain context caches.
|
||||||
ClearAltCache,
|
ClearAltCache,
|
||||||
|
|
||||||
|
@ -360,6 +376,9 @@ pub enum BlockChainContextResponse {
|
||||||
/// Response to [`BlockChainContextRequest::FeeEstimate`]
|
/// Response to [`BlockChainContextRequest::FeeEstimate`]
|
||||||
FeeEstimate(FeeEstimate),
|
FeeEstimate(FeeEstimate),
|
||||||
|
|
||||||
|
/// Response to [`BlockChainContextRequest::CalculatePow`]
|
||||||
|
CalculatePow([u8; 32]),
|
||||||
|
|
||||||
/// Response to [`BlockChainContextRequest::AltChains`]
|
/// Response to [`BlockChainContextRequest::AltChains`]
|
||||||
///
|
///
|
||||||
/// If the inner [`Vec::is_empty`], there were no alternate chains.
|
/// If the inner [`Vec::is_empty`], there were no alternate chains.
|
||||||
|
|
|
@ -324,7 +324,8 @@ impl<D: Database + Clone + Send + 'static> ContextTask<D> {
|
||||||
}
|
}
|
||||||
BlockChainContextRequest::HardForkInfo(_)
|
BlockChainContextRequest::HardForkInfo(_)
|
||||||
| BlockChainContextRequest::FeeEstimate { .. }
|
| BlockChainContextRequest::FeeEstimate { .. }
|
||||||
| BlockChainContextRequest::AltChains => {
|
| BlockChainContextRequest::AltChains
|
||||||
|
| BlockChainContextRequest::CalculatePow { .. } => {
|
||||||
todo!("finish https://github.com/Cuprate/cuprate/pull/297")
|
todo!("finish https://github.com/Cuprate/cuprate/pull/297")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue