Skip to content

Commit 6a7695e

Browse files
dimchanskyfjl
authored andcommitted
ethdb, rpc: support building on js/wasm (#17709)
The changes allow building WebAssembly applications which use ethclient.Client.
1 parent 16e4d0e commit 6a7695e

7 files changed

Lines changed: 236 additions & 68 deletions

File tree

‎ethdb/database.go‎

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17+
// +build !js
18+
1719
package ethdb
1820

1921
import (
@@ -380,71 +382,3 @@ func (b *ldbBatch) Reset() {
380382
b.b.Reset()
381383
b.size = 0
382384
}
383-
384-
type table struct {
385-
db Database
386-
prefix string
387-
}
388-
389-
// NewTable returns a Database object that prefixes all keys with a given
390-
// string.
391-
func NewTable(db Database, prefix string) Database {
392-
return &table{
393-
db: db,
394-
prefix: prefix,
395-
}
396-
}
397-
398-
func (dt *table) Put(key []byte, value []byte) error {
399-
return dt.db.Put(append([]byte(dt.prefix), key...), value)
400-
}
401-
402-
func (dt *table) Has(key []byte) (bool, error) {
403-
return dt.db.Has(append([]byte(dt.prefix), key...))
404-
}
405-
406-
func (dt *table) Get(key []byte) ([]byte, error) {
407-
return dt.db.Get(append([]byte(dt.prefix), key...))
408-
}
409-
410-
func (dt *table) Delete(key []byte) error {
411-
return dt.db.Delete(append([]byte(dt.prefix), key...))
412-
}
413-
414-
func (dt *table) Close() {
415-
// Do nothing; don't close the underlying DB.
416-
}
417-
418-
type tableBatch struct {
419-
batch Batch
420-
prefix string
421-
}
422-
423-
// NewTableBatch returns a Batch object which prefixes all keys with a given string.
424-
func NewTableBatch(db Database, prefix string) Batch {
425-
return &tableBatch{db.NewBatch(), prefix}
426-
}
427-
428-
func (dt *table) NewBatch() Batch {
429-
return &tableBatch{dt.db.NewBatch(), dt.prefix}
430-
}
431-
432-
func (tb *tableBatch) Put(key, value []byte) error {
433-
return tb.batch.Put(append([]byte(tb.prefix), key...), value)
434-
}
435-
436-
func (tb *tableBatch) Delete(key []byte) error {
437-
return tb.batch.Delete(append([]byte(tb.prefix), key...))
438-
}
439-
440-
func (tb *tableBatch) Write() error {
441-
return tb.batch.Write()
442-
}
443-
444-
func (tb *tableBatch) ValueSize() int {
445-
return tb.batch.ValueSize()
446-
}
447-
448-
func (tb *tableBatch) Reset() {
449-
tb.batch.Reset()
450-
}

‎ethdb/database_js.go‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2014 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// +build js
18+
19+
package ethdb
20+
21+
import (
22+
"errors"
23+
)
24+
25+
var errNotSupported = errors.New("ethdb: not supported")
26+
27+
type LDBDatabase struct {
28+
}
29+
30+
// NewLDBDatabase returns a LevelDB wrapped object.
31+
func NewLDBDatabase(file string, cache int, handles int) (*LDBDatabase, error) {
32+
return nil, errNotSupported
33+
}
34+
35+
// Path returns the path to the database directory.
36+
func (db *LDBDatabase) Path() string {
37+
return ""
38+
}
39+
40+
// Put puts the given key / value to the queue
41+
func (db *LDBDatabase) Put(key []byte, value []byte) error {
42+
return errNotSupported
43+
}
44+
45+
func (db *LDBDatabase) Has(key []byte) (bool, error) {
46+
return false, errNotSupported
47+
}
48+
49+
// Get returns the given key if it's present.
50+
func (db *LDBDatabase) Get(key []byte) ([]byte, error) {
51+
return nil, errNotSupported
52+
}
53+
54+
// Delete deletes the key from the queue and database
55+
func (db *LDBDatabase) Delete(key []byte) error {
56+
return errNotSupported
57+
}
58+
59+
func (db *LDBDatabase) Close() {
60+
}
61+
62+
// Meter configures the database metrics collectors and
63+
func (db *LDBDatabase) Meter(prefix string) {
64+
}
65+
66+
func (db *LDBDatabase) NewBatch() Batch {
67+
return nil
68+
}

‎ethdb/database_js_test.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2014 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// +build js
18+
19+
package ethdb_test
20+
21+
import (
22+
"github.com/ethereum/go-ethereum/ethdb"
23+
)
24+
25+
var _ ethdb.Database = &ethdb.LDBDatabase{}

‎ethdb/database_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17+
// +build !js
18+
1719
package ethdb_test
1820

1921
import (

‎ethdb/table.go‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2014 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package ethdb
18+
19+
type table struct {
20+
db Database
21+
prefix string
22+
}
23+
24+
// NewTable returns a Database object that prefixes all keys with a given
25+
// string.
26+
func NewTable(db Database, prefix string) Database {
27+
return &table{
28+
db: db,
29+
prefix: prefix,
30+
}
31+
}
32+
33+
func (dt *table) Put(key []byte, value []byte) error {
34+
return dt.db.Put(append([]byte(dt.prefix), key...), value)
35+
}
36+
37+
func (dt *table) Has(key []byte) (bool, error) {
38+
return dt.db.Has(append([]byte(dt.prefix), key...))
39+
}
40+
41+
func (dt *table) Get(key []byte) ([]byte, error) {
42+
return dt.db.Get(append([]byte(dt.prefix), key...))
43+
}
44+
45+
func (dt *table) Delete(key []byte) error {
46+
return dt.db.Delete(append([]byte(dt.prefix), key...))
47+
}
48+
49+
func (dt *table) Close() {
50+
// Do nothing; don't close the underlying DB.
51+
}

‎ethdb/table_batch.go‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2014 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package ethdb
18+
19+
type tableBatch struct {
20+
batch Batch
21+
prefix string
22+
}
23+
24+
// NewTableBatch returns a Batch object which prefixes all keys with a given string.
25+
func NewTableBatch(db Database, prefix string) Batch {
26+
return &tableBatch{db.NewBatch(), prefix}
27+
}
28+
29+
func (dt *table) NewBatch() Batch {
30+
return &tableBatch{dt.db.NewBatch(), dt.prefix}
31+
}
32+
33+
func (tb *tableBatch) Put(key, value []byte) error {
34+
return tb.batch.Put(append([]byte(tb.prefix), key...), value)
35+
}
36+
37+
func (tb *tableBatch) Delete(key []byte) error {
38+
return tb.batch.Delete(append([]byte(tb.prefix), key...))
39+
}
40+
41+
func (tb *tableBatch) Write() error {
42+
return tb.batch.Write()
43+
}
44+
45+
func (tb *tableBatch) ValueSize() int {
46+
return tb.batch.ValueSize()
47+
}
48+
49+
func (tb *tableBatch) Reset() {
50+
tb.batch.Reset()
51+
}

‎rpc/ipc_js.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2015 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// +build js
18+
19+
package rpc
20+
21+
import (
22+
"context"
23+
"errors"
24+
"net"
25+
)
26+
27+
var errNotSupported = errors.New("rpc: not supported")
28+
29+
// ipcListen will create a named pipe on the given endpoint.
30+
func ipcListen(endpoint string) (net.Listener, error) {
31+
return nil, errNotSupported
32+
}
33+
34+
// newIPCConnection will connect to a named pipe with the given endpoint as name.
35+
func newIPCConnection(ctx context.Context, endpoint string) (net.Conn, error) {
36+
return nil, errNotSupported
37+
}

0 commit comments

Comments
 (0)