Skip to content

Commit

Permalink
internal: add db operations to api (ethereum#24739)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Dec 21, 2024
1 parent 6cade4c commit b95d00d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions common/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package common

import (
"encoding/hex"
"errors"

"github.com/XinFinOrg/XDPoSChain/common/hexutil"
)

// FromHex returns the bytes represented by the hexadecimal string s.
Expand Down Expand Up @@ -99,6 +102,15 @@ func Hex2BytesFixed(str string, flen int) []byte {
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.
func RightPadBytes(slice []byte, l int) []byte {
if l <= len(slice) {
Expand Down
9 changes: 9 additions & 0 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3499,6 +3499,15 @@ func (api *PrivateDebugAPI) SetHead(number hexutil.Uint64) {
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
type PublicNetAPI struct {
net *p2p.Server
Expand Down
5 changes: 5 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ web3._extend({
params: 2,
inputFormatter:[null, null],
}),
new web3._extend.Method({
name: 'dbGet',
call: 'debug_dbGet',
params: 1
}),
],
properties: []
});
Expand Down

0 comments on commit b95d00d

Please sign in to comment.