Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/geth: support string (non-hex) keys in db get/put/delete #23744

Merged
merged 6 commits into from
Oct 18, 2021

Conversation

zhiburt
Copy link
Contributor

@zhiburt zhiburt commented Oct 15, 2021

Hi,

This flag suppose to bring a little more convenience, when observing internal state,
via constant keys like SnapshotSyncStatus, SnapshotRecovery etc.

Now it's necessary to convert these strings to hex and to add a prefix 0x which might be inconvenient.
And not intuitive at a first glance.

It is suppose to be convinient when observing internal state,
via constant keys like `SnapshotSyncStatus`, `SnapshotRecovery` etc.

Otherwise it's necessary to convert these strings to hex which might be inconvinient.

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
@holiman
Copy link
Contributor

holiman commented Oct 15, 2021

Great, I've been annoyed by this same thing myself!

@holiman
Copy link
Contributor

holiman commented Oct 15, 2021

A couple of things:

  • Simiar thing should be applied for db put and db delete aswell.
    • However, for db put, it's a bit more problematic: there's both a key and a value. And most likely, one wants the key in ascii but the value in hex (because most data is not ascii). So the logic should only apply for the key.
  • A simpler (more hacky) variant would be to always try and parse the key as hex first, and, failing that, fall back to ascii interpretation. Then we wouldn't need a flag. Then it wouldn't be possible to use a key which literally is the string 0x123, but I don't know if that matters. I'm leaning towards doing that instead, that makes the UX a bit nicer.
  • Also, I don't know why you call it utf-8. It's simply the raw bytes of the input string. Whether it's UTF-8 or ascii or something else entirely, I guess that's up to the shell / OS. All our string-based keys are representable in ascii.

@zhiburt
Copy link
Contributor Author

zhiburt commented Oct 15, 2021

Simiar thing should be applied for db put and db delete aswell.

However, for db put, it's a bit more problematic: there's both a key and a value. And most likely, one wants the key in ascii but the value in hex (because most data is not ascii). So the logic should only apply for the key.

Agreed

A simpler (more hacky) variant would be to always try and parse the key as hex first, and, failing that, fall back to ascii interpretation. Then we wouldn't need a flag. Then it wouldn't be possible to use a key which literally is the string 0x123, but I don't know if that matters. I'm leaning towards doing that instead, that makes the UX a bit nicer.

I guess it doesn't because it in such case user could provide such a key encoded in hex right after 0x.
So the key would be 0x3078313233

Also, I don't know why you call it utf-8. It's simply the raw bytes of the input string. Whether it's UTF-8 or ascii or something else entirely, I guess that's up to the shell / OS. All our string-based keys are representable in ascii.

You are right.
I was calling it utf8 because in go we get it as utf8 no matter which encoding was on the input.
*I guess

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
@zhiburt
Copy link
Contributor Author

zhiburt commented Oct 15, 2021

Must be fixed according to your comments @holiman

cmd/utils/cmd.go Outdated Show resolved Hide resolved
cmd/utils/cmd.go Outdated Show resolved Hide resolved
zhiburt and others added 3 commits October 15, 2021 23:36
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Martin Holst Swende <martin@swende.se>
Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
@zhiburt zhiburt requested a review from holiman October 16, 2021 19:51
@holiman holiman changed the title cmd/geth/db/get: Add a utf8 flag cmd/geth: support string (non-hex) keys in db get/put/delete Oct 18, 2021
@holiman
Copy link
Contributor

holiman commented Oct 18, 2021

I pushed a fix, it works now:

[user@work go-ethereum]$ geth db get DatabaseVersion
INFO [10-18|10:33:03.906] Maximum peer count                       ETH=50 LES=0 total=50
INFO [10-18|10:33:03.906] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [10-18|10:33:03.907] Set global gas cap                       cap=50,000,000
INFO [10-18|10:33:03.907] Allocated cache and file handles         database=/home/user/.ethereum/geth/chaindata cache=512.00MiB handles=262,144 readonly=true
INFO [10-18|10:33:03.952] Opened ancient database                  database=/home/user/.ethereum/geth/chaindata/ancient readonly=true
key 0x446174616261736556657273696f6e: 0x08

[user@work go-ethereum]$ geth db put DatabaseVersion 0x09
INFO [10-18|10:33:09.782] Maximum peer count                       ETH=50 LES=0 total=50
INFO [10-18|10:33:09.782] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [10-18|10:33:09.784] Set global gas cap                       cap=50,000,000
INFO [10-18|10:33:09.784] Allocated cache and file handles         database=/home/user/.ethereum/geth/chaindata cache=512.00MiB handles=262,144
INFO [10-18|10:33:09.864] Opened ancient database                  database=/home/user/.ethereum/geth/chaindata/ancient readonly=false
Previous value: 0x08
INFO [10-18|10:33:09.868] Freezer shutting down 

[user@work go-ethereum]$ geth db put DatabaseVersion 0x08
INFO [10-18|10:33:13.268] Maximum peer count                       ETH=50 LES=0 total=50
INFO [10-18|10:33:13.268] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [10-18|10:33:13.269] Set global gas cap                       cap=50,000,000
INFO [10-18|10:33:13.269] Allocated cache and file handles         database=/home/user/.ethereum/geth/chaindata cache=512.00MiB handles=262,144
INFO [10-18|10:33:13.322] Opened ancient database                  database=/home/user/.ethereum/geth/chaindata/ancient readonly=false
Previous value: 0x09
INFO [10-18|10:33:13.324] Freezer shutting down 

[user@work go-ethereum]$ geth db delete DatabaseVersion
INFO [10-18|10:34:08.064] Maximum peer count                       ETH=50 LES=0 total=50
INFO [10-18|10:34:08.064] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [10-18|10:34:08.065] Set global gas cap                       cap=50,000,000
INFO [10-18|10:34:08.065] Allocated cache and file handles         database=/home/user/.ethereum/geth/chaindata cache=512.00MiB handles=262,144
INFO [10-18|10:34:08.093] Opened ancient database                  database=/home/user/.ethereum/geth/chaindata/ancient readonly=false
Previous value: 0x08
INFO [10-18|10:34:08.093] Freezer shutting down 

[user@work go-ethereum]$ geth db get DatabaseVersion
INFO [10-18|10:34:12.224] Maximum peer count                       ETH=50 LES=0 total=50
INFO [10-18|10:34:12.224] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [10-18|10:34:12.224] Set global gas cap                       cap=50,000,000
INFO [10-18|10:34:12.224] Allocated cache and file handles         database=/home/user/.ethereum/geth/chaindata cache=512.00MiB handles=262,144 readonly=true
INFO [10-18|10:34:12.236] Opened ancient database                  database=/home/user/.ethereum/geth/chaindata/ancient readonly=true
INFO [10-18|10:34:12.237] Get operation failed                     key=0x0x446174616261736556657273696f6e error="leveldb: not found"
leveldb: not found

[user@work go-ethereum]$ geth db put DatabaseVersion 0x08
INFO [10-18|10:34:15.462] Maximum peer count                       ETH=50 LES=0 total=50
INFO [10-18|10:34:15.462] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [10-18|10:34:15.463] Set global gas cap                       cap=50,000,000
INFO [10-18|10:34:15.463] Allocated cache and file handles         database=/home/user/.ethereum/geth/chaindata cache=512.00MiB handles=262,144
INFO [10-18|10:34:15.492] Opened ancient database                  database=/home/user/.ethereum/geth/chaindata/ancient readonly=false
INFO [10-18|10:34:15.493] Freezer shutting down 

@holiman holiman added this to the 1.10.11 milestone Oct 18, 2021
@zhiburt
Copy link
Contributor Author

zhiburt commented Oct 18, 2021

I pushed a fix, it works now:

thanks, haven't spotted that 😞

@holiman holiman merged commit eaa24a8 into ethereum:master Oct 18, 2021
sidhujag pushed a commit to syscoin/go-ethereum that referenced this pull request Oct 18, 2021
…m#23744)

Adds suppor for passing regular strings to db `put`/`get`/`delete`, to avoid having to hex-encode when operating on fixed-key items like `SnapshotSyncStatus`, `SnapshotRecovery`  etc.


Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
yongjun925 pushed a commit to DODOEX/go-ethereum that referenced this pull request Dec 3, 2022
…m#23744)

Adds suppor for passing regular strings to db `put`/`get`/`delete`, to avoid having to hex-encode when operating on fixed-key items like `SnapshotSyncStatus`, `SnapshotRecovery`  etc.


Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants