Skip to content

Commit

Permalink
Add wasm compatible database using gloo_storage::LocalStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Oct 27, 2022
1 parent 5d5b2fb commit f7055a8
Show file tree
Hide file tree
Showing 4 changed files with 505 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ rocksdb = { version = "0.14", default-features = false, features = ["snappy"], o
cc = { version = ">=1.0.64", optional = true }
socks = { version = "0.3", optional = true }
hwi = { version = "0.3.0", optional = true }
gloo-storage = { version = "0.2.2", optional = true }

bip39 = { version = "1.0.1", optional = true }
bitcoinconsensus = { version = "0.19.0-3", optional = true }
Expand Down Expand Up @@ -61,6 +62,7 @@ all-keys = ["keys-bip39"]
keys-bip39 = ["bip39"]
rpc = ["bitcoincore-rpc"]
hardware-signer = ["hwi"]
wasm-db = ["gloo-storage"]

# We currently provide mulitple implementations of `Blockchain`, all are
# blocking except for the `EsploraBlockchain` which can be either async or
Expand Down
29 changes: 29 additions & 0 deletions src/database/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ macro_rules! impl_inner_method {
$enum_name::Sled(inner) => inner.$name( $($args, )* ),
#[cfg(feature = "sqlite")]
$enum_name::Sqlite(inner) => inner.$name( $($args, )* ),
#[cfg(feature = "wasm-db")]
$enum_name::LocalStorage(inner) => inner.$name( $($args, )* ),
}
}
}
Expand All @@ -89,11 +91,16 @@ pub enum AnyDatabase {
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
/// Sqlite embedded database using [`rusqlite`]
Sqlite(sqlite::SqliteDatabase),
#[cfg(feature = "wasm-db")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasm-db")))]
/// Browser LocalStorage database based on [`gloo_storage`]
LocalStorage(localstorage::LocalStorageDatabase),
}

impl_from!(memory::MemoryDatabase, AnyDatabase, Memory,);
impl_from!(sled::Tree, AnyDatabase, Sled, #[cfg(feature = "key-value-db")]);
impl_from!(sqlite::SqliteDatabase, AnyDatabase, Sqlite, #[cfg(feature = "sqlite")]);
impl_from!(localstorage::LocalStorageDatabase, AnyDatabase, LocalStorage, #[cfg(feature = "wasm-db")]);

/// Type that contains any of the [`BatchDatabase::Batch`] types defined by the library
pub enum AnyBatch {
Expand All @@ -107,6 +114,10 @@ pub enum AnyBatch {
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
/// Sqlite embedded database using [`rusqlite`]
Sqlite(<sqlite::SqliteDatabase as BatchDatabase>::Batch),
#[cfg(feature = "wasm-db")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasm-db")))]
/// Browser LocalStorage database based on [`gloo_storage`]
LocalStorage(<gloo_storage::LocalStorage as BatchDatabase>::Batch),
}

impl_from!(
Expand All @@ -116,6 +127,7 @@ impl_from!(
);
impl_from!(<sled::Tree as BatchDatabase>::Batch, AnyBatch, Sled, #[cfg(feature = "key-value-db")]);
impl_from!(<sqlite::SqliteDatabase as BatchDatabase>::Batch, AnyBatch, Sqlite, #[cfg(feature = "sqlite")]);
impl_from!(<localstorage::LocalStorageDatabase as BatchDatabase>::Batch, AnyBatch, LocalStorage, #[cfg(feature = "wasm-db")]);

impl BatchOperations for AnyDatabase {
fn set_script_pubkey(
Expand Down Expand Up @@ -326,6 +338,8 @@ impl BatchDatabase for AnyDatabase {
AnyDatabase::Sled(inner) => inner.begin_batch().into(),
#[cfg(feature = "sqlite")]
AnyDatabase::Sqlite(inner) => inner.begin_batch().into(),
#[cfg(feature = "wasm-db")]
AnyDatabase::LocalStorage(inner) => inner.begin_batch().into(),
}
}
fn commit_batch(&mut self, batch: Self::Batch) -> Result<(), Error> {
Expand All @@ -345,6 +359,11 @@ impl BatchDatabase for AnyDatabase {
AnyBatch::Sqlite(batch) => db.commit_batch(batch),
_ => unimplemented!("Other batch shouldn't be used with Sqlite db."),
},
#[cfg(feature = "wasm-db")]
AnyDatabase::LocalStorage(db) => match batch {
AnyBatch::LocalStorage(batch) => db.commit_batch(batch),
_ => unimplemented!("Other batch shouldn't be used with LocalStorage db."),
},
}
}
}
Expand Down Expand Up @@ -402,6 +421,10 @@ pub enum AnyDatabaseConfig {
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
/// Sqlite embedded database using [`rusqlite`]
Sqlite(SqliteDbConfiguration),
#[cfg(feature = "wasm-db")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasm-db")))]
/// Browser LocalStorage database based on [`gloo_storage`]
LocalStorage(()),
}

impl ConfigurableDatabase for AnyDatabase {
Expand All @@ -418,10 +441,16 @@ impl ConfigurableDatabase for AnyDatabase {
AnyDatabaseConfig::Sqlite(inner) => {
AnyDatabase::Sqlite(sqlite::SqliteDatabase::from_config(inner)?)
}
#[cfg(feature = "wasm-db")]
AnyDatabaseConfig::LocalStorage(inner) => {
AnyDatabase::LocalStorage(localstorage::LocalStorageDatabase::from_config(inner)?)
}
})
}
}

impl_from!((), AnyDatabaseConfig, Memory,);
impl_from!(SledDbConfiguration, AnyDatabaseConfig, Sled, #[cfg(feature = "key-value-db")]);
impl_from!(SqliteDbConfiguration, AnyDatabaseConfig, Sqlite, #[cfg(feature = "sqlite")]);
impl_from!(SqliteDbConfiguration, AnyDatabaseConfig, Sqlite, #[cfg(feature = "sqlite")]);
impl_from!((), AnyDatabaseConfig, LocalStorage, #[cfg(feature = "wasm-db")]);
Loading

0 comments on commit f7055a8

Please sign in to comment.