Skip to content

Commit 16701c5

Browse files
authored
internal/ethapi: add db operations to api (ethereum#24739)
Adds `debug_dbGet` method to rpc api
1 parent a52bccc commit 16701c5

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

cmd/geth/dbcmd.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package main
1818

1919
import (
2020
"bytes"
21-
"errors"
2221
"fmt"
2322
"os"
2423
"os/signal"
@@ -418,7 +417,7 @@ func dbGet(ctx *cli.Context) error {
418417
db := utils.MakeChainDatabase(ctx, stack, true)
419418
defer db.Close()
420419

421-
key, err := parseHexOrString(ctx.Args().Get(0))
420+
key, err := common.ParseHexOrString(ctx.Args().Get(0))
422421
if err != nil {
423422
log.Info("Could not decode the key", "error", err)
424423
return err
@@ -444,7 +443,7 @@ func dbDelete(ctx *cli.Context) error {
444443
db := utils.MakeChainDatabase(ctx, stack, false)
445444
defer db.Close()
446445

447-
key, err := parseHexOrString(ctx.Args().Get(0))
446+
key, err := common.ParseHexOrString(ctx.Args().Get(0))
448447
if err != nil {
449448
log.Info("Could not decode the key", "error", err)
450449
return err
@@ -477,7 +476,7 @@ func dbPut(ctx *cli.Context) error {
477476
data []byte
478477
err error
479478
)
480-
key, err = parseHexOrString(ctx.Args().Get(0))
479+
key, err = common.ParseHexOrString(ctx.Args().Get(0))
481480
if err != nil {
482481
log.Info("Could not decode the key", "error", err)
483482
return err
@@ -584,15 +583,6 @@ func freezerInspect(ctx *cli.Context) error {
584583
return nil
585584
}
586585

587-
// ParseHexOrString tries to hexdecode b, but if the prefix is missing, it instead just returns the raw bytes
588-
func parseHexOrString(str string) ([]byte, error) {
589-
b, err := hexutil.Decode(str)
590-
if errors.Is(err, hexutil.ErrMissingPrefix) {
591-
return []byte(str), nil
592-
}
593-
return b, err
594-
}
595-
596586
func importLDBdata(ctx *cli.Context) error {
597587
start := 0
598588
switch ctx.NArg() {

common/bytes.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ package common
1919

2020
import (
2121
"encoding/hex"
22+
"errors"
23+
24+
"github.com/ethereum/go-ethereum/common/hexutil"
2225
)
2326

2427
// FromHex returns the bytes represented by the hexadecimal string s.
@@ -92,6 +95,15 @@ func Hex2BytesFixed(str string, flen int) []byte {
9295
return hh
9396
}
9497

98+
// ParseHexOrString tries to hexdecode b, but if the prefix is missing, it instead just returns the raw bytes
99+
func ParseHexOrString(str string) ([]byte, error) {
100+
b, err := hexutil.Decode(str)
101+
if errors.Is(err, hexutil.ErrMissingPrefix) {
102+
return []byte(str), nil
103+
}
104+
return b, err
105+
}
106+
95107
// RightPadBytes zero-pads slice to the right up to length l.
96108
func RightPadBytes(slice []byte, l int) []byte {
97109
if l <= len(slice) {

internal/ethapi/api.go

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

1975+
// DbGet returns the raw value of a key stored in the database.
1976+
func (api *PrivateDebugAPI) DbGet(key string) (hexutil.Bytes, error) {
1977+
blob, err := common.ParseHexOrString(key)
1978+
if err != nil {
1979+
return nil, err
1980+
}
1981+
return api.b.ChainDb().Get(blob)
1982+
}
1983+
19751984
// PublicNetAPI offers network related RPC methods
19761985
type PublicNetAPI struct {
19771986
net *p2p.Server

internal/web3ext/web3ext.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,11 @@ web3._extend({
471471
params: 2,
472472
inputFormatter:[web3._extend.formatters.inputBlockNumberFormatter, web3._extend.formatters.inputBlockNumberFormatter],
473473
}),
474+
new web3._extend.Method({
475+
name: 'dbGet',
476+
call: 'debug_dbGet',
477+
params: 1
478+
}),
474479
],
475480
properties: []
476481
});

0 commit comments

Comments
 (0)