Skip to content

Commit

Permalink
chore(drive): log grovedb operations (#1446)
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov authored Oct 6, 2023
1 parent 6063b91 commit 9a6fca8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/rs-drive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ full = [
"rust_decimal_macros",
"lazy_static",
]
grovedb_operations_logging = []
verify = ["grovedb/verify", "grovedb-costs"]
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use grovedb::batch::{BatchApplyOptions, GroveDbOp};
use grovedb::TransactionArg;
use grovedb_costs::storage_cost::removal::StorageRemovedBytes::BasicStorageRemoval;
use grovedb_costs::storage_cost::transition::OperationStorageTransitionType;
use tracing::Level;

impl Drive {
/// Applies the given groveDB operations batch and gets and passes the costs to `push_drive_operation_result`.
Expand Down Expand Up @@ -42,6 +43,15 @@ impl Drive {
}
}

// Clone ops only if we log them
#[cfg(feature = "grovedb_operations_logging")]
let maybe_ops_for_logs = if tracing::event_enabled!(target: "grovedb_operations", Level::TRACE)
{
Some(ops.clone())
} else {
None
};

let cost_context = self.grove.apply_batch_with_element_flags_update(
ops.operations,
Some(BatchApplyOptions {
Expand Down Expand Up @@ -140,6 +150,26 @@ impl Drive {
},
transaction,
);

#[cfg(feature = "grovedb_operations_logging")]
if tracing::event_enabled!(target: "grovedb_operations", Level::TRACE)
&& cost_context.value.is_ok()
{
let root_hash = self
.grove
.root_hash(transaction)
.unwrap()
.map_err(Error::GroveDB)?;

tracing::trace!(
target = "grovedb_operations",
ops = ?maybe_ops_for_logs.unwrap(),
root_hash = ?root_hash,
is_transactional = transaction.is_some(),
"grovedb batch applied",
);
}

push_drive_operation_result(cost_context, drive_operations)
}
}
34 changes: 32 additions & 2 deletions packages/rs-drive/src/drive/grove_operations/grove_clear/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::error::Error;
use grovedb::operations::delete::ClearOptions;
use grovedb::TransactionArg;
use grovedb_path::SubtreePath;
use tracing::Level;

impl Drive {
/// Pushes the `OperationCost` of deleting an element in groveDB to `drive_operations`.
Expand All @@ -16,10 +17,39 @@ impl Drive {
allow_deleting_subtrees: false,
trying_to_clear_with_subtrees_returns_error: false,
};

#[cfg(feature = "grovedb_operations_logging")]
let maybe_path_for_logs = if tracing::event_enabled!(target: "grovedb_operations", Level::TRACE)
{
Some(path.clone())
} else {
None
};

// we will always return true if there is no error when we don't check for subtrees
self.grove
let result = self
.grove
.clear_subtree(path, Some(options), transaction)
.map_err(Error::GroveDB)
.map(|_| ())
.map(|_| ());

#[cfg(feature = "grovedb_operations_logging")]
if tracing::event_enabled!(target: "grovedb_operations", Level::TRACE) && result.is_ok() {
let root_hash = self
.grove
.root_hash(transaction)
.unwrap()
.map_err(Error::GroveDB)?;

tracing::trace!(
target = "grovedb_operations",
path = ?maybe_path_for_logs.unwrap().to_vec(),
root_hash = ?root_hash,
is_transactional = transaction.is_some(),
"grovedb clear",
);
}

result
}
}

0 comments on commit 9a6fca8

Please sign in to comment.