pop_blocks

This commit is contained in:
hinto.janai 2024-09-10 21:00:05 -04:00
parent 2ac2b7e2a5
commit e15b7d42c0
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
6 changed files with 53 additions and 22 deletions

View file

@ -16,8 +16,8 @@ use cuprate_helper::{
map::split_u128_into_low_high_bits, map::split_u128_into_low_high_bits,
}; };
use cuprate_types::{ use cuprate_types::{
blockchain::BlockchainReadRequest, Chain, ExtendedBlockHeader, OutputOnChain, blockchain::{BlockchainReadRequest, BlockchainWriteRequest},
VerifiedBlockInformation, Chain, ExtendedBlockHeader, OutputOnChain, VerifiedBlockInformation,
}; };
use crate::rpc::{CupratedRpcHandlerState, RESTRICTED_BLOCK_COUNT, RESTRICTED_BLOCK_HEADER_RANGE}; use crate::rpc::{CupratedRpcHandlerState, RESTRICTED_BLOCK_COUNT, RESTRICTED_BLOCK_HEADER_RANGE};
@ -27,7 +27,7 @@ pub(super) async fn chain_height(
state: &mut CupratedRpcHandlerState, state: &mut CupratedRpcHandlerState,
) -> Result<(u64, [u8; 32]), Error> { ) -> Result<(u64, [u8; 32]), Error> {
let BlockchainResponse::ChainHeight(height, hash) = state let BlockchainResponse::ChainHeight(height, hash) = state
.blockchain .blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::ChainHeight) .call(BlockchainReadRequest::ChainHeight)
@ -45,7 +45,7 @@ pub(super) async fn block(
height: u64, height: u64,
) -> Result<VerifiedBlockInformation, Error> { ) -> Result<VerifiedBlockInformation, Error> {
let BlockchainResponse::Block(block) = state let BlockchainResponse::Block(block) = state
.blockchain .blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::Block(u64_to_usize(height))) .call(BlockchainReadRequest::Block(u64_to_usize(height)))
@ -63,7 +63,7 @@ pub(super) async fn block_by_hash(
hash: [u8; 32], hash: [u8; 32],
) -> Result<VerifiedBlockInformation, Error> { ) -> Result<VerifiedBlockInformation, Error> {
let BlockchainResponse::BlockByHash(block) = state let BlockchainResponse::BlockByHash(block) = state
.blockchain .blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::BlockByHash(hash)) .call(BlockchainReadRequest::BlockByHash(hash))
@ -81,7 +81,7 @@ pub(super) async fn block_extended_header(
height: u64, height: u64,
) -> Result<ExtendedBlockHeader, Error> { ) -> Result<ExtendedBlockHeader, Error> {
let BlockchainResponse::BlockExtendedHeader(header) = state let BlockchainResponse::BlockExtendedHeader(header) = state
.blockchain .blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::BlockExtendedHeader(u64_to_usize( .call(BlockchainReadRequest::BlockExtendedHeader(u64_to_usize(
@ -101,7 +101,7 @@ pub(super) async fn block_extended_header_by_hash(
hash: [u8; 32], hash: [u8; 32],
) -> Result<ExtendedBlockHeader, Error> { ) -> Result<ExtendedBlockHeader, Error> {
let BlockchainResponse::BlockExtendedHeaderByHash(header) = state let BlockchainResponse::BlockExtendedHeaderByHash(header) = state
.blockchain .blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::BlockExtendedHeaderByHash(hash)) .call(BlockchainReadRequest::BlockExtendedHeaderByHash(hash))
@ -119,7 +119,7 @@ pub(super) async fn block_hash(
height: u64, height: u64,
) -> Result<[u8; 32], Error> { ) -> Result<[u8; 32], Error> {
let BlockchainResponse::BlockHash(hash) = state let BlockchainResponse::BlockHash(hash) = state
.blockchain .blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::BlockHash( .call(BlockchainReadRequest::BlockHash(
@ -140,7 +140,7 @@ pub(super) async fn key_image_spent(
key_image: [u8; 32], key_image: [u8; 32],
) -> Result<bool, Error> { ) -> Result<bool, Error> {
let BlockchainResponse::KeyImageSpent(is_spent) = state let BlockchainResponse::KeyImageSpent(is_spent) = state
.blockchain .blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::KeyImageSpent(key_image)) .call(BlockchainReadRequest::KeyImageSpent(key_image))
@ -158,7 +158,7 @@ pub(super) async fn outputs(
outputs: HashMap<u64, HashSet<u64>>, outputs: HashMap<u64, HashSet<u64>>,
) -> Result<HashMap<u64, HashMap<u64, OutputOnChain>>, Error> { ) -> Result<HashMap<u64, HashMap<u64, OutputOnChain>>, Error> {
let BlockchainResponse::Outputs(outputs) = state let BlockchainResponse::Outputs(outputs) = state
.blockchain .blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::Outputs(outputs)) .call(BlockchainReadRequest::Outputs(outputs))
@ -170,6 +170,28 @@ pub(super) async fn outputs(
Ok(outputs) Ok(outputs)
} }
/// [`BlockchainResponse::PopBlocks`]
pub(super) async fn pop_blocks(
state: &mut CupratedRpcHandlerState,
nblocks: u64,
) -> Result<u64, Error> {
// TODO: we need access to `BlockchainWriteHandle`
// let BlockchainResponse::PopBlocks(height) = state
// .blockchain_write
// .ready()
// .await?
// .call(BlockchainWriteRequest::PopBlocks(nblocks))
// .await?
// else {
// unreachable!();
// };
let height = todo!();
Ok(usize_to_u64(height))
}
// FindBlock([u8; 32]), // FindBlock([u8; 32]),
// FilterUnknownHashes(HashSet<[u8; 32]>), // FilterUnknownHashes(HashSet<[u8; 32]>),
// BlockExtendedHeaderInRange(Range<usize>, Chain), // BlockExtendedHeaderInRange(Range<usize>, Chain),

View file

@ -7,7 +7,7 @@ use futures::{channel::oneshot::channel, future::BoxFuture};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tower::Service; use tower::Service;
use cuprate_blockchain::service::BlockchainReadHandle; use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
use cuprate_helper::asynch::InfallibleOneshotReceiver; use cuprate_helper::asynch::InfallibleOneshotReceiver;
use cuprate_json_rpc::Id; use cuprate_json_rpc::Id;
use cuprate_rpc_interface::RpcHandler; use cuprate_rpc_interface::RpcHandler;
@ -16,7 +16,7 @@ use cuprate_rpc_types::{
json::{JsonRpcRequest, JsonRpcResponse}, json::{JsonRpcRequest, JsonRpcResponse},
other::{OtherRequest, OtherResponse}, other::{OtherRequest, OtherResponse},
}; };
use cuprate_txpool::service::TxpoolReadHandle; use cuprate_txpool::service::{TxpoolReadHandle, TxpoolWriteHandle};
use crate::rpc::{bin, json, other}; use crate::rpc::{bin, json, other};
@ -38,10 +38,14 @@ pub struct CupratedRpcHandlerState {
pub restricted: bool, pub restricted: bool,
/// Read handle to the blockchain database. /// Read handle to the blockchain database.
pub blockchain: BlockchainReadHandle, pub blockchain_read: BlockchainReadHandle,
// /// Write handle to the blockchain database.
// pub blockchain_write: BlockchainWriteHandle,
/// Read handle to the transaction pool database. /// Read handle to the transaction pool database.
pub txpool: TxpoolReadHandle, pub txpool_read: TxpoolReadHandle,
// /// Write handle to the transaction pool database.
// pub txpool_write: TxpoolWriteHandle,
} }
impl CupratedRpcHandler { impl CupratedRpcHandler {

View file

@ -240,7 +240,7 @@ async fn get_block_headers_range(
let mut headers = Vec::with_capacity(block_len); let mut headers = Vec::with_capacity(block_len);
{ {
let ready = state.blockchain.ready().await?; let ready = state.blockchain_read.ready().await?;
for height in request.start_height..=request.end_height { for height in request.start_height..=request.end_height {
let height = u64_to_usize(height); let height = u64_to_usize(height);
let task = tokio::task::spawn(ready.call(BlockchainReadRequest::Block(height))); let task = tokio::task::spawn(ready.call(BlockchainReadRequest::Block(height)));

View file

@ -396,12 +396,14 @@ async fn update(
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L3242-L3252> /// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L3242-L3252>
async fn pop_blocks( async fn pop_blocks(
state: CupratedRpcHandlerState, mut state: CupratedRpcHandlerState,
request: PopBlocksRequest, request: PopBlocksRequest,
) -> Result<PopBlocksResponse, Error> { ) -> Result<PopBlocksResponse, Error> {
let height = blockchain::pop_blocks(&mut state, request.nblocks).await?;
Ok(PopBlocksResponse { Ok(PopBlocksResponse {
base: ResponseBase::ok(), base: ResponseBase::ok(),
..todo!() height,
}) })
} }

View file

@ -77,16 +77,19 @@ fn pop_blocks(env: &ConcreteEnv, nblocks: u64) -> ResponseResult {
let result = || { let result = || {
let mut tables_mut = env_inner.open_tables_mut(&tx_rw)?; let mut tables_mut = env_inner.open_tables_mut(&tx_rw)?;
let mut height = 0;
for _ in 0..nblocks { for _ in 0..nblocks {
ops::block::pop_block(&mut tables_mut)?; (height, _, _) = ops::block::pop_block(&mut tables_mut)?;
} }
Ok(())
Ok(height)
}; };
match result() { match result() {
Ok(()) => { Ok(height) => {
TxRw::commit(tx_rw)?; TxRw::commit(tx_rw)?;
Ok(BlockchainResponse::WriteBlock) Ok(BlockchainResponse::PopBlocks(height))
} }
Err(e) => { Err(e) => {
// INVARIANT: ensure database atomicity by aborting // INVARIANT: ensure database atomicity by aborting

View file

@ -247,7 +247,7 @@ pub enum BlockchainResponse {
WriteBlock, WriteBlock,
/// TODO /// TODO
PopBlocks, PopBlocks(usize),
} }
//---------------------------------------------------------------------------------------------------- Tests //---------------------------------------------------------------------------------------------------- Tests