mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-01-10 22:36:01 +00:00
1ef7d237cc
This PR allows sharing the pubkey data between validators by using a thread-local cache for pubkey data, netting about a 400mb mem usage reduction on holesky due to us keeping 3 permanent + several ephemeral state copies in memory at all times and each state copy holding a full validator. The PR also introduces a hash cache for the key which gives ~14% speedup for a full state `hash_tree_root` - the key makes up for a large part of the `Validator` htr time. Finally, the time it takes to copy a state goes down as well from ~80m ms to ~60, for reasons similar to htr. We use a `ptr` even if a `ref` could in theory have been used - there is not much practical benefit to a `ref` (given it's mutable) while a `ptr` is cheaper and easier to copy (when copying temporary states). We could go further and cache a cooked pubkey but it turns out this is quite intrusive - in all the relevant places, we're already using a cooked key from the immutable validator data so there are no immediate performance gains of doing so while managing the compressed -> cooked key mapping would become more difficult - something for a future PR perhaps. Co-authored-by: Etan Kissling <etan@status.im>
81 lines
3.2 KiB
Nim
81 lines
3.2 KiB
Nim
# beacon_chain
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
|
# Licensed and distributed under either of
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
{.push raises: [].}
|
|
|
|
# This module exports SSZ.encode and SSZ.decode for spec types - don't use
|
|
# ssz_serialization directly! To bypass root updates, use `readSszBytes`
|
|
# without involving SSZ!
|
|
import
|
|
ssz_serialization,
|
|
./ssz_codec,
|
|
./datatypes/[phase0, altair, bellatrix, capella],
|
|
./eth2_merkleization
|
|
|
|
from ./datatypes/deneb import SignedBeaconBlock, TrustedSignedBeaconBlock
|
|
|
|
export phase0, altair, ssz_codec, ssz_serialization, eth2_merkleization
|
|
|
|
proc readAndUpdateRoot(
|
|
data: openArray[byte], val: var auto, updateRoot = true
|
|
) {.raises: [SszError].} =
|
|
readSszValue(data, val)
|
|
if updateRoot:
|
|
val.root = hash_tree_root(val.message)
|
|
|
|
# TODO this is an ugly way to get a stronger match than the generic readSszBytes
|
|
# and avoid ambiguities - `var` + typeclasses are problematic
|
|
|
|
template readSszBytes*(
|
|
data: openArray[byte], val: var phase0.SignedBeaconBlock, updateRoot = true) =
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
template readSszBytes*(
|
|
data: openArray[byte], val: var phase0.TrustedSignedBeaconBlock, updateRoot = true) =
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
template readSszBytes*(
|
|
data: openArray[byte], val: var altair.SignedBeaconBlock, updateRoot = true) =
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
template readSszBytes*(
|
|
data: openArray[byte], val: var altair.TrustedSignedBeaconBlock, updateRoot = true) =
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
template readSszBytes*(
|
|
data: openArray[byte], val: var bellatrix.SignedBeaconBlock, updateRoot = true) =
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
template readSszBytes*(
|
|
data: openArray[byte], val: var bellatrix.TrustedSignedBeaconBlock, updateRoot = true) =
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
template readSszBytes*(
|
|
data: openArray[byte], val: var capella.SignedBeaconBlock, updateRoot = true) =
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
template readSszBytes*(
|
|
data: openArray[byte], val: var capella.TrustedSignedBeaconBlock, updateRoot = true) =
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
template readSszBytes*(
|
|
data: openArray[byte], val: var deneb.SignedBeaconBlock, updateRoot = true) =
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
template readSszBytes*(
|
|
data: openArray[byte], val: var deneb.TrustedSignedBeaconBlock, updateRoot = true) =
|
|
readAndUpdateRoot(data, val, updateRoot)
|
|
|
|
template readSszBytes*(
|
|
data: openArray[byte], val: var auto, updateRoot: bool) =
|
|
readSszValue(data, val)
|
|
|
|
func readSszBytes(
|
|
T: type, data: openArray[byte], updateRoot = true
|
|
): T {.raises: [SszError].} =
|
|
var res: T
|
|
readSszBytes(data, res, updateRoot)
|
|
res
|
|
|
|
proc fromSszBytes*(
|
|
T: type HashedValidatorPubKey, bytes: openArray[byte]
|
|
): T {.raises: [SszError].} =
|
|
let
|
|
key = ValidatorPubKey.fromSszBytes(bytes)
|
|
|
|
HashedValidatorPubKey.init(key) |