Skip to content

Commit

Permalink
prevent accidentally hashing BeaconState/BeaconBlock in Deneb (#5852
Browse files Browse the repository at this point in the history
)

Extend protection against accidentally calling computationally expensive
functions when a cache is available to Deneb, as done for earlier forks.
  • Loading branch information
etan-status authored Feb 6, 2024
1 parent f556b91 commit 4140302
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions beacon_chain/spec/eth2_merkleization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ from ./datatypes/phase0 import HashedBeaconState, SignedBeaconBlock
from ./datatypes/altair import HashedBeaconState, SignedBeaconBlock
from ./datatypes/bellatrix import HashedBeaconState, SignedBeaconBlock
from ./datatypes/capella import HashedBeaconState, SignedBeaconBlock
from ./datatypes/deneb import HashedBeaconState, SignedBeaconBlock

export ssz_codec, merkleization, proofs

Expand All @@ -28,12 +29,14 @@ type
# creating recursive module dependency through `forks`.
func hash_tree_root*(
x: phase0.HashedBeaconState | altair.HashedBeaconState |
bellatrix.HashedBeaconState | capella.HashedBeaconState) {.
bellatrix.HashedBeaconState | capella.HashedBeaconState |
deneb.HashedBeaconState) {.
error: "HashedBeaconState should not be hashed".}

func hash_tree_root*(
x: phase0.SignedBeaconBlock | altair.SignedBeaconBlock |
bellatrix.SignedBeaconBlock | capella.SignedBeaconBlock) {.
bellatrix.SignedBeaconBlock | capella.SignedBeaconBlock |
deneb.SignedBeaconBlock) {.
error: "SignedBeaconBlock should not be hashed".}

func depositCountBytes*(x: uint64): array[32, byte] =
Expand Down
4 changes: 4 additions & 0 deletions beacon_chain/spec/forks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,10 @@ template PayloadAttributes*(
else:
{.error: "PayloadAttributes does not support " & $kind.}

# `eth2_merkleization` cannot import `forks` (circular), so the check is here
static: doAssert ConsensusFork.high == ConsensusFork.Deneb,
"eth2_merkleization has been checked and `hash_tree_root` is up to date"

# TODO when https://github.com/nim-lang/Nim/issues/21086 fixed, use return type
# `ref T`
func new*(T: type ForkedHashedBeaconState, data: phase0.BeaconState):
Expand Down

0 comments on commit 4140302

Please sign in to comment.