Repository navigation
multi: run the wasm build under a Node host, not only a browser #1064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3457461
9b14732
7a06be9
76342e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -486,8 +486,8 @@ mobile-ios: #? Build the iOS .xcframework for sdk/wavewalletdk | |
| WASM_WALLET_OUT := bin/wasm | ||
| WASMSQLITE_DIR := $(shell $(GOCC) list -m -f '{{.Dir}}' github.com/lightninglabs/go-wasmsqlite 2>/dev/null) | ||
|
|
||
| wasm-wallet: #? Build the wavewalletdk browser wasm blob + runtime assets into bin/wasm | ||
| @$(call print, "Building wavewalletdk browser wasm blob.") | ||
| wasm-wallet: #? Build the wavewalletdk wasm blob + runtime assets into bin/wasm | ||
| @$(call print, "Building wavewalletdk wasm blob.") | ||
| $(RM) -r $(WASM_WALLET_OUT) | ||
| mkdir -p $(WASM_WALLET_OUT) | ||
| GOOS=js GOARCH=wasm $(GOBUILD) -trimpath -ldflags="-s -w" \ | ||
|
|
@@ -501,6 +501,9 @@ wasm-wallet: #? Build the wavewalletdk browser wasm blob + runtime assets into b | |
| cp $(WASMSQLITE_DIR)/assets/sqlite3-opfs-async-proxy.js $(WASM_WALLET_OUT)/ | ||
| cp $(WASMSQLITE_DIR)/bridge/sqlite-bridge.js $(WASM_WALLET_OUT)/ | ||
| cp $(WASMSQLITE_DIR)/bridge/sqlite-worker.js $(WASM_WALLET_OUT)/ | ||
| # sqlite-node-vfs.js is loaded only by a Node host, but it ships in every | ||
| # bundle so one asset set serves both hosts. | ||
| cp $(WASMSQLITE_DIR)/bridge/sqlite-node-vfs.js $(WASM_WALLET_OUT)/ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 blocker — This Separately — and this one survives the go.mod bump — Add |
||
| # The go-wasmsqlite assets are read-only in the module cache; make the | ||
| # copies writable so re-runs (and callers staging the bundle) aren't | ||
| # blocked by a read-only destination. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,33 +12,50 @@ import ( | |
| "time" | ||
|
|
||
| _ "github.com/lightninglabs/go-wasmsqlite" | ||
| "github.com/lightninglabs/wavelength/internal/wasmhost" | ||
| ) | ||
|
|
||
| const ( | ||
| wasmSQLiteDriverName = "wasmsqlite" | ||
| wasmSQLiteVFS = "opfs" | ||
| ) | ||
| const wasmSQLiteDriverName = "wasmsqlite" | ||
|
|
||
| // openSQLiteDatabase opens SQLite through the browser-backed wasmsqlite | ||
| // driver. | ||
| // openSQLiteDatabase opens SQLite through the wasmsqlite driver, against | ||
| // whichever durable storage the host offers. | ||
| func openSQLiteDatabase(cfg SQLiteOpenConfig) (*SQLiteOpenResult, error) { | ||
| values := url.Values{} | ||
| values.Set("file", browserSQLiteFileName(cfg.DatabaseFileName)) | ||
| values.Set("vfs", wasmSQLiteVFS) | ||
| values.Set("vfs", wasmhost.SQLiteVFS()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For every Node-hosted run built from this commit, Useful? React with 👍 / 👎. |
||
| if wasmhost.UnderNode() { | ||
| // A Node host has a real filesystem, so the configured path is | ||
| // usable as-is and the OPFS name mangling below would only | ||
| // obscure where the database actually lives. | ||
| values.Set("file", cfg.DatabaseFileName) | ||
| } else { | ||
| values.Set("file", browserSQLiteFileName(cfg.DatabaseFileName)) | ||
| } | ||
| values.Set("mode", "rwc") | ||
|
|
||
| // The daemon's databases are the only record of VTXO, swap, and round | ||
| // state; there is no server to re-fetch them from. Refusing an | ||
| // in-memory substitute makes a storage failure a startup error rather | ||
| // than a wallet that looks healthy and forgets everything on exit. | ||
| values.Set("require_persistent", "true") | ||
|
|
||
| pragmas := make([]string, 0, len(cfg.Pragmas)+1) | ||
| for _, pragma := range cfg.Pragmas { | ||
| switch strings.ToLower(pragma.Name) { | ||
| case "busy_timeout": | ||
| values.Set("busy_timeout", pragma.Value) | ||
|
|
||
| case "journal_mode": | ||
| // The driver takes the journal mode as its own DSN key | ||
| // rather than as a pragma, because it applies it last | ||
| // and then reads the effective mode back. See the | ||
| // locking_mode note below for why the order matters. | ||
| values.Set("journal_mode", pragma.Value) | ||
|
|
||
| case "fullfsync": | ||
| // fullfsync is a native filesystem durability hint and | ||
| // is not meaningful for browser OPFS. | ||
| // fullfsync asks Darwin for a stronger barrier than | ||
| // fsync. Neither wasm VFS can express it: OPFS has no | ||
| // such concept, and the node:fs VFS already issues a | ||
| // full fsync on every xSync. | ||
|
|
||
| default: | ||
| pragmas = append( | ||
|
|
@@ -47,6 +64,16 @@ func openSQLiteDatabase(cfg SQLiteOpenConfig) (*SQLiteOpenResult, error) { | |
| } | ||
| } | ||
|
|
||
| // Exclusive locking is not merely an optimization for a | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 minor — This comment is now accurate — I verified the hoist and the read-back in the merged driver, and measured The bump changes browser behaviour, not just Node behaviour. The driver's own CHANGELOG says the old pragma ordering "made
The analysis points at the first outcome: SQLite passes So the one VFS whose WAL behaviour is untested is the one that is already in production. Please open the wasm wallet in a real browser once against this bump and confirm the database opens and reports |
||
| // single-connection handle: it is what makes WAL reachable at all here. | ||
| // Neither wasm VFS implements xShmMap, so the only WAL available is the | ||
| // mode SQLite documents for hosts without shared memory, where an | ||
| // EXCLUSIVE connection keeps the WAL index on the heap. The driver | ||
| // hoists the locking mode ahead of the journal mode for exactly that | ||
| // reason, and then reads back the mode it ended up in, so a regression | ||
| // here surfaces as a startup error rather than as a database quietly | ||
| // running on the rollback journal while `synchronous` was chosen for | ||
| // WAL. | ||
| pragmas = append(pragmas, "locking_mode=EXCLUSIVE") | ||
| values.Set("pragma", strings.Join(pragmas, ";")) | ||
|
|
||
|
|
@@ -75,8 +102,8 @@ func openWASMSQLiteWithRetry(dsn string) (*sql.DB, error) { | |
| return nil, err | ||
| } | ||
|
|
||
| // OPFS SQLite handles must be single-connection handles. | ||
| // Multiple SQL connections would race the same browser database | ||
| // A wasm SQLite handle must be a single-connection handle. | ||
| // Multiple SQL connections would race the same database | ||
| // through one worker. | ||
| db.SetMaxOpenConns(1) | ||
| db.SetMaxIdleConns(1) | ||
|
|
@@ -111,6 +138,10 @@ func isWASMCantOpen(err error) bool { | |
| // regtest and signet client.db, or two swaps.db) map to distinct OPFS files | ||
| // within one browser origin instead of silently colliding. This mirrors the | ||
| // scheme lwwallet uses for its own OPFS wallet database. | ||
| // | ||
| // None of that applies to a Node host, which keeps the configured path as | ||
| // given: it is already unique, and it has the considerable advantage of being | ||
| // findable on disk. | ||
| func browserSQLiteFileName(name string) string { | ||
| normalized := filepath.ToSlash(filepath.Clean(name)) | ||
| base := filepath.Base(normalized) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| //go:build js && wasm | ||
|
|
||
| // Package wasmhost answers one question for the js/wasm build: which host is | ||
| // running this binary? | ||
| // | ||
| // The wavewalletdk wasm blob targets two very different hosts. A browser page | ||
| // has no filesystem and persists everything in OPFS. A Node process has a real | ||
| // filesystem, reached both through Go's own os package (once the host wires | ||
| // globalThis.fs to node:fs) and through the go-wasmsqlite nodefs VFS. Nothing | ||
| // else about the daemon differs, but storage differs completely, so the answer | ||
| // is needed in several packages and is worth deriving in exactly one place. | ||
| package wasmhost | ||
|
|
||
| import "syscall/js" | ||
|
|
||
| // UnderNode reports whether this binary is hosted by Node rather than by a | ||
| // browser page. | ||
| // | ||
| // Two signals are checked together because neither is sufficient alone. A | ||
| // bundler may define a process shim in a browser, so a node version string is | ||
| // not proof on its own; and a non-browser host other than Node would also lack | ||
| // a Worker constructor. Requiring both keeps a browser that fakes one of them | ||
| // from being mistaken for a filesystem host, which would send the daemon | ||
| // looking for storage that is not there. | ||
| func UnderNode() bool { | ||
| process := js.Global().Get("process") | ||
| if process.Type() != js.TypeObject { | ||
| return false | ||
| } | ||
|
|
||
| versions := process.Get("versions") | ||
| if versions.Type() != js.TypeObject || | ||
| versions.Get("node").Type() != js.TypeString { | ||
| return false | ||
| } | ||
|
|
||
| return js.Global().Get("Worker").Type() != js.TypeFunction | ||
| } | ||
|
|
||
| // SQLiteVFS names the go-wasmsqlite VFS that gives this host durable storage. | ||
| // Both names are durable, so an open that asks for one and cannot have it | ||
| // fails rather than quietly running against memory. | ||
| func SQLiteVFS() string { | ||
| if UnderNode() { | ||
| return "nodefs" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 blocker —
The important part is the direction of failure: this is a hard open error, not a silent memory-backed database. So the codex P1 is right that the driver must be upgraded first, but it is a broken-startup bug, not a fund-safety one. Merge-order consequence, plainly: this PR must not merge before the go-wasmsqlite bump, because with the currently pinned driver every Node-hosted start dies at the first open. The browser path is unaffected either way. |
||
| } | ||
|
|
||
| return "opfs" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ import ( | |
| "github.com/btcsuite/btcwallet/walletdb" | ||
| _ "github.com/lightninglabs/go-wasmsqlite" | ||
| "github.com/lightninglabs/wavelength/internal/sqlbase" | ||
| "github.com/lightninglabs/wavelength/internal/wasmhost" | ||
| "github.com/lightningnetwork/lnd/lnwallet/btcwallet" | ||
| ) | ||
|
|
||
|
|
@@ -24,6 +25,11 @@ const ( | |
| wasmWalletDBBusyTimeoutMS = "30000" | ||
| wasmWalletDBMaxConnections = 1 | ||
| wasmWalletDBFileNamePattern = "/wallet-%016x.db" | ||
|
|
||
| // nodeWalletDBFileName is the wallet store's name on a real filesystem, | ||
| // where the containing directory already distinguishes one wallet from | ||
| // another and the hashed browser name would only hide it. | ||
| nodeWalletDBFileName = "wallet.db" | ||
| ) | ||
|
|
||
| // newWalletLoaderOptions opens btcwallet through an OPFS-backed SQLite | ||
|
|
@@ -105,14 +111,41 @@ func openWASMWalletDB(dbDir string) (walletdb.DB, error) { | |
| return nil, fmt.Errorf("open OPFS wallet database: %w", lastErr) | ||
| } | ||
|
|
||
| // wasmWalletDBDSN returns a go-wasmsqlite DSN for btcwallet's SQL walletdb. | ||
| // wasmWalletDBDSN returns a go-wasmsqlite DSN for btcwallet's SQL walletdb, | ||
| // pointed at whichever durable storage the js/wasm host provides. | ||
| func wasmWalletDBDSN(dbDir string) string { | ||
| values := url.Values{} | ||
| values.Set("file", wasmWalletDBFileName(dbDir)) | ||
| values.Set("vfs", "opfs") | ||
| values.Set("vfs", wasmhost.SQLiteVFS()) | ||
| if wasmhost.UnderNode() { | ||
| // The wallet database lives beside the daemon's own databases | ||
| // on a real filesystem, under the name the caller asked for. | ||
| values.Set( | ||
| "file", filepath.Join(dbDir, nodeWalletDBFileName), | ||
| ) | ||
| } else { | ||
| values.Set("file", wasmWalletDBFileName(dbDir)) | ||
| } | ||
| values.Set("mode", "rwc") | ||
| values.Set("busy_timeout", wasmWalletDBBusyTimeoutMS) | ||
|
|
||
| // WAL survives here only because of the locking_mode=EXCLUSIVE pragma | ||
| // below. No wasm VFS implements xShmMap, so this is the WAL mode SQLite | ||
| // documents for hosts without shared memory, which keeps the WAL index | ||
| // on the heap and requires the connection to already be exclusive. The | ||
| // driver hoists the locking mode ahead of the journal mode for that | ||
| // reason, and then reads the effective mode back, so dropping the | ||
| // exclusive lock would fail the open rather than silently move the | ||
| // wallet onto the rollback journal. | ||
| // | ||
| // The journal mode travels as its own DSN key rather than in the pragma | ||
| // list, because only that route is checked against the mode SQLite | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⚪ nit — "only that route is checked" is not true of the driver you just pinned. Nothing breaks — passing it as the DSN key is still the right thing to do, and the equivalent comment in (There is a real sharp edge nearby, though it is the dependency's rather than yours: supplying the journal mode as both the key and a pragma with different values is now a hard "conflicting journal modes" error. Neither builder here does that, so it cannot fire.) |
||
| // actually ended up in. | ||
| values.Set("journal_mode", "WAL") | ||
|
|
||
| // The wallet's seed and key state have no second copy anywhere, so an | ||
| // in-memory substitute for the store would be worse than not starting. | ||
| values.Set("require_persistent", "true") | ||
|
|
||
| values.Set( | ||
| "pragma", | ||
| strings.Join( | ||
|
|
@@ -128,7 +161,8 @@ func wasmWalletDBDSN(dbDir string) string { | |
| } | ||
|
|
||
| // wasmWalletDBFileName maps a native wallet DB directory to a stable | ||
| // origin-local OPFS database name. | ||
| // origin-local OPFS database name. It is a browser concern only; a Node host | ||
| // keeps the real directory. | ||
| func wasmWalletDBFileName(dbDir string) string { | ||
| normalized := filepath.ToSlash(filepath.Clean(dbDir)) | ||
| normalized = strings.TrimSpace(normalized) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When users obtain the runtime through either tagged release channel, this newly copied asset is discarded: the archive command in
.github/workflows/mobile-bindings.ymllines 189–197 and the explicit upload list inscripts/publish-wasm-assets.shlines 35–44 both retain the old browser-only file set. A Node consumer of the release archive or hosted bundle therefore cannot loadsqlite-node-vfs.js, so the Node-host support added here is unavailable outside a localmake wasm-walletbuild; add the asset to both packaging lists (and the corresponding consumer manifest).Useful? React with 👍 / 👎.