add BlockchainResponse::PopBlocks

This commit is contained in:
hinto.janai 2024-09-10 20:53:19 -04:00
parent 3b36c10231
commit 2ac2b7e2a5
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
3 changed files with 43 additions and 5 deletions

View file

@ -84,7 +84,7 @@ async fn test_template(
let request = BlockchainWriteRequest::WriteBlock(block);
let response_channel = writer.call(request);
let response = response_channel.await.unwrap();
assert_eq!(response, BlockchainResponse::WriteBlockOk);
assert_eq!(response, BlockchainResponse::WriteBlock);
}
//----------------------------------------------------------------------- Reset the transaction

View file

@ -11,6 +11,7 @@ use cuprate_types::{
};
use crate::{
ops,
service::types::{BlockchainWriteHandle, ResponseResult},
tables::OpenTables,
};
@ -29,6 +30,7 @@ fn handle_blockchain_request(
) -> Result<BlockchainResponse, RuntimeError> {
match req {
BlockchainWriteRequest::WriteBlock(block) => write_block(env, block),
BlockchainWriteRequest::PopBlocks(nblocks) => pop_blocks(env, *nblocks),
}
}
@ -49,13 +51,42 @@ fn write_block(env: &ConcreteEnv, block: &VerifiedBlockInformation) -> ResponseR
let result = {
let mut tables_mut = env_inner.open_tables_mut(&tx_rw)?;
crate::ops::block::add_block(block, &mut tables_mut)
ops::block::add_block(block, &mut tables_mut)
};
match result {
Ok(()) => {
TxRw::commit(tx_rw)?;
Ok(BlockchainResponse::WriteBlockOk)
Ok(BlockchainResponse::WriteBlock)
}
Err(e) => {
// INVARIANT: ensure database atomicity by aborting
// the transaction on `add_block()` failures.
TxRw::abort(tx_rw)
.expect("could not maintain database atomicity by aborting write transaction");
Err(e)
}
}
}
/// [`BlockchainWriteRequest::PopBlocks`].
#[inline]
fn pop_blocks(env: &ConcreteEnv, nblocks: u64) -> ResponseResult {
let env_inner = env.env_inner();
let tx_rw = env_inner.tx_rw()?;
let result = || {
let mut tables_mut = env_inner.open_tables_mut(&tx_rw)?;
for _ in 0..nblocks {
ops::block::pop_block(&mut tables_mut)?;
}
Ok(())
};
match result() {
Ok(()) => {
TxRw::commit(tx_rw)?;
Ok(BlockchainResponse::WriteBlock)
}
Err(e) => {
// INVARIANT: ensure database atomicity by aborting

View file

@ -125,13 +125,17 @@ pub enum BlockchainReadRequest {
///
/// There is currently only 1 write request to the database,
/// as such, the only valid [`BlockchainResponse`] to this request is
/// the proper response for a [`BlockchainResponse::WriteBlockOk`].
/// the proper response for a [`BlockchainResponse::WriteBlock`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(clippy::large_enum_variant)] // TODO
pub enum BlockchainWriteRequest {
/// Request that a block be written to the database.
///
/// Input is an already verified block.
WriteBlock(VerifiedBlockInformation),
/// TODO
PopBlocks(u64),
}
//---------------------------------------------------------------------------------------------------- Response
@ -240,7 +244,10 @@ pub enum BlockchainResponse {
///
/// This response indicates that the requested block has
/// successfully been written to the database without error.
WriteBlockOk,
WriteBlock,
/// TODO
PopBlocks,
}
//---------------------------------------------------------------------------------------------------- Tests