Skip to content

Commit d73f165

Browse files
holimancp-wjhan
authored andcommitted
ethdb/remotedb, cmd: add support for remote (readonly) databases (ethereum#24836)
* ethdb/remotedb, cmd: add support for remote (readonly) databases * ethdb/remotedb: minor changes * ethdb/remotedb: close the conn * cmd, ethdb: add rpc accessor for ancient data * internal/ethapi: license * ethdb/remotedb: linter fixes
1 parent edb1f1d commit d73f165

File tree

5 files changed

+240
-15
lines changed

5 files changed

+240
-15
lines changed

cmd/utils/flags.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949
"github.com/ethereum/go-ethereum/eth/gasprice"
5050
"github.com/ethereum/go-ethereum/eth/tracers"
5151
"github.com/ethereum/go-ethereum/ethdb"
52+
"github.com/ethereum/go-ethereum/ethdb/remotedb"
5253
"github.com/ethereum/go-ethereum/ethstats"
5354
"github.com/ethereum/go-ethereum/graphql"
5455
"github.com/ethereum/go-ethereum/internal/ethapi"
@@ -113,6 +114,10 @@ var (
113114
Usage: "Data directory for the databases and keystore",
114115
Value: DirectoryString(node.DefaultDataDir()),
115116
}
117+
RemoteDBFlag = cli.StringFlag{
118+
Name: "remotedb",
119+
Usage: "URL for remote database",
120+
}
116121
AncientFlag = DirectoryFlag{
117122
Name: "datadir.ancient",
118123
Usage: "Data directory for ancient chain segments (default = inside chaindata)",
@@ -927,6 +932,7 @@ var (
927932
DatabasePathFlags = []cli.Flag{
928933
DataDirFlag,
929934
AncientFlag,
935+
RemoteDBFlag,
930936
}
931937
)
932938

@@ -2113,12 +2119,14 @@ func MakeChainDatabase(ctx *cli.Context, stack *node.Node, readonly bool) ethdb.
21132119
err error
21142120
chainDb ethdb.Database
21152121
)
2116-
if ctx.GlobalString(SyncModeFlag.Name) == "light" {
2117-
name := "lightchaindata"
2118-
chainDb, err = stack.OpenDatabase(name, cache, handles, "", readonly)
2119-
} else {
2120-
name := "chaindata"
2121-
chainDb, err = stack.OpenDatabaseWithFreezer(name, cache, handles, ctx.GlobalString(AncientFlag.Name), "", readonly)
2122+
switch {
2123+
case ctx.GlobalIsSet(RemoteDBFlag.Name):
2124+
log.Info("Using remote db", "url", ctx.GlobalString(RemoteDBFlag.Name))
2125+
chainDb, err = remotedb.New(ctx.GlobalString(RemoteDBFlag.Name))
2126+
case ctx.GlobalString(SyncModeFlag.Name) == "light":
2127+
chainDb, err = stack.OpenDatabase("lightchaindata", cache, handles, "", readonly)
2128+
default:
2129+
chainDb, err = stack.OpenDatabaseWithFreezer("chaindata", cache, handles, ctx.GlobalString(AncientFlag.Name), "", readonly)
21222130
}
21232131
if err != nil {
21242132
Fatalf("Could not open database: %v", err)

ethdb/remotedb/remotedb.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// Copyright 2022 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 remotedb implements the key-value database layer based on a remote geth
18+
// node. Under the hood, it utilises the `debug_dbGet` method to implement a
19+
// read-only database.
20+
// There really are no guarantees in this database, since the local geth does not
21+
// exclusive access, but it can be used for basic diagnostics of a remote node.
22+
package remotedb
23+
24+
import (
25+
"errors"
26+
"strings"
27+
28+
"github.com/ethereum/go-ethereum/common/hexutil"
29+
"github.com/ethereum/go-ethereum/ethdb"
30+
"github.com/ethereum/go-ethereum/rpc"
31+
)
32+
33+
// Database is a key-value lookup for a remote database via debug_dbGet.
34+
type Database struct {
35+
remote *rpc.Client
36+
}
37+
38+
func (db *Database) Has(key []byte) (bool, error) {
39+
if _, err := db.Get(key); err != nil {
40+
return true, nil
41+
}
42+
return false, nil
43+
}
44+
45+
func (db *Database) Get(key []byte) ([]byte, error) {
46+
var resp hexutil.Bytes
47+
err := db.remote.Call(&resp, "debug_dbGet", hexutil.Bytes(key))
48+
if err != nil {
49+
return nil, err
50+
}
51+
return resp, nil
52+
}
53+
54+
func (db *Database) HasAncient(kind string, number uint64) (bool, error) {
55+
if _, err := db.Ancient(kind, number); err != nil {
56+
return true, nil
57+
}
58+
return false, nil
59+
}
60+
61+
func (db *Database) Ancient(kind string, number uint64) ([]byte, error) {
62+
var resp hexutil.Bytes
63+
err := db.remote.Call(&resp, "debug_dbAncient", kind, number)
64+
if err != nil {
65+
return nil, err
66+
}
67+
return resp, nil
68+
}
69+
70+
func (db *Database) AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) {
71+
panic("not supported")
72+
}
73+
74+
func (db *Database) Ancients() (uint64, error) {
75+
var resp uint64
76+
err := db.remote.Call(&resp, "debug_dbAncients")
77+
return resp, err
78+
}
79+
80+
func (db *Database) Tail() (uint64, error) {
81+
panic("not supported")
82+
}
83+
84+
func (db *Database) AncientSize(kind string) (uint64, error) {
85+
panic("not supported")
86+
}
87+
88+
func (db *Database) ReadAncients(fn func(op ethdb.AncientReaderOp) error) (err error) {
89+
return fn(db)
90+
}
91+
92+
func (db *Database) Put(key []byte, value []byte) error {
93+
panic("not supported")
94+
}
95+
96+
func (db *Database) Delete(key []byte) error {
97+
panic("not supported")
98+
}
99+
100+
func (db *Database) ModifyAncients(f func(ethdb.AncientWriteOp) error) (int64, error) {
101+
panic("not supported")
102+
}
103+
104+
func (db *Database) TruncateHead(n uint64) error {
105+
panic("not supported")
106+
}
107+
108+
func (db *Database) TruncateTail(n uint64) error {
109+
panic("not supported")
110+
}
111+
112+
func (db *Database) Sync() error {
113+
return nil
114+
}
115+
116+
func (db *Database) MigrateTable(s string, f func([]byte) ([]byte, error)) error {
117+
panic("not supported")
118+
}
119+
120+
func (db *Database) NewBatch() ethdb.Batch {
121+
panic("not supported")
122+
}
123+
124+
func (db *Database) NewBatchWithSize(size int) ethdb.Batch {
125+
panic("not supported")
126+
}
127+
128+
func (db *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
129+
panic("not supported")
130+
}
131+
132+
func (db *Database) Stat(property string) (string, error) {
133+
panic("not supported")
134+
}
135+
136+
func (db *Database) AncientDatadir() (string, error) {
137+
panic("not supported")
138+
}
139+
140+
func (db *Database) Compact(start []byte, limit []byte) error {
141+
return nil
142+
}
143+
144+
func (db *Database) NewSnapshot() (ethdb.Snapshot, error) {
145+
panic("not supported")
146+
}
147+
148+
func (db *Database) Close() error {
149+
db.remote.Close()
150+
return nil
151+
}
152+
153+
func dialRPC(endpoint string) (*rpc.Client, error) {
154+
if endpoint == "" {
155+
return nil, errors.New("endpoint must be specified")
156+
}
157+
if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
158+
// Backwards compatibility with geth < 1.5 which required
159+
// these prefixes.
160+
endpoint = endpoint[4:]
161+
}
162+
return rpc.Dial(endpoint)
163+
}
164+
165+
func New(endpoint string) (ethdb.Database, error) {
166+
client, err := dialRPC(endpoint)
167+
if err != nil {
168+
return nil, err
169+
}
170+
return &Database{
171+
remote: client,
172+
}, nil
173+
}

internal/ethapi/api.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,15 +2036,6 @@ func (api *PrivateDebugAPI) SetHead(number hexutil.Uint64) {
20362036
api.b.SetHead(uint64(number))
20372037
}
20382038

2039-
// DbGet returns the raw value of a key stored in the database.
2040-
func (api *PrivateDebugAPI) DbGet(key string) (hexutil.Bytes, error) {
2041-
blob, err := common.ParseHexOrString(key)
2042-
if err != nil {
2043-
return nil, err
2044-
}
2045-
return api.b.ChainDb().Get(blob)
2046-
}
2047-
20482039
// PublicNetAPI offers network related RPC methods
20492040
type PublicNetAPI struct {
20502041
net *p2p.Server

internal/ethapi/dbapi.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2022 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 ethapi
18+
19+
import (
20+
"github.com/ethereum/go-ethereum/common"
21+
"github.com/ethereum/go-ethereum/common/hexutil"
22+
)
23+
24+
// DbGet returns the raw value of a key stored in the database.
25+
func (api *PrivateDebugAPI) DbGet(key string) (hexutil.Bytes, error) {
26+
blob, err := common.ParseHexOrString(key)
27+
if err != nil {
28+
return nil, err
29+
}
30+
return api.b.ChainDb().Get(blob)
31+
}
32+
33+
// DbAncient retrieves an ancient binary blob from the append-only immutable files.
34+
// It is a mapping to the `AncientReaderOp.Ancient` method
35+
func (api *PrivateDebugAPI) DbAncient(kind string, number uint64) (hexutil.Bytes, error) {
36+
return api.b.ChainDb().Ancient(kind, number)
37+
}
38+
39+
// DbAncients returns the ancient item numbers in the ancient store.
40+
// It is a mapping to the `AncientReaderOp.Ancients` method
41+
func (api *PrivateDebugAPI) DbAncients() (uint64, error) {
42+
return api.b.ChainDb().Ancients()
43+
}

internal/web3ext/web3ext.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,16 @@ web3._extend({
564564
call: 'debug_dbGet',
565565
params: 1
566566
}),
567+
new web3._extend.Method({
568+
name: 'dbAncient',
569+
call: 'debug_dbAncient',
570+
params: 2
571+
}),
572+
new web3._extend.Method({
573+
name: 'dbAncients',
574+
call: 'debug_dbAncients',
575+
params: 0
576+
}),
567577
],
568578
properties: []
569579
});

0 commit comments

Comments
 (0)