From a598fc38d2eef642a681f7e73e63387c7ae59e3a Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Tue, 23 Apr 2024 13:20:57 -0400 Subject: [PATCH] write: impl write_block() --- database/src/service/write.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/database/src/service/write.rs b/database/src/service/write.rs index e05f9ad2..12b6a838 100644 --- a/database/src/service/write.rs +++ b/database/src/service/write.rs @@ -15,9 +15,11 @@ use cuprate_types::{ }; use crate::{ + env::{Env, EnvInner}, error::RuntimeError, service::types::{ResponseReceiver, ResponseResult, ResponseSender}, - ConcreteEnv, Env, + transaction::TxRw, + ConcreteEnv, }; //---------------------------------------------------------------------------------------------------- Constants @@ -225,6 +227,28 @@ impl DatabaseWriter { /// [`WriteRequest::WriteBlock`]. #[inline] +#[allow(clippy::significant_drop_tightening)] fn write_block(env: &ConcreteEnv, block: &VerifiedBlockInformation) -> ResponseResult { - todo!() + 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)?; + crate::ops::block::add_block(block, &mut tables_mut) + }; + + match result { + Ok(()) => { + tx_rw.commit()?; + Ok(Response::WriteBlockOk) + } + Err(e) => { + // INVARIANT: ensure database atomicity by aborting + // the transaction on `add_block()` failures. + tx_rw + .abort() + .expect("TODO: if we cannot maintain atomicity by aborting, should we panic?"); + Err(e) + } + } }