Skip to content

Commit 21e878b

Browse files
s1najagdeep sidhu
authored andcommitted
internal/ethapi: add db operations to api (ethereum#24739)
Adds `debug_dbGet` method to rpc api
1 parent e465963 commit 21e878b

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"
@@ -434,7 +433,7 @@ func dbGet(ctx *cli.Context) error {
434433
db := utils.MakeChainDatabase(ctx, stack, true)
435434
defer db.Close()
436435

437-
key, err := parseHexOrString(ctx.Args().Get(0))
436+
key, err := common.ParseHexOrString(ctx.Args().Get(0))
438437
if err != nil {
439438
log.Info("Could not decode the key", "error", err)
440439
return err
@@ -460,7 +459,7 @@ func dbDelete(ctx *cli.Context) error {
460459
db := utils.MakeChainDatabase(ctx, stack, false)
461460
defer db.Close()
462461

463-
key, err := parseHexOrString(ctx.Args().Get(0))
462+
key, err := common.ParseHexOrString(ctx.Args().Get(0))
464463
if err != nil {
465464
log.Info("Could not decode the key", "error", err)
466465
return err
@@ -493,7 +492,7 @@ func dbPut(ctx *cli.Context) error {
493492
data []byte
494493
err error
495494
)
496-
key, err = parseHexOrString(ctx.Args().Get(0))
495+
key, err = common.ParseHexOrString(ctx.Args().Get(0))
497496
if err != nil {
498497
log.Info("Could not decode the key", "error", err)
499498
return err
@@ -600,15 +599,6 @@ func freezerInspect(ctx *cli.Context) error {
600599
return nil
601600
}
602601

603-
// ParseHexOrString tries to hexdecode b, but if the prefix is missing, it instead just returns the raw bytes
604-
func parseHexOrString(str string) ([]byte, error) {
605-
b, err := hexutil.Decode(str)
606-
if errors.Is(err, hexutil.ErrMissingPrefix) {
607-
return []byte(str), nil
608-
}
609-
return b, err
610-
}
611-
612602
func importLDBdata(ctx *cli.Context) error {
613603
start := 0
614604
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)