From 73aa40f00e291952460b3e68a0d62672656b5857 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 15 Jul 2021 10:34:27 +0200 Subject: [PATCH 1/5] lnwallet+kvdb: remove duplicate bdb backend imports We only want to register the bbolt DB backend ("bdb") when we're not compiling for a JS/WASM build targets. That's why we want to have that import in only one file that we can add a build tag to. We remove it in two other places since only one import is enough anyway. --- kvdb/interface.go | 1 - lnwallet/btcwallet/config.go | 6 ------ 2 files changed, 7 deletions(-) diff --git a/kvdb/interface.go b/kvdb/interface.go index 616e6317..a54a94d8 100644 --- a/kvdb/interface.go +++ b/kvdb/interface.go @@ -2,7 +2,6 @@ package kvdb import ( "github.com/btcsuite/btcwallet/walletdb" - _ "github.com/btcsuite/btcwallet/walletdb/bdb" // Import to register backend. ) // Update opens a database read/write transaction and executes the function f diff --git a/lnwallet/btcwallet/config.go b/lnwallet/btcwallet/config.go index d8983267..a69217ea 100644 --- a/lnwallet/btcwallet/config.go +++ b/lnwallet/btcwallet/config.go @@ -9,12 +9,6 @@ import ( "github.com/btcsuite/btcwallet/chain" "github.com/btcsuite/btcwallet/wallet" - - // This is required to register bdb as a valid walletdb driver. In the - // init function of the package, it registers itself. The import is used - // to activate the side effects w/o actually binding the package name to - // a file-level variable. - _ "github.com/btcsuite/btcwallet/walletdb/bdb" ) var ( From 724ca7a358b8c66605195fbba4d38287cc318070 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 15 Jul 2021 10:42:46 +0200 Subject: [PATCH 2/5] kvdb: exclude anything bbolt related from JS builds Since bbolt uses syscalls for memory mapping that aren't available in JS/WASM builds, we need to make sure we don't reference that code at all. Otherwise we can't use parts of lnd as a library in projects that are being compiled down to a WASM binary. --- kvdb/backend.go | 2 ++ kvdb/backend_js.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ kvdb/bolt_compact.go | 2 ++ 3 files changed, 52 insertions(+) create mode 100644 kvdb/backend_js.go diff --git a/kvdb/backend.go b/kvdb/backend.go index 23dccddf..a5307dfa 100644 --- a/kvdb/backend.go +++ b/kvdb/backend.go @@ -1,3 +1,5 @@ +// +build !js + package kvdb import ( diff --git a/kvdb/backend_js.go b/kvdb/backend_js.go new file mode 100644 index 00000000..f1774408 --- /dev/null +++ b/kvdb/backend_js.go @@ -0,0 +1,48 @@ +package kvdb + +import ( + "fmt" + "time" +) + +// BoltBackendConfig is a struct that holds settings specific to the bolt +// database backend. +type BoltBackendConfig struct { + // DBPath is the directory path in which the database file should be + // stored. + DBPath string + + // DBFileName is the name of the database file. + DBFileName string + + // NoFreelistSync, if true, prevents the database from syncing its + // freelist to disk, resulting in improved performance at the expense of + // increased startup time. + NoFreelistSync bool + + // AutoCompact specifies if a Bolt based database backend should be + // automatically compacted on startup (if the minimum age of the + // database file is reached). This will require additional disk space + // for the compacted copy of the database but will result in an overall + // lower database size after the compaction. + AutoCompact bool + + // AutoCompactMinAge specifies the minimum time that must have passed + // since a bolt database file was last compacted for the compaction to + // be considered again. + AutoCompactMinAge time.Duration + + // DBTimeout specifies the timeout value to use when opening the wallet + // database. + DBTimeout time.Duration +} + +// GetBoltBackend opens (or creates if doesn't exits) a bbolt backed database +// and returns a kvdb.Backend wrapping it. +func GetBoltBackend(cfg *BoltBackendConfig) (Backend, error) { + return nil, fmt.Errorf("bolt backend not supported in WebAssembly") +} + +func GetTestBackend(path, name string) (Backend, func(), error) { + return nil, nil, fmt.Errorf("bolt backend not supported in WebAssembly") +} diff --git a/kvdb/bolt_compact.go b/kvdb/bolt_compact.go index 9d2676d7..66203ff9 100644 --- a/kvdb/bolt_compact.go +++ b/kvdb/bolt_compact.go @@ -2,6 +2,8 @@ // implemented in this file: // https://github.com/etcd-io/bbolt/blob/master/cmd/bbolt/main.go +// +build !js + package kvdb import ( From 537880e634737119f39efba8f63b72a7faa3edf4 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 15 Jul 2021 10:45:22 +0200 Subject: [PATCH 3/5] healthcheck: exclude unsupported syscalls from JS builds Because the health check uses OS specific syscalls for determining the available space on a disk that aren't available in JS/WASM builds, we need to make sure we don't reference that code at all. Otherwise we can't use parts of lnd as a library in projects that are being compiled down to a WASM binary. --- healthcheck/diskcheck.go | 2 +- healthcheck/diskcheck_js.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 healthcheck/diskcheck_js.go diff --git a/healthcheck/diskcheck.go b/healthcheck/diskcheck.go index 7f30b154..087fc7a7 100644 --- a/healthcheck/diskcheck.go +++ b/healthcheck/diskcheck.go @@ -1,4 +1,4 @@ -// +build !windows,!solaris,!netbsd,!openbsd +// +build !windows,!solaris,!netbsd,!openbsd,!js package healthcheck diff --git a/healthcheck/diskcheck_js.go b/healthcheck/diskcheck_js.go new file mode 100644 index 00000000..4a2f4f57 --- /dev/null +++ b/healthcheck/diskcheck_js.go @@ -0,0 +1,13 @@ +package healthcheck + +// AvailableDiskSpaceRatio returns ratio of available disk space to total +// capacity. +func AvailableDiskSpaceRatio(path string) (float64, error) { + return 0, fmt.Errorf("disk space check not supported in WebAssembly") +} + +// AvailableDiskSpace returns the available disk space in bytes of the given +// file system. +func AvailableDiskSpace(path string) (uint64, error) { + return 0, fmt.Errorf("disk space check not supported in WebAssembly") +} From 8f6b2c7c469ba87cc58def84da2d7ac8c96114cd Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 15 Jul 2021 10:47:14 +0200 Subject: [PATCH 4/5] mod: run go mod tidy Remove any unused entries in the summary file by running go mod tidy. --- go.sum | 1 - 1 file changed, 1 deletion(-) diff --git a/go.sum b/go.sum index 5d2cdf8a..0180777d 100644 --- a/go.sum +++ b/go.sum @@ -53,7 +53,6 @@ github.com/btcsuite/btcwallet/walletdb v1.3.4/go.mod h1:oJDxAEUHVtnmIIBaa22wSBPT github.com/btcsuite/btcwallet/walletdb v1.3.5-0.20210513043850-3a2f12e3a954/go.mod h1:oJDxAEUHVtnmIIBaa22wSBPTVcs6hUp5NKWmI8xDwwU= github.com/btcsuite/btcwallet/walletdb v1.3.5 h1:SoxUPLgJUkyO1XqON6X7x+rjHJoIpRQov8o8X6gNoz8= github.com/btcsuite/btcwallet/walletdb v1.3.5/go.mod h1:oJDxAEUHVtnmIIBaa22wSBPTVcs6hUp5NKWmI8xDwwU= -github.com/btcsuite/btcwallet/wtxmgr v1.3.0 h1:lrZaZXGJjDedYTV7s5UgU9xBe8+N+cIDW7BYwI/B8Fs= github.com/btcsuite/btcwallet/wtxmgr v1.3.0/go.mod h1:awQsh1n/0ZrEQ+JZgWvHeo153ubzEisf/FyNtwI0dDk= github.com/btcsuite/btcwallet/wtxmgr v1.3.1-0.20210706234807-aaf03fee735a h1:25oMK8eFUTVMyKGHc2xX7pNkU4u208Dpf6IPVh5E+cA= github.com/btcsuite/btcwallet/wtxmgr v1.3.1-0.20210706234807-aaf03fee735a/go.mod h1:UM38ixX8VwJ9qey4umf//0H3ndn5kSImFZ46V54Nd5Q= From 04c264006052ea814a997420d2692c1b6bf665ea Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 15 Jul 2021 11:33:56 +0200 Subject: [PATCH 5/5] docs: add release notes for #5526 --- docs/release-notes/release-notes-0.14.0.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/release-notes/release-notes-0.14.0.md b/docs/release-notes/release-notes-0.14.0.md index 293269e7..2b82e8ae 100644 --- a/docs/release-notes/release-notes-0.14.0.md +++ b/docs/release-notes/release-notes-0.14.0.md @@ -13,6 +13,13 @@ PRs ([aside from merge commits](https://github.com/lightningnetwork/lnd/pull/5543)) add an entry in the release notes folder that at leasts links to PR being added. +# Misc + +* The direct use of certain syscalls in packages such as `bbolt` or `lnd`'s own + `healthcheck` package made it impossible to import `lnd` code as a library + into projects that are compiled to WASM binaries. [That problem was fixed by + guarding those syscalls with build tags](https://github.com/lightningnetwork/lnd/pull/5526). + # Code Health ## Code cleanup, refactor, typo fixes @@ -32,3 +39,4 @@ the release notes folder that at leasts links to PR being added. # Contributors (Alphabetical Order) * ErikEk * Zero-1729 +* Oliver Gugger \ No newline at end of file