internal/ethapi: add db operations to api (#24739)

Adds `debug_dbGet` method to rpc api
pull/24776/head
Sina Mahmoodi 2 years ago committed by GitHub
parent a52bcccfe1
commit 16701c5169
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      cmd/geth/dbcmd.go
  2. 12
      common/bytes.go
  3. 9
      internal/ethapi/api.go
  4. 5
      internal/web3ext/web3ext.go

@ -18,7 +18,6 @@ package main
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
@ -418,7 +417,7 @@ func dbGet(ctx *cli.Context) error {
db := utils.MakeChainDatabase(ctx, stack, true) db := utils.MakeChainDatabase(ctx, stack, true)
defer db.Close() defer db.Close()
key, err := parseHexOrString(ctx.Args().Get(0)) key, err := common.ParseHexOrString(ctx.Args().Get(0))
if err != nil { if err != nil {
log.Info("Could not decode the key", "error", err) log.Info("Could not decode the key", "error", err)
return err return err
@ -444,7 +443,7 @@ func dbDelete(ctx *cli.Context) error {
db := utils.MakeChainDatabase(ctx, stack, false) db := utils.MakeChainDatabase(ctx, stack, false)
defer db.Close() defer db.Close()
key, err := parseHexOrString(ctx.Args().Get(0)) key, err := common.ParseHexOrString(ctx.Args().Get(0))
if err != nil { if err != nil {
log.Info("Could not decode the key", "error", err) log.Info("Could not decode the key", "error", err)
return err return err
@ -477,7 +476,7 @@ func dbPut(ctx *cli.Context) error {
data []byte data []byte
err error err error
) )
key, err = parseHexOrString(ctx.Args().Get(0)) key, err = common.ParseHexOrString(ctx.Args().Get(0))
if err != nil { if err != nil {
log.Info("Could not decode the key", "error", err) log.Info("Could not decode the key", "error", err)
return err return err
@ -584,15 +583,6 @@ func freezerInspect(ctx *cli.Context) error {
return nil return nil
} }
// ParseHexOrString tries to hexdecode b, but if the prefix is missing, it instead just returns the raw bytes
func parseHexOrString(str string) ([]byte, error) {
b, err := hexutil.Decode(str)
if errors.Is(err, hexutil.ErrMissingPrefix) {
return []byte(str), nil
}
return b, err
}
func importLDBdata(ctx *cli.Context) error { func importLDBdata(ctx *cli.Context) error {
start := 0 start := 0
switch ctx.NArg() { switch ctx.NArg() {

@ -19,6 +19,9 @@ package common
import ( import (
"encoding/hex" "encoding/hex"
"errors"
"github.com/ethereum/go-ethereum/common/hexutil"
) )
// FromHex returns the bytes represented by the hexadecimal string s. // FromHex returns the bytes represented by the hexadecimal string s.
@ -92,6 +95,15 @@ func Hex2BytesFixed(str string, flen int) []byte {
return hh return hh
} }
// ParseHexOrString tries to hexdecode b, but if the prefix is missing, it instead just returns the raw bytes
func ParseHexOrString(str string) ([]byte, error) {
b, err := hexutil.Decode(str)
if errors.Is(err, hexutil.ErrMissingPrefix) {
return []byte(str), nil
}
return b, err
}
// RightPadBytes zero-pads slice to the right up to length l. // RightPadBytes zero-pads slice to the right up to length l.
func RightPadBytes(slice []byte, l int) []byte { func RightPadBytes(slice []byte, l int) []byte {
if l <= len(slice) { if l <= len(slice) {

@ -1972,6 +1972,15 @@ func (api *PrivateDebugAPI) SetHead(number hexutil.Uint64) {
api.b.SetHead(uint64(number)) api.b.SetHead(uint64(number))
} }
// DbGet returns the raw value of a key stored in the database.
func (api *PrivateDebugAPI) DbGet(key string) (hexutil.Bytes, error) {
blob, err := common.ParseHexOrString(key)
if err != nil {
return nil, err
}
return api.b.ChainDb().Get(blob)
}
// PublicNetAPI offers network related RPC methods // PublicNetAPI offers network related RPC methods
type PublicNetAPI struct { type PublicNetAPI struct {
net *p2p.Server net *p2p.Server

@ -471,6 +471,11 @@ web3._extend({
params: 2, params: 2,
inputFormatter:[web3._extend.formatters.inputBlockNumberFormatter, web3._extend.formatters.inputBlockNumberFormatter], inputFormatter:[web3._extend.formatters.inputBlockNumberFormatter, web3._extend.formatters.inputBlockNumberFormatter],
}), }),
new web3._extend.Method({
name: 'dbGet',
call: 'debug_dbGet',
params: 1
}),
], ],
properties: [] properties: []
}); });

Loading…
Cancel
Save