Skip to content
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

Add support for WASM in goleveldb dependency #1008

Closed
ilgooz opened this issue Jul 31, 2023 · 2 comments · Fixed by #1012
Closed

Add support for WASM in goleveldb dependency #1008

ilgooz opened this issue Jul 31, 2023 · 2 comments · Fixed by #1012
Labels
📦 🌐 tendermint v2 Issues or PRs tm2 related

Comments

@ilgooz
Copy link
Contributor

ilgooz commented Jul 31, 2023

Currently, syndtr/goleveldb used as the storage. As this dependency doesn't have support for WASM it prevents compiling gnovm to WASM.

We should either (1) fork it and apply the following change to bring compatibility or (2) switch to another storage with builtin support. I prefer option (1) because it's the fastest and safest for overall code stability. How should we proceed with this?

The following code introduces js+wasm built target with;

  • rename(), isErrInvalid(), syncDir() funcs, same as with the ones in unix target.
  • A fileLock implementation, which is empty because we don't necessarily need actual locks in the WASM environment and it might be tricky to implement one that both supports browsers and NodeJS.
diff --git a/leveldb/storage/file_storage_js.go b/leveldb/storage/file_storage_js.go
new file mode 100644
index 0000000..36b4bf8
--- /dev/null
+++ b/leveldb/storage/file_storage_js.go
@@ -0,0 +1,63 @@
+// Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
+// All rights reserved.
+//
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+//go:build js && wasm
+// +build js,wasm
+
+package storage
+
+import (
+	"os"
+	"syscall"
+)
+
+type jsFileLock struct{}
+
+func (fl *jsFileLock) release() error {
+	return nil
+}
+
+func newFileLock(path string, readOnly bool) (fl fileLock, err error) {
+	return &jsFileLock{}, nil
+}
+
+func rename(oldpath, newpath string) error {
+	return os.Rename(oldpath, newpath)
+}
+
+func isErrInvalid(err error) bool {
+	if err == os.ErrInvalid {
+		return true
+	}
+	// Go < 1.8
+	if syserr, ok := err.(*os.SyscallError); ok && syserr.Err == syscall.EINVAL {
+		return true
+	}
+	// Go >= 1.8 returns *os.PathError instead
+	if patherr, ok := err.(*os.PathError); ok && patherr.Err == syscall.EINVAL {
+		return true
+	}
+	return false
+}
+
+func syncDir(name string) error {
+	// As per fsync manpage, Linux seems to expect fsync on directory, however
+	// some system don't support this, so we will ignore syscall.EINVAL.
+	//
+	// From fsync(2):
+	//   Calling fsync() does not necessarily ensure that the entry in the
+	//   directory containing the file has also reached disk. For that an
+	//   explicit fsync() on a file descriptor for the directory is also needed.
+	f, err := os.Open(name)
+	if err != nil {
+		return err
+	}
+	defer f.Close()
+	if err := f.Sync(); err != nil && !isErrInvalid(err) {
+		return err
+	}
+	return nil
+}
@moul
Copy link
Member

moul commented Aug 1, 2023

Addresses #1013

@ilgooz
Copy link
Contributor Author

ilgooz commented Aug 1, 2023

Should be solved in #1012.

@ilgooz ilgooz closed this as completed Aug 1, 2023
@moul moul moved this to 🌟 Wanted for Launch in 🚀 The Launch [DEPRECATED] Sep 8, 2023
@moul moul added this to the 🌟 main.gno.land (wanted) milestone Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🌐 tendermint v2 Issues or PRs tm2 related
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants