Run write operations in transaction
This change ensures that all the write operations are done in a single
DB transaction.
@@ -56,6 +56,8 @@ mod schema;
use std::cmp::Reverse;
use std::collections::{HashMap, HashSet};
+use diesel::Connection as _;
+
use crate::error::{InternalError, InvalidStateError};
use crate::state::error::{StatePruneError, StateReadError, StateWriteError};
#[cfg(feature = "state-merkle-leaf-reader")]
@@ -771,6 +773,7 @@ impl<'b> OverlayWriter for SqlOverlay<'b, backend::SqliteBackend> {
deleted_addresses: &[&str],
) -> Result<(), InternalError> {
let conn = self.backend.connection()?;
+ conn.as_inner().transaction(|| {
let operations = MerkleRadixOperations::new(conn.as_inner());
let TreeUpdate {
@@ -822,6 +825,7 @@ impl<'b> OverlayWriter for SqlOverlay<'b, backend::SqliteBackend> {
)?;
Ok(())
+ })
}
fn prune(&self, tree_id: i64, state_root: &str) -> Result<Vec<String>, InternalError> {
@@ -876,6 +880,8 @@ impl<'b> OverlayWriter for SqlOverlay<'b, backend::PostgresBackend> {
deleted_addresses: &[&str],
) -> Result<(), InternalError> {
let conn = self.backend.connection()?;
+
+ conn.as_inner().transaction(|| {
let operations = MerkleRadixOperations::new(conn.as_inner());
let TreeUpdate {
@@ -927,6 +933,7 @@ impl<'b> OverlayWriter for SqlOverlay<'b, backend::PostgresBackend> {
)?;
Ok(())
+ })
}
fn prune(&self, tree_id: i64, state_root: &str) -> Result<Vec<String>, InternalError> {
