Skip to content

Commit

Permalink
linera-client: add IndexedDB persistence backend for the wallet (#2420
Browse files Browse the repository at this point in the history
)

* `linera_client::persistent`: support async persistence

* `linera-client`: add IndexedDB persistence implementation

* `linera_client::persistent`: refactor `mutate` into extension traits so we can use `trait-variant`

* `linera-client`: remove `localStorage` support

* `linera_client::persistent::dirty`: use `derive_more`

* `linera-execution`: update deprecated `derive_more` syntax

* `linera-client`: see if failing to save some changes is problematic

* CI: use `cargo run` for the storage-service instead of building and running separately

* `linera-service`: save the wallet after creation

* `linera-client`: only build IndexedDB support on the Web

* `linera-client`: fix benchmarking build

* `linera-service`: fix `remote_net.rs`

* `linera-client`: clippy fixes

* Documentation tweaks

Co-authored-by: Andreas Fackler <afck@users.noreply.github.com>
Signed-off-by: James Kay <james.kay@linera.io>

* `linera-client`: apply suggestions from code review

* `linera_client::persistent::indexed_db`: simplify and generalize API

* `linera-client`: don't require that the user construct a `WalletState`

* `linera-base`: use the Web console for tracing output

* `linera-client`: add tracing and fix a dirtiness bug

* Cleanup

---------

Signed-off-by: James Kay <james.kay@linera.io>
Co-authored-by: Andreas Fackler <afck@users.noreply.github.com>
  • Loading branch information
Twey and afck authored Sep 4, 2024
1 parent 69354e8 commit 9d767f1
Show file tree
Hide file tree
Showing 30 changed files with 699 additions and 360 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ jobs:
cargo build --locked --release --target wasm32-unknown-unknown
- name: Run end-to-end tests
run: |
cargo build --release -p linera-storage-service
target/release/linera-storage-server memory --endpoint $LINERA_STORAGE_SERVICE &
cargo run --release -p linera-storage-service -- memory --endpoint $LINERA_STORAGE_SERVICE &
cargo test --features storage-service,unstable-oracles -- storage_service --nocapture
- name: Run Ethereum tests
run: |
Expand Down
66 changes: 42 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ convert_case = "0.6.0"
criterion = { version = "0.5.1", default-features = false }
custom_debug_derive = "0.6.1"
dashmap = "5.5.3"
derive_more = "0.99.17"
derive_more = "1.0.0"
dirs = "5.0.1"
ed25519-dalek = { version = "2.1.1", features = ["batch", "serde"] }
either = "1.10.0"
Expand All @@ -87,6 +87,7 @@ hex = "0.4.3"
http = "1.1.0"
glob = "0.3.1"
gloo-storage = "0.3.0"
gloo-utils = "0.2.0"
indexed_db_futures = "0.4.1"
insta = "1.36.1"
is-terminal = "0.4.12"
Expand Down Expand Up @@ -123,6 +124,7 @@ serde_json = "1.0.114"
serde_yaml = "0.8.26"
serde-name = "0.2.1"
serde-reflection = "0.3.6"
serde-wasm-bindgen = "0.6.5"
sha3 = "0.10.8"
similar-asserts = "1.5.0"
static_assertions = "1.1.0"
Expand All @@ -149,6 +151,7 @@ tower-http = "0.5.2"
tower = "0.4.13"
tracing = { version = "0.1.40", features = ["release_max_level_debug"] }
tracing-subscriber = { version = "0.3.18", default-features = false, features = ["env-filter"] }
tracing-web = "0.1.3"
trait-variant = "0.1.1"
url = "2.4"
wasm-bindgen = "0.2.92"
Expand Down
25 changes: 23 additions & 2 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions linera-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ web = [
"rand/getrandom",
"rand/std",
"rand/std_rng",
"tracing-web",
"web-time",
"wasm-bindgen-futures",
]
Expand Down Expand Up @@ -58,6 +59,7 @@ web-time = { workspace = true, optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
ruzstd.workspace = true
tracing-web = { optional = true, workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
chrono.workspace = true
Expand Down
63 changes: 42 additions & 21 deletions linera-base/src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,54 @@

//! This module provides unified handling for tracing subscribers within Linera binaries.

use is_terminal::IsTerminal as _;
use tracing_subscriber::fmt::format::FmtSpan;

fn fmt_span_from_str(events: &str) -> FmtSpan {
let mut fmt_span = FmtSpan::NONE;
for event in events.split(',') {
fmt_span |= match event {
"new" => FmtSpan::NEW,
"enter" => FmtSpan::ENTER,
"exit" => FmtSpan::EXIT,
"close" => FmtSpan::CLOSE,
"active" => FmtSpan::ACTIVE,
"full" => FmtSpan::FULL,
_ => FmtSpan::NONE,
};
}
fmt_span
/// Initializes tracing for the browser, sending messages to the developer console and
/// span events to the [Performance
/// API](https://developer.mozilla.org/en-US/docs/Web/API/Performance).
#[cfg(web)]
pub fn init() {
use tracing_subscriber::{
prelude::__tracing_subscriber_SubscriberExt as _, util::SubscriberInitExt as _,
};

tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.with_ansi(false)
.without_time()
.with_writer(tracing_web::MakeWebConsoleWriter::new()),
)
.with(
tracing_web::performance_layer()
.with_details_from_fields(tracing_subscriber::fmt::format::Pretty::default()),
)
.init();
}

/// Initializes tracing in a standard way.
/// The environment variables `RUST_LOG`, `RUST_LOG_SPAN_EVENTS`, and `RUST_LOG_FORMAT`
/// can be used to control the verbosity, the span event verbosity, and the output format,
/// respectively.
#[cfg(not(web))]
pub fn init() {
use is_terminal::IsTerminal as _;
use tracing_subscriber::fmt::format::FmtSpan;

fn fmt_span_from_str(events: &str) -> FmtSpan {
let mut fmt_span = FmtSpan::NONE;
for event in events.split(',') {
fmt_span |= match event {
"new" => FmtSpan::NEW,
"enter" => FmtSpan::ENTER,
"exit" => FmtSpan::EXIT,
"close" => FmtSpan::CLOSE,
"active" => FmtSpan::ACTIVE,
"full" => FmtSpan::FULL,
_ => FmtSpan::NONE,
};
}
fmt_span
}

let span_events = std::env::var("RUST_LOG_SPAN_EVENTS")
.ok()
.map(|s| fmt_span_from_str(&s))
Expand All @@ -41,10 +65,7 @@ pub fn init() {
.from_env_lossy(),
);

if std::env::var("NO_COLOR").is_ok_and(|x| !x.is_empty())
|| cfg!(feature = "web")
|| !std::io::stderr().is_terminal()
{
if std::env::var("NO_COLOR").is_ok_and(|x| !x.is_empty()) || !std::io::stderr().is_terminal() {
subscriber = subscriber.with_ansi(false);
}

Expand Down
11 changes: 7 additions & 4 deletions linera-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ metrics = [
"linera-views/metrics",
]
web = [
"flarch/wasm",
"dep:web-sys",
"dep:wasm-bindgen-futures",
"linera-base/web",
Expand All @@ -45,18 +44,18 @@ web = [
"linera-execution/web",
"linera-rpc/web",
]
local-storage = ["web"]
web-default = ["web", "wasmer", "local-storage"]
indexed-db = ["web", "indexed_db_futures", "serde-wasm-bindgen", "gloo-utils"]
web-default = ["web", "wasmer", "indexed-db"]

[dependencies]
async-trait.workspace = true
bcs.workspace = true
cfg-if.workspace = true
chrono = { workspace = true, features = ["clock"] }
clap.workspace = true
derive_more = { workspace = true, features = ["deref", "deref_mut"] }
dirs.workspace = true
futures.workspace = true
gloo-storage.workspace = true
linera-base.workspace = true
linera-chain.workspace = true
linera-core.workspace = true
Expand All @@ -75,9 +74,13 @@ thiserror-context.workspace = true
tokio.workspace = true
tokio-stream.workspace = true
tracing.workspace = true
trait-variant.workspace = true

[target.wasm32-unknown-unknown.dependencies]
flarch = { workspace = true, optional = true }
gloo-utils = { workspace = true, optional = true }
indexed_db_futures = { workspace = true, optional = true }
serde-wasm-bindgen = { workspace = true, optional = true }
wasm-bindgen-futures = { workspace = true, optional = true }
web-sys = { workspace = true, optional = true }

Expand Down
4 changes: 2 additions & 2 deletions linera-client/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ fn main() {
feature = "dynamodb",
feature = "storage-service"
) },
with_persist: { any(feature = "fs", with_local_storage) },
with_local_storage: { all(web, feature = "local_storage") },
with_persist: { any(feature = "fs", with_indexed_db) },
with_indexed_db: { all(web, feature = "indexed-db") },
with_testing: { any(test, feature = "test") },
with_metrics: { all(not(target_arch = "wasm32"), feature = "metrics") },
};
Expand Down
Loading

0 comments on commit 9d767f1

Please sign in to comment.