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

Only hashing immutable part of network configuration for dataDir ID #2955

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nimbus/common/chain_config_hash.nim
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func calcHash*(networkId: NetworkId, conf: ChainConfig, genesis: Genesis): Hash3
var ctx: sha256
ctx.init()
ctx.update(networkId)
ctx.update(conf)
ctx.update(conf.chainId)
if genesis.isNil.not:
ctx.update(genesis)
ctx.finish(result.data)
Expand Down
5 changes: 5 additions & 0 deletions nimbus/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ type
desc: "Print RDB statistics at exit"
name: "debug-rdb-print-stats".}: bool

rewriteDatadirId* {.
hidden
desc: "Rewrite selected network config hash to database"
name: "debug-rewrite-datadir-id".}: bool

case cmd* {.
command
defaultValue: NimbusCmd.noCommand }: NimbusCmd
Expand Down
15 changes: 11 additions & 4 deletions nimbus/nimbus_execution_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,24 @@ proc setupMetrics(nimbus: NimbusNode, conf: NimbusConf) =
waitFor nimbus.metricsServer.start()

proc preventLoadingDataDirForTheWrongNetwork(db: CoreDbRef; conf: NimbusConf) =
proc writeDataDirId(kvt: CoreDbKvtRef, calculatedId: Hash32) =
info "Writing data dir ID", ID=calculatedId
kvt.put(dataDirIdKey().toOpenArray, calculatedId.data).isOkOr:
fatal "Cannot write data dir ID", ID=calculatedId
quit(QuitFailure)

let
kvt = db.ctx.getKvt()
calculatedId = calcHash(conf.networkId, conf.networkParams)
dataDirIdBytes = kvt.get(dataDirIdKey().toOpenArray).valueOr:
# an empty database
info "Writing data dir ID", ID=calculatedId
kvt.put(dataDirIdKey().toOpenArray, calculatedId.data).isOkOr:
fatal "Cannot write data dir ID", ID=calculatedId
quit(QuitFailure)
writeDataDirId(kvt, calculatedId)
return

if conf.rewriteDatadirId:
writeDataDirId(kvt, calculatedId)
return

advaita-saha marked this conversation as resolved.
Show resolved Hide resolved
if calculatedId.data != dataDirIdBytes:
fatal "Data dir already initialized with other network configuration",
get=dataDirIdBytes.toHex,
Expand Down
Loading