mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-01-11 14:54:12 +00:00
dcf8a6b05d
about 40% better slot processing times (with LTO enabled) - these don't do BLS but are used heavily during replay (state transition = slot + block transition) tests using a recent medalla state and advancing it 1000 slots: ``` ./ncli slots --preState2:state-302271-3c1dbf19-c1f944bf.ssz --slot:1000 --postState2:xx.ssz ``` pre: ``` All time are ms Average, StdDev, Min, Max, Samples, Test Validation is turned off meaning that no BLS operations are performed 39.236, 0.000, 39.236, 39.236, 1, Load state from file 0.049, 0.002, 0.046, 0.063, 968, Apply slot 256.504, 81.008, 213.471, 591.902, 32, Apply epoch slot 28.597, 0.000, 28.597, 28.597, 1, Save state to file ``` cast: ``` All time are ms Average, StdDev, Min, Max, Samples, Test Validation is turned off meaning that no BLS operations are performed 37.079, 0.000, 37.079, 37.079, 1, Load state from file 0.042, 0.002, 0.040, 0.090, 968, Apply slot 215.552, 68.763, 180.155, 500.103, 32, Apply epoch slot 25.106, 0.000, 25.106, 25.106, 1, Save state to file ``` cast+rewards: ``` All time are ms Average, StdDev, Min, Max, Samples, Test Validation is turned off meaning that no BLS operations are performed 40.049, 0.000, 40.049, 40.049, 1, Load state from file 0.048, 0.001, 0.045, 0.060, 968, Apply slot 164.981, 76.273, 142.099, 477.868, 32, Apply epoch slot 28.498, 0.000, 28.498, 28.498, 1, Save state to file ``` cast+rewards+shr ``` All time are ms Average, StdDev, Min, Max, Samples, Test Validation is turned off meaning that no BLS operations are performed 12.898, 0.000, 12.898, 12.898, 1, Load state from file 0.039, 0.002, 0.038, 0.054, 968, Apply slot 139.971, 68.797, 120.088, 428.844, 32, Apply epoch slot 24.761, 0.000, 24.761, 24.761, 1, Save state to file ```
174 lines
5.2 KiB
Nim
174 lines
5.2 KiB
Nim
import
|
|
std/[os, strutils, stats],
|
|
confutils, chronicles, json_serialization,
|
|
stew/byteutils,
|
|
../research/simutils,
|
|
../beacon_chain/spec/[crypto, datatypes, digest, helpers, state_transition],
|
|
../beacon_chain/extras,
|
|
../beacon_chain/network_metadata,
|
|
../beacon_chain/ssz/[merkleization, ssz_serialization]
|
|
|
|
type
|
|
Cmd* = enum
|
|
hashTreeRoot = "Compute hash tree root of SSZ object"
|
|
pretty = "Pretty-print SSZ object"
|
|
transition = "Run state transition function"
|
|
slots = "Apply empty slots"
|
|
|
|
NcliConf* = object
|
|
|
|
eth2Network* {.
|
|
desc: "The Eth2 network preset to use"
|
|
name: "network" }: Option[string]
|
|
|
|
# TODO confutils argument pragma doesn't seem to do much; also, the cases
|
|
# are largely equivalent, but this helps create command line usage text
|
|
case cmd* {.command}: Cmd
|
|
of hashTreeRoot:
|
|
htrKind* {.
|
|
argument
|
|
desc: "kind of SSZ object: attester_slashing, attestation, signed_block, block, block_body, block_header, deposit, deposit_data, eth1_data, state, proposer_slashing, or voluntary_exit"}: string
|
|
|
|
htrFile* {.
|
|
argument
|
|
desc: "filename of SSZ or JSON-encoded object of which to compute hash tree root"}: string
|
|
|
|
of pretty:
|
|
prettyKind* {.
|
|
argument
|
|
desc: "kind of SSZ object: attester_slashing, attestation, signed_block, block, block_body, block_header, deposit, deposit_data, eth1_data, state, proposer_slashing, or voluntary_exit"}: string
|
|
|
|
prettyFile* {.
|
|
argument
|
|
desc: "filename of SSZ or JSON-encoded object to pretty-print"}: string
|
|
|
|
of transition:
|
|
preState* {.
|
|
argument
|
|
desc: "State to which to apply specified block"}: string
|
|
|
|
blck* {.
|
|
argument
|
|
desc: "Block to apply to preState"}: string
|
|
|
|
postState* {.
|
|
argument
|
|
desc: "Filename of state resulting from applying blck to preState"}: string
|
|
|
|
verifyStateRoot* {.
|
|
argument
|
|
desc: "Verify state root (default true)"
|
|
defaultValue: true}: bool
|
|
|
|
of slots:
|
|
preState2* {.
|
|
argument
|
|
desc: "State to which to apply specified block"}: string
|
|
|
|
slot* {.
|
|
argument
|
|
desc: "Block to apply to preState"}: uint64
|
|
|
|
postState2* {.
|
|
argument
|
|
desc: "Filename of state resulting from applying blck to preState"}: string
|
|
|
|
proc doTransition(conf: NcliConf) =
|
|
let
|
|
stateY = (ref HashedBeaconState)(
|
|
data: SSZ.loadFile(conf.preState, BeaconState),
|
|
)
|
|
blckX = SSZ.loadFile(conf.blck, SignedBeaconBlock)
|
|
flags = if not conf.verifyStateRoot: {skipStateRootValidation} else: {}
|
|
|
|
stateY.root = hash_tree_root(stateY.data)
|
|
|
|
if not state_transition(getRuntimePresetForNetwork(conf.eth2Network),
|
|
stateY[], blckX, flags, noRollback):
|
|
error "State transition failed"
|
|
quit 1
|
|
else:
|
|
SSZ.saveFile(conf.postState, stateY.data)
|
|
|
|
proc doSlots(conf: NcliConf) =
|
|
type
|
|
Timers = enum
|
|
tLoadState = "Load state from file"
|
|
tApplySlot = "Apply slot"
|
|
tApplyEpochSlot = "Apply epoch slot"
|
|
tSaveState = "Save state to file"
|
|
|
|
var timers: array[Timers, RunningStat]
|
|
let
|
|
stateY = withTimerRet(timers[tLoadState]): (ref HashedBeaconState)(
|
|
data: SSZ.loadFile(conf.preState2, BeaconState),
|
|
)
|
|
|
|
stateY.root = hash_tree_root(stateY.data)
|
|
|
|
var cache: StateCache
|
|
for i in 0'u64..<conf.slot:
|
|
let isEpoch = (stateY[].data.slot + 1).isEpoch
|
|
withTimer(timers[if isEpoch: tApplyEpochSlot else: tApplySlot]):
|
|
advance_slot(stateY[], {}, cache)
|
|
|
|
withTimer(timers[tSaveState]):
|
|
SSZ.saveFile(conf.postState, stateY.data)
|
|
|
|
printTimers(false, timers)
|
|
|
|
proc doSSZ(conf: NcliConf) =
|
|
let (kind, file) =
|
|
case conf.cmd:
|
|
of hashTreeRoot: (conf.htrKind, conf.htrFile)
|
|
of pretty: (conf.prettyKind, conf.prettyFile)
|
|
else:
|
|
raiseAssert "doSSZ() only implements hashTreeRoot and pretty commands"
|
|
|
|
template printit(t: untyped) {.dirty.} =
|
|
let v = newClone(
|
|
if cmpIgnoreCase(ext, ".ssz") == 0:
|
|
SSZ.loadFile(file, t)
|
|
elif cmpIgnoreCase(ext, ".json") == 0:
|
|
JSON.loadFile(file, t)
|
|
else:
|
|
echo "Unknown file type: ", ext
|
|
quit 1
|
|
)
|
|
|
|
case conf.cmd:
|
|
of hashTreeRoot:
|
|
when t is SignedBeaconBlock:
|
|
echo hash_tree_root(v.message).data.toHex()
|
|
else:
|
|
echo hash_tree_root(v[]).data.toHex()
|
|
of pretty:
|
|
echo JSON.encode(v[], pretty = true)
|
|
else:
|
|
raiseAssert "doSSZ() only implements hashTreeRoot and pretty commands"
|
|
|
|
let ext = splitFile(file).ext
|
|
|
|
case kind
|
|
of "attester_slashing": printit(AttesterSlashing)
|
|
of "attestation": printit(Attestation)
|
|
of "signed_block": printit(SignedBeaconBlock)
|
|
of "block": printit(BeaconBlock)
|
|
of "block_body": printit(BeaconBlockBody)
|
|
of "block_header": printit(BeaconBlockHeader)
|
|
of "deposit": printit(Deposit)
|
|
of "deposit_data": printit(DepositData)
|
|
of "eth1_data": printit(Eth1Data)
|
|
of "state": printit(BeaconState)
|
|
of "proposer_slashing": printit(ProposerSlashing)
|
|
of "voluntary_exit": printit(VoluntaryExit)
|
|
|
|
when isMainModule:
|
|
let conf = NcliConf.load()
|
|
|
|
case conf.cmd:
|
|
of hashTreeRoot: doSSZ(conf)
|
|
of pretty: doSSZ(conf)
|
|
of transition: doTransition(conf)
|
|
of slots: doSlots(conf)
|