Skip to content

Commit

Permalink
linera-client: add tracing and fix a dirtiness bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Twey committed Sep 4, 2024
1 parent 9591b1f commit ab8ef05
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 3 additions & 1 deletion linera-client/src/persistent/dirty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ impl Dirty {

impl Drop for Dirty {
fn drop(&mut self) {
assert!(!self.0, "object dropped while dirty")
if self.0 {
tracing::error!("object dropped while dirty");
}
}
}
21 changes: 18 additions & 3 deletions linera-client/src/persistent/indexed_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,23 @@ pub struct IndexedDb<T> {
dirty: Dirty,
}

impl<T: serde::Serialize> std::fmt::Debug for IndexedDb<T> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("persistent::IndexedDb")
.field("key", &self.key)
.field("value", &serde_json::to_string(&self.value))
.field("dirty", &*self.dirty)
.finish_non_exhaustive()
}
}

const DATABASE_NAME: &str = "linera-client";
const STORE_NAME: &str = "linera-wallet";

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("marshalling error: {0}")]
Marshalling(#[source] gloo_utils::errors::JsError),
#[error("key not found: {0}")]
KeyNotFound(String),
#[error("DOM exception: {0:?}")]
DomException(#[source] gloo_utils::errors::JsError),
}
Expand Down Expand Up @@ -55,6 +63,7 @@ async fn open_database() -> Result<IdbDatabase, Error> {
}

impl<T> IndexedDb<T> {
#[tracing::instrument(level = "trace", skip(value))]
pub async fn new(key: &str, value: T) -> Result<Self, Error> {
Ok(Self {
key: key.to_owned(),
Expand All @@ -66,6 +75,7 @@ impl<T> IndexedDb<T> {
}

impl<T: serde::de::DeserializeOwned> IndexedDb<T> {
#[tracing::instrument(level = "trace")]
pub async fn read(key: &str) -> Result<Option<Self>, Error> {
let database = open_database().await?;
let tx =
Expand All @@ -83,6 +93,7 @@ impl<T: serde::de::DeserializeOwned> IndexedDb<T> {
}))
}

#[tracing::instrument(level = "trace", fields(value = &serde_json::to_string(&value).unwrap()))]
pub async fn read_or_create(key: &str, value: T) -> Result<Self, Error>
where
T: serde::Serialize,
Expand All @@ -100,6 +111,7 @@ impl<T: serde::de::DeserializeOwned> IndexedDb<T> {
impl<T: serde::Serialize> LocalPersist for IndexedDb<T> {
type Error = Error;

#[tracing::instrument(level = "trace")]
fn as_mut(&mut self) -> &mut T {
*self.dirty = true;
&mut self.value
Expand All @@ -109,12 +121,15 @@ impl<T: serde::Serialize> LocalPersist for IndexedDb<T> {
self.value
}

#[tracing::instrument(level = "trace")]
async fn persist(&mut self) -> Result<(), Error> {
let serializer = serde_wasm_bindgen::Serializer::new().serialize_large_number_types_as_bigints(true);
self.database
.transaction_on_one_with_mode(STORE_NAME, IdbTransactionMode::Readwrite)?
.object_store(STORE_NAME)?
.put_key_val_owned(&self.key, &serde_wasm_bindgen::to_value(&self.value)?)?
.put_key_val_owned(&self.key, &self.value.serialize(&serializer)?)?
.await?;
*self.dirty = false;
Ok(())
}
}

0 comments on commit ab8ef05

Please sign in to comment.