From 158f54d8687faf2868328e5c4501a4401e070f0d Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Fri, 22 Apr 2022 12:01:32 +0200 Subject: [PATCH 1/6] internal: add db operations to api --- internal/ethapi/api.go | 25 +++++++++++++++++++++++++ internal/ethapi/backend.go | 5 +++++ internal/web3ext/web3ext.go | 26 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index d087673797dc..5ad7e5ff49aa 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1998,6 +1998,31 @@ func (s *PublicNetAPI) Version() string { return fmt.Sprintf("%d", s.networkVersion) } +// PublicDbAPI exposes low-level methods for interacting with the database. +type PublicDbAPI struct { + b Backend +} + +// NewPublicDbAPI creates a new instance of PublicDbAPI. +func NewPublicDbAPI(b Backend) *PublicDbAPI { + return &PublicDbAPI{b: b} +} + +// Get returns the raw value of a key stored in the database. +func (api *PublicDbAPI) Get(key hexutil.Bytes) (hexutil.Bytes, error) { + return api.b.ChainDb().Get(key) +} + +// Put stores a raw value under the key in the database. +func (api *PublicDbAPI) Put(key hexutil.Bytes, value hexutil.Bytes) error { + return api.b.ChainDb().Put(key, value) +} + +// Delete removes an item from the database. +func (api *PublicDbAPI) Delete(key hexutil.Bytes) error { + return api.b.ChainDb().Delete(key) +} + // checkTxFee is an internal function used to check whether the fee of // the given transaction is _reasonable_(under the cap). func checkTxFee(gasPrice *big.Int, gas uint64, cap float64) error { diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index bc60fb2a64f6..f85ef90e7a26 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -137,6 +137,11 @@ func GetAPIs(apiBackend Backend) []rpc.API { Version: "1.0", Service: NewPrivateAccountAPI(apiBackend, nonceLock), Public: false, + }, { + Namespace: "db", + Version: "1.0", + Service: NewPublicDbAPI(apiBackend), + Public: true, }, } } diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 87bf464157ba..b6a50df0d04f 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -30,6 +30,7 @@ var Modules = map[string]string{ "txpool": TxpoolJs, "les": LESJs, "vflux": VfluxJs, + "db": DbJS, } const CliqueJs = ` @@ -853,3 +854,28 @@ web3._extend({ ] }); ` + +const DbJS = ` +web3._extend({ + property: 'db', + methods: + [ + new web3._extend.Method({ + name: 'get', + call: 'db_get', + params: 1 + }), + new web3._extend.Method({ + name: 'put', + call: 'db_put', + params: 2 + }), + new web3._extend.Method({ + name: 'delete', + call: 'db_delete', + params: 1 + }), + ], + properties: [] +}); +` From f5294f318122ccddd5171e909206f862a4e7a556 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Fri, 22 Apr 2022 13:42:43 +0200 Subject: [PATCH 2/6] fix attachwelcome test --- cmd/geth/consolecmd_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/geth/consolecmd_test.go b/cmd/geth/consolecmd_test.go index e2923ed0933b..40d3f5614108 100644 --- a/cmd/geth/consolecmd_test.go +++ b/cmd/geth/consolecmd_test.go @@ -30,7 +30,7 @@ import ( ) const ( - ipcAPIs = "admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0" + ipcAPIs = "admin:1.0 db:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0" httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0" ) From 89b1ea2e7d3c7b8bb19d54e677cc36ddf56e7578 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Fri, 22 Apr 2022 14:01:26 +0200 Subject: [PATCH 3/6] review comments --- internal/ethapi/api.go | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 5ad7e5ff49aa..3e53de04c0c0 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -2009,18 +2009,38 @@ func NewPublicDbAPI(b Backend) *PublicDbAPI { } // Get returns the raw value of a key stored in the database. -func (api *PublicDbAPI) Get(key hexutil.Bytes) (hexutil.Bytes, error) { - return api.b.ChainDb().Get(key) +func (api *PublicDbAPI) Get(key string) (hexutil.Bytes, error) { + blob, err := parseHexOrString(key) + if err != nil { + return nil, err + } + return api.b.ChainDb().Get(blob) } // Put stores a raw value under the key in the database. -func (api *PublicDbAPI) Put(key hexutil.Bytes, value hexutil.Bytes) error { - return api.b.ChainDb().Put(key, value) +func (api *PublicDbAPI) Put(key string, value hexutil.Bytes) (hexutil.Bytes, error) { + var ( + ret []byte + db = api.b.ChainDb() + ) + blob, err := parseHexOrString(key) + if err != nil { + return nil, err + } + data, err := db.Get(blob) + if err == nil { + ret = data + } + return ret, db.Put(blob, value) } // Delete removes an item from the database. -func (api *PublicDbAPI) Delete(key hexutil.Bytes) error { - return api.b.ChainDb().Delete(key) +func (api *PublicDbAPI) Delete(key string) error { + blob, err := parseHexOrString(key) + if err != nil { + return err + } + return api.b.ChainDb().Delete(blob) } // checkTxFee is an internal function used to check whether the fee of @@ -2046,3 +2066,12 @@ func toHexSlice(b [][]byte) []string { } return r } + +// 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 +} From 9d234cec7096893688431ae27f059c2ed01443d1 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 26 Apr 2022 14:04:42 +0200 Subject: [PATCH 4/6] mv to debug, drop put and del --- cmd/geth/consolecmd_test.go | 2 +- internal/ethapi/api.go | 60 ++++--------------------------------- internal/ethapi/backend.go | 5 ---- internal/web3ext/web3ext.go | 31 ++++--------------- 4 files changed, 12 insertions(+), 86 deletions(-) diff --git a/cmd/geth/consolecmd_test.go b/cmd/geth/consolecmd_test.go index 40d3f5614108..e2923ed0933b 100644 --- a/cmd/geth/consolecmd_test.go +++ b/cmd/geth/consolecmd_test.go @@ -30,7 +30,7 @@ import ( ) const ( - ipcAPIs = "admin:1.0 db:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0" + ipcAPIs = "admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0" httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0" ) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 3e53de04c0c0..85fe0feb8a18 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1972,6 +1972,12 @@ func (api *PrivateDebugAPI) SetHead(number hexutil.Uint64) { api.b.SetHead(uint64(number)) } +// Get returns the raw value of a key stored in the database. +func (api *PrivateDebugAPI) DbGet(key string) (hexutil.Bytes, error) { + blob := common.FromHex(key) + return api.b.ChainDb().Get(blob) +} + // PublicNetAPI offers network related RPC methods type PublicNetAPI struct { net *p2p.Server @@ -1998,51 +2004,6 @@ func (s *PublicNetAPI) Version() string { return fmt.Sprintf("%d", s.networkVersion) } -// PublicDbAPI exposes low-level methods for interacting with the database. -type PublicDbAPI struct { - b Backend -} - -// NewPublicDbAPI creates a new instance of PublicDbAPI. -func NewPublicDbAPI(b Backend) *PublicDbAPI { - return &PublicDbAPI{b: b} -} - -// Get returns the raw value of a key stored in the database. -func (api *PublicDbAPI) Get(key string) (hexutil.Bytes, error) { - blob, err := parseHexOrString(key) - if err != nil { - return nil, err - } - return api.b.ChainDb().Get(blob) -} - -// Put stores a raw value under the key in the database. -func (api *PublicDbAPI) Put(key string, value hexutil.Bytes) (hexutil.Bytes, error) { - var ( - ret []byte - db = api.b.ChainDb() - ) - blob, err := parseHexOrString(key) - if err != nil { - return nil, err - } - data, err := db.Get(blob) - if err == nil { - ret = data - } - return ret, db.Put(blob, value) -} - -// Delete removes an item from the database. -func (api *PublicDbAPI) Delete(key string) error { - blob, err := parseHexOrString(key) - if err != nil { - return err - } - return api.b.ChainDb().Delete(blob) -} - // checkTxFee is an internal function used to check whether the fee of // the given transaction is _reasonable_(under the cap). func checkTxFee(gasPrice *big.Int, gas uint64, cap float64) error { @@ -2066,12 +2027,3 @@ func toHexSlice(b [][]byte) []string { } return r } - -// 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 -} diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index f85ef90e7a26..bc60fb2a64f6 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -137,11 +137,6 @@ func GetAPIs(apiBackend Backend) []rpc.API { Version: "1.0", Service: NewPrivateAccountAPI(apiBackend, nonceLock), Public: false, - }, { - Namespace: "db", - Version: "1.0", - Service: NewPublicDbAPI(apiBackend), - Public: true, }, } } diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index b6a50df0d04f..4b7a196e4726 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -30,7 +30,6 @@ var Modules = map[string]string{ "txpool": TxpoolJs, "les": LESJs, "vflux": VfluxJs, - "db": DbJS, } const CliqueJs = ` @@ -472,6 +471,11 @@ web3._extend({ params: 2, inputFormatter:[web3._extend.formatters.inputBlockNumberFormatter, web3._extend.formatters.inputBlockNumberFormatter], }), + new web3._extend.Method({ + name: 'dbGet', + call: 'debug_dbGet', + params: 1 + }), ], properties: [] }); @@ -854,28 +858,3 @@ web3._extend({ ] }); ` - -const DbJS = ` -web3._extend({ - property: 'db', - methods: - [ - new web3._extend.Method({ - name: 'get', - call: 'db_get', - params: 1 - }), - new web3._extend.Method({ - name: 'put', - call: 'db_put', - params: 2 - }), - new web3._extend.Method({ - name: 'delete', - call: 'db_delete', - params: 1 - }), - ], - properties: [] -}); -` From 21faa1ba63541909fbb759eec6001ca2865785a3 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 26 Apr 2022 14:15:41 +0200 Subject: [PATCH 5/6] mv parseHexOrString to common --- cmd/geth/dbcmd.go | 16 +++------------- common/bytes.go | 12 ++++++++++++ internal/ethapi/api.go | 5 ++++- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/cmd/geth/dbcmd.go b/cmd/geth/dbcmd.go index 33a7becfcd00..1b5a0c314b49 100644 --- a/cmd/geth/dbcmd.go +++ b/cmd/geth/dbcmd.go @@ -18,7 +18,6 @@ package main import ( "bytes" - "errors" "fmt" "os" "os/signal" @@ -418,7 +417,7 @@ func dbGet(ctx *cli.Context) error { db := utils.MakeChainDatabase(ctx, stack, true) defer db.Close() - key, err := parseHexOrString(ctx.Args().Get(0)) + key, err := common.ParseHexOrString(ctx.Args().Get(0)) if err != nil { log.Info("Could not decode the key", "error", err) return err @@ -444,7 +443,7 @@ func dbDelete(ctx *cli.Context) error { db := utils.MakeChainDatabase(ctx, stack, false) defer db.Close() - key, err := parseHexOrString(ctx.Args().Get(0)) + key, err := common.ParseHexOrString(ctx.Args().Get(0)) if err != nil { log.Info("Could not decode the key", "error", err) return err @@ -477,7 +476,7 @@ func dbPut(ctx *cli.Context) error { data []byte err error ) - key, err = parseHexOrString(ctx.Args().Get(0)) + key, err = common.ParseHexOrString(ctx.Args().Get(0)) if err != nil { log.Info("Could not decode the key", "error", err) return err @@ -584,15 +583,6 @@ func freezerInspect(ctx *cli.Context) error { 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 { start := 0 switch ctx.NArg() { diff --git a/common/bytes.go b/common/bytes.go index 7827bb572e13..d1f5c6c99586 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -19,6 +19,9 @@ package common import ( "encoding/hex" + "errors" + + "github.com/ethereum/go-ethereum/common/hexutil" ) // FromHex returns the bytes represented by the hexadecimal string s. @@ -92,6 +95,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) { diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 85fe0feb8a18..08d86ab14a4a 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1974,7 +1974,10 @@ func (api *PrivateDebugAPI) SetHead(number hexutil.Uint64) { // Get returns the raw value of a key stored in the database. func (api *PrivateDebugAPI) DbGet(key string) (hexutil.Bytes, error) { - blob := common.FromHex(key) + blob, err := common.ParseHexOrString(key) + if err != nil { + return nil, err + } return api.b.ChainDb().Get(blob) } From 24ccd1dacc3f0e714a63c8044fb31cb0d025c9f8 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Wed, 27 Apr 2022 08:35:49 +0200 Subject: [PATCH 6/6] internal/ethapi: fix docstring --- internal/ethapi/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 08d86ab14a4a..33842ff6c48e 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1972,7 +1972,7 @@ func (api *PrivateDebugAPI) SetHead(number hexutil.Uint64) { api.b.SetHead(uint64(number)) } -// Get returns the raw value of a key stored in the database. +// 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 {