internal: add db operations to api (#24739)

This commit is contained in:
Daniel Liu 2024-12-17 15:31:54 +08:00
parent 69ea5327b8
commit d6d6906881
3 changed files with 26 additions and 0 deletions

View file

@ -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.
@ -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) {

View file

@ -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

View file

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