ncli: time operations (#3966)

This commit is contained in:
Jacek Sieka 2022-08-17 00:17:22 +02:00 committed by GitHub
parent cd9b50bbbc
commit 94a479f1c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 11 deletions

View File

@ -22,6 +22,11 @@ type
desc: "The Eth2 network preset to use" desc: "The Eth2 network preset to use"
name: "network" }: Option[string] name: "network" }: Option[string]
printTimes* {.
defaultValue: false # false to avoid polluting minimal output
name: "print-times"
desc: "Print timing information".}: bool
# TODO confutils argument pragma doesn't seem to do much; also, the cases # TODO confutils argument pragma doesn't seem to do much; also, the cases
# are largely equivalent, but this helps create command line usage text # are largely equivalent, but this helps create command line usage text
case cmd* {.command}: Cmd case cmd* {.command}: Cmd
@ -97,9 +102,17 @@ proc loadFile(filename: string, T: type): T =
quit 1 quit 1
proc doTransition(conf: NcliConf) = proc doTransition(conf: NcliConf) =
type
Timers = enum
tLoadState = "Load state from file"
tTransition = "Apply slot"
tSaveState = "Save state to file"
var timers: array[Timers, RunningStat]
let let
cfg = getRuntimeConfig(conf.eth2Network) cfg = getRuntimeConfig(conf.eth2Network)
stateY = newClone(readSszForkedHashedBeaconState( stateY = withTimerRet(timers[tLoadState]):
newClone(readSszForkedHashedBeaconState(
cfg, readAllBytes(conf.preState).tryGet())) cfg, readAllBytes(conf.preState).tryGet()))
blckX = readSszForkedSignedBeaconBlock( blckX = readSszForkedSignedBeaconBlock(
cfg, readAllBytes(conf.blck).tryGet()) cfg, readAllBytes(conf.blck).tryGet())
@ -108,15 +121,19 @@ proc doTransition(conf: NcliConf) =
var var
cache = StateCache() cache = StateCache()
info = ForkedEpochInfo() info = ForkedEpochInfo()
let res = withBlck(blckX): let res = withTimerRet(timers[tTransition]): withBlck(blckX):
state_transition( state_transition(
cfg, stateY[], blck, cache, info, flags, noRollback) cfg, stateY[], blck, cache, info, flags, noRollback)
if res.isErr(): if res.isErr():
error "State transition failed", error = res.error() error "State transition failed", error = res.error()
quit 1 quit 1
else: else:
withTimer(timers[tSaveState]):
saveSSZFile(conf.postState, stateY[]) saveSSZFile(conf.postState, stateY[])
if conf.printTimes:
printTimers(false, timers)
proc doSlots(conf: NcliConf) = proc doSlots(conf: NcliConf) =
type type
Timers = enum Timers = enum
@ -144,9 +161,15 @@ proc doSlots(conf: NcliConf) =
withTimer(timers[tSaveState]): withTimer(timers[tSaveState]):
saveSSZFile(conf.postState2, stateY[]) saveSSZFile(conf.postState2, stateY[])
if conf.printTimes:
printTimers(false, timers) printTimers(false, timers)
proc doSSZ(conf: NcliConf) = proc doSSZ(conf: NcliConf) =
type Timers = enum
tLoad = "Load file"
tCompute = "Compute"
var timers: array[Timers, RunningStat]
let (kind, file) = let (kind, file) =
case conf.cmd: case conf.cmd:
of hashTreeRoot: (conf.htrKind, conf.htrFile) of hashTreeRoot: (conf.htrKind, conf.htrFile)
@ -155,19 +178,26 @@ proc doSSZ(conf: NcliConf) =
raiseAssert "doSSZ() only implements hashTreeRoot and pretty commands" raiseAssert "doSSZ() only implements hashTreeRoot and pretty commands"
template printit(t: untyped) {.dirty.} = template printit(t: untyped) {.dirty.} =
let v = newClone(loadFile(file, t))
let v = withTimerRet(timers[tLoad]):
newClone(loadFile(file, t))
case conf.cmd: case conf.cmd:
of hashTreeRoot: of hashTreeRoot:
let root = withTimerRet(timers[tCompute]):
when t is ForkySignedBeaconBlock: when t is ForkySignedBeaconBlock:
echo hash_tree_root(v.message).data.toHex() hash_tree_root(v[].message)
else: else:
echo hash_tree_root(v[]).data.toHex() hash_tree_root(v[])
echo root.data.toHex()
of pretty: of pretty:
echo RestJson.encode(v[], pretty = true) echo RestJson.encode(v[], pretty = true)
else: else:
raiseAssert "doSSZ() only implements hashTreeRoot and pretty commands" raiseAssert "doSSZ() only implements hashTreeRoot and pretty commands"
if conf.printTimes:
printTimers(false, timers)
case kind case kind
of "attester_slashing": printit(AttesterSlashing) of "attester_slashing": printit(AttesterSlashing)
@ -186,7 +216,7 @@ proc doSSZ(conf: NcliConf) =
of "deposit_data": printit(DepositData) of "deposit_data": printit(DepositData)
of "eth1_data": printit(Eth1Data) of "eth1_data": printit(Eth1Data)
of "phase0_state": printit(phase0.BeaconState) of "phase0_state": printit(phase0.BeaconState)
of "altiar_state": printit(altair.BeaconState) of "altair_state": printit(altair.BeaconState)
of "bellatrix_state": printit(bellatrix.BeaconState) of "bellatrix_state": printit(bellatrix.BeaconState)
of "proposer_slashing": printit(ProposerSlashing) of "proposer_slashing": printit(ProposerSlashing)
of "voluntary_exit": printit(VoluntaryExit) of "voluntary_exit": printit(VoluntaryExit)