Repository navigation
Conversation
`wasm32-unknown-unknown` has no std clock: `SystemTime::now()` panics with "time not implemented on this platform". That target is reachable — the array HLC in `nodedb-array` is what NodeDB-Lite's `array::create_put_slice_roundtrip` drives — so every read on a path Lite compiles needs a clock that answers there. `clock::since_epoch()` owns the split: `js_sys::Date::now()` on `wasm32-unknown-unknown`, std everywhere else, `wasm32-wasip1` included. All nineteen production reads across the five shared crates route through it, each keeping its own conversion, saturation and pre-epoch fallback, so native behaviour is unchanged. No shared crate reads `SystemTime::now()` directly any more; the one that remains is inside `id_gen`'s test module. The wasm arm returns `None` for a pre-epoch clock rather than `Some(0)`: `Date.now()` yields a negative `f64` there, and `as u64` saturates it to zero, so converting before guarding would report a plausible zero timestamp where the std arm reports the absence callers map to an error. `js-sys` is declared in the workspace dependencies like every other shared dependency and referenced from `nodedb-types`, scoped to the one target that reaches it.
Nothing in this repo could compile for the target the clock fix exists for, so the `js_sys` arm had no verification anywhere. Three generations of `getrandom` reach this crate transitively and each refuses the target until its browser backend is selected: * 0.2 (via `aes-gcm`) needs `js` * 0.3 needs `wasm_js` plus the matching cfg * 0.4 (a direct dependency) needs `wasm_js` The workspace already aliases 0.2 and 0.3 with those features for the WAL crate; naming both in this crate's wasm target dependencies is what puts them in its resolution. 0.4 gains `wasm_js` at the workspace root. With those in place `cargo check -p nodedb-types --target wasm32-unknown-unknown` needs `--cfg getrandom_wasm_js` in RUSTFLAGS and then compiles, which is the only way to type-check the browser clock arm. The cfg is scoped to that invocation, not the workspace, so native builds are untouched.
The wasm arm answers None for a clock before the Unix epoch, and no test covered
it: the arm only compiles on a target nothing here runs, so the guard could only
be reviewed by eye — which is how it was missing in the first place.
The guard now lives in duration_from_epoch_millis, declared for every target so
the host can call it, and two tests pin the contract either side of the epoch.
Removing the guard fails them with the exact defect:
assertion `left == right` failed
left: Some(0ns)
right: None
f64 as u64 saturates a negative reading to zero, so without the guard the browser
arm answers Some(0) where the std arm answers None, and a caller mapping absence
to an error would store a zero timestamp instead of reporting the fault.
Contributor
Author
|
Closing: the target split exists to serve wasm32, which this repository does not support. NodeDB is a server; wasm work belongs to NodeDB Lite. The part with standalone value — one clock helper over the 19 native call sites — returns as its own PR, without the wasm arm. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
types: read the wall clock through one target-split helper
Why
wasm32-unknown-unknownhas no std clock:SystemTime::now()panics with"time not implemented on this platform". That target is live, not theoretical —
nodedb-array's HLC is what NodeDB-Lite'sarray::create_put_slice_roundtripdrives — so every read on a path Lite compiles needs a clock that answers there.
Eighteen production reads existed across the five shared crates, each doing its
own
SystemTime::now(). Nothing in the repo compiled for the target either, sonone of them was covered by anything.
What changed
clock::since_epoch()owns the split:js_sys::Date::now()onwasm32-unknown-unknown, std everywhere else,wasm32-wasip1included.conversion, saturation and pre-epoch fallback, so native behaviour is
unchanged. No shared crate reads
SystemTime::now()directly any more; theone that remains is inside
id_gen's test module.getrandomreach
nodedb-typestransitively and each refuses the target until its browserbackend is selected — 0.2 (via
aes-gcm) needsjs, 0.3 needswasm_jsplusthe matching cfg, and the direct 0.4 dependency needs
wasm_js. With those inplace
cargo check -p nodedb-types --target wasm32-unknown-unknowncompiles.js-sysis a workspace dependency, referenced as{ workspace = true }like every other shared dependency in this repo.
The pre-epoch guard
Date.now()returns anf64, andas u64saturates a negative value tozero rather than wrapping. Converting before guarding would report
Some(0)— aplausible-looking zero timestamp — where the std arm reports
None, and callersmap that absence to an error. The guard lives in
duration_from_epoch_millis,declared for every target so the host can test it, and two tests pin the contract
either side of the epoch.
Exclusions, stated
NodeDB runs no scheduled CI.
wasm32-unknown-unknownand never runs theWAL, mem, crdt or client crates under WASI.
msgpack_scanoverflow, the codec elementcount and the
vector_primarylint are separate PRs, each with its ownreproduction.
Steps to test
Red arm for the guard — delete the
millis < 0.0branch and the test fails withleft: Some(0ns),right: None.Not proven here
The browser arm is compile-verified, not runtime-verified: nothing in this
repo executes
wasm32-unknown-unknown. Runtime coverage belongs in Lite, whosenodedb-lite-wasm/tests/array.rsalready reaches the array HLC that panicked.Closes #364