Skip to content

Commit

Permalink
Only hashing immutable part of network configuration for dataDir ID (#…
Browse files Browse the repository at this point in the history
…2955)

* Only hashing immutable part of network configuration for dataDir ID

* Add debug- prefix to rewrite-datadir-id
  • Loading branch information
jangko authored Jan 17, 2025
1 parent ac053bf commit aa85d6a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion nimbus/common/chain_config_hash.nim
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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 @@ -156,17 +156,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

if calculatedId.data != dataDirIdBytes:
fatal "Data dir already initialized with other network configuration",
get=dataDirIdBytes.toHex,
Expand Down

0 comments on commit aa85d6a

Please sign in to comment.