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

feat: make gnovm wasm-friendly #1012

Merged
merged 21 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions .github/workflows/gnovm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ jobs:
strategy:
fail-fast: false
matrix:
go-version: [ "1.19.x", "1.20.x" ]
goarch: [ "amd64" ]
goos: [ "linux" ]
program: [ "gno" ]
go-version: # two latest versions
- "1.19.x"
- "1.20.x"
goenv: # TODO: replace with pairs, so it's easier to read in the GH interface.
- "GOARCH=amd64 GOOS=linux"
- "GOARCH=wasm GOOS=js"
program:
- "gno"
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
Expand All @@ -36,7 +40,7 @@ jobs:
- uses: actions/checkout@v3
- name: go install
working-directory: gnovm
run: GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go install ./cmd/${{ matrix.program }}
run: ${{ matrix.goenv }} go install ./cmd/${{ matrix.program }}

test:
strategy:
Expand Down
19 changes: 7 additions & 12 deletions tm2/pkg/autofile/autofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package autofile

import (
"os"
"os/signal"
"sync"
"syscall"
"time"

"github.com/gnolang/gno/tm2/pkg/random"
Expand Down Expand Up @@ -63,19 +61,16 @@ func OpenAutoFile(path string) (*AutoFile, error) {
closeTicker: time.NewTicker(autoFileClosePeriod),
closeTickerStopc: make(chan struct{}),
}

if err := af.openFile(); err != nil {
af.Close()
return nil, err
}

// Close file on SIGHUP.
af.hupc = make(chan os.Signal, 1)
signal.Notify(af.hupc, syscall.SIGHUP)
go func() {
for range af.hupc {
af.closeFile()
}
}()
// see autofile_*.go implementations, following different build constraints.
if err := af.setupCloseHandler(); err != nil {
return nil, err
}

go af.closeFileRoutine()

Expand Down Expand Up @@ -158,10 +153,10 @@ func (af *AutoFile) openFile() error {
}
// fileInfo, err := file.Stat()
// if err != nil {
// return err
// return err
// }
// if fileInfo.Mode() != autoFilePerms {
// return errors.NewErrPermissionsChanged(file.Name(), fileInfo.Mode(), autoFilePerms)
// return errors.NewErrPermissionsChanged(file.Name(), fileInfo.Mode(), autoFilePerms)
// }
af.file = file
return nil
Expand Down
21 changes: 21 additions & 0 deletions tm2/pkg/autofile/autofile_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build !js || !wasm

package autofile

import (
"os"
"os/signal"
"syscall"
)

func (af *AutoFile) setupCloseHandler() error {
// Close file on SIGHUP.
af.hupc = make(chan os.Signal, 1)
signal.Notify(af.hupc, syscall.SIGHUP)
go func() {
for range af.hupc {
af.closeFile()
}
}()
return nil
}
7 changes: 7 additions & 0 deletions tm2/pkg/autofile/autofile_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build js && wasm

moul marked this conversation as resolved.
Show resolved Hide resolved
package autofile

func (af *AutoFile) setupCloseHandler() error {
return nil
}
4 changes: 2 additions & 2 deletions tm2/pkg/bft/abci/example/kvstore/persistent_kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/gnolang/gno/tm2/pkg/bft/abci/example/errors"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
"github.com/gnolang/gno/tm2/pkg/crypto"
dbm "github.com/gnolang/gno/tm2/pkg/db"
"github.com/gnolang/gno/tm2/pkg/db"
"github.com/gnolang/gno/tm2/pkg/log"
)

Expand All @@ -34,7 +34,7 @@ type PersistentKVStoreApplication struct {

func NewPersistentKVStoreApplication(dbDir string) *PersistentKVStoreApplication {
name := "kvstore"
db, err := dbm.NewGoLevelDB(name, dbDir)
db, err := db.NewDB(name, db.CLevelDBBackend, dbDir)
if err != nil {
panic(err)
}
Expand Down
46 changes: 23 additions & 23 deletions tm2/pkg/crypto/keys/lazy_keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/crypto/hd"
dbm "github.com/gnolang/gno/tm2/pkg/db"
"github.com/gnolang/gno/tm2/pkg/db"
"github.com/gnolang/gno/tm2/pkg/os"
)

Expand All @@ -26,7 +26,7 @@ func NewLazyDBKeybase(name, dir string) Keybase {
}

func (lkb lazyKeybase) List() ([]Info, error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return nil, err
}
Expand All @@ -36,7 +36,7 @@ func (lkb lazyKeybase) List() ([]Info, error) {
}

func (lkb lazyKeybase) GetByNameOrAddress(nameOrBech32 string) (Info, error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return nil, err
}
Expand All @@ -46,7 +46,7 @@ func (lkb lazyKeybase) GetByNameOrAddress(nameOrBech32 string) (Info, error) {
}

func (lkb lazyKeybase) GetByName(name string) (Info, error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return nil, err
}
Expand All @@ -56,7 +56,7 @@ func (lkb lazyKeybase) GetByName(name string) (Info, error) {
}

func (lkb lazyKeybase) GetByAddress(address crypto.Address) (Info, error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return nil, err
}
Expand All @@ -66,7 +66,7 @@ func (lkb lazyKeybase) GetByAddress(address crypto.Address) (Info, error) {
}

func (lkb lazyKeybase) Delete(name, passphrase string, skipPass bool) error {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return err
}
Expand All @@ -76,7 +76,7 @@ func (lkb lazyKeybase) Delete(name, passphrase string, skipPass bool) error {
}

func (lkb lazyKeybase) Sign(name, passphrase string, msg []byte) ([]byte, crypto.PubKey, error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return nil, nil, err
}
Expand All @@ -86,7 +86,7 @@ func (lkb lazyKeybase) Sign(name, passphrase string, msg []byte) ([]byte, crypto
}

func (lkb lazyKeybase) Verify(name string, msg, sig []byte) error {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return err
}
Expand All @@ -96,7 +96,7 @@ func (lkb lazyKeybase) Verify(name string, msg, sig []byte) error {
}

func (lkb lazyKeybase) CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd string, account uint32, index uint32) (Info, error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return nil, err
}
Expand All @@ -106,7 +106,7 @@ func (lkb lazyKeybase) CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd
}

func (lkb lazyKeybase) CreateAccountBip44(name, mnemonic, bip39Passwd, encryptPasswd string, params hd.BIP44Params) (Info, error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return nil, err
}
Expand All @@ -116,7 +116,7 @@ func (lkb lazyKeybase) CreateAccountBip44(name, mnemonic, bip39Passwd, encryptPa
}

func (lkb lazyKeybase) CreateLedger(name string, algo SigningAlgo, hrp string, account, index uint32) (info Info, err error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return nil, err
}
Expand All @@ -126,7 +126,7 @@ func (lkb lazyKeybase) CreateLedger(name string, algo SigningAlgo, hrp string, a
}

func (lkb lazyKeybase) CreateOffline(name string, pubkey crypto.PubKey) (info Info, err error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return nil, err
}
Expand All @@ -136,7 +136,7 @@ func (lkb lazyKeybase) CreateOffline(name string, pubkey crypto.PubKey) (info In
}

func (lkb lazyKeybase) CreateMulti(name string, pubkey crypto.PubKey) (info Info, err error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return nil, err
}
Expand All @@ -146,7 +146,7 @@ func (lkb lazyKeybase) CreateMulti(name string, pubkey crypto.PubKey) (info Info
}

func (lkb lazyKeybase) Update(name, oldpass string, getNewpass func() (string, error)) error {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return err
}
Expand All @@ -156,7 +156,7 @@ func (lkb lazyKeybase) Update(name, oldpass string, getNewpass func() (string, e
}

func (lkb lazyKeybase) Import(name string, armor string) (err error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return err
}
Expand All @@ -171,7 +171,7 @@ func (lkb lazyKeybase) ImportPrivKey(
decryptPassphrase,
encryptPassphrase string,
) error {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return err
}
Expand All @@ -185,7 +185,7 @@ func (lkb lazyKeybase) ImportPrivKeyUnsafe(
armor string,
encryptPassphrase string,
) error {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return err
}
Expand All @@ -195,7 +195,7 @@ func (lkb lazyKeybase) ImportPrivKeyUnsafe(
}

func (lkb lazyKeybase) ImportPubKey(name string, armor string) (err error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return err
}
Expand All @@ -205,7 +205,7 @@ func (lkb lazyKeybase) ImportPubKey(name string, armor string) (err error) {
}

func (lkb lazyKeybase) Export(name string) (armor string, err error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return "", err
}
Expand All @@ -215,7 +215,7 @@ func (lkb lazyKeybase) Export(name string) (armor string, err error) {
}

func (lkb lazyKeybase) ExportPubKey(name string) (armor string, err error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return "", err
}
Expand All @@ -225,7 +225,7 @@ func (lkb lazyKeybase) ExportPubKey(name string) (armor string, err error) {
}

func (lkb lazyKeybase) ExportPrivateKeyObject(name string, passphrase string) (crypto.PrivKey, error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return nil, err
}
Expand All @@ -237,7 +237,7 @@ func (lkb lazyKeybase) ExportPrivateKeyObject(name string, passphrase string) (c
func (lkb lazyKeybase) ExportPrivKey(name string, decryptPassphrase string,
encryptPassphrase string,
) (armor string, err error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return "", err
}
Expand All @@ -247,7 +247,7 @@ func (lkb lazyKeybase) ExportPrivKey(name string, decryptPassphrase string,
}

func (lkb lazyKeybase) ExportPrivKeyUnsafe(name string, decryptPassphrase string) (string, error) {
db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir)
db, err := db.NewDB(lkb.name, db.CLevelDBBackend, lkb.dir)
if err != nil {
return "", err
}
Expand Down
2 changes: 2 additions & 0 deletions tm2/pkg/db/go_level_db.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !wasm || !js

package db

import (
Expand Down
8 changes: 0 additions & 8 deletions tm2/pkg/iavl/nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,6 @@ func (ndb *nodeDB) SaveNode(node *Node) {
// Has checks if a hash exists in the database.
func (ndb *nodeDB) Has(hash []byte) bool {
key := ndb.nodeKey(hash)

if ldb, ok := ndb.db.(*dbm.GoLevelDB); ok {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @jaekwon, something seems off here. GoLevelDB.Has() is essentially checking if GoLevelDB.Get() is not nil, making this condition alter the function's behavior to panic instead of returning false, specifically when using a particular driver.

Would you be alright with removing this code, or do you have a different approach in mind?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR should be ready after resolving this.

exists, err := ldb.DB().Has(key, nil)
if err != nil {
panic("Got error from leveldb: " + err.Error())
}
return exists
}
return ndb.db.Get(key) != nil
}

Expand Down