go-ethereum icon indicating copy to clipboard operation
go-ethereum copied to clipboard

cmd/geth: make dumpgenesis command load genesis from the datadir if it exists

Open jwasinger opened this issue 3 years ago • 3 comments
trafficstars

geth dumpgenesis current does not respect the content of the data directory. Instead, it outputs the genesis block created by command-line flags. This PR fixes it to read the genesis from the database, if the database already exists.

jwasinger avatar Jun 20 '22 17:06 jwasinger

whoops. pushed this to upstream instead of my personal repo.

jwasinger avatar Jun 21 '22 02:06 jwasinger

Some behavioural changes. I'll just dump them here, for the record. They might be totally fine changes

Non-existing dir changes

[user@work go-ethereum]$ ./build/bin/gethmaster --datadir /tmp/foo/blala dumpgenesis | jq . | head
{
  "config": {
    "chainId": 1,
    "homesteadBlock": 1150000,
    "daoForkBlock": 1920000,
    "daoForkSupport": true,
    "eip150Block": 2463000,
    "eip150Hash": "0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0",
    "eip155Block": 2675000,
    "eip158Block": 2675000,
[user@work go-ethereum]$ ./build/bin/geth --datadir /tmp/foo/blarg dumpgenesis | head
INFO [09-21|08:38:48.509] Maximum peer count                       ETH=50 LES=0 total=50
INFO [09-21|08:38:48.522] Set global gas cap                       cap=50,000,000
INFO [09-21|08:38:48.523] Allocated cache and file handles         database=/tmp/foo/blarg/geth/chaindata cache=16.00MiB handles=16 readonly=true
INFO [09-21|08:38:48.523] Allocated cache and file handles         database=/tmp/foo/blarg/geth/lightchaindata cache=16.00MiB handles=16 readonly=true
Fatal: no existing datadir at /tmp/foo/blarg
Fatal: no existing datadir at /tmp/foo/blarg

I think it's correct to fail on non-existing datadir, rather than init with mainnet.

Dumping the genesis

Init with sepolia:

$ ./build/bin/geth --datadir /tmp/foo/blahonga init sepolia.json
INFO [09-21|08:41:29.679] Maximum peer count                       ETH=50 LES=0 total=50
INFO [09-21|08:41:29.687] Set global gas cap                       cap=50,000,000
INFO [09-21|08:41:29.688] Allocated cache and file handles         database=/tmp/foo/blahonga/geth/chaindata cache=16.00MiB handles=16
INFO [09-21|08:41:29.689] Opened ancient database                  database=/tmp/foo/blahonga/geth/chaindata/ancient readonly=false
INFO [09-21|08:41:29.689] Writing custom genesis block 
INFO [09-21|08:41:29.690] Persisted trie from memory database      nodes=19 size=2.93KiB time="64.868µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [09-21|08:41:29.691] Successfully wrote genesis state         database=chaindata                                hash=25a5cc..3e6dd9
INFO [09-21|08:41:29.691] Allocated cache and file handles         database=/tmp/foo/blahonga/geth/lightchaindata    cache=16.00MiB handles=16
INFO [09-21|08:41:29.692] Opened ancient database                  database=/tmp/foo/blahonga/geth/lightchaindata/ancient readonly=false
INFO [09-21|08:41:29.692] Writing custom genesis block 
INFO [09-21|08:41:29.693] Persisted trie from memory database      nodes=19 size=2.93KiB time="104.798µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [09-21|08:41:29.694] Successfully wrote genesis state         database=lightchaindata                           hash=25a5cc..3e6dd9
[user@work go-ethereum]$ ./build/bin/geth --datadir /tmp/foo/blahonga dumpgenesis | jq . | grep chainid
INFO [09-21|08:41:44.271] Maximum peer count                       ETH=50 LES=0 total=50
INFO [09-21|08:41:44.279] Set global gas cap                       cap=50,000,000
INFO [09-21|08:41:44.281] Allocated cache and file handles         database=/tmp/foo/blahonga/geth/chaindata cache=16.00MiB handles=16 readonly=true

Dump with this PR (correct behaviour):

[user@work go-ethereum]$ ./build/bin/geth --datadir /tmp/foo/blahonga dumpgenesis 2>/dev/null | jq . | grep chainId
    "chainId": 11155111,

Dump with master (incorrectly dumps out mainnet):

[user@work go-ethereum]$ ./build/bin/gethmaster --datadir /tmp/foo/blahonga dumpgenesis 2>/dev/null | jq . | grep chainId
    "chainId": 1,

Other

In most cases they are in agreement

[user@work go-ethereum]$ ./build/bin/gethmaster  dumpgenesis 2>/dev/null | jq . | grep chainId
    "chainId": 1,
[user@work go-ethereum]$ ./build/bin/geth  dumpgenesis 2>/dev/null | jq . | grep chainId
    "chainId": 1,
[user@work go-ethereum]$ ./build/bin/gethmaster  --sepolia dumpgenesis 2>/dev/null | jq . | grep chainId
    "chainId": 11155111,
[user@work go-ethereum]$ ./build/bin/geth  --sepolia dumpgenesis 2>/dev/null | jq . | grep chainId
    "chainId": 11155111,
[user@work go-ethereum]$ ./build/bin/geth  --goerli  dumpgenesis 2>/dev/null | jq . | grep chainId
    "chainId": 5,
[user@work go-ethereum]$ ./build/bin/gethmaster  --goerli  dumpgenesis 2>/dev/null | jq . | grep chainId
    "chainId": 5,

holiman avatar Sep 21 '22 06:09 holiman

Rebased, hoping for some more :green_circle: from CI now.

holiman avatar Sep 21 '22 07:09 holiman