generate_blocks

This commit is contained in:
hinto.janai 2024-10-15 20:14:15 -04:00
parent d56477cd61
commit 548639b2f6
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
3 changed files with 69 additions and 4 deletions

View file

@ -23,7 +23,6 @@ use crate::rpc::{bin, json, other};
/// TODO: use real type when public.
#[derive(Clone)]
#[expect(clippy::large_enum_variant)]
pub enum BlockchainManagerRequest {
/// Pop blocks off the top of the blockchain.
///
@ -72,9 +71,23 @@ pub enum BlockchainManagerRequest {
/// TODO
AddAuxPow {
/// TODO
blocktemplate_blob: Vec<u8>,
/// TODO
aux_pow: Vec<AuxPow>,
},
/// TODO
GenerateBlocks {
/// TODO
amount_of_blocks: u64,
/// TODO
prev_block: [u8; 32],
/// TODO
starting_nonce: u32,
/// TODO
wallet_address: String,
},
}
/// TODO: use real type when public.
@ -113,6 +126,14 @@ pub enum BlockchainManagerResponse {
/// Response to [`BlockchainManagerRequest::AddAuxPow`]
AddAuxPow(AddAuxPow),
/// Response to [`BlockchainManagerRequest::GenerateBlocks`]
GenerateBlocks {
/// TODO
blocks: Vec<[u8; 32]>,
/// TODO
height: usize,
},
}
/// TODO: use real type when public.

View file

@ -46,7 +46,7 @@ use cuprate_rpc_types::{
},
CORE_RPC_VERSION,
};
use cuprate_types::HardFork;
use cuprate_types::{Chain, HardFork};
use crate::rpc::{
helper,
@ -161,10 +161,29 @@ async fn generate_blocks(
state: CupratedRpcHandler,
request: GenerateBlocksRequest,
) -> Result<GenerateBlocksResponse, Error> {
if todo!("active cuprated chain") != todo!("regtest chain") {
return Err(anyhow!("Regtest required when generating blocks"));
}
// TODO: is this value only used as a local variable in the handler?
// it may not be needed in the request type.
let prev_block = helper::hex_to_hash(request.prev_block)?;
let (blocks, height) = blockchain_manager::generate_blocks(
&mut state.blockchain_manager,
request.amount_of_blocks,
prev_block,
request.starting_nonce,
request.wallet_address,
)
.await?;
let blocks = blocks.into_iter().map(hex::encode).collect();
Ok(GenerateBlocksResponse {
base: ResponseBase::OK,
blocks: todo!(),
height: todo!(),
blocks,
height,
})
}

View file

@ -189,3 +189,28 @@ pub(crate) async fn add_aux_pow(
Ok(response)
}
/// [`BlockchainManagerRequest::GenerateBlocks`]
pub(crate) async fn generate_blocks(
blockchain_manager: &mut BlockchainManagerHandle,
amount_of_blocks: u64,
prev_block: [u8; 32],
starting_nonce: u32,
wallet_address: String,
) -> Result<(Vec<[u8; 32]>, u64), Error> {
let BlockchainManagerResponse::GenerateBlocks { blocks, height } = blockchain_manager
.ready()
.await?
.call(BlockchainManagerRequest::GenerateBlocks {
amount_of_blocks,
prev_block,
starting_nonce,
wallet_address,
})
.await?
else {
unreachable!();
};
Ok((blocks, usize_to_u64(height)))
}